feat: auto-populate department_id on create + seed default campus with backfill
This commit is contained in:
@@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, Like, Not, In } from 'typeorm';
|
||||
import { CampusScope } from '../common/campus-scope';
|
||||
import { Student } from '../entities/student.entity';
|
||||
import { Class } from '../entities/class.entity';
|
||||
import { ClassStudent } from '../entities/class-student.entity';
|
||||
import { AttendanceRecord } from '../entities/attendance-record.entity';
|
||||
import { CreateStudentDto, UpdateStudentDto } from './dto/student.dto';
|
||||
@@ -13,6 +14,7 @@ export class StudentsService {
|
||||
constructor(
|
||||
@InjectRepository(Student) private repo: Repository<Student>,
|
||||
@InjectRepository(ClassStudent) private classStudentRepo: Repository<ClassStudent>,
|
||||
@InjectRepository(Class) private classRepo: Repository<Class>,
|
||||
@InjectRepository(AttendanceRecord) private attendanceRepo: Repository<AttendanceRecord>,
|
||||
private readonly scope: CampusScope,
|
||||
) {}
|
||||
@@ -39,7 +41,14 @@ export class StudentsService {
|
||||
}
|
||||
|
||||
async create(dto: CreateStudentDto) {
|
||||
return this.repo.save(this.repo.create(dto));
|
||||
const entity = this.repo.create(dto);
|
||||
if (dto.departmentId) {
|
||||
entity.departmentId = dto.departmentId;
|
||||
} else if (dto.classId) {
|
||||
const cls = await this.classRepo.findOne({ where: { id: dto.classId } });
|
||||
if (cls) entity.departmentId = cls.departmentId;
|
||||
}
|
||||
return this.repo.save(entity);
|
||||
}
|
||||
|
||||
async update(id: number, dto: UpdateStudentDto) {
|
||||
|
||||
Reference in New Issue
Block a user