Send message to comercial by RocketChat #1897
|
@ -1,6 +1,6 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
|
||||
fdescribe('Chat send()', () => {
|
||||
describe('Chat send()', () => {
|
||||
it('should return true as response', async() => {
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
let response = await models.Chat.send(ctx, '@salesPerson', 'I changed something');
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
const models = require('vn-loopback/server/server').models;
|
||||
|
||||
fdescribe('Chat sendCheckingPresence()', () => {
|
||||
describe('Chat sendCheckingPresence()', () => {
|
||||
const today = Date.vnNew();
|
||||
today.setHours(6, 0);
|
||||
const chatModel = models.Chat;
|
||||
|
|
|
@ -1,70 +0,0 @@
|
|||
module.exports = Self => {
|
||||
Self.remoteMethod('claimObservation', {
|
||||
description: 'Update a claim with privileges',
|
||||
accessType: 'WRITE',
|
||||
accepts: [{
|
||||
arg: 'ctx',
|
||||
type: 'object',
|
||||
http: {source: 'context'}
|
||||
},
|
||||
{
|
||||
arg: 'data',
|
||||
type: 'object',
|
||||
http: {source: 'body'}
|
||||
}],
|
||||
returns: {
|
||||
type: 'object',
|
||||
root: true
|
||||
},
|
||||
http: {
|
||||
verb: 'post',
|
||||
path: `/claimObservation`
|
||||
}
|
||||
});
|
||||
|
||||
Self.claimObservation = async(ctx, data, options) => {
|
||||
const {claimFk: id} = data;
|
||||
const {models} = Self.app;
|
||||
const myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
Object.assign(myOptions, options);
|
||||
|
||||
try {
|
||||
const claim = await models.Claim.findById(id, {
|
||||
include: {
|
||||
relation: 'client',
|
||||
scope: {
|
||||
include: {
|
||||
relation: 'salesPersonUser'
|
||||
}
|
||||
}
|
||||
}
|
||||
}, myOptions);
|
||||
// Get sales person from claim client
|
||||
const salesPerson = claim.client().salesPersonUser();
|
||||
const message = {
|
||||
body: data.text
|
||||
};
|
||||
// await notifyClaimObservation(ctx, claim.workerFk, claim, newState.code);
|
||||
await models.Chat.send(ctx, salesPerson.username, message);
|
||||
|
||||
return updatedClaim;
|
||||
} catch (e) {
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
// async function notifyClaimObservation(ctx, from, to, claim, observation) {
|
||||
// const models = Self.app.models;
|
||||
// const url = await models.Url.getUrl();
|
||||
// const $t = ctx.req.__; // $translate
|
||||
|
||||
// const message = $t(`Claim state has changed to ${state}`, {
|
||||
// claimId: claim.id,
|
||||
// clientName: claim.client().name,
|
||||
// claimUrl: `${url}claim/${claim.id}/summary`
|
||||
// });
|
||||
// await models.Chat.sendQueue(ctx, workerId, message);
|
||||
// }
|
||||
};
|
|
@ -1,25 +0,0 @@
|
|||
const {models} = require('vn-loopback/server/server');
|
||||
|
||||
fdescribe('Claim observation()', () => {
|
||||
it('should save observation', async() => {
|
||||
let ctx = {req: {accessToken: {userId: 1}}};
|
||||
const tx = await models.Supplier.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
const data = {
|
||||
claimFk: 9,
|
||||
workerFk: 9,
|
||||
text: 'test'
|
||||
};
|
||||
try {
|
||||
let response = await models.Claim.claimObservation(ctx, data, options);
|
||||
|
||||
if (tx) await tx.commit();
|
||||
|
||||
expect(response).not.toBeUndefined();
|
||||
} catch (error) {
|
||||
expect(error).toBeUndefined();
|
||||
if (tx) await tx.rollback();
|
||||
throw new Error(e);
|
||||
}
|
||||
});
|
||||
});
|
|
@ -1,7 +1,7 @@
|
|||
const app = require('vn-loopback/server/server');
|
||||
const LoopBackContext = require('loopback-context');
|
||||
const i18n = require('i18n');
|
||||
fdescribe('Update Claim', () => {
|
||||
describe('Update Claim', () => {
|
||||
let url;
|
||||
let claimStatesMap = {};
|
||||
beforeAll(async() => {
|
||||
|
@ -32,7 +32,7 @@ fdescribe('Update Claim', () => {
|
|||
observation: 'observation'
|
||||
};
|
||||
|
||||
fit(`should throw an error as the user doesn't have rights`, async() => {
|
||||
it(`should throw an error as the user doesn't have rights`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
let error;
|
||||
|
||||
|
@ -65,47 +65,6 @@ fdescribe('Update Claim', () => {
|
|||
expect(error.message).toEqual(`You don't have enough privileges to change that field`);
|
||||
});
|
||||
|
||||
fit(`should success to update the claimState to 'canceled' and send a rocket message`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const newClaim = await app.models.Claim.create(originalData, options);
|
||||
|
||||
const chatModel = app.models.Chat;
|
||||
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
||||
|
||||
const canceledState = 4;
|
||||
const claimManagerId = 72;
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: claimManagerId},
|
||||
headers: {origin: url}
|
||||
},
|
||||
args: {
|
||||
observation: 'valid observation',
|
||||
claimStateFk: canceledState,
|
||||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = (value, params) => {
|
||||
return params.nickname;
|
||||
};
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
||||
expect(updatedClaim.observation).toEqual(ctx.args.observation);
|
||||
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it(`should success to update the claimState to 'pending' and send a rocket message`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
|
@ -130,7 +89,124 @@ fdescribe('Update Claim', () => {
|
|||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = i18n;
|
||||
ctx.req.__ = i18n.__;
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
||||
expect(updatedClaim.observation).toEqual(ctx.args.observation);
|
||||
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it(`should success to update the claimState to 'managed' and send a rocket message`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const newClaim = await app.models.Claim.create(originalData, options);
|
||||
|
||||
const chatModel = app.models.Chat;
|
||||
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
||||
|
||||
const managedState = claimStatesMap.managed;
|
||||
const claimManagerId = 72;
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: claimManagerId},
|
||||
headers: {origin: url}
|
||||
},
|
||||
args: {
|
||||
observation: 'valid observation',
|
||||
claimStateFk: managedState,
|
||||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = i18n.__;
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
||||
expect(updatedClaim.observation).toEqual(ctx.args.observation);
|
||||
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it(`should success to update the claimState to 'resolved' and send a rocket message`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const newClaim = await app.models.Claim.create(originalData, options);
|
||||
|
||||
const chatModel = app.models.Chat;
|
||||
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
||||
|
||||
const resolvedState = claimStatesMap.resolved;
|
||||
const claimManagerId = 72;
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: claimManagerId},
|
||||
headers: {origin: url}
|
||||
},
|
||||
args: {
|
||||
observation: 'valid observation',
|
||||
claimStateFk: resolvedState,
|
||||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = i18n.__;
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
||||
expect(updatedClaim.observation).toEqual(ctx.args.observation);
|
||||
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it(`should success to update the claimState to 'canceled' and send a rocket message`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const newClaim = await app.models.Claim.create(originalData, options);
|
||||
|
||||
const chatModel = app.models.Chat;
|
||||
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
||||
|
||||
const canceledState = claimStatesMap.canceled;
|
||||
const claimManagerId = 72;
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: claimManagerId},
|
||||
headers: {origin: url}
|
||||
},
|
||||
args: {
|
||||
observation: 'valid observation',
|
||||
claimStateFk: canceledState,
|
||||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = i18n.__;
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
@ -169,50 +245,7 @@ fdescribe('Update Claim', () => {
|
|||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = (value, params) => {
|
||||
return params.nickname;
|
||||
};
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
||||
expect(updatedClaim.observation).toEqual(ctx.args.observation);
|
||||
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
await tx.rollback();
|
||||
throw e;
|
||||
}
|
||||
});
|
||||
|
||||
it(`should success to update the claimState to 'incomplete' and send a rocket message`, async() => {
|
||||
const tx = await app.models.Claim.beginTransaction({});
|
||||
|
||||
try {
|
||||
const options = {transaction: tx};
|
||||
|
||||
const newClaim = await app.models.Claim.create(originalData, options);
|
||||
|
||||
const chatModel = app.models.Chat;
|
||||
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
||||
|
||||
const incompleteState = 5;
|
||||
const claimManagerId = 72;
|
||||
const ctx = {
|
||||
req: {
|
||||
accessToken: {userId: claimManagerId},
|
||||
headers: {origin: url}
|
||||
},
|
||||
args: {
|
||||
observation: 'valid observation',
|
||||
claimStateFk: incompleteState,
|
||||
hasToPickUp: false
|
||||
}
|
||||
};
|
||||
ctx.req.__ = (value, params) => {
|
||||
return params.nickname;
|
||||
};
|
||||
ctx.req.__ = i18n.__;
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
@ -251,9 +284,7 @@ fdescribe('Update Claim', () => {
|
|||
hasToPickUp: true
|
||||
}
|
||||
};
|
||||
ctx.req.__ = (value, params) => {
|
||||
return params.nickname;
|
||||
};
|
||||
ctx.req.__ = i18n.__;
|
||||
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
|
||||
|
||||
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
|
||||
|
|
|
@ -1,49 +1,14 @@
|
|||
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);
|
||||
require('../methods/claim/updateClaim')(Self);
|
||||
require('../methods/claim/regularizeClaim')(Self);
|
||||
require('../methods/claim/uploadFile')(Self);
|
||||
require('../methods/claim/claimObservation')(Self);
|
||||
require('../methods/claim/updateClaimAction')(Self);
|
||||
require('../methods/claim/updateClaimDestination')(Self);
|
||||
require('../methods/claim/downloadFile')(Self);
|
||||
require('../methods/claim/claimPickupPdf')(Self);
|
||||
require('../methods/claim/claimPickupEmail')(Self);
|
||||
require('../methods/claim/logs')(Self);
|
||||
|
||||
Self.observe('before save', async ctx => {
|
||||
if (ctx.isNewInstance) return;
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue