2021-04-01 15:36:27 +00:00
|
|
|
const validateIban = require('vn-loopback/util/validateIban');
|
2021-08-27 07:01:21 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2021-04-01 15:36:27 +00:00
|
|
|
|
|
|
|
module.exports = Self => {
|
2021-04-16 16:35:03 +00:00
|
|
|
Self.validateAsync('iban', ibanValidation, {
|
2021-04-01 15:36:27 +00:00
|
|
|
message: 'The IBAN does not have the correct format'
|
|
|
|
});
|
|
|
|
|
2021-04-16 16:35:03 +00:00
|
|
|
async function ibanValidation(err, done) {
|
2021-04-01 15:36:27 +00:00
|
|
|
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();
|
|
|
|
}
|
2021-08-27 07:01:21 +00:00
|
|
|
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({
|
2021-10-25 09:58:23 +00:00
|
|
|
receiver: 'finanzas@verdnatura.es',
|
2021-08-27 07:01:21 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
2021-04-01 15:36:27 +00:00
|
|
|
};
|