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
import { lessons } from "https://esm.town/v/petermillspaugh/lessons";
import { email as sendEmail } from "https://esm.town/v/std/email?v=11";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
type SubscriberRow = [emailAddress: string, currentLesson: number];
export default async function sendDailyLessons(interval: Interval) {
// No-op if I accidentally click "Run now" to avoid double sending lessons
const now = Date.now();
const time23hrs57min = (23 * 60 + 57) * 60 * 1000;
if (!interval.lastRunAt || now - interval.lastRunAt.getTime() < time23hrs57min) {
console.log("Don't manually run this function! It will email all subscribers.");
return;
}
const data = await sqlite.execute(`
SELECT email, current_lesson
FROM students
WHERE has_completed_current_lesson = 1
AND verified = 1
AND subscribed_at IS NOT NULL;
`);
for (const row of data.rows) {
const [emailAddress, currentLesson] = row as unknown as SubscriberRow;
const nextLesson = currentLesson + 1;
const res = await sqlite.execute({
sql: `
UPDATE students
SET current_lesson = ?, has_completed_current_lesson = 0
WHERE email = ?;
`,
args: [nextLesson, emailAddress],
});
await sendEmail({
to: emailAddress,
from: {
name: "Make It Stick (in 10 days, via email)",
email: "petermillspaugh.sendLesson@valtown.email",
},
replyTo: "pete@petemillspaugh.com",
subject: `Lesson ${nextLesson + 1}: ${lessons[nextLesson].title}`,
html: lessons[nextLesson].fetchHtml(emailAddress, nextLesson),
});
}
}