feat: auto-populate department_id on create + seed default campus with backfill

This commit is contained in:
2026-07-06 00:05:00 +08:00
parent cd6364268d
commit df61ebbc5b
16 changed files with 130 additions and 23 deletions

View File

@@ -36,8 +36,13 @@ export class AttendanceService {
throw new BadRequestException('records array must not be empty');
}
const entities = dto.records.map((r) =>
this.attendanceRepo.create({
// Batch-load student departmentIds
const ids = [...new Set(dto.records.map((r) => r.studentId))];
const students = await this.studentRepo.find({ where: { id: In(ids) } });
const deptMap = new Map(students.map((s) => [s.id, s.departmentId]));
const entities = dto.records.map((r) => {
const entity = this.attendanceRepo.create({
studentId: r.studentId,
classId: r.classId ?? undefined,
attendanceDate: r.attendanceDate,
@@ -45,8 +50,10 @@ export class AttendanceService {
status: r.status,
remark: r.remark,
source: r.source || 'manual',
}),
);
});
entity.departmentId = deptMap.get(r.studentId)!;
return entity;
});
const saved = await this.attendanceRepo.save(entities);
return { count: saved.length, records: saved };