From b40ecf9e19f8577fad9b1e2d546f251f5695eee6 Mon Sep 17 00:00:00 2001 From: guillermo Date: Mon, 2 Sep 2024 11:48:32 +0200 Subject: [PATCH] feat: refs #7882 Improved sendOrders and added more config params --- .../quadminds-api-config/sendOrders.js | 22 +++++++++++++------ back/methods/quadminds-api-config/sendPois.js | 3 +++ back/models/quadminds-api-config.json | 6 +++++ .../11196-blackCymbidium/00-firstScript.sql | 4 ++++ 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/back/methods/quadminds-api-config/sendOrders.js b/back/methods/quadminds-api-config/sendOrders.js index d71de9edd..179cad28e 100644 --- a/back/methods/quadminds-api-config/sendOrders.js +++ b/back/methods/quadminds-api-config/sendOrders.js @@ -25,14 +25,22 @@ module.exports = Self => { const config = await Self.app.models.QuadmindsApiConfig.findOne(); if (!config) throw new UserError('Config params not set'); - const pois = await axios.get(`${config.url}pois/search?limit=10000&offset=0`, { - headers: { - 'Accept': 'application/json', - 'X-Saas-Apikey': config.key - } - }); + if (tickets.length > config.maxObjects) + throw new UserError(`Quadminds does not support more than ${config.maxObjects} tickets`); - const poiMap = new Map(pois.data.data.map(poi => [poi.code, poi._id])); + let poisData = []; + let isOk; + for (let offset = 0; !isOk; offset = offset + config.limit) { + const pois = await axios.get(`${config.url}pois/search?limit=${config.limit}&offset=${offset}`, { + headers: { + 'Accept': 'application/json', + 'X-Saas-Apikey': config.key + } + }); + pois.data.data.length ? poisData.push(...pois.data.data) : isOk = true; + } + + const poiMap = new Map(poisData.map(poi => [poi.code, poi._id])); let orders = await Self.rawSql(` SELECT a.id poiCode, diff --git a/back/methods/quadminds-api-config/sendPois.js b/back/methods/quadminds-api-config/sendPois.js index 933536eb1..cb5eef93e 100644 --- a/back/methods/quadminds-api-config/sendPois.js +++ b/back/methods/quadminds-api-config/sendPois.js @@ -24,6 +24,9 @@ module.exports = Self => { const config = await Self.app.models.QuadmindsApiConfig.findOne(); if (!config) throw new UserError('Config params not set'); + if (tickets.length > config.maxObjects) + throw new UserError(`Quadminds does not support more than ${config.maxObjects} tickets`); + let pois = await Self.rawSql(` WITH deliveryNotes AS ( SELECT t.id, t.routeFk, tn.description diff --git a/back/models/quadminds-api-config.json b/back/models/quadminds-api-config.json index a78fb01ed..4213699a9 100644 --- a/back/models/quadminds-api-config.json +++ b/back/models/quadminds-api-config.json @@ -17,6 +17,12 @@ }, "key": { "type": "string" + }, + "maxObjects": { + "type": "number" + }, + "limit": { + "type": "number" } } } diff --git a/db/versions/11196-blackCymbidium/00-firstScript.sql b/db/versions/11196-blackCymbidium/00-firstScript.sql index 637ed31b7..7c4a476c5 100644 --- a/db/versions/11196-blackCymbidium/00-firstScript.sql +++ b/db/versions/11196-blackCymbidium/00-firstScript.sql @@ -1 +1,5 @@ RENAME TABLE vn.quadMindsApiConfig TO vn.quadmindsApiConfig; + +ALTER TABLE vn.quadmindsApiConfig + ADD maxObjects INT NULL COMMENT 'Número máximo de objetos en el array por petición', + ADD `limit` INT NULL COMMENT 'Limite de objetos solicitados por petición';