From 44765b5a64dc00913c59b83776c1c7fbd10df7a7 Mon Sep 17 00:00:00 2001 From: jgallego Date: Tue, 14 Jan 2025 09:07:00 +0100 Subject: [PATCH] feat: refs #8361 add hasToDownloadRate field to currency model and update exchange rate logic --- db/dump/fixtures.before.sql | 12 +++++------ .../11406-bronzeMoss/00-currrencyAlter.sql | 2 ++ .../11406-bronzeMoss/01-currrencyUpdate.sql | 3 +++ .../methods/invoice-in/exchangeRateUpdate.js | 21 +++++-------------- modules/travel/back/models/currency.json | 3 +++ 5 files changed, 19 insertions(+), 22 deletions(-) create mode 100644 db/versions/11406-bronzeMoss/00-currrencyAlter.sql create mode 100644 db/versions/11406-bronzeMoss/01-currrencyUpdate.sql diff --git a/db/dump/fixtures.before.sql b/db/dump/fixtures.before.sql index ff896b84d..788854b4e 100644 --- a/db/dump/fixtures.before.sql +++ b/db/dump/fixtures.before.sql @@ -158,13 +158,13 @@ INSERT INTO `account`.`mailForward`(`account`, `forwardTo`) -INSERT INTO `vn`.`currency`(`id`, `code`, `name`, `ratio`) +INSERT INTO `vn`.`currency`(`id`, `code`, `name`, `ratio`, `hasToDownloadRate`) VALUES - (1, 'EUR', 'Euro', 1), - (2, 'USD', 'Dollar USA', 1.4), - (3, 'GBP', 'Libra', 1), - (4, 'JPY', 'Yen Japones', 1), - (5, 'CNY', 'Yuan Chino', 1.2); + (1, 'EUR', 'Euro', 1, FALSE), + (2, 'USD', 'Dollar USA', 1.4, TRUE), + (3, 'GBP', 'Libra', 1, TRUE), + (4, 'JPY', 'Yen Japones', 1, FALSE), + (5, 'CNY', 'Yuan Chino', 1.2, TRUE); INSERT INTO `vn`.`country`(`id`, `name`, `isUeeMember`, `code`, `currencyFk`, `ibanLength`, `continentFk`, `hasDailyInvoice`, `CEE`) VALUES diff --git a/db/versions/11406-bronzeMoss/00-currrencyAlter.sql b/db/versions/11406-bronzeMoss/00-currrencyAlter.sql new file mode 100644 index 000000000..86465545e --- /dev/null +++ b/db/versions/11406-bronzeMoss/00-currrencyAlter.sql @@ -0,0 +1,2 @@ +ALTER TABLE `vn`.`currency` +ADD COLUMN `hasToDownloadRate` TINYINT(1) NOT NULL DEFAULT 0 comment 'Si se guarda el tipo de cambio diariamente en referenceRate'; diff --git a/db/versions/11406-bronzeMoss/01-currrencyUpdate.sql b/db/versions/11406-bronzeMoss/01-currrencyUpdate.sql new file mode 100644 index 000000000..5e0882de2 --- /dev/null +++ b/db/versions/11406-bronzeMoss/01-currrencyUpdate.sql @@ -0,0 +1,3 @@ +UPDATE `vn`.`currency` + SET `hasToDownloadRate` = TRUE + WHERE `code` IN ('USD', 'CNY', 'GBP'); diff --git a/modules/invoiceIn/back/methods/invoice-in/exchangeRateUpdate.js b/modules/invoiceIn/back/methods/invoice-in/exchangeRateUpdate.js index a6bad405f..99ff4cd79 100644 --- a/modules/invoiceIn/back/methods/invoice-in/exchangeRateUpdate.js +++ b/modules/invoiceIn/back/methods/invoice-in/exchangeRateUpdate.js @@ -36,6 +36,7 @@ module.exports = Self => { if (!cubes || cubes.length === 0) throw new UserError('No cubes found. Exiting the method.'); + const currencies = await models.Currency.find({where: {hasToDownloadRate: true}}, myOptions); const maxDateRecord = await models.ReferenceRate.findOne({order: 'dated DESC'}, myOptions); const maxDate = maxDateRecord?.dated ? new Date(maxDateRecord.dated) : null; let lastProcessedDate = maxDate; @@ -51,32 +52,20 @@ module.exports = Self => { if (!maxDate || xmlDateWithoutTime > maxDate) { if (lastProcessedDate && xmlDateWithoutTime > lastProcessedDate) { - for (const code of ['USD', 'CNY', 'GBP']) { - const currency = await models.Currency.findOne( - {where: {code}}, - myOptions - ); - if (!currency) - throw new UserError(`Currency not found for code: ${code}`); - + for (const currency of currencies) { await fillMissingDates( models, currency, lastProcessedDate, xmlDateWithoutTime, myOptions ); } } } + for (const rateCube of Array.from(cube.childNodes)) { if (rateCube.nodeType === doc.ELEMENT_NODE) { const currencyCode = rateCube.getAttribute('currency'); const rate = rateCube.getAttribute('rate'); - if (['USD', 'CNY', 'GBP'].includes(currencyCode)) { - const currency = await models.Currency.findOne( - {where: {code: currencyCode}}, - myOptions - ); - if (!currency) - throw new UserError(`Currency not found for code: ${currencyCode}`); - + const currency = currencies.find(c => c.code === currencyCode); + if (currency) { const existingRate = await models.ReferenceRate.findOne({ where: {currencyFk: currency.id, dated: xmlDateWithoutTime} }, myOptions); diff --git a/modules/travel/back/models/currency.json b/modules/travel/back/models/currency.json index f3241fad1..427a18e31 100644 --- a/modules/travel/back/models/currency.json +++ b/modules/travel/back/models/currency.json @@ -20,6 +20,9 @@ }, "ratio": { "type": "number" + }, + "hasToDownloadRate": { + "type": "boolean" } }, "acls": [