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

@@ -60,6 +60,10 @@ export class CreateScheduleDto {
@IsOptional()
@IsString()
notes?: string;
@IsOptional()
@IsInt()
departmentId?: number;
}
export class UpdateScheduleDto {

View File

@@ -1,7 +1,7 @@
import { Injectable, NotFoundException, ConflictException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { ClassSchedule } from '../entities';
import { ClassSchedule, Class } from '../entities';
import { CampusScope } from '../common/campus-scope';
import {
CreateScheduleDto,
@@ -16,6 +16,7 @@ export class SchedulesService {
@InjectRepository(ClassSchedule)
private readonly scheduleRepo: Repository<ClassSchedule>,
private readonly scope: CampusScope,
@InjectRepository(Class) private readonly classRepo: Repository<Class>,
) {}
async findAll(query: QueryScheduleDto) {
@@ -47,6 +48,12 @@ export class SchedulesService {
await this.checkConflict(dto.classroomId, dto.weekDay, dto.startTime, dto.endTime, dto.startDate, dto.endDate);
const schedule = this.scheduleRepo.create(dto);
if (dto.departmentId) {
schedule.departmentId = dto.departmentId;
} else if (dto.classId) {
const cls = await this.classRepo.findOne({ where: { id: dto.classId } });
if (cls) schedule.departmentId = cls.departmentId;
}
const saved = await this.scheduleRepo.save(schedule);
return this.findOne(saved.id);
}