forked from Cv-Keep/cvkeep-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcronjobs.js
27 lines (25 loc) · 863 Bytes
/
cronjobs.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
const cron = require('node-cron');
const log = require('logflake')('cron');
const Credentials = require('./models/credentials.js');
/**
* Delete deactivated accounts that hasnt been logged for more than 30 days.
* If the user login again in 30 days, the account is reactivated. Everytime
* the user delete an account, its username is imediately released. This cron
* will run everyday at 3AM.
*/
cron.schedule('* 3 * * *', () => {
const fromDate = new Date(Date.now() - 60 * 60 * 24 * 30 * 1000);
log('info', 'Started cron: removing old deactivated accounts');
Credentials.deleteMany({
active: false,
deactivated_at: {
$gte: fromDate,
},
})
.then(result => {
log('info', 'Deactivated accounts deletion done with status: ', result);
})
.catch(error => {
log('error', 'Error while deleting deactivated accounts: ', error);
});
});