feat: 新增备课模块并修复全模块 P0/P1/P2 缺陷
Some checks failed
Security / deep-security-scan (push) Failing after 20m5s
DR Drill / dr-drill (push) Failing after 1m31s
CI / scheduled-backup (push) Failing after 1m31s
CI / backup-verify (push) Has been skipped
CI / weekly-dr-drill (push) Failing after 0s
CI / build-deploy (push) Has been cancelled
CI / security-scan (push) Has been cancelled
Some checks failed
Security / deep-security-scan (push) Failing after 20m5s
DR Drill / dr-drill (push) Failing after 1m31s
CI / scheduled-backup (push) Failing after 1m31s
CI / backup-verify (push) Has been skipped
CI / weekly-dr-drill (push) Failing after 0s
CI / build-deploy (push) Has been cancelled
CI / security-scan (push) Has been cancelled
主要变更: - 新增 lesson-preparation 模块: 备课编辑器、节点编辑、AI 建议、知识点选择、版本历史、作业发布 - 新增 shared 通用组件: charts/question-bank-filters/schedule-list/ui (chip-nav/filter-bar/page-header/stat-card/stat-item) - 新增 student/admin 端 loading.tsx 与 error.tsx, 优化加载与错误态体验 - 新增 teacher/lesson-plans 页面 (列表/新建/编辑) - 新增 drizzle 迁移 0002_tiny_lionheart 及 snapshot - 新增 textbooks/schema.ts 与 exams/utils/normalize-structure.ts - 修复 Tiptap v3 SSR hydration 崩溃 (rich-text-block immediatelyRender: false) - 重构多模块 data-access/actions/组件, 修复权限校验与类型规范 - 同步架构文档 004/005 反映新增模块、导出、依赖关系 - 归档 bugs/* 测试报告与 e2e 测试脚本 (admin/parent/student/teacher web_test)
This commit is contained in:
175
src/modules/lesson-preparation/publish-service.ts
Normal file
175
src/modules/lesson-preparation/publish-service.ts
Normal file
@@ -0,0 +1,175 @@
|
||||
import "server-only";
|
||||
|
||||
import { eq } from "drizzle-orm";
|
||||
import { createId } from "@paralleldrive/cuid2";
|
||||
|
||||
import { db } from "@/shared/db";
|
||||
import {
|
||||
lessonPlans,
|
||||
examQuestions,
|
||||
} from "@/shared/db/schema";
|
||||
import { createQuestionWithRelations } from "@/modules/questions/data-access";
|
||||
import { persistExamDraft } from "@/modules/exams/data-access";
|
||||
import { createHomeworkAssignment } from "@/modules/homework/data-access-write";
|
||||
import { normalizeDocument } from "./data-access";
|
||||
import type { LessonPlanDocument, ExerciseBlockData } from "./types";
|
||||
|
||||
interface PublishInput {
|
||||
planId: string;
|
||||
blockId: string;
|
||||
userId: string;
|
||||
classIds: string[];
|
||||
availableAt?: Date;
|
||||
dueAt?: Date;
|
||||
}
|
||||
|
||||
interface PublishResult {
|
||||
examId: string;
|
||||
assignmentId: string;
|
||||
updatedContent: LessonPlanDocument;
|
||||
}
|
||||
|
||||
// 查询班级学生列表(避免直接依赖 classes 模块的内部表)
|
||||
async function getStudentIdsByClassIds(
|
||||
classIds: string[],
|
||||
): Promise<string[]> {
|
||||
if (classIds.length === 0) return [];
|
||||
const { inArray } = await import("drizzle-orm");
|
||||
const { classEnrollments } = await import("@/shared/db/schema");
|
||||
const rows = await db
|
||||
.select({ studentId: classEnrollments.studentId })
|
||||
.from(classEnrollments)
|
||||
.where(inArray(classEnrollments.classId, classIds));
|
||||
return rows.map((r) => r.studentId);
|
||||
}
|
||||
|
||||
export async function publishLessonPlanHomework(
|
||||
input: PublishInput,
|
||||
): Promise<PublishResult> {
|
||||
// 1. 读取课案
|
||||
const rows = await db
|
||||
.select()
|
||||
.from(lessonPlans)
|
||||
.where(eq(lessonPlans.id, input.planId))
|
||||
.limit(1);
|
||||
if (rows.length === 0) throw new Error("课案不存在");
|
||||
const row = rows[0] as unknown as {
|
||||
id: string;
|
||||
content: unknown;
|
||||
creatorId: string;
|
||||
title: string;
|
||||
textbookId: string | null;
|
||||
chapterId: string | null;
|
||||
subjectId: string | null;
|
||||
gradeId: string | null;
|
||||
};
|
||||
const plan = {
|
||||
...row,
|
||||
content: normalizeDocument(row.content),
|
||||
};
|
||||
if (plan.creatorId !== input.userId)
|
||||
throw new Error("无权发布");
|
||||
|
||||
// 2. 定位 exercise block
|
||||
const block = plan.content.nodes.find((b) => b.id === input.blockId);
|
||||
if (!block || block.type !== "exercise")
|
||||
throw new Error("练习块不存在");
|
||||
const data = block.data as ExerciseBlockData;
|
||||
if (data.items.length === 0) throw new Error("练习块无题目");
|
||||
if (data.publishedAssignmentId)
|
||||
throw new Error("该练习块已发布,请使用'重新发布'");
|
||||
|
||||
// 3. inline 题目入库,替换占位 ID
|
||||
const newContent: LessonPlanDocument = JSON.parse(
|
||||
JSON.stringify(plan.content),
|
||||
);
|
||||
const newBlock = newContent.nodes.find((b) => b.id === input.blockId);
|
||||
if (!newBlock || newBlock.type !== "exercise")
|
||||
throw new Error("练习块不存在");
|
||||
const newData = newBlock.data as ExerciseBlockData;
|
||||
|
||||
for (let i = 0; i < newData.items.length; i++) {
|
||||
const item = newData.items[i];
|
||||
if (item.source === "inline" && item.inlineContent) {
|
||||
const questionId = await createQuestionWithRelations(
|
||||
{
|
||||
content: item.inlineContent.content,
|
||||
type: item.inlineContent.type as never,
|
||||
difficulty: item.inlineContent.difficulty,
|
||||
knowledgePointIds: item.inlineContent.knowledgePointIds,
|
||||
},
|
||||
input.userId,
|
||||
);
|
||||
newData.items[i] = {
|
||||
...item,
|
||||
questionId,
|
||||
inlineContent: undefined,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 打包 exam 草稿
|
||||
const examId = createId();
|
||||
if (!plan.subjectId || !plan.gradeId) {
|
||||
throw new Error("课案缺少学科或年级信息,无法发布");
|
||||
}
|
||||
await persistExamDraft({
|
||||
examId,
|
||||
title: `${plan.title} - 作业`,
|
||||
creatorId: input.userId,
|
||||
subjectId: plan.subjectId,
|
||||
gradeId: plan.gradeId,
|
||||
scheduledAt: undefined,
|
||||
description: `来自课案:${plan.title}`,
|
||||
});
|
||||
// 插入 examQuestions
|
||||
if (newData.items.length > 0) {
|
||||
await db.insert(examQuestions).values(
|
||||
newData.items.map((it, i) => ({
|
||||
examId,
|
||||
questionId: it.questionId,
|
||||
score: it.score,
|
||||
order: i,
|
||||
})),
|
||||
);
|
||||
}
|
||||
|
||||
// 5. 下发作业
|
||||
const assignmentId = createId();
|
||||
const targetStudentIds = await getStudentIdsByClassIds(input.classIds);
|
||||
if (targetStudentIds.length === 0) {
|
||||
throw new Error("所选班级无学生");
|
||||
}
|
||||
await createHomeworkAssignment({
|
||||
assignmentId,
|
||||
sourceExamId: examId,
|
||||
title: `${plan.title} - 作业`,
|
||||
description: `来自课案:${plan.title}`,
|
||||
structure: null,
|
||||
status: "published",
|
||||
creatorId: input.userId,
|
||||
availableAt: input.availableAt ?? null,
|
||||
dueAt: input.dueAt ?? null,
|
||||
allowLate: false,
|
||||
lateDueAt: null,
|
||||
maxAttempts: 1,
|
||||
publish: true,
|
||||
questions: newData.items.map((it, i) => ({
|
||||
questionId: it.questionId,
|
||||
score: it.score,
|
||||
order: i,
|
||||
})),
|
||||
targetStudentIds,
|
||||
});
|
||||
|
||||
// 6. 回写溯源标记
|
||||
newData.publishedExamId = examId;
|
||||
newData.publishedAssignmentId = assignmentId;
|
||||
newData.publishedAt = new Date().toISOString();
|
||||
await db
|
||||
.update(lessonPlans)
|
||||
.set({ content: newContent })
|
||||
.where(eq(lessonPlans.id, input.planId));
|
||||
|
||||
return { examId, assignmentId, updatedContent: newContent };
|
||||
}
|
||||
Reference in New Issue
Block a user