-
Notifications
You must be signed in to change notification settings - Fork 0
/
birthday-cron.js
44 lines (37 loc) · 1.54 KB
/
birthday-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
const cron = require('node-cron');
const { db, statements } = require('./database');
const { triggerBirthdayCollection, postBirthdayThread } = require('./birthday-service');
function setupCronJobs(app) {
// Run every day at 9:00 AM Europe/London time
cron.schedule('0 9 * * *', async () => {
// Run every minute (for testing)
// cron.schedule('*/1 * * * *', async () => {
try {
// Get today's birthdays and upcoming (7 days) birthdays
const upcomingBirthdays = statements.getAllBirthdays.all();
for (const birthday of upcomingBirthdays) {
const today = new Date();
const birthdayDate = new Date(today.getFullYear(),
parseInt(birthday.birth_date.split('-')[1]) - 1,
parseInt(birthday.birth_date.split('-')[0])
);
const diffTime = birthdayDate.getTime() - today.getTime();
const diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24));
if (diffDays === 7) {
console.log(`Triggering collection for ${birthday.user_id}`)
// Trigger collection for birthdays in 7 days
await triggerBirthdayCollection(app.client, birthday.user_id);
} else if (diffDays === 0) {
console.log(`Posting thread for ${birthday.user_id}`)
// Post thread for today's birthdays
await postBirthdayThread(app.client, birthday.user_id);
}
}
} catch (error) {
console.error('Error in birthday cron job:', error);
}
}, {
timezone: "Europe/London"
});
}
module.exports = { setupCronJobs };