feat(server): 统一课表解析并完善权限范围与年度升级
This commit is contained in:
1 parent
66ef878a71
commit
4dc824c283
54 files changed
+2401
-625
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { z } from 'zod';
|
||||
import { MAX_TERM_WEEK, SUBJECT_CATALOG, isDayKey } from '@classhelper/shared';
|
||||
import { SUBJECT_CATALOG, isDayKey } from '@classhelper/shared';
|
||||
import {
|
||||
classIndexSchema,
|
||||
enrollmentYearSchema,
|
||||
@@ -27,11 +27,11 @@ export const createClassSchema = z.object({
|
||||
.regex(/^[A-Za-z0-9]{4,16}$/, '班级码需为 4~16 位字母或数字')
|
||||
.optional(),
|
||||
/**
|
||||
* 本学期教学周数。上限与课表周次口径统一取 MAX_TERM_WEEK(30):
|
||||
* 本学期教学周数。动态上限由服务层统一取 getMaxTermWeeks()(默认30、最多60):
|
||||
* 周次选择器与 weekNumberSchema 都只到 30,允许更大的值只会让用户设出一个
|
||||
* 系统兑现不了的学期长度(第 31 周之后永远为空)。
|
||||
*/
|
||||
termWeeks: z.coerce.number().int().min(1).max(MAX_TERM_WEEK).optional(),
|
||||
termWeeks: z.coerce.number().int().min(1).max(60).optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
@@ -48,7 +48,7 @@ export const updateClassSchema = z.object({
|
||||
.nullish(),
|
||||
enrollmentYear: enrollmentYearSchema.optional(),
|
||||
classIndex: classIndexSchema.optional(),
|
||||
termWeeks: z.coerce.number().int().min(1).max(MAX_TERM_WEEK).optional(),
|
||||
termWeeks: z.coerce.number().int().min(1).max(60).optional(),
|
||||
});
|
||||
|
||||
/** 学年升级(批量改年级,不归档) */
|
||||
|
||||
@@ -26,10 +26,12 @@ import type {
|
||||
UpdateClassInput,
|
||||
UpdateStudentGradeQueryInput,
|
||||
} from './classes.schemas.js';
|
||||
import { activeStudentWhere, isActiveStudent } from '../../lib/students.js';
|
||||
import { getMaxTermWeeks } from '../../lib/term.js';
|
||||
import { logger } from '../../lib/logger.js';
|
||||
|
||||
const countSelect = {
|
||||
students: true,
|
||||
students: { where: activeStudentWhere() },
|
||||
courses: true,
|
||||
homeworks: true,
|
||||
notifications: true,
|
||||
@@ -48,7 +50,7 @@ const teacherBriefSelect = { select: { id: true, name: true, username: true } }
|
||||
* 管理员(管理全部班级)或该班级的班主任(要发给学生用,只读展示)。
|
||||
* 科任老师与 ClassHelper 班级端都看不到别人班的班级码。
|
||||
*/
|
||||
function canSeeClassAccount(user: TokenPayload, item: { teacherId: string }): boolean {
|
||||
function canSeeClassAccount(user: TokenPayload, item: { teacherId: string | null }): boolean {
|
||||
if (user.role === 'ADMIN') return true;
|
||||
return user.role === 'TEACHER' && item.teacherId === user.sub;
|
||||
}
|
||||
@@ -103,7 +105,7 @@ export async function getClassDetail(user: TokenPayload, classId: string): Promi
|
||||
include: {
|
||||
teacher: teacherBriefSelect,
|
||||
_count: { select: countSelect },
|
||||
students: { orderBy: { studentNo: 'asc' } },
|
||||
students: { where: activeStudentWhere(), orderBy: { studentNo: 'asc' } },
|
||||
courses: {
|
||||
orderBy: { name: 'asc' },
|
||||
include: { teacher: teacherBriefSelect },
|
||||
@@ -158,6 +160,13 @@ export async function listSubjectTeachers(
|
||||
/** 创建班级 */
|
||||
export async function createClass(user: TokenPayload, input: CreateClassInput): Promise<ClassDto> {
|
||||
assertCanManageClasses(user);
|
||||
if (
|
||||
'termWeeks' in input &&
|
||||
typeof input.termWeeks === 'number' &&
|
||||
input.termWeeks > (await getMaxTermWeeks())
|
||||
)
|
||||
throw ApiError.badRequest('班级周数不能超过全校上限');
|
||||
|
||||
let teacherId = user.sub;
|
||||
if (input.teacherId && user.role === 'ADMIN') {
|
||||
teacherId = await requireTeacher(input.teacherId, '指定的班主任不存在或角色不是教师');
|
||||
@@ -244,6 +253,7 @@ export async function assignSubjectTeachers(
|
||||
(await prisma.class.findUniqueOrThrow({ where: { id: classId }, select: { teacherId: true } }))
|
||||
.teacherId;
|
||||
|
||||
if (!teacherId) throw ApiError.badRequest('请先指定班主任或为该科指定任课老师');
|
||||
const existing = assignment.courseId
|
||||
? await prisma.course.findUnique({
|
||||
where: { id: assignment.courseId },
|
||||
@@ -285,6 +295,13 @@ export async function updateClassAccount(
|
||||
input: { code?: string; password?: string },
|
||||
): Promise<ClassDto> {
|
||||
assertCanManageClasses(user);
|
||||
if (
|
||||
'termWeeks' in input &&
|
||||
typeof input.termWeeks === 'number' &&
|
||||
input.termWeeks > (await getMaxTermWeeks())
|
||||
)
|
||||
throw ApiError.badRequest('班级周数不能超过全校上限');
|
||||
|
||||
await updateClassAccountRecord(classId, input);
|
||||
|
||||
const updated = await prisma.class.findUnique({
|
||||
@@ -371,7 +388,7 @@ export async function deleteClass(user: TokenPayload, classId: string): Promise<
|
||||
export async function listClassStudents(user: TokenPayload, classId: string): Promise<StudentDto[]> {
|
||||
await assertClassAccess(user, classId);
|
||||
const students = await prisma.student.findMany({
|
||||
where: { classId, archivedYearId: null },
|
||||
where: { classId, archivedYearId: null, ...activeStudentWhere() },
|
||||
include: { class: { select: { name: true, grade: true } } },
|
||||
orderBy: { studentNo: 'asc' },
|
||||
});
|
||||
@@ -395,6 +412,8 @@ export async function addStudent(
|
||||
const existing = await prisma.student.findUnique({ where: { studentNo: input.studentNo } });
|
||||
let studentId: string;
|
||||
|
||||
if (existing && !isActiveStudent(existing))
|
||||
throw ApiError.conflict('学号已被历史学生占用;请使用新学号或在归档页撤回转出');
|
||||
if (existing) {
|
||||
studentId = existing.id;
|
||||
if (existing.classId !== classId) {
|
||||
|
||||
Reference in new issue
Block a user