feat: auto-populate department_id on create + seed default campus with backfill
This commit is contained in:
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user