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:
@@ -3,10 +3,10 @@
|
||||
import { requirePermission, PermissionDeniedError } from "@/shared/lib/auth-guard";
|
||||
import { Permissions } from "@/shared/types/permissions";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import {
|
||||
createTextbook,
|
||||
createChapter,
|
||||
updateChapterContent,
|
||||
import {
|
||||
createTextbook,
|
||||
createChapter,
|
||||
updateChapterContent,
|
||||
deleteChapter,
|
||||
createKnowledgePoint,
|
||||
deleteKnowledgePoint,
|
||||
@@ -15,7 +15,12 @@ import {
|
||||
deleteTextbook,
|
||||
reorderChapters
|
||||
} from "./data-access";
|
||||
import { CreateTextbookInput, UpdateTextbookInput } from "./types";
|
||||
import type { CreateTextbookInput, UpdateTextbookInput } from "./types";
|
||||
|
||||
const getStringValue = (formData: FormData, key: string): string => {
|
||||
const value = formData.get(key)
|
||||
return typeof value === "string" ? value : ""
|
||||
}
|
||||
|
||||
// ... existing code ...
|
||||
|
||||
@@ -52,10 +57,10 @@ export async function createTextbookAction(
|
||||
): Promise<ActionState> {
|
||||
// ... implementation same as before
|
||||
const rawData: CreateTextbookInput = {
|
||||
title: formData.get("title") as string,
|
||||
subject: formData.get("subject") as string,
|
||||
grade: formData.get("grade") as string,
|
||||
publisher: formData.get("publisher") as string,
|
||||
title: getStringValue(formData, "title"),
|
||||
subject: getStringValue(formData, "subject"),
|
||||
grade: getStringValue(formData, "grade"),
|
||||
publisher: getStringValue(formData, "publisher"),
|
||||
};
|
||||
|
||||
if (!rawData.title || !rawData.subject || !rawData.grade) {
|
||||
@@ -91,10 +96,10 @@ export async function updateTextbookAction(
|
||||
): Promise<ActionState> {
|
||||
const rawData: UpdateTextbookInput = {
|
||||
id: textbookId,
|
||||
title: formData.get("title") as string,
|
||||
subject: formData.get("subject") as string,
|
||||
grade: formData.get("grade") as string,
|
||||
publisher: formData.get("publisher") as string,
|
||||
title: getStringValue(formData, "title"),
|
||||
subject: getStringValue(formData, "subject"),
|
||||
grade: getStringValue(formData, "grade"),
|
||||
publisher: getStringValue(formData, "publisher"),
|
||||
};
|
||||
|
||||
if (!rawData.title || !rawData.subject || !rawData.grade) {
|
||||
@@ -151,7 +156,7 @@ export async function createChapterAction(
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
const title = formData.get("title") as string;
|
||||
const title = getStringValue(formData, "title");
|
||||
|
||||
if (!title) return { success: false, message: "Title is required" };
|
||||
|
||||
@@ -214,9 +219,9 @@ export async function createKnowledgePointAction(
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
const name = formData.get("name") as string;
|
||||
const description = formData.get("description") as string;
|
||||
const anchorText = formData.get("anchorText") as string;
|
||||
const name = getStringValue(formData, "name");
|
||||
const description = getStringValue(formData, "description");
|
||||
const anchorText = getStringValue(formData, "anchorText");
|
||||
|
||||
if (!name) return { success: false, message: "Name is required" };
|
||||
|
||||
@@ -256,9 +261,9 @@ export async function updateKnowledgePointAction(
|
||||
prevState: ActionState | null,
|
||||
formData: FormData
|
||||
): Promise<ActionState> {
|
||||
const name = formData.get("name") as string;
|
||||
const description = formData.get("description") as string;
|
||||
const anchorText = formData.get("anchorText") as string;
|
||||
const name = getStringValue(formData, "name");
|
||||
const description = getStringValue(formData, "description");
|
||||
const anchorText = getStringValue(formData, "anchorText");
|
||||
|
||||
if (!name) return { success: false, message: "Name is required" };
|
||||
|
||||
|
||||
@@ -30,25 +30,37 @@ const sortChapters = (a: Chapter, b: Chapter) => {
|
||||
}
|
||||
|
||||
const buildChapterTree = (rows: Chapter[]): Chapter[] => {
|
||||
const byId = new Map<string, Chapter & { children: Chapter[] }>()
|
||||
type ChapterNode = Chapter & { children: ChapterNode[] }
|
||||
|
||||
const isChapterNode = (n: Chapter): n is ChapterNode =>
|
||||
Array.isArray(n.children)
|
||||
|
||||
const byId = new Map<string, ChapterNode>()
|
||||
for (const ch of rows) {
|
||||
byId.set(ch.id, { ...ch, children: [] })
|
||||
}
|
||||
|
||||
const roots: Array<Chapter & { children: Chapter[] }> = []
|
||||
const roots: ChapterNode[] = []
|
||||
for (const ch of byId.values()) {
|
||||
const pid = ch.parentId
|
||||
if (pid && byId.has(pid)) {
|
||||
byId.get(pid)!.children.push(ch)
|
||||
if (pid) {
|
||||
const parent = byId.get(pid)
|
||||
if (parent) {
|
||||
parent.children.push(ch)
|
||||
} else {
|
||||
roots.push(ch)
|
||||
}
|
||||
} else {
|
||||
roots.push(ch)
|
||||
}
|
||||
}
|
||||
|
||||
const sortRecursive = (nodes: Array<Chapter & { children: Chapter[] }>) => {
|
||||
const sortRecursive = (nodes: ChapterNode[]) => {
|
||||
nodes.sort(sortChapters)
|
||||
for (const n of nodes) {
|
||||
sortRecursive(n.children as Array<Chapter & { children: Chapter[] }>)
|
||||
if (isChapterNode(n)) {
|
||||
sortRecursive(n.children)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -62,14 +74,13 @@ export const getTextbooks = cache(async (query?: string, subject?: string, grade
|
||||
const q = query?.trim()
|
||||
if (q) {
|
||||
const needle = `%${q}%`
|
||||
conditions.push(
|
||||
or(
|
||||
like(textbooks.title, needle),
|
||||
like(textbooks.subject, needle),
|
||||
like(textbooks.grade, needle),
|
||||
like(textbooks.publisher, needle)
|
||||
)!
|
||||
const nameCond = or(
|
||||
like(textbooks.title, needle),
|
||||
like(textbooks.subject, needle),
|
||||
like(textbooks.grade, needle),
|
||||
like(textbooks.publisher, needle)
|
||||
)
|
||||
if (nameCond) conditions.push(nameCond)
|
||||
}
|
||||
|
||||
const s = subject?.trim()
|
||||
|
||||
Reference in New Issue
Block a user