refactor: P0-1/2/4 解耦修复 - 拆分过耦合文件 + dashboard 解耦

This commit is contained in:
SpecialX
2026-06-18 01:45:55 +08:00
parent 220061d62e
commit 62be0b9404
18 changed files with 2534 additions and 2130 deletions

View File

@@ -1,6 +1,6 @@
import { db } from "@/shared/db"
import { exams, examQuestions, questions, subjects, grades, classes } from "@/shared/db/schema"
import { eq, desc, like, and, or, inArray } from "drizzle-orm"
import { count, eq, desc, like, and, or, inArray } from "drizzle-orm"
import { cache } from "react"
import type { Exam, ExamDifficulty, ExamStatus } from "./types"
@@ -337,3 +337,37 @@ export const persistAiGeneratedExamDraft = async (input: {
}
})
}
export type ExamsDashboardStats = {
examCount: number
}
export const getExamsDashboardStats = cache(async (scope?: DataScope): Promise<ExamsDashboardStats> => {
const conditions = []
if (scope && scope.type !== "all") {
if (scope.type === "owned") {
conditions.push(eq(exams.creatorId, scope.userId))
}
if (scope.type === "grade_managed" && scope.gradeIds.length > 0) {
conditions.push(inArray(exams.gradeId, scope.gradeIds))
}
if (scope.type === "class_taught" && scope.classIds.length > 0) {
const teacherGradeIds = await db
.selectDistinct({ gradeId: classes.gradeId })
.from(classes)
.where(inArray(classes.id, scope.classIds))
const gradeIds = teacherGradeIds.map((g) => g.gradeId).filter(Boolean) as string[]
if (gradeIds.length > 0) {
conditions.push(inArray(exams.gradeId, gradeIds))
}
}
}
const [row] = await db
.select({ value: count() })
.from(exams)
.where(conditions.length ? and(...conditions) : undefined)
return { examCount: Number(row?.value ?? 0) }
})