From bb6ea31bf715e59f49a656fa505a722f50761010 Mon Sep 17 00:00:00 2001 From: jgallego Date: Fri, 12 Apr 2024 10:26:32 +0200 Subject: [PATCH] feat: funcional a falta de test --- back/methods/collection/exchangeRateUpdate.js | 41 ++++++++++--------- back/models/collection.json | 6 ++- back/models/reference-rate.json | 17 ++++---- db/versions/10992-goldenIvy/00-acl.sql | 2 + .../10992-goldenIvy/00-referenceRate.sql | 4 ++ 5 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 db/versions/10992-goldenIvy/00-acl.sql create mode 100644 db/versions/10992-goldenIvy/00-referenceRate.sql diff --git a/back/methods/collection/exchangeRateUpdate.js b/back/methods/collection/exchangeRateUpdate.js index 8525fa980..093ee6e48 100644 --- a/back/methods/collection/exchangeRateUpdate.js +++ b/back/methods/collection/exchangeRateUpdate.js @@ -28,33 +28,36 @@ module.exports = Self => { const models = Self.app.models; const maxDateRecord = await models.ReferenceRate.findOne({order: 'dated DESC'}); - let maxDate = maxDateRecord ? new Date(maxDateRecord.dated) : null; - for (const cube of cubes) { - if (cube.attributes.getNamedItem('time')) { + const maxDate = maxDateRecord && maxDateRecord.dated ? new Date(maxDateRecord.dated) : null; + + for (let i = 0; i < cubes.length; i++) { + const cube = cubes[i]; + if (cube.nodeType === 1 && cube.attributes.getNamedItem('time')) { const xmlDate = new Date(cube.getAttribute('time')); - if (!maxDate || maxDate < xmlDate) { - for (const rateCube of cube.childNodes) { - if (rateCube.nodeType === Node.ELEMENT_NODE) { + const xmlDateWithoutTime = new Date(xmlDate.getFullYear(), xmlDate.getMonth(), xmlDate.getDate()); + if (!maxDate || maxDate < xmlDateWithoutTime) { + for (let j = 0; j < cube.childNodes.length; j++) { + const rateCube = cube.childNodes[j]; + 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}}); if (!currency) throw new UserError(`Currency not found for code: ${currencyCode}`); + const existingRate = await models.ReferenceRate.findOne({ + where: {currencyFk: currency.id, dated: xmlDate} + }); - try { - await models.ReferenceRate.upsertWithWhere( - {currencyFk: currency.id, dated: xmlDate}, - { - currencyFk: currency.id, - dated: xmlDate, - value: rate - } - ); - } catch (error) { - console.error(`Failed to upsert rate for ${currencyCode} on ${xmlDate}: ${error}`); - // Handle or throw the error accordingly - throw error; + if (existingRate) { + if (existingRate.value !== rate) + await existingRate.updateAttributes({value: rate}); + } else { + await models.ReferenceRate.create({ + currencyFk: currency.id, + dated: xmlDate, + value: rate + }); } } } diff --git a/back/models/collection.json b/back/models/collection.json index 3e428ef60..cb8dc3d7c 100644 --- a/back/models/collection.json +++ b/back/models/collection.json @@ -1,6 +1,11 @@ { "name": "Collection", "base": "VnModel", + "options": { + "mysql": { + "table": "collection" + } + }, "acls": [{ "property": "validations", "accessType": "EXECUTE", @@ -9,4 +14,3 @@ "permission": "ALLOW" }] } - \ No newline at end of file diff --git a/back/models/reference-rate.json b/back/models/reference-rate.json index 3553df5c5..b77213b29 100644 --- a/back/models/reference-rate.json +++ b/back/models/reference-rate.json @@ -8,21 +8,18 @@ } }, "properties": { + "id": { + "type": "number", + "id": true, + "description": "Identifier" + }, "currencyFk": { "type": "number", - "required": true, - "id": 1, - "mysql": { - "dataType": "tinyint", - "dataLength": 3, - "unsigned": true, - "primaryKey": true - } + "required": true }, "dated": { "type": "date", - "required": true, - "id": 2 + "required": true }, "value": { "type": "number", diff --git a/db/versions/10992-goldenIvy/00-acl.sql b/db/versions/10992-goldenIvy/00-acl.sql new file mode 100644 index 000000000..551905be1 --- /dev/null +++ b/db/versions/10992-goldenIvy/00-acl.sql @@ -0,0 +1,2 @@ +INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`) + VALUES ('Collection', 'exchangeRateUpdate', '*', 'ALLOW', 'ROLE', 'employee'); diff --git a/db/versions/10992-goldenIvy/00-referenceRate.sql b/db/versions/10992-goldenIvy/00-referenceRate.sql new file mode 100644 index 000000000..db53f328f --- /dev/null +++ b/db/versions/10992-goldenIvy/00-referenceRate.sql @@ -0,0 +1,4 @@ +ALTER TABLE vn.referenceRate DROP INDEX `PRIMARY`; +ALTER TABLE vn.referenceRate ADD id INT auto_increment PRIMARY KEY; +ALTER TABLE vn.referenceRate CHANGE id id int(11) auto_increment NOT NULL FIRST; +CREATE UNIQUE INDEX referenceRate_currencyFk_IDX USING BTREE ON vn.referenceRate (currencyFk,dated);