#7108 - exchange-rate #2295
|
@ -17,8 +17,10 @@ module.exports = Self => {
|
|||
const response = await axios.get('http://www.ecb.europa.eu/stats/eurofxref/eurofxref-hist-90d.xml');
|
||||
const xmlData = response.data;
|
||||
|
||||
const doc = new DOMParser().parseFromString(xmlData, 'text/xml');
|
||||
const cubes = doc.getElementsByTagName('Cube');
|
||||
const doc = new DOMParser({errorHandler: {warning: () => {}}})?.parseFromString(xmlData, 'text/xml');
|
||||
const cubes = doc?.getElementsByTagName('Cube');
|
||||
if (!cubes || cubes.length === 0)
|
||||
throw new UserError('No cubes found. Exiting the method.');
|
||||
|
||||
const models = Self.app.models;
|
||||
|
||||
|
@ -27,7 +29,7 @@ module.exports = Self => {
|
|||
const maxDate = maxDateRecord?.dated ? new Date(maxDateRecord.dated) : null;
|
||||
|
||||
for (const cube of Array.from(cubes)) {
|
||||
if (cube.nodeType === 1 && cube.attributes.getNamedItem('time')) {
|
||||
if (cube.nodeType === doc.ELEMENT_NODE && cube.attributes.getNamedItem('time')) {
|
||||
jgallego marked this conversation as resolved
Outdated
|
||||
const xmlDate = new Date(cube.getAttribute('time'));
|
||||
const xmlDateWithoutTime = new Date(xmlDate.getFullYear(), xmlDate.getMonth(), xmlDate.getDate());
|
||||
jgallego marked this conversation as resolved
Outdated
alexm
commented
Si no cal la "i" i la "j", vec mes legible fer:
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
jgallego
commented
no era iterable li he posat Array.from( no era iterable li he posat Array.from(
|
||||
if (!maxDate || maxDate < xmlDateWithoutTime) {
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
describe('exchangeRateUpdate functionality', function() {
|
||||
const axios = require('axios');
|
||||
const models = require('vn-loopback/server/server').models;
|
||||
|
||||
beforeEach(function() {
|
||||
spyOn(axios, 'get').and.returnValue(Promise.resolve({
|
||||
data: `<Cube>
|
||||
<Cube time='2024-04-12'>
|
||||
<Cube currency='USD' rate='1.1'/>
|
||||
<Cube currency='CNY' rate='1.2'/>
|
||||
</Cube>
|
||||
</Cube>`
|
||||
}));
|
||||
});
|
||||
|
||||
it('should process XML data and update or create rates in the database', async function() {
|
||||
spyOn(models.ReferenceRate, 'findOne').and.returnValue(Promise.resolve(null));
|
||||
spyOn(models.ReferenceRate, 'create').and.returnValue(Promise.resolve());
|
||||
|
||||
await models.Collection.exchangeRateUpdate();
|
||||
|
||||
expect(models.ReferenceRate.create).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('should not create or update rates when no XML data is available', async function() {
|
||||
axios.get.and.returnValue(Promise.resolve({}));
|
||||
spyOn(models.ReferenceRate, 'create');
|
||||
|
||||
let thrownError = null;
|
||||
try {
|
||||
await models.Collection.exchangeRateUpdate();
|
||||
} catch (error) {
|
||||
thrownError = error;
|
||||
}
|
||||
|
||||
expect(thrownError.message).toBe('No cubes found. Exiting the method.');
|
||||
});
|
||||
|
||||
it('should handle errors gracefully', async function() {
|
||||
axios.get.and.returnValue(Promise.reject(new Error('Network error')));
|
||||
let error;
|
||||
|
||||
try {
|
||||
await models.Collection.exchangeRateUpdate();
|
||||
} catch (e) {
|
||||
error = e;
|
||||
}
|
||||
|
||||
expect(error).toBeDefined();
|
||||
expect(error.message).toBe('Network error');
|
||||
});
|
||||
});
|
Loading…
Reference in New Issue
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