From 2f64a41f645de5deeff913894cd631850f40ef2d Mon Sep 17 00:00:00 2001 From: guillermo Date: Tue, 8 Aug 2023 14:16:47 +0200 Subject: [PATCH 1/4] refs #5995 Added getExternalCmrs --- .../back/methods/route/getExternalCmrs.js | 139 ++++++++++++++++++ modules/route/back/models/route.js | 1 + 2 files changed, 140 insertions(+) create mode 100644 modules/route/back/methods/route/getExternalCmrs.js diff --git a/modules/route/back/methods/route/getExternalCmrs.js b/modules/route/back/methods/route/getExternalCmrs.js new file mode 100644 index 000000000..4947aeffa --- /dev/null +++ b/modules/route/back/methods/route/getExternalCmrs.js @@ -0,0 +1,139 @@ +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +const buildFilter = require('vn-loopback/util/filter').buildFilter; +const mergeFilters = require('vn-loopback/util/filter').mergeFilters; + +module.exports = Self => { + Self.remoteMethodCtx('getExternalCmrs', { + description: 'Returns an array of external cmrs', + accessType: 'READ', + accepts: [ + { + arg: 'filter', + type: 'object', + description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', + http: {source: 'query'} + }, + { + arg: 'cmrFk', + type: 'integer', + description: 'Searchs the route by id', + http: {source: 'query'} + }, + { + arg: 'ticketFk', + type: 'integer', + description: 'The worker id', + http: {source: 'query'} + }, + { + arg: 'country', + type: 'string', + description: 'The agencyMode id', + http: {source: 'query'} + }, + { + arg: 'clientFk', + type: 'integer', + description: 'The vehicle id', + http: {source: 'query'} + }, + { + arg: 'hasCmrDms', + type: 'boolean', + description: 'The vehicle id', + http: {source: 'query'} + }, + { + arg: 'shipped', + type: 'date', + description: 'The to date filter', + http: {source: 'query'} + }, + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/getExternalCmrs`, + verb: 'GET' + } + }); + + Self.getExternalCmrs = async(ctx, filter, options) => { + const conn = Self.dataSource.connector; + + let where = buildFilter(ctx.args, (param, value) => { + switch (param) { + case 'cmrFk': + return {'cmrFk': value}; + case 'ticketFk': + return {'ticketFk': value}; + case 'country': + return {'country': value}; + case 'clientFk': + return {'clientFk': value}; + case 'hasCmrDms': + return {'hasCmrDms': value}; + case 'shipped': + return {'shipped': value}; + } + }); + + filter = mergeFilters(ctx.args.filter, {where}); + + if (!filter.where) { + const yesterday = new Date(); + yesterday.setDate(yesterday.getDate() - 1); + filter.where = {'shipped': yesterday.toISOString().split('T')[0]} + } + + const myOptions = {}; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + let stmts = []; + const stmt = new ParameterizedSQL(` + SELECT * + FROM ( + SELECT t.cmrFk, + t.id ticketFk, + co.country, + t.clientFk, + IF(sub.id, TRUE, FALSE) hasCmrDms, + DATE(t.shipped) shipped + FROM ticket t + JOIN ticketState ts ON ts.ticketFk = t.id + JOIN state s ON s.id = ts.stateFk + JOIN alertLevel al ON al.id = s.alertLevel + JOIN client c ON c.id = t.clientFk + JOIN address a ON a.id = t.addressFk + JOIN province p ON p.id = a.provinceFk + JOIN country co ON co.id = p.countryFk + JOIN agencyMode am ON am.id = t.agencyModeFk + JOIN deliveryMethod dm ON dm.id = am.deliveryMethodFk + JOIN warehouse w ON w.id = t.warehouseFk + LEFT JOIN ( + SELECT td.ticketFk, d.id + FROM ticketDms td + JOIN dms d ON d.id = td.dmsFk + JOIN dmsType dt ON dt.id = d.dmsTypeFk + WHERE dt.name = 'cmr' + ) sub ON sub.ticketFk = t.id + WHERE co.code <> 'ES' + AND am.name <> 'ABONO' + AND w.code = 'ALG' + AND dm.code = 'DELIVERY' + AND t.cmrFk + ) sub + `); + + stmt.merge(conn.makeSuffix(filter)); + let itemsIndex = stmts.push(stmt) - 1; + + let sql = ParameterizedSQL.join(stmts, ';'); + let result = await conn.executeStmt(sql); + return itemsIndex === 0 ? result : result[itemsIndex]; + }; +}; diff --git a/modules/route/back/models/route.js b/modules/route/back/models/route.js index a8d44cd05..7e61acf25 100644 --- a/modules/route/back/models/route.js +++ b/modules/route/back/models/route.js @@ -15,6 +15,7 @@ module.exports = Self => { require('../methods/route/sendSms')(Self); require('../methods/route/downloadZip')(Self); require('../methods/route/cmr')(Self); + require('../methods/route/getExternalCmrs')(Self); Self.validate('kmStart', validateDistance, { message: 'Distance must be lesser than 1000' From ed88a5b25f807fd83e40cb73bfef9b3bacc4668f Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 10 Aug 2023 07:54:49 +0200 Subject: [PATCH 2/4] refs #5995 Minor change --- modules/route/back/methods/route/getExternalCmrs.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/route/back/methods/route/getExternalCmrs.js b/modules/route/back/methods/route/getExternalCmrs.js index 4947aeffa..a232b98cd 100644 --- a/modules/route/back/methods/route/getExternalCmrs.js +++ b/modules/route/back/methods/route/getExternalCmrs.js @@ -101,7 +101,7 @@ module.exports = Self => { t.id ticketFk, co.country, t.clientFk, - IF(sub.id, TRUE, FALSE) hasCmrDms, + sub.id hasCmrDms, DATE(t.shipped) shipped FROM ticket t JOIN ticketState ts ON ts.ticketFk = t.id From d6b30271f287d437314b35d66ed916afa2f46e34 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 10 Aug 2023 08:12:52 +0200 Subject: [PATCH 3/4] refs #5995 Requested changes --- .../back/methods/route/getExternalCmrs.js | 32 ++++++++++++------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/modules/route/back/methods/route/getExternalCmrs.js b/modules/route/back/methods/route/getExternalCmrs.js index a232b98cd..b7312d02e 100644 --- a/modules/route/back/methods/route/getExternalCmrs.js +++ b/modules/route/back/methods/route/getExternalCmrs.js @@ -3,7 +3,7 @@ const buildFilter = require('vn-loopback/util/filter').buildFilter; const mergeFilters = require('vn-loopback/util/filter').mergeFilters; module.exports = Self => { - Self.remoteMethodCtx('getExternalCmrs', { + Self.remoteMethod('getExternalCmrs', { description: 'Returns an array of external cmrs', accessType: 'READ', accepts: [ @@ -11,43 +11,36 @@ module.exports = Self => { arg: 'filter', type: 'object', description: 'Filter defining where, order, offset, and limit - must be a JSON-encoded string', - http: {source: 'query'} }, { arg: 'cmrFk', type: 'integer', description: 'Searchs the route by id', - http: {source: 'query'} }, { arg: 'ticketFk', type: 'integer', description: 'The worker id', - http: {source: 'query'} }, { arg: 'country', type: 'string', description: 'The agencyMode id', - http: {source: 'query'} }, { arg: 'clientFk', type: 'integer', description: 'The vehicle id', - http: {source: 'query'} }, { arg: 'hasCmrDms', type: 'boolean', description: 'The vehicle id', - http: {source: 'query'} }, { arg: 'shipped', type: 'date', description: 'The to date filter', - http: {source: 'query'} }, ], returns: { @@ -60,10 +53,27 @@ module.exports = Self => { } }); - Self.getExternalCmrs = async(ctx, filter, options) => { + Self.getExternalCmrs = async( + filter, + cmrFk, + ticketFk, + country, + clientFk, + hasCmrDms, + shipped, + options + ) => { + const params = { + cmrFk, + ticketFk, + country, + clientFk, + hasCmrDms, + shipped, + }; const conn = Self.dataSource.connector; - let where = buildFilter(ctx.args, (param, value) => { + let where = buildFilter(params, (param, value) => { switch (param) { case 'cmrFk': return {'cmrFk': value}; @@ -80,7 +90,7 @@ module.exports = Self => { } }); - filter = mergeFilters(ctx.args.filter, {where}); + filter = mergeFilters(filter, {where}); if (!filter.where) { const yesterday = new Date(); From b1971095566ffe7a6c62ab1cd0925f25329affe1 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 10 Aug 2023 10:14:22 +0200 Subject: [PATCH 4/4] refs #5995 Requested changes --- .../back/methods/route/getExternalCmrs.js | 24 ++++--------------- 1 file changed, 4 insertions(+), 20 deletions(-) diff --git a/modules/route/back/methods/route/getExternalCmrs.js b/modules/route/back/methods/route/getExternalCmrs.js index b7312d02e..5b08cf34a 100644 --- a/modules/route/back/methods/route/getExternalCmrs.js +++ b/modules/route/back/methods/route/getExternalCmrs.js @@ -73,23 +73,7 @@ module.exports = Self => { }; const conn = Self.dataSource.connector; - let where = buildFilter(params, (param, value) => { - switch (param) { - case 'cmrFk': - return {'cmrFk': value}; - case 'ticketFk': - return {'ticketFk': value}; - case 'country': - return {'country': value}; - case 'clientFk': - return {'clientFk': value}; - case 'hasCmrDms': - return {'hasCmrDms': value}; - case 'shipped': - return {'shipped': value}; - } - }); - + let where = buildFilter(params, (param, value) => {return {[param]: value}}); filter = mergeFilters(filter, {where}); if (!filter.where) { @@ -140,10 +124,10 @@ module.exports = Self => { `); stmt.merge(conn.makeSuffix(filter)); - let itemsIndex = stmts.push(stmt) - 1; + const itemsIndex = stmts.push(stmt) - 1; - let sql = ParameterizedSQL.join(stmts, ';'); - let result = await conn.executeStmt(sql); + const sql = ParameterizedSQL.join(stmts, ';'); + const result = await conn.executeStmt(sql); return itemsIndex === 0 ? result : result[itemsIndex]; }; };