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

View File

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