import { fetchConfirmationHtml } from "https://esm.town/v/petermillspaugh/fetchConfirmationHtml";
import { fetchSignupHtml } from "https://esm.town/v/petermillspaugh/fetchSignupHtml";
import { markLessonComplete } from "https://esm.town/v/petermillspaugh/markLessonComplete";
import { refreshToken } from "https://esm.town/v/petermillspaugh/refreshToken";
import { sendLesson } from "https://esm.town/v/petermillspaugh/sendLesson";
import { sendLessonResponses } from "https://esm.town/v/petermillspaugh/sendLessonResponses";
import { sendVerification } from "https://esm.town/v/petermillspaugh/sendVerification";
import { upsertStudent } from "https://esm.town/v/petermillspaugh/upsertStudent";
import { email as sendEmail } from "https://esm.town/v/std/email?v=11";
import { sqlite } from "https://esm.town/v/std/sqlite?v=4";
import { Hono } from "npm:hono";
import { JSX } from "npm:react";
export async function emailSubscription(req: Request) {
const app = new Hono();
app.get("/", async (c) => {
return c.html(fetchSignupHtml());
});
app.post("/send-verification", async c => {
const formData = await c.req.formData();
const name = formData.get("name");
const email = formData.get("email");
if (typeof name !== "string" || typeof email !== "string") {
return Response.json({ message: "Unexpected missing value for name or email." });
}
const token = crypto.randomUUID();
await upsertStudent({ name, email, token });
sendVerification({ emailAddress: email, html: fetchConfirmationHtml({ email, token }) });
return Response.json({ success: true, message: "Sent verification email." });
});
app.put("/confirm-verification", async c => {
const email = c.req.query("email");
const token = c.req.query("token");
const { newToken, didRefresh } = await refreshToken({ email, token });
if (didRefresh) {
sendVerification({
emailAddress: email,
html: fetchConfirmationHtml({ email, token: newToken, reVerifying: true }),
});
return Response.json({ message: "Resent confirmation email." });
}
await sqlite.execute({
sql: `UPDATE students SET verified = 1 WHERE email = ?;`,
args: [email],
});
await sendLesson({ emailAddress: email, lesson: 0 });
sendEmail({
subject: `${email} verified for subscription to: Make It Stick (in 10 days, via email)`,
text: `Verification complete for ${email}'s subscription to: Make It Stick (in 10 days, via email)`,
});
return Response.json({ confirmed: true, message: "Verified email address." });
});
app.post("/complete-lesson", async c => {
const email = c.req.query("email");
if (typeof email !== "string") {
sendEmail({
subject: "Unexpected missing email query param on /complete-lesson",
text: "Email query param should be populated: check lesson form submission.",
});
return;
}
await markLessonComplete(email);
const lesson = c.req.query("lesson");
const formData = await c.req.formData();
await sendLessonResponses({ email, lesson, formData });
const responseHtml = `
<main>
<p>Thanks for completing the lesson! Your responses should be in your inbox any second, and the next lesson will go out tomorrow.</p>
<p>In the meantime, feel free to send any feedback to <a href="mailto:pete@petemillspaugh.com">pete@petemillspaugh.com</a>, or by replying directly to any lesson email.</p>
</main>
`;