refactor: remove departmentId from Class entity

This commit is contained in:
2026-07-09 17:32:37 +08:00
parent 40fef906cd
commit 22bc1876d8
9 changed files with 4 additions and 116 deletions

View File

@@ -1,7 +1,7 @@
import { Injectable, NotFoundException, BadRequestException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, In, Like } from 'typeorm';
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Department, Classroom, Student, StudentDingMapping } from '../entities';
import { Class, ClassStudent, ClassTeacher, ClassSchedule, AttendanceRecord, Classroom, Student, StudentDingMapping } from '../entities';
import { CreateClassDto, UpdateClassDto, QueryClassDto, AddTeacherDto, QueryClassScheduleDto, QueryClassAttendanceSummaryDto, BatchImportStudentsDto } from './dto/class.dto';
interface RawStudentCount {
@@ -22,8 +22,6 @@ export class ClassesService {
private scheduleRepo: Repository<ClassSchedule>,
@InjectRepository(AttendanceRecord)
private attendanceRepo: Repository<AttendanceRecord>,
@InjectRepository(Department)
private deptRepo: Repository<Department>,
@InjectRepository(Student)
private studentRepo: Repository<Student>,
@InjectRepository(StudentDingMapping)
@@ -32,7 +30,6 @@ export class ClassesService {
async findAll(query: QueryClassDto) {
let where: Record<string, unknown> = {};
if (query.departmentId) where.departmentId = query.departmentId;
if (query.status) where.status = query.status;
if (query.classType) where.classType = query.classType;
if (query.keyword) where.name = Like(`%${query.keyword}%`);
@@ -99,21 +96,6 @@ export class ClassesService {
async create(dto: CreateClassDto) {
const { studentIds, teachers, dingUserIds, ...classData } = dto;
// Resolve departmentId: frontend may pass DingTalk dept ID, map to local
if (classData.departmentId) {
const localDept = await this.deptRepo.findOne({
where: { source: 'dingtalk', sourceId: String(classData.departmentId) },
});
if (localDept) {
classData.departmentId = localDept.id;
} else {
// Verify it's a valid local department ID
const exists = await this.deptRepo.findOne({ where: { id: classData.departmentId } });
if (!exists) {
throw new BadRequestException(`部门 ID ${classData.departmentId} 不存在`);
}
}
}
const cls = this.classRepo.create(classData);
const saved = await this.classRepo.save(cls);
@@ -165,7 +147,7 @@ export class ClassesService {
this.studentRepo.create({
name: `dd_${dingUserId}`,
status: 'active',
departmentId: classEntity.departmentId ?? undefined,
departmentId: undefined,
})
);
const savedStudents = await this.studentRepo.save(newStudents);
@@ -240,32 +222,6 @@ export class ClassesService {
return { success: true };
}
async createFromDepartment(dto: { departmentId: number; name?: string; classType?: string }) {
const dept = await this.deptRepo.findOne({
where: { id: dto.departmentId, source: 'dingtalk' },
});
if (!dept) {
throw new BadRequestException('所选部门不存在或非钉钉同步部门');
}
const className = dto.name || dept.name;
const code = `DT_${dto.departmentId}`;
const existing = await this.classRepo.findOne({ where: { code } });
if (existing) {
throw new BadRequestException(`班级"${className}"已存在(编码: ${code}`);
}
const cls = this.classRepo.create({
name: className,
code,
departmentId: dto.departmentId,
classType: dto.classType || 'culture',
status: 'enrolling',
});
return this.classRepo.save(cls);
}
async getStudents(classId: number) {
return this.classStudentRepo.find({