Add email notification on new lesson comment
Notify admins/curators (or configured notificationEmails) when a student leaves a comment under a lesson. New setting notifyOnComment (default on) + toggle in admin settings. Fires only for student comments, not admin/curator replies; comment text is HTML-escaped in the email. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -4,7 +4,7 @@ import { prisma } from "@/lib/prisma";
|
||||
import { auth } from "@/lib/auth";
|
||||
import { headers } from "next/headers";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { sendHomeworkSubmittedEmail } from "@/lib/email";
|
||||
import { sendHomeworkSubmittedEmail, sendCommentNotificationEmail } from "@/lib/email";
|
||||
import { getSettings, parseNotificationEmails, asBool } from "@/lib/settings";
|
||||
|
||||
// ── Lesson Progress ───────────────────────────────────────────────────────────
|
||||
@@ -156,7 +156,7 @@ export async function addComment(lessonId: string, slug: string, text: string, p
|
||||
|
||||
const lesson = await prisma.lesson.findUnique({
|
||||
where: { id: lessonId },
|
||||
select: { module: { select: { course: { select: { id: true } } } } },
|
||||
select: { title: true, module: { select: { course: { select: { id: true, slug: true } } } } },
|
||||
});
|
||||
if (!lesson) throw new Error("Lesson not found");
|
||||
|
||||
@@ -185,6 +185,26 @@ export async function addComment(lessonId: string, slug: string, text: string, p
|
||||
data: { lessonId, userId: session.user.id, text: trimmed, ...(parentId ? { parentId } : {}) },
|
||||
});
|
||||
|
||||
// Уведомление админам/кураторам о новом комментарии студента (не о своих ответах)
|
||||
if (!isAdmin && !isCurator) {
|
||||
const settings = await getSettings();
|
||||
if (asBool(settings.notifyOnComment)) {
|
||||
const configured = parseNotificationEmails(settings.notificationEmails);
|
||||
const recipients = configured.length > 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}`);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user