feat: 完成 P1 全部功能 + 修复 proxy 导出 + 切换 MySQL 端口至 14013
## P1 功能(20 项) - 站内消息系统、家长仪表盘、学生考勤管理 - Excel 导入导出、用户批量导入、成绩导出 - 排课规则+自动排课+课表调整 - 成绩趋势+对比分析、密码安全策略、速率限制 - 数据变更日志、文件预览+存储策略、全文检索 - 依赖审计集成 CI、数据库定时备份、E2E 测试完善 - 通知偏好管理 ## 基础设施修复 - src/proxy.ts: 将 middleware 导出重命名为 proxy(Next.js 16 要求) - .env: MySQL 端口从 13002 切换至 14013 - scripts/create-db.ts: 新增数据库初始化脚本 ## 架构文档同步 - 004_architecture_impact_map.md 和 005_architecture_data.json 完整记录所有新增表、模块、路由、权限、依赖关系
This commit is contained in:
234
src/modules/parent/data-access.ts
Normal file
234
src/modules/parent/data-access.ts
Normal file
@@ -0,0 +1,234 @@
|
||||
import "server-only"
|
||||
|
||||
import { cache } from "react"
|
||||
import { and, asc, eq } from "drizzle-orm"
|
||||
|
||||
import { db } from "@/shared/db"
|
||||
import {
|
||||
classes,
|
||||
classEnrollments,
|
||||
grades,
|
||||
parentStudentRelations,
|
||||
users,
|
||||
} from "@/shared/db/schema"
|
||||
import { getStudentClasses, getStudentSchedule } from "@/modules/classes/data-access"
|
||||
import {
|
||||
getStudentDashboardGrades,
|
||||
getStudentHomeworkAssignments,
|
||||
} from "@/modules/homework/data-access"
|
||||
import { getStudentGradeSummary } from "@/modules/grades/data-access"
|
||||
import type {
|
||||
ChildDashboardData,
|
||||
ChildHomeworkSummary,
|
||||
ChildScheduleItem,
|
||||
ParentChildRelation,
|
||||
ParentDashboardData,
|
||||
} from "./types"
|
||||
|
||||
const toWeekday = (d: Date): 1 | 2 | 3 | 4 | 5 | 6 | 7 => {
|
||||
const day = d.getDay()
|
||||
return (day === 0 ? 7 : day) as 1 | 2 | 3 | 4 | 5 | 6 | 7
|
||||
}
|
||||
|
||||
export const getChildren = cache(async (parentId: string): Promise<ParentChildRelation[]> => {
|
||||
const id = parentId.trim()
|
||||
if (!id) return []
|
||||
|
||||
const rows = await db
|
||||
.select({
|
||||
id: parentStudentRelations.id,
|
||||
parentId: parentStudentRelations.parentId,
|
||||
studentId: parentStudentRelations.studentId,
|
||||
relation: parentStudentRelations.relation,
|
||||
createdAt: parentStudentRelations.createdAt,
|
||||
})
|
||||
.from(parentStudentRelations)
|
||||
.where(eq(parentStudentRelations.parentId, id))
|
||||
.orderBy(asc(parentStudentRelations.createdAt))
|
||||
|
||||
return rows.map((r) => ({
|
||||
id: r.id,
|
||||
parentId: r.parentId,
|
||||
studentId: r.studentId,
|
||||
relation: r.relation,
|
||||
createdAt: r.createdAt.toISOString(),
|
||||
}))
|
||||
})
|
||||
|
||||
export const getChildBasicInfo = cache(async (studentId: string, relation: string | null = null) => {
|
||||
const [student] = await db
|
||||
.select({
|
||||
id: users.id,
|
||||
name: users.name,
|
||||
email: users.email,
|
||||
image: users.image,
|
||||
gradeId: users.gradeId,
|
||||
})
|
||||
.from(users)
|
||||
.where(eq(users.id, studentId))
|
||||
.limit(1)
|
||||
|
||||
if (!student) return null
|
||||
|
||||
let gradeName: string | null = null
|
||||
if (student.gradeId) {
|
||||
const [grade] = await db
|
||||
.select({ name: grades.name })
|
||||
.from(grades)
|
||||
.where(eq(grades.id, student.gradeId))
|
||||
.limit(1)
|
||||
gradeName = grade?.name ?? null
|
||||
}
|
||||
|
||||
const [enrollment] = await db
|
||||
.select({
|
||||
classId: classEnrollments.classId,
|
||||
status: classEnrollments.status,
|
||||
})
|
||||
.from(classEnrollments)
|
||||
.where(and(eq(classEnrollments.studentId, studentId), eq(classEnrollments.status, "active")))
|
||||
.orderBy(asc(classEnrollments.createdAt))
|
||||
.limit(1)
|
||||
|
||||
let className: string | null = null
|
||||
let classId: string | null = null
|
||||
if (enrollment) {
|
||||
const [cls] = await db
|
||||
.select({ id: classes.id, name: classes.name })
|
||||
.from(classes)
|
||||
.where(eq(classes.id, enrollment.classId))
|
||||
.limit(1)
|
||||
if (cls) {
|
||||
classId = cls.id
|
||||
className = cls.name
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
id: student.id,
|
||||
name: student.name,
|
||||
email: student.email,
|
||||
image: student.image,
|
||||
gradeName,
|
||||
className,
|
||||
classId,
|
||||
relation,
|
||||
}
|
||||
})
|
||||
|
||||
const buildHomeworkSummary = (
|
||||
assignments: Awaited<ReturnType<typeof getStudentHomeworkAssignments>>,
|
||||
): ChildHomeworkSummary => {
|
||||
const now = new Date()
|
||||
const in7Days = new Date(now)
|
||||
in7Days.setDate(in7Days.getDate() + 7)
|
||||
|
||||
let pendingCount = 0
|
||||
let submittedCount = 0
|
||||
let gradedCount = 0
|
||||
let overdueCount = 0
|
||||
|
||||
for (const a of assignments) {
|
||||
if (a.progressStatus === "graded") {
|
||||
gradedCount += 1
|
||||
continue
|
||||
}
|
||||
if (a.progressStatus === "submitted") {
|
||||
submittedCount += 1
|
||||
}
|
||||
if (a.progressStatus === "not_started" || a.progressStatus === "in_progress") {
|
||||
pendingCount += 1
|
||||
}
|
||||
if (a.dueAt) {
|
||||
const due = new Date(a.dueAt)
|
||||
if (due < now && a.progressStatus !== "submitted") {
|
||||
overdueCount += 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const recentAssignments = [...assignments]
|
||||
.sort((a, b) => {
|
||||
const aDue = a.dueAt ? new Date(a.dueAt).getTime() : Number.POSITIVE_INFINITY
|
||||
const bDue = b.dueAt ? new Date(b.dueAt).getTime() : Number.POSITIVE_INFINITY
|
||||
return aDue - bDue
|
||||
})
|
||||
.slice(0, 5)
|
||||
|
||||
return {
|
||||
pendingCount,
|
||||
submittedCount,
|
||||
gradedCount,
|
||||
overdueCount,
|
||||
recentAssignments,
|
||||
}
|
||||
}
|
||||
|
||||
const buildTodaySchedule = (
|
||||
schedule: Awaited<ReturnType<typeof getStudentSchedule>>,
|
||||
): ChildScheduleItem[] => {
|
||||
const todayWeekday = toWeekday(new Date())
|
||||
return schedule
|
||||
.filter((s) => s.weekday === todayWeekday)
|
||||
.map((s) => ({
|
||||
id: s.id,
|
||||
classId: s.classId,
|
||||
className: s.className,
|
||||
course: s.course,
|
||||
startTime: s.startTime,
|
||||
endTime: s.endTime,
|
||||
location: s.location ?? null,
|
||||
}))
|
||||
.sort((a, b) => a.startTime.localeCompare(b.startTime))
|
||||
}
|
||||
|
||||
export const getChildDashboardData = cache(
|
||||
async (studentId: string, relation: string | null = null): Promise<ChildDashboardData | null> => {
|
||||
const basicInfo = await getChildBasicInfo(studentId, relation)
|
||||
if (!basicInfo) return null
|
||||
|
||||
const [enrolledClasses, schedule, assignments, gradeTrend, gradeSummary] = await Promise.all([
|
||||
getStudentClasses(studentId),
|
||||
getStudentSchedule(studentId),
|
||||
getStudentHomeworkAssignments(studentId),
|
||||
getStudentDashboardGrades(studentId),
|
||||
getStudentGradeSummary(studentId),
|
||||
])
|
||||
|
||||
return {
|
||||
basicInfo,
|
||||
enrolledClasses,
|
||||
todaySchedule: buildTodaySchedule(schedule),
|
||||
homeworkSummary: buildHomeworkSummary(assignments),
|
||||
gradeTrend,
|
||||
gradeSummary,
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
export const getParentDashboardData = cache(
|
||||
async (parentId: string): Promise<ParentDashboardData> => {
|
||||
const id = parentId.trim()
|
||||
if (!id) return { parentName: null, children: [] }
|
||||
|
||||
const [parent, relations] = await Promise.all([
|
||||
db.select({ name: users.name }).from(users).where(eq(users.id, id)).limit(1),
|
||||
getChildren(id),
|
||||
])
|
||||
|
||||
const parentName = parent[0]?.name ?? null
|
||||
|
||||
if (relations.length === 0) {
|
||||
return { parentName, children: [] }
|
||||
}
|
||||
|
||||
const children = await Promise.all(
|
||||
relations.map((r) => getChildDashboardData(r.studentId, r.relation)),
|
||||
)
|
||||
|
||||
return {
|
||||
parentName,
|
||||
children: children.filter((c): c is ChildDashboardData => c !== null),
|
||||
}
|
||||
},
|
||||
)
|
||||
Reference in New Issue
Block a user