feat: refs #8361 add hasToDownloadRate field to currency model and update exchange rate logic
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Javi Gallego 2025-01-14 09:07:00 +01:00
parent dbd8d816c0
commit 44765b5a64
5 changed files with 19 additions and 22 deletions

View File

@ -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

View File

@ -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';

View File

@ -0,0 +1,3 @@
UPDATE `vn`.`currency`
SET `hasToDownloadRate` = TRUE
WHERE `code` IN ('USD', 'CNY', 'GBP');

View File

@ -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);

View File

@ -20,6 +20,9 @@
},
"ratio": {
"type": "number"
},
"hasToDownloadRate": {
"type": "boolean"
}
},
"acls": [