forked from Bee-Balanced/Server-Repo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsendReminders.js
More file actions
66 lines (56 loc) · 2.02 KB
/
sendReminders.js
File metadata and controls
66 lines (56 loc) · 2.02 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// sendReminders.js - schedules and sends reminder emails using Knock every 3 days
import { Knock } from "@knocklabs/node";
import nodeSchedule from "node-schedule";
import db from "./db.js";
if (!process.env.KNOCK_API_KEY) {
throw new Error("KNOCK_API_KEY is not defined in your environment.");
}
const knock = new Knock(process.env.KNOCK_API_KEY);
const anchorDate = new Date("2025-01-01");
const msPerDay = 1000 * 60 * 60 * 24;
const scheduleTime = "0 12 * * *"; // every day at 12:00 PM
// send a single email using Knock
const sendEmail = async (userId, email, name) => {
try {
const unsubscribeUrl = `beebalancedhealth.com/unsubscribe?userId=${encodeURIComponent(userId)}`;
await knock.workflows.trigger("reminder", {
recipients: [{
id: String(userId),
email,
profile: {
user_id: String(userId)
}
}],
data: {
subject: "Bee Balanced Reminder",
body: "This is your Bee Balanced check-in reminder!",
date: new Date().toLocaleDateString(),
name,
unsubscribe_url: unsubscribeUrl,
},
});
console.log(`Sent reminder to ${email}`);
} catch (err) {
console.error(`Failed to send email to ${email}:`, err.message);
}
};
// run a daily scheduled job to check if a reminder should be sent
const scheduleReminderJob = () => {
nodeSchedule.scheduleJob(scheduleTime, async () => {
const today = new Date();
const daysSinceAnchor = Math.floor((today - anchorDate) / msPerDay);
if (daysSinceAnchor % 3 === 0) {
console.log("Sending reminders to eligible users...");
try {
const [users] = await db.query("SELECT id, email, full_name FROM users WHERE unsubscribed = FALSE");
const emails = users
.filter(user => user.email)
.map(user => sendEmail(user.id, user.email, user.full_name));
await Promise.all(emails);
} catch (err) {
console.error("Failed to fetch users or send reminders:", err.message);
}
}
});
};
export { scheduleReminderJob };