#7108 - exchange-rate #2295
|
@ -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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,11 @@
|
|||
{
|
||||
"name": "Collection",
|
||||
"base": "VnModel",
|
||||
"options": {
|
||||
"mysql": {
|
||||
"table": "collection"
|
||||
}
|
||||
},
|
||||
"acls": [{
|
||||
"property": "validations",
|
||||
"accessType": "EXECUTE",
|
||||
|
@ -9,4 +14,3 @@
|
|||
"permission": "ALLOW"
|
||||
}]
|
||||
}
|
||||
|
|
@ -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",
|
||||
|
|
|
@ -0,0 +1,2 @@
|
|||
INSERT INTO `salix`.`ACL` (`model`, `property`, `accessType`, `permission`, `principalType`, `principalId`)
|
||||
VALUES ('Collection', 'exchangeRateUpdate', '*', 'ALLOW', 'ROLE', 'employee');
|
|
@ -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);
|
Loading…
Reference in New Issue