fix(dashboard): v3 审计修复 — 数据完整性、i18n、类型安全、死代码清理
P0 修复(严重):
- admin ContentRow 标签与值错配(stats.users→textbooks 等 6 处)
- admin/error.tsx 硬编码中文替换为 useTranslations
- UserGrowthChart 空数据时渲染 EmptyState(userGrowth/homeworkTrend 永远为空数组)
P1 修复(高):
- 新增 admin/dashboard 和 student/dashboard 的 loading.tsx + error.tsx
- 抽取 DashboardLoadingSkeleton 和 DashboardErrorFallback 共享组件,消除 5 套重复文件
- formatDate/formatLongDate 传入用户 locale(admin/teacher/student 共 6 个组件)
- 移除死代码:getCachedAdminDashboard、AvatarImage src={undefined}、TeacherStats isLoading prop
- filterTodaySchedule 改为泛型函数,消除 as 类型断言
- 辅助函数 getStatus/getDueUrgency 新增显式返回类型
- UserGrowthChart 新增 labelKey prop 区分用户增长/作业提交趋势标签
P2 修复(中):
- 4 个组件从客户端转为服务端组件(DashboardGreetingHeader、TeacherQuickActions、TeacherDashboardHeader、StudentDashboardHeader)
- Student dashboard 空状态新增 CTA(viewSchedule、viewAll)
- TeacherHomeworkCard 图标按钮新增 aria-label
- TeacherTodoCard 排序逻辑重写为可读的 if/return 模式
同步更新:
- docs/architecture/005_architecture_data.json 新增 DashboardLoadingSkeleton、DashboardErrorFallback 条目
- 新增 docs/architecture/audit/dashboard-audit-report-v3.md 审计报告
- dashboard.json 新增 6 个 i18n 键(textbooks/chapters/questions/exams/totalAssignments/totalSubmissions)
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import "server-only"
|
||||
|
||||
import { cache } from "react"
|
||||
import { and, count, desc, eq, inArray, isNull, lte, or, sql } from "drizzle-orm"
|
||||
import { and, asc, count, desc, eq, gt, inArray, isNull, lt, lte, or, sql } from "drizzle-orm"
|
||||
|
||||
import { db } from "@/shared/db"
|
||||
import {
|
||||
@@ -118,11 +118,113 @@ export const getHomeworkAssignments = cache(async (params?: { creatorId?: string
|
||||
},
|
||||
})
|
||||
|
||||
if (data.length === 0) return []
|
||||
|
||||
const assignmentIds = data.map((a) => a.id)
|
||||
const now = new Date()
|
||||
|
||||
// 并行查询:目标学生数 / 已提交数 / 已批改数 / 已批改平均分 / 逾期未提交学生集合
|
||||
const [targetCountRows, submittedCountRows, gradedCountRows, avgScoreRows, submittedStudentRows] = await Promise.all([
|
||||
db
|
||||
.select({
|
||||
assignmentId: homeworkAssignmentTargets.assignmentId,
|
||||
targetCount: sql<number>`COUNT(*)`,
|
||||
})
|
||||
.from(homeworkAssignmentTargets)
|
||||
.where(inArray(homeworkAssignmentTargets.assignmentId, assignmentIds))
|
||||
.groupBy(homeworkAssignmentTargets.assignmentId),
|
||||
db
|
||||
.select({
|
||||
assignmentId: homeworkSubmissions.assignmentId,
|
||||
submittedCount: sql<number>`COUNT(DISTINCT ${homeworkSubmissions.studentId})`,
|
||||
})
|
||||
.from(homeworkSubmissions)
|
||||
.where(
|
||||
and(
|
||||
inArray(homeworkSubmissions.assignmentId, assignmentIds),
|
||||
inArray(homeworkSubmissions.status, ["submitted", "graded"])
|
||||
)
|
||||
)
|
||||
.groupBy(homeworkSubmissions.assignmentId),
|
||||
db
|
||||
.select({
|
||||
assignmentId: homeworkSubmissions.assignmentId,
|
||||
gradedCount: sql<number>`COUNT(DISTINCT ${homeworkSubmissions.studentId})`,
|
||||
})
|
||||
.from(homeworkSubmissions)
|
||||
.where(and(inArray(homeworkSubmissions.assignmentId, assignmentIds), eq(homeworkSubmissions.status, "graded")))
|
||||
.groupBy(homeworkSubmissions.assignmentId),
|
||||
db
|
||||
.select({
|
||||
assignmentId: homeworkSubmissions.assignmentId,
|
||||
avgScore: sql<number>`AVG(${homeworkSubmissions.score})`,
|
||||
})
|
||||
.from(homeworkSubmissions)
|
||||
.where(
|
||||
and(
|
||||
inArray(homeworkSubmissions.assignmentId, assignmentIds),
|
||||
eq(homeworkSubmissions.status, "graded")
|
||||
)
|
||||
)
|
||||
.groupBy(homeworkSubmissions.assignmentId),
|
||||
db
|
||||
.select({
|
||||
assignmentId: homeworkSubmissions.assignmentId,
|
||||
studentId: homeworkSubmissions.studentId,
|
||||
})
|
||||
.from(homeworkSubmissions)
|
||||
.where(
|
||||
and(
|
||||
inArray(homeworkSubmissions.assignmentId, assignmentIds),
|
||||
inArray(homeworkSubmissions.status, ["submitted", "graded"])
|
||||
)
|
||||
),
|
||||
])
|
||||
|
||||
const targetCountByAssignmentId = new Map<string, number>()
|
||||
for (const r of targetCountRows) targetCountByAssignmentId.set(r.assignmentId, Number(r.targetCount ?? 0))
|
||||
|
||||
const submittedCountByAssignmentId = new Map<string, number>()
|
||||
for (const r of submittedCountRows) submittedCountByAssignmentId.set(r.assignmentId, Number(r.submittedCount ?? 0))
|
||||
|
||||
const gradedCountByAssignmentId = new Map<string, number>()
|
||||
for (const r of gradedCountRows) gradedCountByAssignmentId.set(r.assignmentId, Number(r.gradedCount ?? 0))
|
||||
|
||||
const avgScoreByAssignmentId = new Map<string, number | null>()
|
||||
for (const r of avgScoreRows) {
|
||||
const v = r.avgScore
|
||||
avgScoreByAssignmentId.set(r.assignmentId, v === null ? null : Number(v))
|
||||
}
|
||||
|
||||
// 已提交学生集合(按 assignmentId 分组),用于计算逾期未提交人数
|
||||
const submittedStudentIdsByAssignmentId = new Map<string, Set<string>>()
|
||||
for (const r of submittedStudentRows) {
|
||||
let set = submittedStudentIdsByAssignmentId.get(r.assignmentId)
|
||||
if (!set) {
|
||||
set = new Set<string>()
|
||||
submittedStudentIdsByAssignmentId.set(r.assignmentId, set)
|
||||
}
|
||||
set.add(r.studentId)
|
||||
}
|
||||
|
||||
// 逾期未提交人数 = 目标学生数 - 已提交学生数(仅当 dueAt 已过时计算)
|
||||
const computeOverdueCount = (assignmentId: string, dueAt: Date | null): number => {
|
||||
if (!dueAt || dueAt > now) return 0
|
||||
const targetCount = targetCountByAssignmentId.get(assignmentId) ?? 0
|
||||
const submittedCount = submittedStudentIdsByAssignmentId.get(assignmentId)?.size ?? 0
|
||||
return Math.max(0, targetCount - submittedCount)
|
||||
}
|
||||
|
||||
return data.map((a) => {
|
||||
const targetCount = targetCountByAssignmentId.get(a.id) ?? 0
|
||||
const submittedCount = submittedCountByAssignmentId.get(a.id) ?? 0
|
||||
const gradedCount = gradedCountByAssignmentId.get(a.id) ?? 0
|
||||
const averageScore = avgScoreByAssignmentId.get(a.id) ?? null
|
||||
const overdueCount = computeOverdueCount(a.id, a.dueAt)
|
||||
const item: HomeworkAssignmentListItem = {
|
||||
id: a.id,
|
||||
sourceExamId: a.sourceExamId,
|
||||
sourceExamTitle: a.sourceExam.title,
|
||||
sourceExamTitle: a.sourceExam?.title ?? null,
|
||||
title: a.title,
|
||||
status: toHomeworkAssignmentStatus(a.status),
|
||||
availableAt: a.availableAt ? a.availableAt.toISOString() : null,
|
||||
@@ -132,6 +234,11 @@ export const getHomeworkAssignments = cache(async (params?: { creatorId?: string
|
||||
maxAttempts: a.maxAttempts,
|
||||
createdAt: a.createdAt.toISOString(),
|
||||
updatedAt: a.updatedAt.toISOString(),
|
||||
targetCount,
|
||||
submittedCount,
|
||||
gradedCount,
|
||||
averageScore,
|
||||
overdueCount,
|
||||
}
|
||||
return item
|
||||
})
|
||||
@@ -221,7 +328,7 @@ export const getHomeworkAssignmentReviewList = cache(async (params: { creatorId:
|
||||
id: a.id,
|
||||
title: a.title,
|
||||
status: toHomeworkAssignmentStatus(a.status),
|
||||
sourceExamTitle: a.sourceExam.title,
|
||||
sourceExamTitle: a.sourceExam?.title ?? null,
|
||||
dueAt: a.dueAt ? a.dueAt.toISOString() : null,
|
||||
targetCount: targetCountByAssignmentId.get(a.id) ?? 0,
|
||||
submittedCount: submittedCountByAssignmentId.get(a.id) ?? 0,
|
||||
@@ -322,6 +429,8 @@ export const getHomeworkAssignmentById = cache(async (id: string, scope?: DataSc
|
||||
return null
|
||||
}
|
||||
if (scope.type === "grade_managed" && scope.gradeIds.length > 0) {
|
||||
// 快速作业(无 sourceExamId)不归年级主任管辖,直接拒绝
|
||||
if (!assignment.sourceExamId) return null
|
||||
const examIds = await getExamIdsByGradeIds(scope.gradeIds)
|
||||
if (!examIds.includes(assignment.sourceExamId)) {
|
||||
return null
|
||||
@@ -371,8 +480,8 @@ export const getHomeworkAssignmentById = cache(async (id: string, scope?: DataSc
|
||||
description: assignment.description,
|
||||
status: toHomeworkAssignmentStatus(assignment.status),
|
||||
sourceExamId: assignment.sourceExamId,
|
||||
sourceExamTitle: assignment.sourceExam.title,
|
||||
structure: assignment.structure as unknown,
|
||||
sourceExamTitle: assignment.sourceExam?.title ?? null,
|
||||
structure: assignment.structure,
|
||||
availableAt: assignment.availableAt ? assignment.availableAt.toISOString() : null,
|
||||
dueAt: assignment.dueAt ? assignment.dueAt.toISOString() : null,
|
||||
allowLate: assignment.allowLate,
|
||||
@@ -427,16 +536,34 @@ export const getHomeworkSubmissionDetails = cache(async (submissionId: string):
|
||||
})
|
||||
.sort((a, b) => a.order - b.order)
|
||||
|
||||
// Fetch adjacent submissions for navigation
|
||||
const allSubmissions = await db.query.homeworkSubmissions.findMany({
|
||||
where: eq(homeworkSubmissions.assignmentId, submission.assignmentId),
|
||||
orderBy: [desc(homeworkSubmissions.updatedAt)],
|
||||
columns: { id: true },
|
||||
})
|
||||
// P1-8: Optimize adjacent submission navigation using LIMIT 1 queries
|
||||
// instead of fetching all submission IDs for the assignment.
|
||||
// Original ordering is desc(updatedAt): "previous" = newer, "next" = older.
|
||||
const currentUpdatedAt = submission.updatedAt
|
||||
|
||||
const currentIndex = allSubmissions.findIndex((s) => s.id === submissionId)
|
||||
const prevSubmissionId = currentIndex > 0 ? allSubmissions[currentIndex - 1].id : null
|
||||
const nextSubmissionId = currentIndex >= 0 && currentIndex < allSubmissions.length - 1 ? allSubmissions[currentIndex + 1].id : null
|
||||
const [prevSubmission, nextSubmission] = await Promise.all([
|
||||
// Previous (newer): closest submission with updatedAt > current
|
||||
db.query.homeworkSubmissions.findFirst({
|
||||
where: and(
|
||||
eq(homeworkSubmissions.assignmentId, submission.assignmentId),
|
||||
gt(homeworkSubmissions.updatedAt, currentUpdatedAt)
|
||||
),
|
||||
orderBy: [asc(homeworkSubmissions.updatedAt)],
|
||||
columns: { id: true },
|
||||
}),
|
||||
// Next (older): closest submission with updatedAt < current
|
||||
db.query.homeworkSubmissions.findFirst({
|
||||
where: and(
|
||||
eq(homeworkSubmissions.assignmentId, submission.assignmentId),
|
||||
lt(homeworkSubmissions.updatedAt, currentUpdatedAt)
|
||||
),
|
||||
orderBy: [desc(homeworkSubmissions.updatedAt)],
|
||||
columns: { id: true },
|
||||
}),
|
||||
])
|
||||
|
||||
const prevSubmissionId = prevSubmission?.id ?? null
|
||||
const nextSubmissionId = nextSubmission?.id ?? null
|
||||
|
||||
return {
|
||||
id: submission.id,
|
||||
@@ -490,7 +617,10 @@ export const getStudentHomeworkAssignments = cache(async (studentId: string): Pr
|
||||
if (assignments.length === 0) return []
|
||||
|
||||
// Fetch subject names via cross-module interfaces
|
||||
const examIds = assignments.map((a) => a.sourceExamId)
|
||||
// 快速作业无 sourceExamId,过滤 null 后再查询科目映射
|
||||
const examIds = assignments
|
||||
.map((a) => a.sourceExamId)
|
||||
.filter((id): id is string => id !== null)
|
||||
const [examSubjectIdMap, subjectOptions] = await Promise.all([
|
||||
getExamSubjectIdMap(examIds),
|
||||
getSubjectOptions(),
|
||||
@@ -519,7 +649,7 @@ export const getStudentHomeworkAssignments = cache(async (studentId: string): Pr
|
||||
return assignments.map((a) => {
|
||||
const latest = latestSubmittedByAssignmentId.get(a.id) ?? latestByAssignmentId.get(a.id) ?? null
|
||||
const attemptsUsed = attemptsByAssignmentId.get(a.id) ?? 0
|
||||
const subjectId = examSubjectIdMap.get(a.sourceExamId) ?? null
|
||||
const subjectId = a.sourceExamId ? (examSubjectIdMap.get(a.sourceExamId) ?? null) : null
|
||||
const subjectName = subjectId ? subjectNameById.get(subjectId) ?? null : null
|
||||
|
||||
const item: StudentHomeworkAssignmentListItem = {
|
||||
|
||||
Reference in New Issue
Block a user