From a25e61903688c89be4b4a2bb8cf74bf161e5ab99 Mon Sep 17 00:00:00 2001 From: guillermo Date: Thu, 29 Aug 2024 14:48:21 +0200 Subject: [PATCH] fix: refs #7882 Added send orders method --- .../quadminds-api-config/sendOrders.js | 57 +++++++-------- back/methods/quadminds-api-config/sendPois.js | 70 +++++++++++++++++++ back/models/quadminds-api-config.js | 1 + 3 files changed, 100 insertions(+), 28 deletions(-) create mode 100644 back/methods/quadminds-api-config/sendPois.js diff --git a/back/methods/quadminds-api-config/sendOrders.js b/back/methods/quadminds-api-config/sendOrders.js index 5806a36ea..d71de9edd 100644 --- a/back/methods/quadminds-api-config/sendOrders.js +++ b/back/methods/quadminds-api-config/sendOrders.js @@ -1,12 +1,13 @@ const axios = require('axios'); const UserError = require('vn-loopback/util/user-error'); +const moment = require('moment'); module.exports = Self => { Self.remoteMethod('sendOrders', { - description: 'Sends a set of orders/tickets', + description: 'Sends a set of orders', accessType: 'WRITE', accepts: [{ - arg: 'orders', + arg: 'tickets', type: ['number'], required: true } @@ -20,45 +21,45 @@ module.exports = Self => { verb: 'POST' } }); - Self.sendOrders = async orders => { + Self.sendOrders = async tickets => { 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.socialName 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 + const pois = await axios.get(`${config.url}pois/search?limit=10000&offset=0`, { + headers: { + 'Accept': 'application/json', + 'X-Saas-Apikey': config.key + } + }); + + const poiMap = new Map(pois.data.data.map(poi => [poi.code, poi._id])); + + let orders = await Self.rawSql(` + SELECT a.id poiCode, + t.id code, + t.shipped date, + 'PEDIDO' operation, + t.totalWithVat totalAmount, + t.totalWithoutVat totalAmountWithoutTaxes FROM ticket t 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]); + `, [tickets]); // Transformo code en string ya que lo obtenermos como integer - pois = pois.map(poi => { + orders = orders.map(order => { return { - ...poi, - code: poi.code.toString(), - poiDeliveryComments: poi.poiDeliveryComments || undefined, - phoneNumber: poi.phoneNumber || undefined + ...order, + poiId: poiMap.get(order.poiCode.toString()) || undefined, + code: order.code.toString(), + date: moment(order.date).format('YYYY-MM-DD'), + totalAmount: order.totalAmount || undefined, + totalAmountWithoutTaxes: order.totalAmountWithoutTaxes || undefined }; }); - await axios.post(`${config.url}pois`, pois, { + await axios.post(`${config.url}orders`, orders, { headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', diff --git a/back/methods/quadminds-api-config/sendPois.js b/back/methods/quadminds-api-config/sendPois.js new file mode 100644 index 000000000..af0cafcb3 --- /dev/null +++ b/back/methods/quadminds-api-config/sendPois.js @@ -0,0 +1,70 @@ +const axios = require('axios'); +const UserError = require('vn-loopback/util/user-error'); + +module.exports = Self => { + Self.remoteMethod('sendPois', { + description: 'Sends a set of pois', + accessType: 'WRITE', + accepts: [{ + arg: 'tickets', + type: ['number'], + required: true + } + ], + returns: { + type: 'string', + root: true + }, + http: { + path: `/sendPois`, + verb: 'POST' + } + }); + Self.sendPois = async tickets => { + 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.socialName 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 ticket t + 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 + `, [tickets]); + + // 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, + email: poi.email || 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/models/quadminds-api-config.js b/back/models/quadminds-api-config.js index f2f36d0db..c2773fa0b 100644 --- a/back/models/quadminds-api-config.js +++ b/back/models/quadminds-api-config.js @@ -1,3 +1,4 @@ module.exports = Self => { + require('../methods/quadminds-api-config/sendPois')(Self); require('../methods/quadminds-api-config/sendOrders')(Self); };