Merge pull request '1447-updateClaim_refactor' (#405) from 1447-updateClaim_refactor into dev
gitea/salix/pipeline/head This commit looks good
Details
gitea/salix/pipeline/head This commit looks good
Details
Reviewed-on: #405 Reviewed-by: Joan Sanchez <joan@verdnatura.es>
This commit is contained in:
commit
72ca310c32
|
@ -29,18 +29,18 @@ describe('Update Claim', () => {
|
||||||
it(`should throw an error as the user doesn't have rights`, async() => {
|
it(`should throw an error as the user doesn't have rights`, async() => {
|
||||||
const forbiddenState = 3;
|
const forbiddenState = 3;
|
||||||
const salesPersonId = 18;
|
const salesPersonId = 18;
|
||||||
let data = {
|
|
||||||
claimStateFk: forbiddenState,
|
|
||||||
observation: 'valid observation'
|
|
||||||
};
|
|
||||||
let ctx = {
|
let ctx = {
|
||||||
req: {
|
req: {
|
||||||
accessToken: {
|
accessToken: {
|
||||||
userId: salesPersonId
|
userId: salesPersonId
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
args: {
|
||||||
|
claimStateFk: forbiddenState,
|
||||||
|
observation: 'valid observation'
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
await app.models.Claim.updateClaim(ctx, newInstance.id, data)
|
await app.models.Claim.updateClaim(ctx, newInstance.id)
|
||||||
.catch(e => {
|
.catch(e => {
|
||||||
error = e;
|
error = e;
|
||||||
});
|
});
|
||||||
|
@ -51,23 +51,23 @@ describe('Update Claim', () => {
|
||||||
it(`should success to update the claim within privileges `, async() => {
|
it(`should success to update the claim within privileges `, async() => {
|
||||||
const correctState = 4;
|
const correctState = 4;
|
||||||
const salesPersonId = 18;
|
const salesPersonId = 18;
|
||||||
let data = {
|
|
||||||
observation: 'valid observation',
|
|
||||||
claimStateFk: correctState,
|
|
||||||
hasToPickUp: false
|
|
||||||
};
|
|
||||||
let ctx = {
|
let ctx = {
|
||||||
req: {
|
req: {
|
||||||
accessToken: {
|
accessToken: {
|
||||||
userId: salesPersonId
|
userId: salesPersonId
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
args: {
|
||||||
|
observation: 'valid observation',
|
||||||
|
claimStateFk: correctState,
|
||||||
|
hasToPickUp: false
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
await app.models.Claim.updateClaim(ctx, newInstance.id, data);
|
await app.models.Claim.updateClaim(ctx, newInstance.id);
|
||||||
|
|
||||||
let claimUpdated = await app.models.Claim.findById(newInstance.id);
|
let claimUpdated = await app.models.Claim.findById(newInstance.id);
|
||||||
|
|
||||||
expect(claimUpdated.observation).toEqual(data.observation);
|
expect(claimUpdated.observation).toEqual(ctx.args.observation);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should change some sensible fields as salesAssistant', async() => {
|
it('should change some sensible fields as salesAssistant', async() => {
|
||||||
|
@ -75,28 +75,28 @@ describe('Update Claim', () => {
|
||||||
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
|
||||||
|
|
||||||
const salesAssistantId = 21;
|
const salesAssistantId = 21;
|
||||||
let data = {
|
|
||||||
claimStateFk: 3,
|
|
||||||
workerFk: 5,
|
|
||||||
observation: 'another valid observation',
|
|
||||||
hasToPickUp: true
|
|
||||||
};
|
|
||||||
const ctx = {
|
const ctx = {
|
||||||
req: {
|
req: {
|
||||||
accessToken: {userId: salesAssistantId},
|
accessToken: {userId: salesAssistantId},
|
||||||
headers: {origin: 'http://localhost'}
|
headers: {origin: 'http://localhost'}
|
||||||
|
},
|
||||||
|
args: {
|
||||||
|
claimStateFk: 3,
|
||||||
|
workerFk: 5,
|
||||||
|
observation: 'another valid observation',
|
||||||
|
hasToPickUp: true
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
ctx.req.__ = (value, params) => {
|
ctx.req.__ = (value, params) => {
|
||||||
return params.nickname;
|
return params.nickname;
|
||||||
};
|
};
|
||||||
await app.models.Claim.updateClaim(ctx, newInstance.id, data);
|
await app.models.Claim.updateClaim(ctx, newInstance.id);
|
||||||
|
|
||||||
let claimUpdated = await app.models.Claim.findById(newInstance.id);
|
let claimUpdated = await app.models.Claim.findById(newInstance.id);
|
||||||
|
|
||||||
expect(claimUpdated.observation).toEqual(data.observation);
|
expect(claimUpdated.observation).toEqual(ctx.args.observation);
|
||||||
expect(claimUpdated.claimStateFk).toEqual(data.claimStateFk);
|
expect(claimUpdated.claimStateFk).toEqual(ctx.args.claimStateFk);
|
||||||
expect(claimUpdated.workerFk).toEqual(data.workerFk);
|
expect(claimUpdated.workerFk).toEqual(ctx.args.workerFk);
|
||||||
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -1,35 +1,47 @@
|
||||||
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: 'ctx',
|
||||||
|
type: 'Object',
|
||||||
|
http: {source: 'context'}
|
||||||
|
}, {
|
||||||
arg: 'id',
|
arg: 'id',
|
||||||
type: 'number',
|
type: 'Number',
|
||||||
required: true,
|
|
||||||
description: 'Claim id',
|
description: 'Claim id',
|
||||||
http: {source: 'path'}
|
http: {source: 'path'}
|
||||||
}, {
|
},
|
||||||
arg: 'data',
|
{
|
||||||
type: 'object',
|
arg: 'workerFk',
|
||||||
required: true,
|
type: 'Number'
|
||||||
description: 'Data to update on the model',
|
},
|
||||||
http: {source: 'body'}
|
{
|
||||||
|
arg: 'claimStateFk',
|
||||||
|
type: 'Number'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'observation',
|
||||||
|
type: 'String'
|
||||||
|
},
|
||||||
|
{
|
||||||
|
arg: 'hasToPickUp',
|
||||||
|
type: 'boolean'
|
||||||
}],
|
}],
|
||||||
returns: {
|
returns: {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
root: true
|
root: true
|
||||||
},
|
},
|
||||||
http: {
|
http: {
|
||||||
path: `/:id/updateClaim`,
|
verb: 'patch',
|
||||||
verb: 'post'
|
path: `/updateClaim/:id`
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
Self.updateClaim = async(ctx, id, data) => {
|
Self.updateClaim = async(ctx, id) => {
|
||||||
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;
|
||||||
const $t = ctx.req.__; // $translate
|
const $t = ctx.req.__; // $translate
|
||||||
const claim = await models.Claim.findById(id, {
|
const claim = await models.Claim.findById(id, {
|
||||||
include: {
|
include: {
|
||||||
|
@ -41,17 +53,20 @@ module.exports = Self => {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
let changedHasToPickUp = false;
|
||||||
|
if (args.hasToPickUp)
|
||||||
|
changedHasToPickUp = true;
|
||||||
|
|
||||||
const canUpdate = await canChangeState(ctx, claim.claimStateFk);
|
if (args.claimStateFk) {
|
||||||
const hasRights = await canChangeState(ctx, data.claimStateFk);
|
const canUpdate = await canChangeState(ctx, claim.claimStateFk);
|
||||||
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant');
|
const hasRights = await canChangeState(ctx, args.claimStateFk);
|
||||||
const changedHasToPickUp = claim.hasToPickUp != data.hasToPickUp;
|
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant');
|
||||||
|
|
||||||
if (!canUpdate || !hasRights || changedHasToPickUp && !isSalesAssistant)
|
|
||||||
throw new UserError(`You don't have enough privileges to change that field`);
|
|
||||||
|
|
||||||
const updatedClaim = await claim.updateAttributes(data);
|
|
||||||
|
|
||||||
|
if (!canUpdate || !hasRights || changedHasToPickUp && !isSalesAssistant)
|
||||||
|
throw new UserError(`You don't have enough privileges to change that field`);
|
||||||
|
}
|
||||||
|
delete args.ctx;
|
||||||
|
const updatedClaim = await claim.updateAttributes(args);
|
||||||
// Get sales person from claim client
|
// Get sales person from claim client
|
||||||
const salesPerson = claim.client().salesPersonUser();
|
const salesPerson = claim.client().salesPersonUser();
|
||||||
if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) {
|
if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) {
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
<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"
|
||||||
insert-mode="true"
|
|
||||||
form="form">
|
form="form">
|
||||||
</vn-watcher>
|
</vn-watcher>
|
||||||
<vn-crud-model
|
<vn-crud-model
|
||||||
|
|
Loading…
Reference in New Issue