From 60e908d82ac160d63cd9d9d3975c0304eda69b31 Mon Sep 17 00:00:00 2001 From: alexm Date: Mon, 28 Feb 2022 12:40:44 +0100 Subject: [PATCH] feat(claim_summary): parentReload(), and test --- modules/claim/back/methods/claim/filter.js | 3 +- .../claim/back/methods/claim/getSummary.js | 5 +-- .../back/methods/claim/specs/filter.spec.js | 2 +- .../methods/claim/specs/getSummary.spec.js | 11 +++++-- modules/claim/front/index/index.html | 2 +- modules/claim/front/index/index.js | 6 +++- modules/claim/front/main/index.js | 8 +++-- modules/claim/front/summary/index.html | 2 +- modules/claim/front/summary/index.js | 31 +++++++++---------- modules/claim/front/summary/index.spec.js | 27 +++++++++++----- 10 files changed, 61 insertions(+), 36 deletions(-) diff --git a/modules/claim/back/methods/claim/filter.js b/modules/claim/back/methods/claim/filter.js index cf2748e4f..f4eecae6d 100644 --- a/modules/claim/back/methods/claim/filter.js +++ b/modules/claim/back/methods/claim/filter.js @@ -97,12 +97,11 @@ module.exports = Self => { {'cl.socialName': {like: `%${value}%`}} ] }; - case 'workerFk': - return {['cl.workerFk']: value}; case 'id': case 'claimStateFk': case 'priority': return {[`cl.${param}`]: value}; + case 'salesPersonFk': case 'attenderFk': return {'cl.workerFk': value}; case 'created': diff --git a/modules/claim/back/methods/claim/getSummary.js b/modules/claim/back/methods/claim/getSummary.js index 512e4a77f..0accbf920 100644 --- a/modules/claim/back/methods/claim/getSummary.js +++ b/modules/claim/back/methods/claim/getSummary.js @@ -1,5 +1,5 @@ module.exports = Self => { - Self.remoteMethod('getSummary', { + Self.remoteMethodCtx('getSummary', { description: 'Return the claim summary', accessType: 'READ', accepts: [{ @@ -19,7 +19,7 @@ module.exports = Self => { } }); - Self.getSummary = async(id, options) => { + Self.getSummary = async(ctx, id, options) => { const myOptions = {}; if (typeof options == 'object') @@ -135,6 +135,7 @@ module.exports = Self => { const res = await Promise.all(promises); + summary.isEditable = await Self.isEditable(ctx, id, myOptions); [summary.claim] = res[0]; summary.salesClaimed = res[1]; summary.developments = res[2]; diff --git a/modules/claim/back/methods/claim/specs/filter.spec.js b/modules/claim/back/methods/claim/specs/filter.spec.js index b26afe8c4..c54318e45 100644 --- a/modules/claim/back/methods/claim/specs/filter.spec.js +++ b/modules/claim/back/methods/claim/specs/filter.spec.js @@ -25,7 +25,7 @@ describe('claim filter()', () => { try { const options = {transaction: tx}; - const result = await app.models.Claim.filter({args: {filter: {}, search: 'Tony Stark'}}, null, options); + const result = await app.models.Claim.filter({args: {filter: {}, search: 'Iron man'}}, null, options); expect(result.length).toEqual(1); expect(result[0].id).toEqual(4); diff --git a/modules/claim/back/methods/claim/specs/getSummary.spec.js b/modules/claim/back/methods/claim/specs/getSummary.spec.js index 541f42cfb..6eb920b29 100644 --- a/modules/claim/back/methods/claim/specs/getSummary.spec.js +++ b/modules/claim/back/methods/claim/specs/getSummary.spec.js @@ -3,17 +3,24 @@ const app = require('vn-loopback/server/server'); describe('claim getSummary()', () => { it('should return summary with claim, salesClaimed, developments and actions defined ', async() => { const tx = await app.models.Claim.beginTransaction({}); - + const ctx = { + req: { + accessToken: { + userId: 9 + } + } + }; try { const options = {transaction: tx}; - const result = await app.models.Claim.getSummary(1, options); + const result = await app.models.Claim.getSummary(ctx, 1, options); const keys = Object.keys(result); expect(keys).toContain('claim'); expect(keys).toContain('salesClaimed'); expect(keys).toContain('developments'); expect(keys).toContain('actions'); + expect(keys).toContain('isEditable'); await tx.rollback(); } catch (e) { diff --git a/modules/claim/front/index/index.html b/modules/claim/front/index/index.html index bb2e2f711..341d6eb2f 100644 --- a/modules/claim/front/index/index.html +++ b/modules/claim/front/index/index.html @@ -19,7 +19,7 @@ Created - + Worker diff --git a/modules/claim/front/index/index.js b/modules/claim/front/index/index.js index a2cc8b5e1..bf0cb25c0 100644 --- a/modules/claim/front/index/index.js +++ b/modules/claim/front/index/index.js @@ -49,10 +49,10 @@ class Controller extends Section { case 'clientFk': return {['cl.socialName']: value}; case 'id': - case 'workerFk': case 'claimStateFk': case 'priority': return {[`cl.${param}`]: value}; + case 'salesPersonFk': case 'attenderFk': return {'cl.workerFk': value}; case 'created': @@ -79,6 +79,10 @@ class Controller extends Section { this.claimSelected = claim; this.$.summary.show(); } + + reload() { + this.$.model.refresh(); + } } ngModule.vnComponent('vnClaimIndex', { diff --git a/modules/claim/front/main/index.js b/modules/claim/front/main/index.js index 77b051897..a9fad2eeb 100644 --- a/modules/claim/front/main/index.js +++ b/modules/claim/front/main/index.js @@ -1,9 +1,13 @@ import ngModule from '../module'; import ModuleMain from 'salix/components/module-main'; -export default class Claim extends ModuleMain {} +export default class Claims extends ModuleMain { + constructor($element, $) { + super($element, $); + } +} ngModule.vnComponent('vnClaim', { - controller: Claim, + controller: Claims, template: require('./index.html') }); diff --git a/modules/claim/front/summary/index.html b/modules/claim/front/summary/index.html index f46b3622a..c1d056eed 100644 --- a/modules/claim/front/summary/index.html +++ b/modules/claim/front/summary/index.html @@ -13,7 +13,7 @@ {{::$ctrl.summary.claim.id}} - {{::$ctrl.summary.claim.client.name}} { + if (res && res.data) + this.summary = res.data; + }); } reload() { - if (this.card) { - console.log('CARD'); - this.card.reload(); - } - if (this.parentReload) - this.parentReload(); + this.loadData() + .then(() => { + if (this.card) + this.card.reload(); + // Refresh index model + if (this.parentReload) + this.parentReload(); + }); } + get isSalesPerson() { return this.aclService.hasAny(['salesPerson']); } @@ -54,15 +60,6 @@ class Controller extends Summary { this.$.model.refresh(); } - getSummary() { - this.$http.get(`Claims/${this.claim.id}/getSummary`).then(response => { - this.summary = response.data; - }); - this.$http.get(`Claims/${this.claim.id}/isEditable`).then(response => { - this.isEditable = response.data; - }); - } - getImagePath(dmsId) { return this.vnFile.getPath(`/api/dms/${dmsId}/downloadFile`); } diff --git a/modules/claim/front/summary/index.spec.js b/modules/claim/front/summary/index.spec.js index 31e10eccb..02bdf21d2 100644 --- a/modules/claim/front/summary/index.spec.js +++ b/modules/claim/front/summary/index.spec.js @@ -18,24 +18,37 @@ describe('Claim', () => { controller.$.model = crudModel; })); - describe('getSummary()', () => { + describe('loadData()', () => { it('should perform a query to set summary', () => { - $httpBackend.expect('GET', `Claims/1/getSummary`).respond(200, 24); - controller.getSummary(); - $httpBackend.expect('GET', `Claims/1/isEditable`).respond(200, true); + $httpBackend.when('GET', `Claims/1/getSummary`).respond(200, 24); + controller.loadData(); $httpBackend.flush(); expect(controller.summary).toEqual(24); }); }); + describe('changeState()', () => { + it('should make an HTTP post query, then call the showSuccess(), reload() and resetChanges() methods', () => { + jest.spyOn(controller.vnApp, 'showSuccess').mockReturnThis(); + + const expectedParams = {id: 1, claimStateFk: 1}; + $httpBackend.when('GET', `Claims/1/getSummary`).respond(200, 24); + $httpBackend.expect('PATCH', `Claims/updateClaim/1`, expectedParams).respond(200); + controller.changeState(1); + $httpBackend.flush(); + + expect(controller.vnApp.showSuccess).toHaveBeenCalled(); + }); + }); + describe('$onChanges()', () => { - it('should call getSummary when item.id is defined', () => { - jest.spyOn(controller, 'getSummary'); + it('should call loadData when item.id is defined', () => { + jest.spyOn(controller, 'loadData'); controller.$onChanges(); - expect(controller.getSummary).toHaveBeenCalledWith(); + expect(controller.loadData).toHaveBeenCalledWith(); }); }); });