refactor updateClaim
gitea/salix/pipeline/head This commit looks good Details

This commit is contained in:
Bernat Exposito Domenech 2020-10-06 10:57:40 +02:00
parent eb731eb648
commit e6b4d21ddb
2 changed files with 34 additions and 19 deletions

View File

@ -1,37 +1,50 @@
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('updateClaim', {
Self.remoteMethod('updateClaim', {
description: 'Update a claim with privileges',
accessType: 'WRITE',
accepts: [{
arg: 'id',
arg: 'ctx',
type: 'Object',
http: {source: 'context'}
}, {
arg: 'claimId',
type: 'number',
required: true,
description: 'Claim id',
http: {source: 'path'}
}, {
arg: 'data',
type: 'object',
required: true,
description: 'Data to update on the model',
http: {source: 'body'}
},
{
arg: 'worker',
type: 'String'
},
{
arg: 'claimState',
type: 'String'
},
{
arg: 'observation',
type: 'String'
},
{
arg: 'hasToPickUp',
type: 'String'
}],
returns: {
type: 'object',
root: true
},
http: {
path: `/:id/updateClaim`,
verb: 'post'
verb: 'post',
path: `/updateClaim/:claimId`
}
});
Self.updateClaim = async(ctx, id, data) => {
Self.updateClaim = async(ctx, claimId) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const args = ctx.args;
console.log('args', args);
const $t = ctx.req.__; // $translate
const claim = await models.Claim.findById(id, {
const claim = await models.Claim.findById(claimId, {
include: {
relation: 'client',
scope: {
@ -43,14 +56,15 @@ module.exports = Self => {
});
const canUpdate = await canChangeState(ctx, claim.claimStateFk);
const hasRights = await canChangeState(ctx, data.claimStateFk);
const hasRights = await canChangeState(ctx, args.claimState);
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant');
const changedHasToPickUp = claim.hasToPickUp != data.hasToPickUp;
const changedHasToPickUp = claim.hasToPickUp != args.hasToPickUp;
if (!canUpdate || !hasRights || changedHasToPickUp && !isSalesAssistant)
throw new UserError(`You don't have enough privileges to change that field`);
const updatedClaim = await claim.updateAttributes(data);
delete args.ctx;
const updatedClaim = await claim.updateAttributes(ctx.args);
// Get sales person from claim client
const salesPerson = claim.client().salesPersonUser();

View File

@ -1,7 +1,8 @@
<vn-watcher
vn-id="watcher"
url="Claims/{{$ctrl.$params.id}}/updateClaim"
url="Claims/updateClaim"
data="$ctrl.claim"
id-field="id"
insert-mode="true"
form="form">
</vn-watcher>