fix(grades): 批量成绩校验与写入使用同一事务
This commit is contained in:
1 parent
e49dd0bc39
commit
ff33d163e3
1 file changed
+36
-14
@@ -25,6 +25,7 @@ import {
|
||||
} from '../../lib/access.js';
|
||||
import { activeStudentWhere, isActiveStudent } from '../../lib/students.js';
|
||||
import { prisma } from '../../lib/db.js';
|
||||
import type { Prisma } from '../../generated/prisma/client.js';
|
||||
import { ApiError } from '../../lib/http.js';
|
||||
import type { TokenPayload } from '../../lib/jwt.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[]> {
|
||||
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 levelType: GradeLevelType = input.levelType ?? 'percent';
|
||||
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) {
|
||||
await assertStudentInClass(item.studentId, input.classId);
|
||||
const dto = await upsertGrade({
|
||||
const student = byId.get(item.studentId);
|
||||
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,
|
||||
courseId: input.courseId ?? null,
|
||||
studentId: item.studentId,
|
||||
@@ -269,9 +282,15 @@ export async function bulkCreateGrades(user: TokenPayload, input: BulkCreateGrad
|
||||
levelType,
|
||||
level: resolveLevel(levelType, item.level, item.score, totalScore),
|
||||
publishedAt,
|
||||
});
|
||||
results.push(dto);
|
||||
},
|
||||
tx,
|
||||
),
|
||||
);
|
||||
}
|
||||
return rows;
|
||||
},
|
||||
{ timeout: 30_000 },
|
||||
);
|
||||
|
||||
if (results[0]) {
|
||||
await emitGradeUpdated(input.classId, {
|
||||
@@ -518,7 +537,8 @@ function resolveLevel(
|
||||
* 同一学生 + 同一课程 + 同一考试名称视为同一条记录,
|
||||
* 重复录入时覆盖旧分数,避免教师"改分"产生重复行。
|
||||
*/
|
||||
async function upsertGrade(data: {
|
||||
async function upsertGrade(
|
||||
data: {
|
||||
classId: string;
|
||||
courseId: string | null;
|
||||
studentId: string;
|
||||
@@ -528,8 +548,10 @@ async function upsertGrade(data: {
|
||||
levelType: GradeLevelType;
|
||||
level: string;
|
||||
publishedAt: Date;
|
||||
}): Promise<GradeDto> {
|
||||
const existing = await prisma.grade.findFirst({
|
||||
},
|
||||
db: Prisma.TransactionClient = prisma,
|
||||
): Promise<GradeDto> {
|
||||
const existing = await db.grade.findFirst({
|
||||
where: {
|
||||
classId: data.classId,
|
||||
courseId: data.courseId,
|
||||
@@ -540,7 +562,7 @@ async function upsertGrade(data: {
|
||||
});
|
||||
|
||||
if (existing) {
|
||||
const updated = await prisma.grade.update({
|
||||
const updated = await db.grade.update({
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
score: data.score,
|
||||
@@ -554,7 +576,7 @@ async function upsertGrade(data: {
|
||||
return toGradeDto(updated);
|
||||
}
|
||||
|
||||
const created = await prisma.grade.create({ data, include });
|
||||
const created = await db.grade.create({ data, include });
|
||||
return toGradeDto(created);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user