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

This commit is contained in:
Bernat Exposito Domenech 2020-10-06 14:23:35 +02:00
parent 134d1c4385
commit 3d39467613
3 changed files with 49 additions and 45 deletions

View File

@ -29,18 +29,18 @@ describe('Update Claim', () => {
it(`should throw an error as the user doesn't have rights`, async() => {
const forbiddenState = 3;
const salesPersonId = 18;
let data = {
claimStateFk: forbiddenState,
observation: 'valid observation'
};
let ctx = {
req: {
accessToken: {
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 => {
error = e;
});
@ -51,23 +51,23 @@ describe('Update Claim', () => {
it(`should success to update the claim within privileges `, async() => {
const correctState = 4;
const salesPersonId = 18;
let data = {
observation: 'valid observation',
claimStateFk: correctState,
hasToPickUp: false
};
let ctx = {
req: {
accessToken: {
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);
expect(claimUpdated.observation).toEqual(data.observation);
expect(claimUpdated.observation).toEqual(ctx.args.observation);
});
it('should change some sensible fields as salesAssistant', async() => {
@ -75,28 +75,28 @@ describe('Update Claim', () => {
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
const salesAssistantId = 21;
let data = {
claimStateFk: 3,
workerFk: 5,
observation: 'another valid observation',
hasToPickUp: true
};
const ctx = {
req: {
accessToken: {userId: salesAssistantId},
headers: {origin: 'http://localhost'}
},
args: {
claimStateFk: 3,
workerFk: 5,
observation: 'another valid observation',
hasToPickUp: true
}
};
ctx.req.__ = (value, params) => {
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);
expect(claimUpdated.observation).toEqual(data.observation);
expect(claimUpdated.claimStateFk).toEqual(data.claimStateFk);
expect(claimUpdated.workerFk).toEqual(data.workerFk);
expect(claimUpdated.observation).toEqual(ctx.args.observation);
expect(claimUpdated.claimStateFk).toEqual(ctx.args.claimStateFk);
expect(claimUpdated.workerFk).toEqual(ctx.args.workerFk);
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
});
});

View File

@ -7,18 +7,18 @@ module.exports = Self => {
type: 'Object',
http: {source: 'context'}
}, {
arg: 'claimId',
type: 'number',
arg: 'id',
type: 'Number',
description: 'Claim id',
http: {source: 'path'}
},
{
arg: 'worker',
type: 'String'
arg: 'workerFk',
type: 'Number'
},
{
arg: 'claimState',
type: 'String'
arg: 'claimStateFk',
type: 'Number'
},
{
arg: 'observation',
@ -26,25 +26,24 @@ module.exports = Self => {
},
{
arg: 'hasToPickUp',
type: 'String'
type: 'boolean'
}],
returns: {
type: 'object',
root: true
},
http: {
verb: 'post',
path: `/updateClaim/:claimId`
verb: 'patch',
path: `/updateClaim/:id`
}
});
Self.updateClaim = async(ctx, claimId) => {
Self.updateClaim = async(ctx, id) => {
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(claimId, {
const claim = await models.Claim.findById(id, {
include: {
relation: 'client',
scope: {
@ -54,18 +53,20 @@ module.exports = Self => {
}
}
});
let changedHasToPickUp = false;
if (args.hasToPickUp)
changedHasToPickUp = true;
const canUpdate = await canChangeState(ctx, claim.claimStateFk);
const hasRights = await canChangeState(ctx, args.claimState);
const isSalesAssistant = await models.Account.hasRole(userId, 'salesAssistant');
const changedHasToPickUp = claim.hasToPickUp != args.hasToPickUp;
if (!canUpdate || !hasRights || changedHasToPickUp && !isSalesAssistant)
throw new UserError(`You don't have enough privileges to change that field`);
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');
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(ctx.args);
const updatedClaim = await claim.updateAttributes(args);
// Get sales person from claim client
const salesPerson = claim.client().salesPersonUser();
if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) {

View File

@ -1,9 +1,12 @@
<!-- <mg-ajax
path="Claims/{{edit.params.id}}"
actions="$ctrl.claim = edit.model"
options="mgEdit">
</mg-ajax> -->
<vn-watcher
vn-id="watcher"
url="Claims/updateClaim"
data="$ctrl.claim"
id-field="id"
insert-mode="true"
form="form">
</vn-watcher>
<vn-crud-model