diff --git a/apps/server/src/classes/classes.service.ts b/apps/server/src/classes/classes.service.ts index 89b3799..c69c0f1 100644 --- a/apps/server/src/classes/classes.service.ts +++ b/apps/server/src/classes/classes.service.ts @@ -99,6 +99,22 @@ 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);