2018-12-27 11:54:16 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2018-09-19 05:41:07 +00:00
|
|
|
module.exports = Self => {
|
2020-10-06 08:57:40 +00:00
|
|
|
Self.remoteMethod('updateClaim', {
|
2018-09-19 05:41:07 +00:00
|
|
|
description: 'Update a claim with privileges',
|
|
|
|
accepts: [{
|
2020-10-06 08:57:40 +00:00
|
|
|
arg: 'ctx',
|
|
|
|
type: 'Object',
|
|
|
|
http: {source: 'context'}
|
|
|
|
}, {
|
2020-10-06 12:23:35 +00:00
|
|
|
arg: 'id',
|
|
|
|
type: 'Number',
|
2019-09-26 07:13:18 +00:00
|
|
|
description: 'Claim id',
|
2019-01-29 10:14:01 +00:00
|
|
|
http: {source: 'path'}
|
2020-10-06 08:57:40 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-06 12:23:35 +00:00
|
|
|
arg: 'workerFk',
|
|
|
|
type: 'Number'
|
2020-10-06 08:57:40 +00:00
|
|
|
},
|
|
|
|
{
|
2020-10-06 12:23:35 +00:00
|
|
|
arg: 'claimStateFk',
|
|
|
|
type: 'Number'
|
2020-10-06 08:57:40 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'observation',
|
|
|
|
type: 'String'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
arg: 'hasToPickUp',
|
2020-10-06 12:23:35 +00:00
|
|
|
type: 'boolean'
|
2018-09-19 05:41:07 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2019-09-26 07:13:18 +00:00
|
|
|
type: 'object',
|
2018-09-19 05:41:07 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
2020-10-06 12:23:35 +00:00
|
|
|
verb: 'patch',
|
|
|
|
path: `/updateClaim/:id`
|
2018-09-19 05:41:07 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-06 12:23:35 +00:00
|
|
|
Self.updateClaim = async(ctx, id) => {
|
2020-06-03 12:46:58 +00:00
|
|
|
const models = Self.app.models;
|
2020-06-04 06:21:08 +00:00
|
|
|
const userId = ctx.req.accessToken.userId;
|
2020-10-06 08:57:40 +00:00
|
|
|
const args = ctx.args;
|
2020-06-03 12:46:58 +00:00
|
|
|
const $t = ctx.req.__; // $translate
|
2020-10-06 12:23:35 +00:00
|
|
|
const claim = await models.Claim.findById(id, {
|
2020-06-03 12:46:58 +00:00
|
|
|
include: {
|
|
|
|
relation: 'client',
|
|
|
|
scope: {
|
|
|
|
include: {
|
2020-08-14 15:47:13 +00:00
|
|
|
relation: 'salesPersonUser'
|
2020-06-03 12:46:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2020-10-06 12:23:35 +00:00
|
|
|
let changedHasToPickUp = false;
|
|
|
|
if (args.hasToPickUp)
|
|
|
|
changedHasToPickUp = true;
|
2018-09-19 05:41:07 +00:00
|
|
|
|
2020-10-06 12:23:35 +00:00
|
|
|
if (args.claimStateFk) {
|
|
|
|
const canUpdate = await canChangeState(ctx, claim.claimStateFk);
|
|
|
|
const hasRights = await canChangeState(ctx, args.claimStateFk);
|
|
|
|
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant');
|
2018-09-19 05:41:07 +00:00
|
|
|
|
2020-10-06 12:23:35 +00:00
|
|
|
if (!canUpdate || !hasRights || changedHasToPickUp && !isSalesAssistant)
|
|
|
|
throw new UserError(`You don't have enough privileges to change that field`);
|
|
|
|
}
|
2020-10-06 08:57:40 +00:00
|
|
|
delete args.ctx;
|
2020-10-06 12:23:35 +00:00
|
|
|
const updatedClaim = await claim.updateAttributes(args);
|
2020-06-03 12:46:58 +00:00
|
|
|
// Get sales person from claim client
|
2020-08-14 16:42:45 +00:00
|
|
|
const salesPerson = claim.client().salesPersonUser();
|
2020-06-04 08:12:54 +00:00
|
|
|
if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) {
|
2020-06-03 12:46:58 +00:00
|
|
|
const origin = ctx.req.headers.origin;
|
|
|
|
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, salesPerson.id, message);
|
|
|
|
}
|
|
|
|
|
2020-06-04 05:53:06 +00:00
|
|
|
return updatedClaim;
|
2018-09-19 05:41:07 +00:00
|
|
|
};
|
2019-09-26 07:13:18 +00:00
|
|
|
|
|
|
|
async function canChangeState(ctx, id) {
|
|
|
|
let models = Self.app.models;
|
|
|
|
let userId = ctx.req.accessToken.userId;
|
|
|
|
|
|
|
|
let state = await models.ClaimState.findById(id, {
|
|
|
|
include: {
|
|
|
|
relation: 'writeRole'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let stateRole = state.writeRole().name;
|
|
|
|
let canUpdate = await models.Account.hasRole(userId, stateRole);
|
|
|
|
|
|
|
|
return canUpdate;
|
|
|
|
}
|
2018-09-19 05:41:07 +00:00
|
|
|
};
|