From 457d23dc161200e6de645bd682270b4e2e9926aa Mon Sep 17 00:00:00 2001 From: carlosjr Date: Wed, 12 May 2021 09:51:32 +0200 Subject: [PATCH] reoute tickets order columns + endpoint refactor --- db/dump/fixtures.sql | 23 +-- .../route/back/methods/route/getTickets.js | 132 +++++++----------- .../methods/route/specs/getTickets.spec.js | 3 +- modules/route/back/methods/route/summary.js | 2 +- modules/route/front/summary/index.html | 6 +- modules/route/front/tickets/index.html | 29 ++-- 6 files changed, 85 insertions(+), 110 deletions(-) diff --git a/db/dump/fixtures.sql b/db/dump/fixtures.sql index f386ad780..db9f561b0 100644 --- a/db/dump/fixtures.sql +++ b/db/dump/fixtures.sql @@ -410,11 +410,12 @@ INSERT INTO `vn`.`clientObservation`(`id`, `clientFk`, `workerFk`, `text`, `crea INSERT INTO `vn`.`observationType`(`id`,`description`, `code`) VALUES - (1, 'observation one', 'observation one'), - (2, 'observation two', 'observation two'), - (3, 'observation three', 'observation three'), - (4, 'comercial', 'salesPerson'), - (5, 'delivery', 'delivery'); + (1, 'Sacador', 'itemPicker'), + (2, 'Encajador', 'packager'), + (3, 'Repartidor', 'delivery'), + (4, 'Comercial', 'salesPerson'), + (5, 'Administración', 'administrative'), + (6, 'Peso Aduana', 'weight'); INSERT INTO `vn`.`addressObservation`(`id`,`addressFk`,`observationTypeFk`,`description`) VALUES @@ -607,16 +608,16 @@ INSERT INTO `vn`.`ticketObservation`(`id`, `ticketFk`, `observationTypeFk`, `des VALUES (1, 11, 1, 'ready'), (2, 2, 2, 'do it fast please'), - (3, 3, 3, 'Faster faster fasteeeeeer!!!'), - (4, 4, 3, 'Deliver before 8am'), - (5, 13, 3, 'You can run from the disappointments you are trying to forget. But its only when you embrace your past that you truly move forward. Maybe I never get to go home again, but I found my way there. And I am glad I did.'), - (6, 14, 3, 'Careful, armed warhead'), + (3, 3, 5, 'Faster faster fasteeeeeer!!!'), + (4, 4, 5, 'Deliver before 8am'), + (5, 13, 5, 'You can run from the disappointments you are trying to forget. But its only when you embrace your past that you truly move forward. Maybe I never get to go home again, but I found my way there. And I am glad I did.'), + (6, 14, 5, 'Careful, armed warhead'), (7, 23, 1, 'under the floor'), (8, 23, 2, 'wears leather and goes out at night'), - (9, 23, 3, 'care with the dog'), + (9, 23, 5, 'care with the dog'), (10, 23, 4, 'Reclama ticket: 8'), (11, 24, 4, 'Reclama ticket: 7'), - (12, 11, 5, 'Delivery after 10am'); + (12, 11, 3, 'Delivery after 10am'); -- FIX for state hours on local, inter_afterInsert UPDATE vncontrol.inter SET odbc_date = DATE_ADD(CURDATE(), INTERVAL -10 SECOND); diff --git a/modules/route/back/methods/route/getTickets.js b/modules/route/back/methods/route/getTickets.js index 211a2e7bb..7d9f3f254 100644 --- a/modules/route/back/methods/route/getTickets.js +++ b/modules/route/back/methods/route/getTickets.js @@ -1,101 +1,73 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; + module.exports = Self => { Self.remoteMethod('getTickets', { description: 'Return the tickets information displayed on the route module', accessType: 'READ', - accepts: [{ - arg: 'id', - type: 'number', - required: true, - description: 'The route id', - http: {source: 'path'} - }], + accepts: [ + { + arg: 'filter', + type: 'object', + description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', + http: {source: 'query'} + } + ], returns: { type: 'object', root: true }, http: { - path: `/:id/getTickets`, + path: `/getTickets`, verb: 'GET' } }); - Self.getTickets = async id => { - let filter = { - where: {id: id}, - include: [ - {relation: 'ticket', - scope: { - fields: ['id', 'packages', 'warehouseFk', 'nickname', 'clientFk', 'priority', 'addressFk'], - order: 'priority', - include: [ - { - relation: 'ticketState', - scope: { - fields: ['id', 'stateFk'], - include: [{relation: 'state'}] - } - }, - { - relation: 'warehouse', - scope: { - fields: ['id', 'name'] - } - }, - { - relation: 'notes', - scope: { - where: {observationTypeFk: 3} - } - }, - { - relation: 'address', - scope: { - fields: ['id', 'street', 'postalCode', 'city'], - } - }, + Self.getTickets = async(filter, options) => { + const conn = Self.dataSource.connector; - ] - } - }, { - 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'] - } - } - ], - }; + const stmt = new ParameterizedSQL( + `SELECT + t.id, + t.packages, + t.warehouseFk, + t.nickname, + t.clientFk, + t.priority, + t.addressFk, + st.code AS ticketStateCode, + st.name AS ticketStateName, + wh.name AS warehouseName, + tob.description AS ticketObservation, + a.street, + a.postalCode, + a.city, + am.name AS agencyModeName, + u.nickname AS userNickname, + vn.ticketTotalVolume(t.id) AS volume + FROM route r + LEFT JOIN ticket t ON t.routeFk = r.id + LEFT JOIN ticketState ts ON ts.ticketFk = t.id + LEFT JOIN state st ON st.id = ts.stateFk + LEFT JOIN warehouse wh ON wh.id = t.warehouseFk + LEFT JOIN ticketObservation tob ON tob.ticketFk = t.id + LEFT JOIN observationType ot ON tob.observationTypeFk = ot.id + AND ot.code = 'delivery' + LEFT JOIN address a ON a.id = t.addressFk + LEFT JOIN agencyMode am ON am.id = t.agencyModeFk + LEFT JOIN account.user u ON u.id = r.workerFk + LEFT JOIN vehicle v ON v.id = r.vehicleFk` + ); - route = await Self.app.models.Route.findOne(filter); + if (!filter.where) filter.where = {}; - for (let i = 0; i < route.ticket().length; i++) { - let ticket = route.ticket()[i]; - let query = ` - SELECT vn.ticketTotalVolume(?) AS m3`; + const where = filter.where; + where['r.id'] = filter.id; - let options = [ticket.id]; - let [volume] = await Self.rawSql(query, options); + stmt.merge(conn.makeSuffix(filter)); - ticket.volume = volume.m3; - } + const tickets = await conn.executeStmt(stmt, options); - return route.ticket(); + return tickets; }; }; diff --git a/modules/route/back/methods/route/specs/getTickets.spec.js b/modules/route/back/methods/route/specs/getTickets.spec.js index 15028309f..687547e45 100644 --- a/modules/route/back/methods/route/specs/getTickets.spec.js +++ b/modules/route/back/methods/route/specs/getTickets.spec.js @@ -2,7 +2,8 @@ const app = require('vn-loopback/server/server'); describe('route getTickets()', () => { it('should return the tickets for a given route', async() => { - let result = await app.models.Route.getTickets(2); + const filter = {id: 2}; + let result = await app.models.Route.getTickets(filter); expect(result.length).toEqual(1); }); diff --git a/modules/route/back/methods/route/summary.js b/modules/route/back/methods/route/summary.js index 52927d974..ad65edb4a 100644 --- a/modules/route/back/methods/route/summary.js +++ b/modules/route/back/methods/route/summary.js @@ -53,7 +53,7 @@ module.exports = Self => { }; summary.route = await Self.app.models.Route.findOne(filter); - summary.tickets = await Self.app.models.Route.getTickets(id); + summary.tickets = await Self.app.models.Route.getTickets({id: id}); return summary; }; diff --git a/modules/route/front/summary/index.html b/modules/route/front/summary/index.html index b284851a0..86f558634 100644 --- a/modules/route/front/summary/index.html +++ b/modules/route/front/summary/index.html @@ -106,9 +106,9 @@ {{ticket.packages}} {{ticket.volume}} - {{ticket.warehouse.name}} - {{ticket.address.postalCode}} - {{ticket.address.street}} + {{ticket.warehouseName}} + {{ticket.postalCode}} + {{ticket.street}} @@ -34,14 +35,14 @@ model="model"> - Order - Street - City - PC - Client - Packages - - Ticket + Order + Street + City + PC + Client + Packages + + Ticket @@ -62,9 +63,9 @@ display-controls=true> - {{ticket.address.street}} - {{ticket.address.city}} - {{ticket.address.postalCode}} + {{::ticket.street}} + {{::ticket.city}} + {{::ticket.postalCode}} - {{ticket.packages}} + {{::ticket.packages}} {{::ticket.volume | number:1}} - {{ticket.id}} + {{::ticket.id}}