From 462879a40bf0c4e575d725cfcb48fb106256cb57 Mon Sep 17 00:00:00 2001 From: Gerard Date: Mon, 25 Mar 2019 13:16:34 +0100 Subject: [PATCH] #1241 summary refactor --- .../route/back/methods/route/getTickets.js | 101 ++++++++++++++++++ .../methods/route/specs/getTickets.spec.js | 9 ++ .../back/methods/route/specs/summary.spec.js | 16 +++ modules/route/back/methods/route/summary.js | 48 +-------- modules/route/back/models/route.js | 1 + modules/route/front/summary/index.html | 4 +- modules/route/front/summary/index.js | 4 +- modules/route/front/summary/index.spec.js | 14 +++ 8 files changed, 147 insertions(+), 50 deletions(-) create mode 100644 modules/route/back/methods/route/getTickets.js create mode 100644 modules/route/back/methods/route/specs/getTickets.spec.js create mode 100644 modules/route/back/methods/route/specs/summary.spec.js diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js new file mode 100644 index 000000000..579a90dba --- /dev/null +++ b/modules/route/back/methods/route/getTickets.js @@ -0,0 +1,101 @@ +module.exports = Self => { + Self.remoteMethod('getTickets', { + description: 'Updates the item taxes', + accessType: 'READ', + accepts: [{ + arg: 'id', + type: 'number', + required: true, + description: 'The item id', + http: {source: 'path'} + }], + returns: { + type: 'object', + root: true + }, + http: { + path: `/:id/getTickets`, + verb: 'GET' + } + }); + + Self.getTickets = async id => { + let filter = { + where: {id: id}, + include: [ + {relation: 'ticket', + scope: { + fields: ['id', 'packages', 'warehouseFk', 'nickname', 'clientFk', 'priority'], + order: 'priority', + include: [ + { + relation: 'client', + scope: { + fields: ['id', 'street', 'postcode'], + } + }, + { + relation: 'state', + scope: { + fields: ['id', 'stateFk'], + include: [{relation: 'state'}] + } + }, + { + relation: 'warehouse', + scope: { + fields: ['id', 'name'] + } + }, + { + relation: 'notes', + scope: { + where: {observationTypeFk: 3} + } + } + + ] + } + }, { + relation: 'agencyMode', + scope: { + fields: ['id', 'name'] + } + }, { + relation: 'worker', + scope: { + fields: ['id', 'userFk'], + include: [ + { + relation: 'user', + scope: { + fields: ['id', 'nickname'] + } + } + ] + } + }, { + relation: 'vehicle', + scope: { + fields: ['id', 'm3', 'numberPlate'] + } + } + ], + }; + + route = await Self.app.models.Route.findOne(filter); + + for (let i = 0; i < route.ticket().length; i++) { + let ticket = route.ticket()[i]; + let query = ` + SELECT vn.ticketTotalVolume(?) AS m3`; + + let options = [ticket.id]; + let [volume] = await Self.rawSql(query, options); + + ticket.volume = volume.m3; + } + + return route.ticket(); + }; +}; diff --git a/modules/route/back/methods/route/specs/getTickets.spec.js b/modules/route/back/methods/route/specs/getTickets.spec.js new file mode 100644 index 000000000..1c850099c --- /dev/null +++ b/modules/route/back/methods/route/specs/getTickets.spec.js @@ -0,0 +1,9 @@ +const app = require('vn-loopback/server/server'); + +describe('route summary()', () => { + it('should return a summary object containing data freom his tickets', async() => { + let result = await app.models.Route.getTickets(1); + + expect(result[2].id).toEqual(11); + }); +}); diff --git a/modules/route/back/methods/route/specs/summary.spec.js b/modules/route/back/methods/route/specs/summary.spec.js new file mode 100644 index 000000000..658519c51 --- /dev/null +++ b/modules/route/back/methods/route/specs/summary.spec.js @@ -0,0 +1,16 @@ +const app = require('vn-loopback/server/server'); + +describe('route summary()', () => { + it('should return a summary object containing data from 1 route', async() => { + let result = await app.models.Route.summary(2); + + expect(result.route.id).toEqual(2); + expect(result.route.workerFk).toEqual(56); + }); + + it('should return a summary object containing data freom his tickets', async() => { + let result = await app.models.Route.summary(1); + + expect(result.tickets[2].id).toEqual(11); + }); +}); diff --git a/modules/route/back/methods/route/summary.js b/modules/route/back/methods/route/summary.js index ee36d4647..0119e971e 100644 --- a/modules/route/back/methods/route/summary.js +++ b/modules/route/back/methods/route/summary.js @@ -22,44 +22,10 @@ module.exports = Self => { Self.summary = async id => { let summary = {}; - // Item basic data and taxes let filter = { where: {id: id}, include: [ - {relation: 'ticket', - scope: { - fields: ['id', 'packages', 'warehouseFk', 'nickname', 'clientFk', 'priority'], - order: 'priority', - include: [ - { - relation: 'client', - scope: { - fields: ['id', 'street', 'postcode'], - } - }, - { - relation: 'state', - scope: { - fields: ['id', 'stateFk'], - include: [{relation: 'state'}] - } - }, - { - relation: 'warehouse', - scope: { - fields: ['id', 'name'] - } - }, - { - relation: 'notes', - scope: { - where: {observationTypeFk: 3} - } - } - - ] - } - }, { + { relation: 'agencyMode', scope: { fields: ['id', 'name'] @@ -87,17 +53,7 @@ module.exports = Self => { }; summary.route = await Self.app.models.Route.findOne(filter); - - for (let i = 0; i < summary.route.ticket().length; i++) { - let ticket = summary.route.ticket()[i]; - let query = ` - SELECT vn.ticketTotalVolume(?) AS m3`; - - let options = [ticket.id]; - let [volume] = await Self.rawSql(query, options); - - ticket.volume = volume.m3; - } + summary.tickets = await Self.app.models.Route.getTickets(id); return summary; }; diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index a3871d07e..d5febe23e 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -1,3 +1,4 @@ module.exports = Self => { require('../methods/route/summary')(Self); + require('../methods/route/getTickets')(Self); }; diff --git a/modules/route/front/summary/index.html b/modules/route/front/summary/index.html index 450276a24..43f4ff654 100644 --- a/modules/route/front/summary/index.html +++ b/modules/route/front/summary/index.html @@ -66,8 +66,8 @@ - - {{ticket.priority}} + + {{ticket.priority | dashIfEmpty}} { + this.summary.tickets.forEach(ticket => { this.packagesTotal += ticket.packages; }); } @@ -41,7 +41,7 @@ class Controller { getSummary() { this.$http.get(`/api/Routes/${this.route.id}/summary`).then(response => { this.summary = response.data; - if (response.data && response.data.route && response.data.route.ticket) + if (response.data && response.data.tickets) this.sumPackages(); }); } diff --git a/modules/route/front/summary/index.spec.js b/modules/route/front/summary/index.spec.js index 1da9a412d..34b306de8 100644 --- a/modules/route/front/summary/index.spec.js +++ b/modules/route/front/summary/index.spec.js @@ -23,5 +23,19 @@ describe('Route', () => { expect(controller.summary).toEqual(24); }); }); + + describe('sumPackages()', () => { + it('should calculate the packages total', () => { + controller.summary = { + tickets: [ + {packages: 3}, + {packages: 1} + ] + }; + controller.sumPackages(); + + expect(controller.packagesTotal).toEqual(4); + }); + }); }); });