const validateIban = require('vn-loopback/util/validateIban'); const LoopBackContext = require('loopback-context'); module.exports = Self => { Self.validateAsync('iban', ibanValidation, { message: 'The IBAN does not have the correct format' }); async function ibanValidation(err, done) { let filter = { fields: ['code'], where: {id: this.countryFk} }; let country = await Self.app.models.Country.findOne(filter); let code = country ? country.code.toLowerCase() : null; if (code != 'es') return done(); if (!validateIban(this.iban)) err(); done(); } Self.observe('after save', async ctx => { const loopBackContext = LoopBackContext.getCurrentContext(); const models = Self.app.models; const user = await models.user.findById(loopBackContext.active.accessToken.userId); const bankEntity = await models.BankEntity.findById(ctx.instance.bankEntityFk); await Self.app.models.Mail.create({ receiver: 'finanzas@verdnatura.es', subject: 'Añadida cuenta bancaria al proveedor' + ctx.instance.supplierFk, body: user.username + ' ha añadido: ' + ctx.instance.iban + ', entidad: ' + bankEntity.name + ', bic: ' + bankEntity.bic }); }); };