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)
144 lines
3.8 KiB
TypeScript
144 lines
3.8 KiB
TypeScript
import "server-only";
|
|
|
|
import { and, desc, eq, inArray, max } from "drizzle-orm";
|
|
import { createId } from "@paralleldrive/cuid2";
|
|
|
|
import { db } from "@/shared/db";
|
|
import { lessonPlanVersions, lessonPlans } from "@/shared/db/schema";
|
|
import { normalizeDocument } from "./data-access";
|
|
import type { LessonPlanDocument, LessonPlanVersion } from "./types";
|
|
|
|
export async function getLessonPlanVersions(
|
|
planId: string,
|
|
userId: string,
|
|
): Promise<LessonPlanVersion[]> {
|
|
// 校验归属
|
|
const plan = await db
|
|
.select({ id: lessonPlans.id })
|
|
.from(lessonPlans)
|
|
.where(
|
|
and(eq(lessonPlans.id, planId), eq(lessonPlans.creatorId, userId)),
|
|
)
|
|
.limit(1);
|
|
if (plan.length === 0) return [];
|
|
|
|
const rows = await db
|
|
.select()
|
|
.from(lessonPlanVersions)
|
|
.where(eq(lessonPlanVersions.planId, planId))
|
|
.orderBy(desc(lessonPlanVersions.versionNo));
|
|
return rows as unknown as LessonPlanVersion[];
|
|
}
|
|
|
|
export async function createLessonPlanVersion(input: {
|
|
planId: string;
|
|
content: LessonPlanDocument;
|
|
userId: string;
|
|
isAuto: boolean;
|
|
label?: string;
|
|
}): Promise<{ versionNo: number }> {
|
|
// 取当前最大 versionNo
|
|
const maxRow = await db
|
|
.select({ maxNo: max(lessonPlanVersions.versionNo) })
|
|
.from(lessonPlanVersions)
|
|
.where(eq(lessonPlanVersions.planId, input.planId));
|
|
const nextNo = (maxRow[0]?.maxNo ?? 0) + 1;
|
|
|
|
await db.insert(lessonPlanVersions).values({
|
|
id: createId(),
|
|
planId: input.planId,
|
|
versionNo: nextNo,
|
|
label: input.label ?? null,
|
|
content: input.content,
|
|
isAuto: input.isAuto,
|
|
creatorId: input.userId,
|
|
});
|
|
return { versionNo: nextNo };
|
|
}
|
|
|
|
export async function getVersionContent(
|
|
planId: string,
|
|
versionNo: number,
|
|
userId: string,
|
|
): Promise<LessonPlanDocument | null> {
|
|
// 校验归属
|
|
const plan = await db
|
|
.select({ id: lessonPlans.id })
|
|
.from(lessonPlans)
|
|
.where(
|
|
and(eq(lessonPlans.id, planId), eq(lessonPlans.creatorId, userId)),
|
|
)
|
|
.limit(1);
|
|
if (plan.length === 0) return null;
|
|
|
|
const rows = await db
|
|
.select({ content: lessonPlanVersions.content })
|
|
.from(lessonPlanVersions)
|
|
.where(
|
|
and(
|
|
eq(lessonPlanVersions.planId, planId),
|
|
eq(lessonPlanVersions.versionNo, versionNo),
|
|
),
|
|
)
|
|
.limit(1);
|
|
if (rows.length === 0) return null;
|
|
return normalizeDocument(rows[0].content);
|
|
}
|
|
|
|
export async function revertToVersion(
|
|
planId: string,
|
|
versionNo: number,
|
|
userId: string,
|
|
): Promise<{ newVersionNo: number } | null> {
|
|
const content = await getVersionContent(planId, versionNo, userId);
|
|
if (!content) return null;
|
|
|
|
// 用该版本 content 覆盖当前 + 生成新版本
|
|
await db
|
|
.update(lessonPlans)
|
|
.set({ content, lastSavedAt: new Date() })
|
|
.where(
|
|
and(eq(lessonPlans.id, planId), eq(lessonPlans.creatorId, userId)),
|
|
);
|
|
|
|
const { versionNo: newNo } = await createLessonPlanVersion({
|
|
planId,
|
|
content,
|
|
userId,
|
|
isAuto: false,
|
|
label: `回退到 v${versionNo}`,
|
|
});
|
|
return { newVersionNo: newNo };
|
|
}
|
|
|
|
export async function pruneAutoVersions(
|
|
planId: string,
|
|
keep = 50,
|
|
): Promise<void> {
|
|
const rows = await db
|
|
.select({
|
|
id: lessonPlanVersions.id,
|
|
isAuto: lessonPlanVersions.isAuto,
|
|
versionNo: lessonPlanVersions.versionNo,
|
|
})
|
|
.from(lessonPlanVersions)
|
|
.where(eq(lessonPlanVersions.planId, planId))
|
|
.orderBy(desc(lessonPlanVersions.versionNo));
|
|
|
|
if (rows.length <= keep) return;
|
|
// 保留前 keep 条;超出部分只删 isAuto=true 的
|
|
const toDelete = rows.slice(keep).filter((r) => r.isAuto);
|
|
if (toDelete.length === 0) return;
|
|
await db
|
|
.delete(lessonPlanVersions)
|
|
.where(
|
|
and(
|
|
eq(lessonPlanVersions.planId, planId),
|
|
inArray(
|
|
lessonPlanVersions.id,
|
|
toDelete.map((r) => r.id),
|
|
),
|
|
),
|
|
);
|
|
}
|