Add email notifications via Resend
- src/lib/email.ts: HTML templates for 4 email types (Second Brain design) - Welcome email on user registration (Better Auth databaseHooks) - Course access email when admin grants enrollment - Homework submitted email to all admins/curators (first submission only) - Feedback received email to student with feedback text and lesson link - Update TECHNICAL.md: Resend domain, from-address, email vars, Stage 3 summary Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +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";
|
||||
|
||||
interface HomeworkFile {
|
||||
name: string;
|
||||
@@ -31,15 +32,37 @@ export async function submitHomework(
|
||||
throw new Error("Работа уже проверена");
|
||||
}
|
||||
|
||||
let submissionId: string;
|
||||
if (existing) {
|
||||
await prisma.homeworkSubmission.update({
|
||||
const updated = await prisma.homeworkSubmission.update({
|
||||
where: { id: existing.id },
|
||||
data: { text, files: files as object[], submittedAt: new Date() },
|
||||
});
|
||||
submissionId = updated.id;
|
||||
} else {
|
||||
await prisma.homeworkSubmission.create({
|
||||
const created = await prisma.homeworkSubmission.create({
|
||||
data: { homeworkId, userId: session.user.id, text, files: files as object[] },
|
||||
});
|
||||
submissionId = created.id;
|
||||
|
||||
// Notify admins/curators on first submission only
|
||||
const [lesson, admins] = await Promise.all([
|
||||
prisma.homework.findUnique({
|
||||
where: { id: homeworkId },
|
||||
include: { lesson: { select: { title: true } } },
|
||||
}),
|
||||
prisma.user.findMany({
|
||||
where: { role: { in: ["admin", "curator"] } },
|
||||
select: { email: true, name: true },
|
||||
}),
|
||||
]);
|
||||
if (lesson) {
|
||||
await Promise.all(
|
||||
admins.map((a) =>
|
||||
sendHomeworkSubmittedEmail(a.email, a.name, session.user.name, lesson.lesson.title, submissionId)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
revalidatePath(`/courses/${slug}/lessons/${lessonId}`);
|
||||
|
||||
Reference in New Issue
Block a user