118 lines
3.6 KiB
JavaScript
118 lines
3.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: 'pickup',
|
|
type: 'any'
|
|
},
|
|
{
|
|
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);
|
|
|
|
const changedPickup = args.pickup && args.pickup != claim.pickup;
|
|
|
|
if (args.claimStateFk) {
|
|
const canEditOldState = await models.ClaimState.isEditable(ctx, claim.claimStateFk, myOptions);
|
|
const canEditNewState = await models.ClaimState.isEditable(ctx, args.claimStateFk, myOptions);
|
|
const canEditPickup = await models.ACL.checkAccessAcl(ctx, 'Claim', 'editPickup', 'WRITE');
|
|
|
|
if (!canEditOldState || !canEditNewState || (changedPickup && !canEditPickup))
|
|
throw new UserError(`You don't have enough privileges to change that field`);
|
|
}
|
|
|
|
delete args.ctx;
|
|
const updatedClaim = await claim.updateAttributes(args, myOptions);
|
|
|
|
const salesPerson = claim.client().salesPersonUser();
|
|
if (salesPerson && args.claimStateFk) {
|
|
const newState = await models.ClaimState.findById(args.claimStateFk, null, myOptions);
|
|
await notifyStateChange(ctx, salesPerson.id, claim, newState.description);
|
|
if (newState.code == 'canceled')
|
|
await notifyStateChange(ctx, claim.workerFk, claim, newState.description);
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return updatedClaim;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
|
|
async function notifyStateChange(ctx, workerId, claim, newState) {
|
|
const models = Self.app.models;
|
|
const url = await models.Url.getUrl();
|
|
const $t = ctx.req.__;
|
|
|
|
const message = $t(`Claim state has changed to`, {
|
|
claimId: claim.id,
|
|
clientName: claim.client().name,
|
|
claimUrl: `${url}claim/${claim.id}/summary`,
|
|
newState
|
|
});
|
|
await models.Chat.sendCheckingPresence(ctx, workerId, message);
|
|
}
|
|
};
|