-
Notifications
You must be signed in to change notification settings - Fork 1
/
controller.js
43 lines (36 loc) · 1.25 KB
/
controller.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
36
37
38
39
40
41
42
43
'use strict'
const providers = require('./providers');
const moment = require('moment');
const logger = require('./logger');
const storage = require('./storage/storage');
const _ = require('lodash');
const Promise = require('bluebird');
const currencies = require('./common/currencies');
const banks = require('./common/banks');
const todayInterval = moment.duration(15, 'minutes').asMilliseconds();
const lastWeekInterval = moment.duration(1, 'hour').asMilliseconds();
function updateRates(updateFunc) {
providers.forEach(provider => {
if (typeof provider[updateFunc] === 'function') {
provider[updateFunc](storage);
}
});
}
module.exports.startUpdatingRates = function startUpdatingRates() {
setInterval(updateRates, todayInterval, 'updateTodayRates');
setInterval(updateRates, lastWeekInterval, 'updateLastWeekRates');
};
module.exports.getWeeklyRates = function getWeeklyRates(weekMoments) {
const redisPromises = [];
for (const m of weekMoments) {
for (const b of banks) {
for (const c of currencies) {
redisPromises.push(storage.get(b, c, m));
}
}
}
return Promise.all(redisPromises)
.then(data => data.filter(d => !!d))
.then(data => _.groupBy(data, 'date'))
.catch(err => logger.error(err));
};