diff --git a/src/components/admin/settings-form.tsx b/src/components/admin/settings-form.tsx index ab73b33..62df6ae 100644 --- a/src/components/admin/settings-form.tsx +++ b/src/components/admin/settings-form.tsx @@ -274,6 +274,11 @@ export function SettingsForm({ initial }: { initial: Settings }) { checked={bool("notifyOnHomework")} onChange={(v) => set("notifyOnHomework", v ? "true" : "false")} /> + set("notifyOnComment", v ? "true" : "false")} + /> 0 + ? configured.map((email) => ({ email, name: "" })) + : await prisma.user.findMany({ + where: { role: { in: ["admin", "curator"] } }, + select: { email: true, name: true }, + }); + const lessonUrl = `${process.env.BETTER_AUTH_URL ?? "https://school.second-brain.ru"}/courses/${slug}/lessons/${lessonId}`; + await Promise.all( + recipients.map((r) => + sendCommentNotificationEmail(r.email, r.name, session.user.name, lesson.title, trimmed, lessonUrl) + ) + ); + } + } + revalidatePath(`/courses/${slug}/lessons/${lessonId}`); } diff --git a/src/lib/email.ts b/src/lib/email.ts index 71e51b3..628e448 100644 --- a/src/lib/email.ts +++ b/src/lib/email.ts @@ -137,6 +137,31 @@ export async function sendHomeworkSubmittedEmail( }).catch((e) => console.error("[email] sendHomeworkSubmittedEmail:", e)); } +export async function sendCommentNotificationEmail( + to: string, + recipientName: string, + studentName: string, + lessonTitle: string, + commentText: string, + lessonUrl: string +) { + const school = await getSchoolName(); + const esc = (t: string) => t.replace(/&/g, "&").replace(//g, ">"); + const preview = commentText.length > 400 ? commentText.slice(0, 400) + "…" : commentText; + await getResend().emails.send({ + from: FROM, + to, + subject: `Новый комментарий под уроком — ${lessonTitle}`, + html: base(` +

Привет${recipientName ? `, ${esc(recipientName)}` : ""}!

+

${esc(studentName)} оставил комментарий под уроком «${lessonTitle}»:

+ ${quote(esc(preview))} +

Открыть урок и ответить:

+ ${btn(lessonUrl, "Открыть урок")} + `, school), + }).catch((e) => console.error("[email] sendCommentNotificationEmail:", e)); +} + export async function sendFeedbackReceivedEmail( to: string, studentName: string, diff --git a/src/lib/settings.ts b/src/lib/settings.ts index 511290a..cdb0a06 100644 --- a/src/lib/settings.ts +++ b/src/lib/settings.ts @@ -14,6 +14,7 @@ export const SETTINGS_DEFAULTS = { // Notifications notificationEmails: "", // newline-separated list notifyOnHomework: "true", + notifyOnComment: "true", notifyOnRegistration: "true", notifyStudentOnFeedback: "true",