feat: funcional a falta de test
gitea/salix/pipeline/pr-dev This commit looks good Details

This commit is contained in:
Javi Gallego 2024-04-12 10:26:32 +02:00
parent bc9149ec37
commit bb6ea31bf7
5 changed files with 40 additions and 30 deletions

View File

@ -28,33 +28,36 @@ module.exports = Self => {
const models = Self.app.models; const models = Self.app.models;
const maxDateRecord = await models.ReferenceRate.findOne({order: 'dated DESC'}); const maxDateRecord = await models.ReferenceRate.findOne({order: 'dated DESC'});
let maxDate = maxDateRecord ? new Date(maxDateRecord.dated) : null;
for (const cube of cubes) { const maxDate = maxDateRecord && maxDateRecord.dated ? new Date(maxDateRecord.dated) : null;
if (cube.attributes.getNamedItem('time')) {
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')); const xmlDate = new Date(cube.getAttribute('time'));
if (!maxDate || maxDate < xmlDate) { const xmlDateWithoutTime = new Date(xmlDate.getFullYear(), xmlDate.getMonth(), xmlDate.getDate());
for (const rateCube of cube.childNodes) { if (!maxDate || maxDate < xmlDateWithoutTime) {
if (rateCube.nodeType === Node.ELEMENT_NODE) { 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 currencyCode = rateCube.getAttribute('currency');
const rate = rateCube.getAttribute('rate'); const rate = rateCube.getAttribute('rate');
if (['USD', 'CNY', 'GBP'].includes(currencyCode)) { if (['USD', 'CNY', 'GBP'].includes(currencyCode)) {
const currency = await models.Currency.findOne({where: {code: currencyCode}}); const currency = await models.Currency.findOne({where: {code: currencyCode}});
if (!currency) throw new UserError(`Currency not found for 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 { if (existingRate) {
await models.ReferenceRate.upsertWithWhere( if (existingRate.value !== rate)
{currencyFk: currency.id, dated: xmlDate}, await existingRate.updateAttributes({value: rate});
{ } else {
currencyFk: currency.id, await models.ReferenceRate.create({
dated: xmlDate, currencyFk: currency.id,
value: rate 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;
} }
} }
} }

View File

@ -1,6 +1,11 @@
{ {
"name": "Collection", "name": "Collection",
"base": "VnModel", "base": "VnModel",
"options": {
"mysql": {
"table": "collection"
}
},
"acls": [{ "acls": [{
"property": "validations", "property": "validations",
"accessType": "EXECUTE", "accessType": "EXECUTE",
@ -9,4 +14,3 @@
"permission": "ALLOW" "permission": "ALLOW"
}] }]
} }

View File

@ -8,21 +8,18 @@
} }
}, },
"properties": { "properties": {
"id": {
"type": "number",
"id": true,
"description": "Identifier"
},
"currencyFk": { "currencyFk": {
"type": "number", "type": "number",
"required": true, "required": true
"id": 1,
"mysql": {
"dataType": "tinyint",
"dataLength": 3,
"unsigned": true,
"primaryKey": true
}
}, },
"dated": { "dated": {
"type": "date", "type": "date",
"required": true, "required": true
"id": 2
}, },
"value": { "value": {
"type": "number", "type": "number",

View File

@ -0,0 +1,2 @@
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
VALUES ('Collection', 'exchangeRateUpdate', '*', 'ALLOW', 'ROLE', 'employee');

View File

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