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
+36 -14
@@ -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,17 +250,29 @@ 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) => {
await assertContentWrite(user, 'grade:write', input.classId, input.courseId ?? null, '录入成绩', {
db: tx,
});
const students = await tx.student.findMany({
where: { id: { in: [...new Set(input.items.map((item) => item.studentId))] } },
select: { id: true, classId: true, status: true },
});
const byId = new Map(students.map((student) => [student.id, student]));
for (const item of input.items) { for (const item of input.items) {
await assertStudentInClass(item.studentId, input.classId); const student = byId.get(item.studentId);
const dto = await upsertGrade({ if (!student || !isActiveStudent(student)) throw ApiError.badRequest('学生不存在');
if (student.classId !== input.classId) throw ApiError.badRequest('该学生不属于所选班级');
}
const rows: GradeDto[] = [];
for (const item of input.items) {
rows.push(
await upsertGrade(
{
classId: input.classId, classId: input.classId,
courseId: input.courseId ?? null, courseId: input.courseId ?? null,
studentId: item.studentId, studentId: item.studentId,
@@ -269,9 +282,15 @@ export async function bulkCreateGrades(user: TokenPayload, input: BulkCreateGrad
levelType, levelType,
level: resolveLevel(levelType, item.level, item.score, totalScore), level: resolveLevel(levelType, item.level, item.score, totalScore),
publishedAt, publishedAt,
}); },
results.push(dto); tx,
),
);
} }
return rows;
},
{ timeout: 30_000 },
);
if (results[0]) { if (results[0]) {
await emitGradeUpdated(input.classId, { await emitGradeUpdated(input.classId, {
@@ -518,7 +537,8 @@ function resolveLevel(
* 同一学生 + 同一课程 + 同一考试名称视为同一条记录, * 同一学生 + 同一课程 + 同一考试名称视为同一条记录,
* 重复录入时覆盖旧分数,避免教师"改分"产生重复行。 * 重复录入时覆盖旧分数,避免教师"改分"产生重复行。
*/ */
async function upsertGrade(data: { async function upsertGrade(
data: {
classId: string; classId: string;
courseId: string | null; courseId: string | null;
studentId: string; studentId: string;
@@ -528,8 +548,10 @@ async function upsertGrade(data: {
levelType: GradeLevelType; levelType: GradeLevelType;
level: string; level: string;
publishedAt: Date; publishedAt: Date;
}): Promise<GradeDto> { },
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);
} }