const LoopBackContext = require('loopback-context'); module.exports = function(Self) { require('../methods/receipt/filter')(Self); require('../methods/receipt/receiptPdf')(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) { const loopBackContext = LoopBackContext.getCurrentContext(); ctx.instance.workerFk = loopBackContext.active.accessToken.userId; await Self.app.models.Till.create({ workerFk: ctx.instance.workerFk, 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 }); } }); };