142 lines
4.6 KiB
JavaScript
142 lines
4.6 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
module.exports = Self => {
|
|
Self.remoteMethod('updateClaim', {
|
|
description: 'Update a claim with privileges',
|
|
accessType: 'WRITE',
|
|
accepts: [{
|
|
arg: 'ctx',
|
|
type: 'object',
|
|
http: {source: 'context'}
|
|
},
|
|
{
|
|
arg: 'id',
|
|
type: 'number',
|
|
description: 'Claim id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'workerFk',
|
|
type: 'number'
|
|
},
|
|
{
|
|
arg: 'claimStateFk',
|
|
type: 'number'
|
|
},
|
|
{
|
|
arg: 'observation',
|
|
type: 'string'
|
|
},
|
|
{
|
|
arg: 'hasToPickUp',
|
|
type: 'boolean'
|
|
},
|
|
{
|
|
arg: 'packages',
|
|
type: 'number'
|
|
}],
|
|
returns: {
|
|
type: 'object',
|
|
root: true
|
|
},
|
|
http: {
|
|
verb: 'patch',
|
|
path: `/updateClaim/:id`
|
|
}
|
|
});
|
|
|
|
Self.updateClaim = async(ctx, id, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
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();
|
|
|
|
let changedHasToPickUp = false;
|
|
if (args.hasToPickUp)
|
|
changedHasToPickUp = true;
|
|
|
|
// Validate when claimState has been changed
|
|
if (args.claimStateFk) {
|
|
const canEditOldState = await models.ClaimState.isEditable(ctx, claim.claimStateFk, myOptions);
|
|
const canEditNewState = await models.ClaimState.isEditable(ctx, args.claimStateFk, myOptions);
|
|
const canEditState = await models.ACL.checkAccessAcl(ctx, 'Claim', 'editState', 'WRITE');
|
|
|
|
if (!canEditOldState || !canEditNewState || changedHasToPickUp && !canEditState)
|
|
throw new UserError(`You don't have enough privileges to change that field`);
|
|
}
|
|
|
|
delete args.ctx;
|
|
const updatedClaim = await claim.updateAttributes(args, myOptions);
|
|
|
|
// When hasToPickUp has been changed
|
|
if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp)
|
|
notifyPickUp(ctx, salesPerson.id, claim);
|
|
|
|
// When claimState has been changed
|
|
if (args.claimStateFk) {
|
|
const newState = await models.ClaimState.findById(args.claimStateFk, null, options);
|
|
if (newState.hasToNotify) {
|
|
if (newState.code == 'incomplete')
|
|
notifyStateChange(ctx, salesPerson.id, claim, newState.code);
|
|
if (newState.code == 'canceled')
|
|
notifyStateChange(ctx, claim.workerFk, claim, newState.code);
|
|
}
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedClaim;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
async function notifyStateChange(ctx, workerId, claim, state) {
|
|
const models = Self.app.models;
|
|
const origin = ctx.req.headers.origin;
|
|
const $t = ctx.req.__; // $translate
|
|
|
|
const message = $t(`Claim state has changed to ${state}`, {
|
|
claimId: claim.id,
|
|
clientName: claim.client().name,
|
|
claimUrl: `${origin}/#!/claim/${claim.id}/summary`
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, workerId, message);
|
|
}
|
|
|
|
async function notifyPickUp(ctx, workerId, claim) {
|
|
const origin = ctx.req.headers.origin;
|
|
const models = Self.app.models;
|
|
const $t = ctx.req.__; // $translate
|
|
|
|
const message = $t('Claim will be picked', {
|
|
claimId: claim.id,
|
|
clientName: claim.client().name,
|
|
claimUrl: `${origin}/#!/claim/${claim.id}/summary`
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, workerId, message);
|
|
}
|
|
};
|