fix(server): 钉钉导入配置错误改抛 BadRequestException,使 create() 补偿删除真正生效
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { BadRequestException } from '@nestjs/common';
|
||||
import { EntityManager, In } from 'typeorm';
|
||||
import { Organization, Student, StudentDingMapping } from '../entities';
|
||||
|
||||
@@ -37,7 +38,11 @@ export async function syncDingTalkStudents(
|
||||
const name = input.name?.trim();
|
||||
const mobile = input.mobile?.trim() || undefined;
|
||||
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;
|
||||
}
|
||||
if (mobile && mobile.length > 20) {
|
||||
@@ -94,7 +99,9 @@ export async function syncDingTalkStudents(
|
||||
}
|
||||
|
||||
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
|
||||
? await manager.find(Student, { where: { phone: In(mobiles) } })
|
||||
: [];
|
||||
@@ -144,7 +151,9 @@ export async function syncDingTalkStudents(
|
||||
const host = creatable.length
|
||||
? await manager.findOne(Organization, { where: { isHost: true, status: 'active' } })
|
||||
: null;
|
||||
if (creatable.length && !host) throw new Error('尚未配置本机构');
|
||||
// 配置缺失属于业务错误(与其他模块一致抛 BadRequestException),
|
||||
// 调用方 create() 据此判定为数据错误并补偿删除已建班级
|
||||
if (creatable.length && !host) throw new BadRequestException('尚未配置本机构');
|
||||
|
||||
const bindableStudent = new Map<string, Student>();
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user