From e6b4d21ddbf0d68c2212ab5bfd9f82a696d0a8b0 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 6 Oct 2020 10:57:40 +0200 Subject: [PATCH] refactor updateClaim --- .../claim/back/methods/claim/updateClaim.js | 50 ++++++++++++------- modules/claim/front/basic-data/index.html | 3 +- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index b7156feef..4457aaa6b 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -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(); diff --git a/modules/claim/front/basic-data/index.html b/modules/claim/front/basic-data/index.html index c73e04b24..70584af6e 100644 --- a/modules/claim/front/basic-data/index.html +++ b/modules/claim/front/basic-data/index.html @@ -1,7 +1,8 @@