2020-01-23 12:31:07 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
2018-03-14 10:36:57 +00:00
|
|
|
module.exports = function(Self) {
|
2018-03-26 09:43:17 +00:00
|
|
|
Self.validateCredit = function(credit) {
|
|
|
|
return credit >= 0;
|
|
|
|
};
|
|
|
|
|
2018-03-14 10:36:57 +00:00
|
|
|
Self.validateBinded('credit', Self.validateCredit, {
|
|
|
|
message: 'The credit must be an integer greater than or equal to zero',
|
2019-10-14 08:59:10 +00:00
|
|
|
allowNull: false,
|
2018-03-14 10:36:57 +00:00
|
|
|
allowBlank: false
|
|
|
|
});
|
|
|
|
|
2018-03-26 09:43:17 +00:00
|
|
|
Self.validateGrade = function(grade) {
|
|
|
|
return typeof grade === 'undefined' || grade >= 0;
|
2018-03-14 10:36:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Self.validateBinded('grade', Self.validateGrade, {
|
|
|
|
message: 'The grade must be an integer greater than or equal to zero',
|
|
|
|
allowNull: true
|
|
|
|
});
|
2018-04-20 13:16:03 +00:00
|
|
|
|
2018-05-31 05:59:42 +00:00
|
|
|
async function validateNullGrade(err, done) {
|
|
|
|
let filter = {
|
2018-08-02 07:49:00 +00:00
|
|
|
fields: ['grade'],
|
2018-05-31 05:59:42 +00:00
|
|
|
where: {
|
2022-06-09 05:13:54 +00:00
|
|
|
creditClassificationFk: this.creditClassificationFk
|
2018-05-31 05:59:42 +00:00
|
|
|
},
|
|
|
|
order: 'created DESC'
|
|
|
|
};
|
|
|
|
let insurance = await Self.findOne(filter);
|
|
|
|
|
2018-08-21 11:38:16 +00:00
|
|
|
if (insurance && (!insurance.grade && this.grade || insurance.grade && !this.grade))
|
2018-05-31 05:59:42 +00:00
|
|
|
err();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
|
|
|
Self.validateAsync('nullGrade', validateNullGrade, {
|
|
|
|
message: 'The grade must be similar to the last one'
|
|
|
|
});
|
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
Self.observe('after save', async function(ctx) {
|
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const httpCtx = {req: loopBackContext.active};
|
|
|
|
const options = ctx.options ? ctx.options : null;
|
|
|
|
const models = Self.app.models;
|
2018-04-20 13:16:03 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
if (!ctx.isNewInstance) return;
|
2018-09-14 06:08:38 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
const data = ctx.instance;
|
|
|
|
const insurance = await Self.findById(data.id, null, options);
|
|
|
|
const client = insurance.classification().customer();
|
2020-09-15 17:43:07 +00:00
|
|
|
const salesPerson = client.salesPersonUser();
|
2018-04-20 13:16:03 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
if (!salesPerson) return;
|
2018-04-20 13:16:03 +00:00
|
|
|
|
2020-01-23 12:31:07 +00:00
|
|
|
const httpRequest = httpCtx.req.http.req;
|
|
|
|
const $t = httpRequest.__;
|
2023-10-16 16:24:58 +00:00
|
|
|
const url = await Self.app.models.Application.getUrl();
|
|
|
|
const fullPath = `${url}client/${client.id}/credit-insurance/index`;
|
2020-01-23 12:31:07 +00:00
|
|
|
const message = $t('MESSAGE_INSURANCE_CHANGE', {
|
|
|
|
clientId: client.id,
|
|
|
|
clientName: client.name,
|
|
|
|
credit: data.credit,
|
|
|
|
url: fullPath
|
|
|
|
});
|
|
|
|
await models.Chat.sendCheckingPresence(httpCtx, salesPerson.id, message);
|
2018-04-20 13:16:03 +00:00
|
|
|
});
|
2018-03-14 10:36:57 +00:00
|
|
|
};
|