diff --git a/modules/claim/back/methods/claim/specs/updateClaim.spec.js b/modules/claim/back/methods/claim/specs/updateClaim.spec.js index 7ce6a4d3c..a5de92ccf 100644 --- a/modules/claim/back/methods/claim/specs/updateClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/updateClaim.spec.js @@ -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,89 +14,114 @@ 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 forbiddenState = 3; - const salesPersonId = 18; - const ctx = { - req: { - accessToken: { - userId: salesPersonId + 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 = { + req: { + accessToken: { + userId: salesPersonId + } + }, + args: { + claimStateFk: forbiddenState, + observation: 'valid observation' } - }, - args: { - claimStateFk: forbiddenState, - observation: 'valid observation' - } - }; - await app.models.Claim.updateClaim(ctx, newClaim.id) - .catch(e => { - error = 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({}); - const canceledState = 4; - const claimManagerId = 72; - const ctx = { - req: { - accessToken: { - userId: claimManagerId + try { + const options = {transaction: tx}; + + const newClaim = await app.models.Claim.create(originalData, options); + + const canceledState = 4; + const claimManagerId = 72; + const ctx = { + req: { + accessToken: { + userId: claimManagerId + } + }, + args: { + observation: 'valid observation', + claimStateFk: canceledState, + hasToPickUp: false } - }, - args: { - observation: 'valid observation', - claimStateFk: canceledState, - 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); + 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 chatModel = app.models.Chat; - spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); + const tx = await app.models.Claim.beginTransaction({}); - const claimManagerId = 72; - const ctx = { - req: { - accessToken: {userId: claimManagerId}, - 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, newClaim.id); + try { + const options = {transaction: tx}; - let updatedClaim = await app.models.Claim.findById(newClaim.id); + const newClaim = await app.models.Claim.create(originalData, 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(); + const chatModel = app.models.Chat; + spyOn(chatModel, 'sendCheckingPresence').and.callThrough(); - // restores - await app.models.Claim.destroyById(newClaim.id); + const claimManagerId = 72; + const ctx = { + req: { + accessToken: {userId: claimManagerId}, + 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, newClaim.id, options); + + 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(); + + await tx.rollback(); + } catch (e) { + await tx.rollback(); + throw e; + } }); }); diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index 6c16b5737..02515ed66 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -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,51 +39,69 @@ 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 - const claim = await models.Claim.findById(id, { - include: { - relation: 'client', - scope: { - include: { - relation: 'salesPersonUser' + 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', + scope: { + include: { + relation: 'salesPersonUser' + } } } + }, myOptions); + let changedHasToPickUp = false; + if (args.hasToPickUp) + changedHasToPickUp = true; + + if (args.claimStateFk) { + 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, myOptions); + // Get sales person from claim client + const salesPerson = claim.client().salesPersonUser(); + if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) { + const origin = ctx.req.headers.origin; + const message = $t('Claim will be picked', { + claimId: claim.id, + clientName: claim.client().name, + claimUrl: `${origin}/#!/claim/${claim.id}/summary` + }); + await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); } - }); - 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'); + if (tx) await tx.commit(); - if (!canUpdate || !hasRights || changedHasToPickUp && !isClaimManager) - throw new UserError(`You don't have enough privileges to change that field`); + return updatedClaim; + } catch (e) { + if (tx) await tx.rollback(); + throw e; } - delete args.ctx; - const updatedClaim = await claim.updateAttributes(args); - // Get sales person from claim client - const salesPerson = claim.client().salesPersonUser(); - if (salesPerson && changedHasToPickUp && updatedClaim.hasToPickUp) { - const origin = ctx.req.headers.origin; - const message = $t('Claim will be picked', { - claimId: claim.id, - clientName: claim.client().name, - claimUrl: `${origin}/#!/claim/${claim.id}/summary` - }); - await models.Chat.sendCheckingPresence(ctx, salesPerson.id, message); - } - - return updatedClaim; }; - 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; }