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-12-23 07:09:48 +00:00
|
|
|
const supplier = await Self.app.models.Supplier.findById(this.supplierFk);
|
|
|
|
const filter = {
|
2021-04-01 15:36:27 +00:00
|
|
|
fields: ['code'],
|
2021-12-23 07:09:48 +00:00
|
|
|
where: {id: supplier.countryFk}
|
2021-04-01 15:36:27 +00:00
|
|
|
};
|
2021-12-23 07:09:48 +00:00
|
|
|
|
|
|
|
const country = await Self.app.models.Country.findOne(filter);
|
|
|
|
const code = country ? country.code.toLowerCase() : null;
|
2021-04-01 15:36:27 +00:00
|
|
|
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;
|
2023-01-31 13:57:24 +00:00
|
|
|
const user = await models.VnUser.findById(loopBackContext.active.accessToken.userId);
|
2021-08-27 07:01:21 +00:00
|
|
|
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
|
|
|
|
});
|
|
|
|
});
|
2022-03-01 09:44:04 +00:00
|
|
|
|
|
|
|
Self.observe('after save', async ctx => {
|
|
|
|
const options = {};
|
|
|
|
|
|
|
|
if (ctx.options && ctx.options.transaction)
|
|
|
|
options.transaction = ctx.options.transaction;
|
2022-03-03 12:48:18 +00:00
|
|
|
const supplier = await Self.app.models.Supplier.findById(ctx.instance.supplierFk, null, options);
|
2022-03-01 09:44:04 +00:00
|
|
|
|
|
|
|
if (supplier.isPayMethodChecked)
|
|
|
|
await supplier.updateAttribute('isPayMethodChecked', false, options);
|
|
|
|
});
|
2021-04-01 15:36:27 +00:00
|
|
|
};
|