updateClaim refactor + tests

This commit is contained in:
Carlos Jimenez Ruiz 2021-03-29 15:17:10 +02:00
parent c4cbc064c9
commit 3054cbcadb
2 changed files with 152 additions and 108 deletions

View File

@ -1,7 +1,7 @@
const app = require('vn-loopback/server/server');
describe('Update Claim', () => {
let newDate = new Date();
const newDate = new Date();
const originalData = {
ticketFk: 3,
clientFk: 101,
@ -14,7 +14,15 @@ describe('Update Claim', () => {
};
it(`should throw an error as the user doesn't have rights`, async() => {
let newClaim = await app.models.Claim.create(originalData);
const tx = await app.models.Claim.beginTransaction({});
let error;
try {
const options = {transaction: tx};
const newClaim = await app.models.Claim.create(originalData, options);
const forbiddenState = 3;
const salesPersonId = 18;
const ctx = {
@ -28,19 +36,24 @@ describe('Update Claim', () => {
observation: 'valid observation'
}
};
await app.models.Claim.updateClaim(ctx, newClaim.id)
.catch(e => {
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
await tx.rollback();
} catch (e) {
error = e;
});
await tx.rollback();
}
expect(error.message).toEqual(`You don't have enough privileges to change that field`);
// restores
await app.models.Claim.destroyById(newClaim.id);
});
it(`should success to update the claim within privileges `, async() => {
let newClaim = await app.models.Claim.create(originalData);
const tx = await app.models.Claim.beginTransaction({});
try {
const options = {transaction: tx};
const newClaim = await app.models.Claim.create(originalData, options);
const canceledState = 4;
const claimManagerId = 72;
@ -56,18 +69,27 @@ describe('Update Claim', () => {
hasToPickUp: false
}
};
await app.models.Claim.updateClaim(ctx, newClaim.id);
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
let updatedClaim = await app.models.Claim.findById(newClaim.id);
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
expect(updatedClaim.observation).toEqual(ctx.args.observation);
// restores
await app.models.Claim.destroyById(newClaim.id);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should change some sensible fields as claimManager', async() => {
let newClaim = await app.models.Claim.create(originalData);
const tx = await app.models.Claim.beginTransaction({});
try {
const options = {transaction: tx};
const newClaim = await app.models.Claim.create(originalData, options);
const chatModel = app.models.Chat;
spyOn(chatModel, 'sendCheckingPresence').and.callThrough();
@ -87,16 +109,19 @@ describe('Update Claim', () => {
ctx.req.__ = (value, params) => {
return params.nickname;
};
await app.models.Claim.updateClaim(ctx, newClaim.id);
await app.models.Claim.updateClaim(ctx, newClaim.id, options);
let updatedClaim = await app.models.Claim.findById(newClaim.id);
let updatedClaim = await app.models.Claim.findById(newClaim.id, null, options);
expect(updatedClaim.observation).toEqual(ctx.args.observation);
expect(updatedClaim.claimStateFk).toEqual(ctx.args.claimStateFk);
expect(updatedClaim.workerFk).toEqual(ctx.args.workerFk);
expect(chatModel.sendCheckingPresence).toHaveBeenCalled();
// restores
await app.models.Claim.destroyById(newClaim.id);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
});

View File

@ -4,25 +4,26 @@ module.exports = Self => {
description: 'Update a claim with privileges',
accepts: [{
arg: 'ctx',
type: 'Object',
type: 'object',
http: {source: 'context'}
}, {
},
{
arg: 'id',
type: 'Number',
type: 'number',
description: 'Claim id',
http: {source: 'path'}
},
{
arg: 'workerFk',
type: 'Number'
type: 'number'
},
{
arg: 'claimStateFk',
type: 'Number'
type: 'number'
},
{
arg: 'observation',
type: 'String'
type: 'string'
},
{
arg: 'hasToPickUp',
@ -38,11 +39,23 @@ module.exports = Self => {
}
});
Self.updateClaim = async(ctx, id) => {
Self.updateClaim = async(ctx, id, options) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const args = ctx.args;
const $t = ctx.req.__; // $translate
let tx;
let myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
if (!myOptions.transaction) {
tx = await Self.beginTransaction({});
myOptions.transaction = tx;
}
try {
const claim = await models.Claim.findById(id, {
include: {
relation: 'client',
@ -52,21 +65,21 @@ module.exports = Self => {
}
}
}
});
}, myOptions);
let changedHasToPickUp = false;
if (args.hasToPickUp)
changedHasToPickUp = true;
if (args.claimStateFk) {
const canUpdate = await canChangeState(ctx, claim.claimStateFk);
const hasRights = await canChangeState(ctx, args.claimStateFk);
const isClaimManager = await models.Account.hasRole(userId, 'claimManager');
const canUpdate = await canChangeState(ctx, claim.claimStateFk, myOptions);
const hasRights = await canChangeState(ctx, args.claimStateFk, myOptions);
const isClaimManager = await models.Account.hasRole(userId, 'claimManager', myOptions);
if (!canUpdate || !hasRights || changedHasToPickUp && !isClaimManager)
throw new UserError(`You don't have enough privileges to change that field`);
}
delete args.ctx;
const updatedClaim = await claim.updateAttributes(args);
const updatedClaim = await claim.updateAttributes(args, myOptions);
// Get sales person from claim client
const salesPerson = claim.client().salesPersonUser();
if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) {
@ -79,10 +92,16 @@ module.exports = Self => {
await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message);
}
if (tx) await tx.commit();
return updatedClaim;
} catch (e) {
if (tx) await tx.rollback();
throw e;
}
};
async function canChangeState(ctx, id) {
async function canChangeState(ctx, id, options) {
let models = Self.app.models;
let userId = ctx.req.accessToken.userId;
@ -90,9 +109,9 @@ module.exports = Self => {
include: {
relation: 'writeRole'
}
});
}, options);
let stateRole = state.writeRole().name;
let canUpdate = await models.Account.hasRole(userId, stateRole);
let canUpdate = await models.Account.hasRole(userId, stateRole, options);
return canUpdate;
}