2023-12-20 13:08:11 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2018-09-05 11:47:15 +00:00
|
|
|
module.exports = Self => {
|
2023-12-20 13:08:11 +00:00
|
|
|
let cache = {};
|
2019-07-30 06:51:38 +00:00
|
|
|
require('../methods/claim/filter')(Self);
|
2018-09-05 11:47:15 +00:00
|
|
|
require('../methods/claim/getSummary')(Self);
|
2018-09-11 10:49:31 +00:00
|
|
|
require('../methods/claim/createFromSales')(Self);
|
2018-09-19 05:41:07 +00:00
|
|
|
require('../methods/claim/updateClaim')(Self);
|
2018-10-08 05:31:55 +00:00
|
|
|
require('../methods/claim/regularizeClaim')(Self);
|
2019-07-30 06:51:38 +00:00
|
|
|
require('../methods/claim/uploadFile')(Self);
|
2023-12-20 11:09:01 +00:00
|
|
|
require('../methods/claim/claimObservation')(Self);
|
2019-09-26 07:13:18 +00:00
|
|
|
require('../methods/claim/updateClaimAction')(Self);
|
2022-04-08 05:37:17 +00:00
|
|
|
require('../methods/claim/updateClaimDestination')(Self);
|
2022-04-08 07:34:46 +00:00
|
|
|
require('../methods/claim/downloadFile')(Self);
|
2022-09-22 07:17:14 +00:00
|
|
|
require('../methods/claim/claimPickupPdf')(Self);
|
|
|
|
require('../methods/claim/claimPickupEmail')(Self);
|
2023-03-06 11:37:37 +00:00
|
|
|
require('../methods/claim/logs')(Self);
|
2023-12-20 11:09:01 +00:00
|
|
|
|
2023-12-20 13:08:11 +00:00
|
|
|
Self.observe('before save', async ctx => {
|
2023-12-20 11:09:01 +00:00
|
|
|
if (ctx.isNewInstance) return;
|
2023-12-20 13:08:11 +00:00
|
|
|
const {data, currentInstance} = ctx;
|
|
|
|
let changes = {};
|
|
|
|
for (const [key, value] of Object.entries(data)) {
|
|
|
|
const change = currentInstance[key];
|
|
|
|
if (change !== value)
|
|
|
|
changes[key] = value;
|
|
|
|
}
|
|
|
|
cache[currentInstance.id] = changes;
|
|
|
|
});
|
|
|
|
Self.observe('after save', async ctx => {
|
|
|
|
const changes = cache[ctx.instance.id];
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
if (Object.keys(changes).length > 0) await sendMessage(ctx, changes);
|
|
|
|
|
|
|
|
delete cache[ctx.instance.id];
|
2023-12-20 11:09:01 +00:00
|
|
|
});
|
2023-12-20 13:08:11 +00:00
|
|
|
async function sendMessage(ctx, changes) {
|
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
const {http} = loopBackContext.active;
|
|
|
|
|
|
|
|
const message = buildMessage(http.req.__, ctx.instance, changes);
|
|
|
|
const instance = ctx.instance.client();
|
|
|
|
await Self.app.models.Chat.send({...http}, instance.salesPersonUser().username, message);
|
|
|
|
}
|
|
|
|
|
|
|
|
function buildMessage($t, instance, changes) {
|
|
|
|
let message = $t('This claim has been updated', {claimId: instance.id});
|
|
|
|
return message;
|
|
|
|
}
|
2018-09-05 11:47:15 +00:00
|
|
|
};
|