fix(grades): 批量成绩校验与写入使用同一事务

This commit is contained in:
LoveChengke committed 2026-11-07 02:02:03 +08:00
1 parent e49dd0bc39
commit ff33d163e3
1 file changed
+56 -34
@@ -25,6 +25,7 @@ import {
} from '../../lib/access.js'; } from '../../lib/access.js';
import { activeStudentWhere, isActiveStudent } from '../../lib/students.js'; import { activeStudentWhere, isActiveStudent } from '../../lib/students.js';
import { prisma } from '../../lib/db.js'; import { prisma } from '../../lib/db.js';
import type { Prisma } from '../../generated/prisma/client.js';
import { ApiError } from '../../lib/http.js'; import { ApiError } from '../../lib/http.js';
import type { TokenPayload } from '../../lib/jwt.js'; import type { TokenPayload } from '../../lib/jwt.js';
import { logger } from '../../lib/logger.js'; import { logger } from '../../lib/logger.js';
@@ -249,29 +250,47 @@ export async function createGrade(user: TokenPayload, input: CreateGradeInput):
/** 批量录入:同一个考试一次提交整班成绩 */ /** 批量录入:同一个考试一次提交整班成绩 */
export async function bulkCreateGrades(user: TokenPayload, input: BulkCreateGradeInput): Promise<GradeDto[]> { export async function bulkCreateGrades(user: TokenPayload, input: BulkCreateGradeInput): Promise<GradeDto[]> {
await assertContentWrite(user, 'grade:write', input.classId, input.courseId ?? null, '录入成绩');
if (input.courseId) await assertCourseInClass(input.courseId, input.classId);
const publishedAt = parseOptionalDate(input.publishedAt) ?? new Date(); const publishedAt = parseOptionalDate(input.publishedAt) ?? new Date();
const levelType: GradeLevelType = input.levelType ?? 'percent'; const levelType: GradeLevelType = input.levelType ?? 'percent';
const totalScore = input.totalScore ?? 100; const totalScore = input.totalScore ?? 100;
const results: GradeDto[] = []; const results = await prisma.$transaction(
async (tx) => {
for (const item of input.items) { await assertContentWrite(user, 'grade:write', input.classId, input.courseId ?? null, '录入成绩', {
await assertStudentInClass(item.studentId, input.classId); db: tx,
const dto = await upsertGrade({ });
classId: input.classId, const students = await tx.student.findMany({
courseId: input.courseId ?? null, where: { id: { in: [...new Set(input.items.map((item) => item.studentId))] } },
studentId: item.studentId, select: { id: true, classId: true, status: true },
examName: input.examName, });
score: item.score, const byId = new Map(students.map((student) => [student.id, student]));
totalScore, for (const item of input.items) {
levelType, const student = byId.get(item.studentId);
level: resolveLevel(levelType, item.level, item.score, totalScore), if (!student || !isActiveStudent(student)) throw ApiError.badRequest('学生不存在');
publishedAt, if (student.classId !== input.classId) throw ApiError.badRequest('该学生不属于所选班级');
}); }
results.push(dto); const rows: GradeDto[] = [];
} for (const item of input.items) {
rows.push(
await upsertGrade(
{
classId: input.classId,
courseId: input.courseId ?? null,
studentId: item.studentId,
examName: input.examName,
score: item.score,
totalScore,
levelType,
level: resolveLevel(levelType, item.level, item.score, totalScore),
publishedAt,
},
tx,
),
);
}
return rows;
},
{ timeout: 30_000 },
);
if (results[0]) { if (results[0]) {
await emitGradeUpdated(input.classId, { await emitGradeUpdated(input.classId, {
@@ -518,18 +537,21 @@ function resolveLevel(
* 同一学生 + 同一课程 + 同一考试名称视为同一条记录, * 同一学生 + 同一课程 + 同一考试名称视为同一条记录,
* 重复录入时覆盖旧分数,避免教师"改分"产生重复行。 * 重复录入时覆盖旧分数,避免教师"改分"产生重复行。
*/ */
async function upsertGrade(data: { async function upsertGrade(
classId: string; data: {
courseId: string | null; classId: string;
studentId: string; courseId: string | null;
examName: string; studentId: string;
score: number; examName: string;
totalScore: number; score: number;
levelType: GradeLevelType; totalScore: number;
level: string; levelType: GradeLevelType;
publishedAt: Date; level: string;
}): Promise<GradeDto> { publishedAt: Date;
const existing = await prisma.grade.findFirst({ },
db: Prisma.TransactionClient = prisma,
): Promise<GradeDto> {
const existing = await db.grade.findFirst({
where: { where: {
classId: data.classId, classId: data.classId,
courseId: data.courseId, courseId: data.courseId,
@@ -540,7 +562,7 @@ async function upsertGrade(data: {
}); });
if (existing) { if (existing) {
const updated = await prisma.grade.update({ const updated = await db.grade.update({
where: { id: existing.id }, where: { id: existing.id },
data: { data: {
score: data.score, score: data.score,
@@ -554,7 +576,7 @@ async function upsertGrade(data: {
return toGradeDto(updated); return toGradeDto(updated);
} }
const created = await prisma.grade.create({ data, include }); const created = await db.grade.create({ data, include });
return toGradeDto(created); return toGradeDto(created);
} }