-
Notifications
You must be signed in to change notification settings - Fork 0
/
exchange-rate.js
35 lines (32 loc) · 1.03 KB
/
exchange-rate.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const request = require('request');
module.exports = {
run: async function() {
sails.log.info(new Date().toISOString(), '-', 'Run update exchange rates job');
const currencies = await Currency.find();
const realCurrencies = [];
currencies.forEach(async item => {
realCurrencies.push(item['realSymbol']);
});
const url = `http://data.fixer.io/api/latest?access_key=${sails.config.fixerApiKey}&symbols=${realCurrencies.toString()}&format=1`;
request(
{
url: url,
json: true,
},
async (error, response, body) => {
if (!error && response.statusCode === 200) {
console.log(body);
currencies.forEach(async item => {
const realRate = body.rates[item['realSymbol']];
await ExchangeRateHistory.create({
realSymbol: item['realSymbol'],
symbol: item['symbol'],
exchangeRate: realRate,
baseCurrency: body.base,
}).fetch();
});
}
},
);
},
};