fix: resolve DingTalk dept ID to local department ID in class create

This commit is contained in:
2026-07-09 17:28:12 +08:00
parent 9b7a8e37b4
commit 40fef906cd

View File

@@ -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);