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) {
|
2023-08-25 07:46:53 +00:00
|
|
|
if (!this.bankEntityFk)
|
|
|
|
return done();
|
|
|
|
|
2023-08-22 12:58:26 +00:00
|
|
|
const bankEntity = await Self.app.models.BankEntity.findById(this.bankEntityFk);
|
2021-12-23 07:09:48 +00:00
|
|
|
const filter = {
|
2021-04-01 15:36:27 +00:00
|
|
|
fields: ['code'],
|
2023-09-13 05:07:22 +00:00
|
|
|
where: {id: bankEntity?.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);
|
2021-04-01 15:36:27 +00:00
|
|
|
|
2023-08-22 12:58:26 +00:00
|
|
|
if (!validateIban(this.iban, country?.code))
|
2021-04-01 15:36:27 +00:00
|
|
|
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
|
|
|
};
|