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:
@@ -1,12 +1,12 @@
|
||||
"use server";
|
||||
"use server"
|
||||
|
||||
import { requirePermission, PermissionDeniedError } from "@/shared/lib/auth-guard";
|
||||
import { Permissions } from "@/shared/types/permissions";
|
||||
import { CreateQuestionSchema } from "./schema";
|
||||
import type { CreateQuestionInput } from "./schema";
|
||||
import type { ActionState } from "@/shared/types/action-state";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { z } from "zod";
|
||||
import { requirePermission, PermissionDeniedError } from "@/shared/lib/auth-guard"
|
||||
import { Permissions } from "@/shared/types/permissions"
|
||||
import { CreateQuestionSchema } from "./schema"
|
||||
import type { CreateQuestionInput } from "./schema"
|
||||
import type { ActionState } from "@/shared/types/action-state"
|
||||
import { revalidatePath } from "next/cache"
|
||||
import { z } from "zod"
|
||||
import {
|
||||
createQuestionWithRelations,
|
||||
deleteQuestionByIdRecursive,
|
||||
@@ -14,69 +14,68 @@ import {
|
||||
getQuestions,
|
||||
updateQuestionById,
|
||||
type GetQuestionsParams,
|
||||
} from "./data-access";
|
||||
import type { KnowledgePointOption } from "./types";
|
||||
} from "./data-access"
|
||||
import type { KnowledgePointOption } from "./types"
|
||||
|
||||
/** Result type of getQuestions (data + meta) */
|
||||
type QuestionsListResult = Awaited<ReturnType<typeof getQuestions>>;
|
||||
type QuestionsListResult = Awaited<ReturnType<typeof getQuestions>>
|
||||
|
||||
/** Result type of getKnowledgePointOptions */
|
||||
type KnowledgePointOptionsResult = KnowledgePointOption[];
|
||||
type KnowledgePointOptionsResult = KnowledgePointOption[]
|
||||
|
||||
export async function createNestedQuestion(
|
||||
export async function createQuestionAction(
|
||||
prevState: ActionState<string> | undefined,
|
||||
formData: FormData | CreateQuestionInput
|
||||
formData: FormData | CreateQuestionInput,
|
||||
): Promise<ActionState<string>> {
|
||||
try {
|
||||
const ctx = await requirePermission(Permissions.QUESTION_CREATE);
|
||||
const ctx = await requirePermission(Permissions.QUESTION_CREATE)
|
||||
|
||||
let rawInput: unknown = formData;
|
||||
let rawInput: unknown = formData
|
||||
|
||||
if (formData instanceof FormData) {
|
||||
const jsonString = formData.get("json");
|
||||
if (typeof jsonString === "string") {
|
||||
rawInput = JSON.parse(jsonString) as unknown;
|
||||
} else {
|
||||
return { success: false, message: "Invalid submission format. Expected JSON." };
|
||||
}
|
||||
const jsonString = formData.get("json")
|
||||
if (typeof jsonString === "string") {
|
||||
rawInput = JSON.parse(jsonString) as unknown
|
||||
} else {
|
||||
return { success: false, message: "Invalid submission format. Expected JSON." }
|
||||
}
|
||||
}
|
||||
|
||||
const validatedFields = CreateQuestionSchema.safeParse(rawInput);
|
||||
const validatedFields = CreateQuestionSchema.safeParse(rawInput)
|
||||
|
||||
if (!validatedFields.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Validation failed",
|
||||
errors: validatedFields.error.flatten().fieldErrors,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const input = validatedFields.data;
|
||||
const input = validatedFields.data
|
||||
|
||||
await createQuestionWithRelations(input, ctx.userId);
|
||||
await createQuestionWithRelations(input, ctx.userId)
|
||||
|
||||
revalidatePath("/teacher/questions");
|
||||
revalidatePath("/teacher/questions")
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: "Question created successfully",
|
||||
};
|
||||
|
||||
}
|
||||
} catch (e) {
|
||||
if (e instanceof PermissionDeniedError) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
if (e instanceof Error) {
|
||||
return {
|
||||
success: false,
|
||||
message: e.message || "Database error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: "An unexpected error occurred",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -86,90 +85,90 @@ const UpdateQuestionSchema = z.object({
|
||||
difficulty: z.number().min(1).max(5),
|
||||
content: z.unknown(),
|
||||
knowledgePointIds: z.array(z.string()).optional(),
|
||||
});
|
||||
})
|
||||
|
||||
export async function updateQuestionAction(
|
||||
prevState: ActionState<string> | undefined,
|
||||
formData: FormData
|
||||
formData: FormData,
|
||||
): Promise<ActionState<string>> {
|
||||
try {
|
||||
const ctx = await requirePermission(Permissions.QUESTION_UPDATE);
|
||||
const canEditAll = ctx.dataScope.type === "all";
|
||||
const ctx = await requirePermission(Permissions.QUESTION_UPDATE)
|
||||
const canEditAll = ctx.dataScope.type === "all"
|
||||
|
||||
const jsonString = formData.get("json");
|
||||
const jsonString = formData.get("json")
|
||||
if (typeof jsonString !== "string") {
|
||||
return { success: false, message: "Invalid submission format. Expected JSON." };
|
||||
return { success: false, message: "Invalid submission format. Expected JSON." }
|
||||
}
|
||||
|
||||
const parsed = UpdateQuestionSchema.safeParse(JSON.parse(jsonString));
|
||||
const parsed = UpdateQuestionSchema.safeParse(JSON.parse(jsonString))
|
||||
if (!parsed.success) {
|
||||
return {
|
||||
success: false,
|
||||
message: "Validation failed",
|
||||
errors: parsed.error.flatten().fieldErrors,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const { id, ...updateData } = parsed.data;
|
||||
const { id, ...updateData } = parsed.data
|
||||
|
||||
await updateQuestionById(id, updateData, canEditAll, ctx.userId);
|
||||
await updateQuestionById(id, updateData, canEditAll, ctx.userId)
|
||||
|
||||
revalidatePath("/teacher/questions");
|
||||
revalidatePath("/teacher/questions")
|
||||
|
||||
return { success: true, message: "Question updated successfully" };
|
||||
return { success: true, message: "Question updated successfully" }
|
||||
} catch (e) {
|
||||
if (e instanceof PermissionDeniedError) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
if (e instanceof Error) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
return { success: false, message: "An unexpected error occurred" };
|
||||
return { success: false, message: "An unexpected error occurred" }
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteQuestionAction(
|
||||
prevState: ActionState<string> | undefined,
|
||||
formData: FormData
|
||||
formData: FormData,
|
||||
): Promise<ActionState<string>> {
|
||||
try {
|
||||
const ctx = await requirePermission(Permissions.QUESTION_DELETE);
|
||||
const canDeleteAll = ctx.dataScope.type === "all";
|
||||
const ctx = await requirePermission(Permissions.QUESTION_DELETE)
|
||||
const canDeleteAll = ctx.dataScope.type === "all"
|
||||
|
||||
const questionId = formData.get("questionId");
|
||||
const questionId = formData.get("questionId")
|
||||
if (typeof questionId !== "string") {
|
||||
return { success: false, message: "Invalid question ID" };
|
||||
return { success: false, message: "Invalid question ID" }
|
||||
}
|
||||
|
||||
await deleteQuestionByIdRecursive(questionId, canDeleteAll, ctx.userId);
|
||||
await deleteQuestionByIdRecursive(questionId, canDeleteAll, ctx.userId)
|
||||
|
||||
revalidatePath("/teacher/questions");
|
||||
revalidatePath("/teacher/questions")
|
||||
|
||||
return { success: true, message: "Question deleted successfully" };
|
||||
return { success: true, message: "Question deleted successfully" }
|
||||
} catch (e) {
|
||||
if (e instanceof PermissionDeniedError) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
if (e instanceof Error) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
return { success: false, message: "Failed to delete question" };
|
||||
return { success: false, message: "Failed to delete question" }
|
||||
}
|
||||
}
|
||||
|
||||
export async function getQuestionsAction(
|
||||
params: GetQuestionsParams
|
||||
params: GetQuestionsParams,
|
||||
): Promise<ActionState<QuestionsListResult>> {
|
||||
try {
|
||||
await requirePermission(Permissions.QUESTION_READ);
|
||||
const data = await getQuestions(params);
|
||||
return { success: true, data };
|
||||
await requirePermission(Permissions.QUESTION_READ)
|
||||
const data = await getQuestions(params)
|
||||
return { success: true, data }
|
||||
} catch (e) {
|
||||
if (e instanceof PermissionDeniedError) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
const message = e instanceof Error ? e.message : "Failed to fetch questions";
|
||||
return { success: false, message };
|
||||
const message = e instanceof Error ? e.message : "Failed to fetch questions"
|
||||
return { success: false, message }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -177,14 +176,14 @@ export async function getKnowledgePointOptionsAction(): Promise<
|
||||
ActionState<KnowledgePointOptionsResult>
|
||||
> {
|
||||
try {
|
||||
await requirePermission(Permissions.QUESTION_READ);
|
||||
const data = await getKnowledgePointOptions();
|
||||
return { success: true, data };
|
||||
await requirePermission(Permissions.QUESTION_READ)
|
||||
const data = await getKnowledgePointOptions()
|
||||
return { success: true, data }
|
||||
} catch (e) {
|
||||
if (e instanceof PermissionDeniedError) {
|
||||
return { success: false, message: e.message };
|
||||
return { success: false, message: e.message }
|
||||
}
|
||||
const message = e instanceof Error ? e.message : "Failed to fetch knowledge point options";
|
||||
return { success: false, message };
|
||||
const message = e instanceof Error ? e.message : "Failed to fetch knowledge point options"
|
||||
return { success: false, message }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user