module.exports = function(Self) { require('../methods/receipt/filter')(Self); Self.validateBinded('amountPaid', isNotZero, { message: 'Amount cannot be zero', allowNull: false, allowBlank: false }); function isNotZero(value) { return !isNaN(value) && value != 0; } Self.validateAsync('companyFk', isOfficialCompany, { message: 'Company has to be official' }); async function isOfficialCompany(err, done) { const hasCompany = await Self.app.models.Company.exists(this.companyFk); if (!hasCompany) err(); done(); } Self.observe('before save', async function(ctx) { if (ctx.isNewInstance) { let token = ctx.options.accessToken; let userId = token && token.userId; ctx.instance.workerFk = userId; await Self.app.models.Till.create({ workerFk: userId, bankFk: ctx.instance.bankFk, in: ctx.instance.amountPaid, concept: ctx.instance.description, dated: ctx.instance.payed, serie: 'A', isAccountable: true, number: ctx.instance.clientFk, companyFk: ctx.instance.companyFk }); } }); };