From e6b4d21ddbf0d68c2212ab5bfd9f82a696d0a8b0 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 6 Oct 2020 10:57:40 +0200 Subject: [PATCH 1/6] refactor updateClaim --- .../claim/back/methods/claim/updateClaim.js | 50 ++++++++++++------- modules/claim/front/basic-data/index.html | 3 +- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index b7156feef0..4457aaa6b8 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -1,37 +1,50 @@ const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { - Self.remoteMethodCtx('updateClaim', { + Self.remoteMethod('updateClaim', { description: 'Update a claim with privileges', - accessType: 'WRITE', accepts: [{ - arg: 'id', + arg: 'ctx', + type: 'Object', + http: {source: 'context'} + }, { + arg: 'claimId', type: 'number', - required: true, description: 'Claim id', http: {source: 'path'} - }, { - arg: 'data', - type: 'object', - required: true, - description: 'Data to update on the model', - http: {source: 'body'} + }, + { + arg: 'worker', + type: 'String' + }, + { + arg: 'claimState', + type: 'String' + }, + { + arg: 'observation', + type: 'String' + }, + { + arg: 'hasToPickUp', + type: 'String' }], returns: { type: 'object', root: true }, 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 userId = ctx.req.accessToken.userId; - + const args = ctx.args; + console.log('args', args); const $t = ctx.req.__; // $translate - const claim = await models.Claim.findById(id, { + const claim = await models.Claim.findById(claimId, { include: { relation: 'client', scope: { @@ -43,14 +56,15 @@ module.exports = Self => { }); 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 changedHasToPickUp = claim.hasToPickUp != data.hasToPickUp; + const changedHasToPickUp = claim.hasToPickUp != args.hasToPickUp; if (!canUpdate || !hasRights || changedHasToPickUp && !isSalesAssistant) 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 const salesPerson = claim.client().salesPersonUser(); diff --git a/modules/claim/front/basic-data/index.html b/modules/claim/front/basic-data/index.html index c73e04b241..70584af6ee 100644 --- a/modules/claim/front/basic-data/index.html +++ b/modules/claim/front/basic-data/index.html @@ -1,7 +1,8 @@ From 3d3946761370f22c6640f2b83f1822543f417938 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 6 Oct 2020 14:23:35 +0200 Subject: [PATCH 2/6] refactor updateclaim --- .../methods/claim/specs/updateClaim.spec.js | 44 +++++++++---------- .../claim/back/methods/claim/updateClaim.js | 43 +++++++++--------- modules/claim/front/basic-data/index.html | 7 ++- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/modules/claim/back/methods/claim/specs/updateClaim.spec.js b/modules/claim/back/methods/claim/specs/updateClaim.spec.js index 0222164ecb..b6ae232819 100644 --- a/modules/claim/back/methods/claim/specs/updateClaim.spec.js +++ b/modules/claim/back/methods/claim/specs/updateClaim.spec.js @@ -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(); }); }); diff --git a/modules/claim/back/methods/claim/updateClaim.js b/modules/claim/back/methods/claim/updateClaim.js index 4457aaa6b8..c2c97d9254 100644 --- a/modules/claim/back/methods/claim/updateClaim.js +++ b/modules/claim/back/methods/claim/updateClaim.js @@ -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) { diff --git a/modules/claim/front/basic-data/index.html b/modules/claim/front/basic-data/index.html index 70584af6ee..d5b25db5ba 100644 --- a/modules/claim/front/basic-data/index.html +++ b/modules/claim/front/basic-data/index.html @@ -1,9 +1,12 @@ + Date: Tue, 6 Oct 2020 14:46:58 +0200 Subject: [PATCH 3/6] delete comments --- modules/claim/front/basic-data/index.html | 5 ----- 1 file changed, 5 deletions(-) diff --git a/modules/claim/front/basic-data/index.html b/modules/claim/front/basic-data/index.html index d5b25db5ba..2267949039 100644 --- a/modules/claim/front/basic-data/index.html +++ b/modules/claim/front/basic-data/index.html @@ -1,8 +1,3 @@ - Date: Wed, 7 Oct 2020 10:52:28 +0200 Subject: [PATCH 4/6] route ticket fixes --- modules/route/front/tickets/index.html | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/modules/route/front/tickets/index.html b/modules/route/front/tickets/index.html index 9e48d50edd..8db6bbc0f8 100644 --- a/modules/route/front/tickets/index.html +++ b/modules/route/front/tickets/index.html @@ -31,10 +31,10 @@ model="model"> - Order + Order Ticket Client - Packages + Packages Warehouse Postcode @@ -50,7 +50,7 @@ ng-model="ticket.checked"> - + - + {{ticket.nickname}} - {{ticket.packages}} - {{ticket.volume}} + {{ticket.packages}} + {{::ticket.volume | currency: 'EUR': 1}} {{ticket.warehouse.name}} {{ticket.address.postalCode}} {{ticket.address.street}} From 6e081df4e82ce48a2cca5992e14123fd875231cf Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 13 Oct 2020 08:17:00 +0200 Subject: [PATCH 5/6] fix test --- modules/item/back/methods/item/specs/getWasteDetail.spec.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/modules/item/back/methods/item/specs/getWasteDetail.spec.js b/modules/item/back/methods/item/specs/getWasteDetail.spec.js index 6ed202178b..b5556e1541 100644 --- a/modules/item/back/methods/item/specs/getWasteDetail.spec.js +++ b/modules/item/back/methods/item/specs/getWasteDetail.spec.js @@ -1,6 +1,6 @@ const app = require('vn-loopback/server/server'); -xdescribe('item getWasteDetail()', () => { +describe('item getWasteDetail()', () => { it('should check for the waste breakdown for every worker', async() => { let result = await app.models.Item.getWasteDetail(); @@ -14,10 +14,10 @@ xdescribe('item getWasteDetail()', () => { expect(result.length).toEqual(3); expect(firstBuyer).toEqual('CharlesXavier'); expect(firstBuyerLines.length).toEqual(4); - expect(secondBuyer).toEqual('DavidCharlesHaller'); + expect(secondBuyer).toEqual('HankPym'); expect(secondBuyerLines.length).toEqual(3); - expect(thirdBuyer).toEqual('HankPym'); + expect(thirdBuyer).toEqual('DavidCharlesHaller'); expect(thirdBuyerLines.length).toEqual(3); }); }); From 8a580175976dce18b3a9c35be9d451945b10a3b2 Mon Sep 17 00:00:00 2001 From: Bernat Exposito Domenech Date: Tue, 13 Oct 2020 08:50:47 +0200 Subject: [PATCH 6/6] fix route/ticket --- modules/route/front/tickets/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/route/front/tickets/index.html b/modules/route/front/tickets/index.html index 8db6bbc0f8..1f91dd3f1c 100644 --- a/modules/route/front/tickets/index.html +++ b/modules/route/front/tickets/index.html @@ -73,7 +73,7 @@ {{ticket.packages}} - {{::ticket.volume | currency: 'EUR': 1}} + {{::ticket.volume | number:1}} {{ticket.warehouse.name}} {{ticket.address.postalCode}} {{ticket.address.street}}