#7108 - exchange-rate #2295

Merged
jgallego merged 11 commits from 7108-exchange-rate into dev 2024-04-26 05:00:07 +00:00
5 changed files with 40 additions and 30 deletions
Showing only changes of commit bb6ea31bf7 - Show all commits

View File

@ -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;
jgallego marked this conversation as resolved Outdated
Outdated
Review

Se pot simplificar en const maxDate = maxDateRecord?.dated ? new Date(maxDateRecord.dated) : null;

Se pot simplificar en `const maxDate = maxDateRecord?.dated ? new Date(maxDateRecord.dated) : null;`

const maxDate = maxDateRecord?.dated ? new Date(maxDateRecord.dated) : null;

Unhandled error for request POST /api/Collections/exchangeRateUpdate?access_token=YQ8SPYxYrHrY4veSEyY5q9YJD3AtcjEqcGXxyA5rweDZO92PLp0FB2WhBxlbTvUO: TypeError: Cannot read properties of null (reading 'dated')

Ahir em va fallar..ací t'he posat la conversa amb chatGpt...ara sí va..ho canvie de nou

const maxDate = maxDateRecord?.dated ? new Date(maxDateRecord.dated) : null; Unhandled error for request POST /api/Collections/exchangeRateUpdate?access_token=YQ8SPYxYrHrY4veSEyY5q9YJD3AtcjEqcGXxyA5rweDZO92PLp0FB2WhBxlbTvUO: TypeError: Cannot read properties of null (reading 'dated') Ahir em va fallar..ací t'he posat la conversa amb chatGpt...ara sí va..ho canvie de nou
for (let i = 0; i < cubes.length; i++) {
jgallego marked this conversation as resolved Outdated
Outdated
Review

Si no cal la "i" i la "j", vec mes legible fer:

for (const/let cube of cubes) 

i lo mateix en el altre for

Si no cal la "i" i la "j", vec mes legible fer: ``` for (const/let cube of cubes) ``` i lo mateix en el altre for

no era iterable li he posat Array.from(

no era iterable li he posat Array.from(
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
});
}
}
}

View File

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

View File

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

View File

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

'invoiceIn' deuria ser en PascalCase ns si aixina fallaria.

'invoiceIn' deuria ser en PascalCase ns si aixina fallaria.

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