salix/modules/supplier/back/models/supplier-account.js

49 lines
1.8 KiB
JavaScript
Raw Normal View History

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) {
if (!this.bankEntityFk)
return done();
const bankEntity = await Self.app.models.BankEntity.findById(this.bankEntityFk);
const filter = {
2021-04-01 15:36:27 +00:00
fields: ['code'],
where: {id: bankEntity.countryFk}
2021-04-01 15:36:27 +00:00
};
const country = await Self.app.models.Country.findOne(filter);
2021-04-01 15:36:27 +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({
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
});
});
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);
if (supplier.isPayMethodChecked)
await supplier.updateAttribute('isPayMethodChecked', false, options);
});
2021-04-01 15:36:27 +00:00
};