fix(server): 钉钉导入配置错误改抛 BadRequestException,使 create() 补偿删除真正生效
All checks were successful
CI / check (pull_request) Successful in 2m46s
Dependency Check / check (pull_request) Successful in 2m18s

This commit is contained in:
2026-08-11 12:53:56 +08:00
parent 2871407f76
commit 2e5e3fb7e5

View File

@@ -1,3 +1,4 @@
import { BadRequestException } from '@nestjs/common';
import { EntityManager, In } from 'typeorm'; import { EntityManager, In } from 'typeorm';
import { Organization, Student, StudentDingMapping } from '../entities'; import { Organization, Student, StudentDingMapping } from '../entities';
@@ -37,7 +38,11 @@ export async function syncDingTalkStudents(
const name = input.name?.trim(); const name = input.name?.trim();
const mobile = input.mobile?.trim() || undefined; const mobile = input.mobile?.trim() || undefined;
if (!dingUserId || dingUserId.length > 100 || !name || name.length > 50) { if (!dingUserId || dingUserId.length > 100 || !name || name.length > 50) {
conflicts.push({ dingUserId: dingUserId || '', name: name || '', reason: '钉钉用户ID或姓名无效' }); conflicts.push({
dingUserId: dingUserId || '',
name: name || '',
reason: '钉钉用户ID或姓名无效',
});
continue; continue;
} }
if (mobile && mobile.length > 20) { if (mobile && mobile.length > 20) {
@@ -94,7 +99,9 @@ export async function syncDingTalkStudents(
} }
const newUsers = [...users.values()].filter((user) => !mappingByDingId.has(user.dingUserId)); const newUsers = [...users.values()].filter((user) => !mappingByDingId.has(user.dingUserId));
const mobiles = [...new Set(newUsers.map((user) => user.mobile).filter((mobile): mobile is string => !!mobile))]; const mobiles = [
...new Set(newUsers.map((user) => user.mobile).filter((mobile): mobile is string => !!mobile)),
];
const occupiedPhones = mobiles.length const occupiedPhones = mobiles.length
? await manager.find(Student, { where: { phone: In(mobiles) } }) ? await manager.find(Student, { where: { phone: In(mobiles) } })
: []; : [];
@@ -144,7 +151,9 @@ export async function syncDingTalkStudents(
const host = creatable.length const host = creatable.length
? await manager.findOne(Organization, { where: { isHost: true, status: 'active' } }) ? await manager.findOne(Organization, { where: { isHost: true, status: 'active' } })
: null; : null;
if (creatable.length && !host) throw new Error('尚未配置本机构'); // 配置缺失属于业务错误(与其他模块一致抛 BadRequestException
// 调用方 create() 据此判定为数据错误并补偿删除已建班级
if (creatable.length && !host) throw new BadRequestException('尚未配置本机构');
const bindableStudent = new Map<string, Student>(); const bindableStudent = new Map<string, Student>();
for (const user of bindable) { for (const user of bindable) {
@@ -206,7 +215,9 @@ export async function syncDingTalkStudents(
}), }),
), ),
); );
createdStudents.forEach((student, index) => studentIds.set(creatable[index].dingUserId, student.id)); createdStudents.forEach((student, index) =>
studentIds.set(creatable[index].dingUserId, student.id),
);
} }
return { return {