refactor: fix all P0/P1/P2 bugs and architecture issues
Bug fixes (from bugs/ directory): - Fix cross-module DB queries in 9 modules (homework, grades, parent, diagnostic, elective, proctoring, notifications, scheduling, classes) by routing through data-access functions - Fix shared/lib <-> auth circular dependency via new session.ts module - Fix divide-by-zero guard in grades data-access - Fix audit export data truncation (paginated fetch for full datasets) - Fix missing transactions in homework grading and elective lottery - Fix missing revalidatePath in course-plans actions - Fix frontend permission checks using requirePermission instead of requireAuth - Fix dashboard role routing using session.user.roles - Fix student auth pattern (migrate getDemoStudentUser to users module) - Fix ActionState return type handling in components Code quality fixes: - Remove 60+ as type assertions (replace with type guards) - Remove non-null assertions (use optional chaining or explicit checks) - Convert dynamic imports to static imports (grades, diagnostic) - Add React.cache() wrapping for read functions - Parallelize independent queries with Promise.all - Add explicit return types to 30+ arrow functions - Replace any with unknown + type guards - Fix import type for type-only imports - Add Zod validation schemas for classes and diagnostic modules - Extract duplicate code (normalizeRoleName, normalizeBcryptHash, logger IP extraction) - Add console.error to silent catch blocks - Fix permission naming consistency (exam:proctor_read -> exam:proctor:read) Architecture doc sync: - Update 004_architecture_impact_map.md and 005_architecture_data.json - Update management-modules-audit.md for P0-7 cross-module fix Moved deleted proctoring event route to deletes/ folder.
This commit is contained in:
@@ -5,15 +5,18 @@ import { and, count, desc, eq, inArray, or, sql } from "drizzle-orm"
|
||||
|
||||
import { db } from "@/shared/db"
|
||||
import {
|
||||
classEnrollments,
|
||||
classes,
|
||||
exams,
|
||||
homeworkAssignmentQuestions,
|
||||
homeworkAssignmentTargets,
|
||||
homeworkAssignments,
|
||||
homeworkSubmissions,
|
||||
users,
|
||||
} from "@/shared/db/schema"
|
||||
import {
|
||||
getActiveStudentIdsByClassId,
|
||||
getGradeIdsByClassIds,
|
||||
getStudentActiveClassId,
|
||||
} from "@/modules/classes/data-access"
|
||||
import { getExamIdsByGradeIds } from "@/modules/exams/data-access"
|
||||
import { getUserNamesByIds } from "@/modules/users/data-access"
|
||||
|
||||
import type {
|
||||
HomeworkAssignmentAnalytics,
|
||||
@@ -27,6 +30,12 @@ import type {
|
||||
import type { DataScope } from "@/shared/types/permissions"
|
||||
import { getAssignmentMaxScoreById, isRecord, toQuestionContent } from "./data-access"
|
||||
|
||||
const isHomeworkAssignmentStatus = (v: unknown): v is HomeworkAssignmentStatus =>
|
||||
v === "draft" || v === "published" || v === "archived"
|
||||
|
||||
const toHomeworkAssignmentStatus = (v: string | null | undefined): HomeworkAssignmentStatus =>
|
||||
isHomeworkAssignmentStatus(v) ? v : "draft"
|
||||
|
||||
/**
|
||||
* Get grade trend data for a teacher's recent assignments.
|
||||
* Used by the teacher dashboard to visualize class performance over time.
|
||||
@@ -217,7 +226,7 @@ export const getHomeworkAssignmentAnalytics = cache(
|
||||
id: assignment.id,
|
||||
title: assignment.title,
|
||||
description: assignment.description,
|
||||
status: (assignment.status as HomeworkAssignmentStatus) ?? "draft",
|
||||
status: toHomeworkAssignmentStatus(assignment.status),
|
||||
sourceExamId: assignment.sourceExamId,
|
||||
sourceExamTitle: assignment.sourceExam.title,
|
||||
structure: assignment.structure as unknown,
|
||||
@@ -310,19 +319,10 @@ export const getStudentDashboardGrades = cache(async (studentId: string): Promis
|
||||
const trend = trendSubmissions.map(toAnalytics)
|
||||
const recent = recentSubmissions.map(toAnalytics).slice(0, 5)
|
||||
|
||||
const enrollment = await db.query.classEnrollments.findFirst({
|
||||
where: and(eq(classEnrollments.studentId, id), eq(classEnrollments.status, "active")),
|
||||
orderBy: (e, { asc }) => [asc(e.createdAt)],
|
||||
})
|
||||
const classId = await getStudentActiveClassId(id)
|
||||
if (!classId) return { trend, recent, ranking: null }
|
||||
|
||||
if (!enrollment) return { trend, recent, ranking: null }
|
||||
|
||||
const classStudents = await db
|
||||
.select({ studentId: classEnrollments.studentId })
|
||||
.from(classEnrollments)
|
||||
.where(and(eq(classEnrollments.classId, enrollment.classId), eq(classEnrollments.status, "active")))
|
||||
|
||||
const classStudentIds = Array.from(new Set(classStudents.map((r) => r.studentId)))
|
||||
const classStudentIds = await getActiveStudentIdsByClassId(classId)
|
||||
const classSize = classStudentIds.length
|
||||
if (classSize === 0) return { trend, recent, ranking: null }
|
||||
|
||||
@@ -363,13 +363,8 @@ export const getStudentDashboardGrades = cache(async (studentId: string): Promis
|
||||
})
|
||||
}
|
||||
|
||||
const classUsers = await db
|
||||
.select({ id: users.id, name: users.name })
|
||||
.from(users)
|
||||
.where(inArray(users.id, classStudentIds))
|
||||
|
||||
const nameByStudentId = new Map(classUsers.map((u) => [u.id, u.name ?? "Student"] as const))
|
||||
const myName = nameByStudentId.get(id) ?? "Student"
|
||||
const userNamesMap = await getUserNamesByIds(classStudentIds)
|
||||
const myName = userNamesMap.get(id)?.name ?? "Student"
|
||||
|
||||
const ranked = classStudentIds
|
||||
.map((studentId) => {
|
||||
@@ -422,10 +417,7 @@ export const getHomeworkDashboardStats = cache(async (scope?: DataScope): Promis
|
||||
submissionConditions.push(inArray(homeworkSubmissions.assignmentId, ownedAssignmentIds))
|
||||
}
|
||||
if (scope.type === "grade_managed" && scope.gradeIds.length > 0) {
|
||||
const gradeExamIds = db
|
||||
.select({ id: exams.id })
|
||||
.from(exams)
|
||||
.where(inArray(exams.gradeId, scope.gradeIds))
|
||||
const gradeExamIds = await getExamIdsByGradeIds(scope.gradeIds)
|
||||
homeworkConditions.push(inArray(homeworkAssignments.sourceExamId, gradeExamIds))
|
||||
const gradeAssignmentIds = db
|
||||
.select({ id: homeworkAssignments.id })
|
||||
@@ -434,16 +426,9 @@ export const getHomeworkDashboardStats = cache(async (scope?: DataScope): Promis
|
||||
submissionConditions.push(inArray(homeworkSubmissions.assignmentId, gradeAssignmentIds))
|
||||
}
|
||||
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[]
|
||||
const gradeIds = await getGradeIdsByClassIds(scope.classIds)
|
||||
if (gradeIds.length > 0) {
|
||||
const gradeExamIds = db
|
||||
.select({ id: exams.id })
|
||||
.from(exams)
|
||||
.where(inArray(exams.gradeId, gradeIds))
|
||||
const gradeExamIds = await getExamIdsByGradeIds(gradeIds)
|
||||
homeworkConditions.push(inArray(homeworkAssignments.sourceExamId, gradeExamIds))
|
||||
const gradeAssignmentIds = db
|
||||
.select({ id: homeworkAssignments.id })
|
||||
|
||||
Reference in New Issue
Block a user