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',
|
|
|
|
allowNull: false, // FIXME: Ignored by loopback when it's false
|
|
|
|
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: {
|
|
|
|
creditClassification: this.creditClassification
|
|
|
|
},
|
|
|
|
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'
|
|
|
|
});
|
|
|
|
|
2018-04-20 13:16:03 +00:00
|
|
|
Self.messageSend = async function(data, accessToken) {
|
|
|
|
let filter = {
|
|
|
|
include: {
|
|
|
|
relation: 'classification',
|
|
|
|
scope: {
|
|
|
|
fields: ['client'],
|
|
|
|
include: {
|
|
|
|
relation: 'customer',
|
|
|
|
scope: {
|
|
|
|
fields: ['name', 'salesPersonFk'],
|
|
|
|
include: {
|
|
|
|
relation: 'salesPerson',
|
|
|
|
scope: {
|
|
|
|
fields: 'userFk',
|
|
|
|
include: {
|
|
|
|
relation: 'user',
|
|
|
|
scope: {
|
|
|
|
fields: ['name']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let ctx = {req: {accessToken: accessToken}};
|
|
|
|
let insurance = await Self.findById(data.id, filter);
|
|
|
|
let customer = insurance.classification().customer();
|
|
|
|
let salesPerson = customer.salesPerson().user().name;
|
|
|
|
let grade = data.grade ? `(Grado ${data.grade})` : '(Sin grado)';
|
|
|
|
let message = {
|
|
|
|
message: `He cambiado el crédito asegurado del cliente "${customer.name}" a ${data.credit} € ${grade}`
|
|
|
|
};
|
|
|
|
|
|
|
|
Self.app.models.Message.send(salesPerson, message, ctx);
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update from transaction misses ctx accessToken.
|
|
|
|
// Fixed passing accessToken from method messageSend()
|
|
|
|
Self.observe('after save', async function(ctx) {
|
|
|
|
if (ctx.options.accessToken)
|
|
|
|
await Self.messageSend(ctx.instance, ctx.options.accessToken);
|
|
|
|
});
|
2018-03-14 10:36:57 +00:00
|
|
|
};
|