From 0610cbc32e64386c6bf8ec94dfa4d5cf56daff8c Mon Sep 17 00:00:00 2001 From: guillermo Date: Fri, 23 Aug 2024 13:12:11 +0200 Subject: [PATCH] feat: refs #7882 Added model and method --- .../quadminds-api-config/sendOrders.js | 70 +++++++++++++++++++ back/model-config.json | 3 + back/models/quadminds-api-config.js | 3 + back/models/quadminds-api-config.json | 22 ++++++ .../11196-blackCymbidium/00-firstScript.sql | 1 + 5 files changed, 99 insertions(+) create mode 100644 back/methods/quadminds-api-config/sendOrders.js create mode 100644 back/models/quadminds-api-config.js create mode 100644 back/models/quadminds-api-config.json create mode 100644 db/versions/11196-blackCymbidium/00-firstScript.sql diff --git a/back/methods/quadminds-api-config/sendOrders.js b/back/methods/quadminds-api-config/sendOrders.js new file mode 100644 index 000000000..edaf9a25a --- /dev/null +++ b/back/methods/quadminds-api-config/sendOrders.js @@ -0,0 +1,70 @@ +const axios = require('axios'); +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethod('sendOrders', { + description: 'Sends a set of orders/tickets', + accessType: 'WRITE', + accepts: [{ + arg: 'orders', + type: ['number'], + required: true + } + ], + returns: { + type: 'string', + root: true + }, + http: { + path: `/sendOrders`, + verb: 'POST' + } + }); + Self.sendOrders = async orders => { + const config = await Self.app.models.QuadmindsApiConfig.findOne(); + if (!config) throw new UserError('Config params not set'); + + let pois = await Self.rawSql(` + WITH deliveryNotes AS ( + SELECT t.id, t.routeFk, tn.description + FROM ticket t + JOIN ticketObservation tn ON tn.ticketFk = t.id + JOIN observationType ot ON ot.id = tn.observationTypeFk + WHERE ot.code = 'delivery' + ) + SELECT a.id code, + c.name, + CONCAT_WS(', ', IFNULL(a.street, ''), IFNULL(a.city, ''), IFNULL(p.name, '')) longAddress, + CONCAT(IFNULL(a.mobile, c.mobile)) phoneNumber, + dn.description poiDeliveryComments, + c.email email + FROM route r + JOIN ticket t ON t.routeFk = r.id + 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 client c ON c.id = t.clientFk + LEFT JOIN deliveryNotes dn ON dn.id = t.id + WHERE t.id IN (?) + GROUP BY t.id + `, [orders]); + + // Transformo code en string ya que lo obtenermos como integer + pois = pois.map(poi => { + return { + ...poi, + code: poi.code.toString(), + poiDeliveryComments: poi.poiDeliveryComments || undefined, + phoneNumber: poi.phoneNumber || undefined + }; + }); + + await axios.post(`${config.url}pois`, pois, { + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'X-Saas-Apikey': config.key + } + }); + }; +}; diff --git a/back/model-config.json b/back/model-config.json index cb9ee4fdb..37a9ff9f7 100644 --- a/back/model-config.json +++ b/back/model-config.json @@ -124,6 +124,9 @@ "Province": { "dataSource": "vn" }, + "QuadmindsApiConfig": { + "dataSource": "vn" + }, "Autonomy": { "dataSource": "vn" }, diff --git a/back/models/quadminds-api-config.js b/back/models/quadminds-api-config.js new file mode 100644 index 000000000..f2f36d0db --- /dev/null +++ b/back/models/quadminds-api-config.js @@ -0,0 +1,3 @@ +module.exports = Self => { + require('../methods/quadminds-api-config/sendOrders')(Self); +}; diff --git a/back/models/quadminds-api-config.json b/back/models/quadminds-api-config.json new file mode 100644 index 000000000..a78fb01ed --- /dev/null +++ b/back/models/quadminds-api-config.json @@ -0,0 +1,22 @@ +{ + "name": "QuadmindsApiConfig", + "base": "VnModel", + "options": { + "mysql": { + "table": "quadmindsApiConfig" + } + }, + "properties": { + "id": { + "type": "number", + "id": true, + "required": true + }, + "url": { + "type": "string" + }, + "key": { + "type": "string" + } + } +} diff --git a/db/versions/11196-blackCymbidium/00-firstScript.sql b/db/versions/11196-blackCymbidium/00-firstScript.sql new file mode 100644 index 000000000..637ed31b7 --- /dev/null +++ b/db/versions/11196-blackCymbidium/00-firstScript.sql @@ -0,0 +1 @@ +RENAME TABLE vn.quadMindsApiConfig TO vn.quadmindsApiConfig;