-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron.js
45 lines (38 loc) · 1.14 KB
/
cron.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
44
45
const cron = require('node-cron');
const UserSettings = require('./models/UserSettings');
const {
_calculateDayLimit,
_getNumberOfDaysBeforePayment,
} = require('./controllers/userSettings.controller');
const updateUserSettings = async () => {
const allUserSettings = await UserSettings.find({});
if (!allUserSettings) return;
allUserSettings.map(async (userSettings) => {
const {
user_id,
isSavedThisMonth,
paydate,
advance_date,
savings_percent,
curr_balance,
} = userSettings;
const numberOfDaysBeforePayment = _getNumberOfDaysBeforePayment({
advance_date,
paydate,
});
const newDayLimit = await _calculateDayLimit({
user_id,
numberOfDaysBeforePayment,
isSavedThisMonth,
paydate,
curr_balance,
savings_percent,
});
userSettings.day_limit = newDayLimit;
console.log('NEW DAY LIMIT', newDayLimit);
await userSettings.save();
});
};
cron.schedule('40 23 * * *', () => {
updateUserSettings();
});