Send message to comercial by RocketChat #1897

Merged
jsegarra merged 10 commits from 5499_claim_rocketMessage_comercial into dev 2024-01-11 07:00:13 +00:00
2 changed files with 34 additions and 8 deletions
Showing only changes of commit 5c5be788c1 - Show all commits

View File

@ -329,5 +329,6 @@
"The amount cannot be less than the minimum": "La cantidad no puede ser menor que la cantidad mínima",
"quantityLessThanMin": "La cantidad no puede ser menor que la cantidad mínima",
"Cannot past travels with entries": "No se pueden pasar envíos con entradas",
"It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}"
}
"It was not able to remove the next expeditions:": "No se pudo eliminar las siguientes expediciones: {{expeditions}}",
"This claim has been updated": "La reclamación con Id: {{claimId}}, ha sido actualizada"
}

View File

@ -1,4 +1,6 @@
const LoopBackContext = require('loopback-context');
module.exports = Self => {
let cache = {};
require('../methods/claim/filter')(Self);
require('../methods/claim/getSummary')(Self);
require('../methods/claim/createFromSales')(Self);
@ -13,12 +15,35 @@ module.exports = Self => {
require('../methods/claim/claimPickupEmail')(Self);
require('../methods/claim/logs')(Self);
Self.observe('after save', ctx => {
Self.observe('before save', async ctx => {
if (ctx.isNewInstance) return;
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
const loopBackContext = LoopBackContext.getCurrentContext();
const accessToken = {req: loopBackContext.active};
throw new Error('');
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];
});
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;
}
};