fix: handle duplicate usernames and failed role lookups in importDingTalkUsers

This commit is contained in:
2026-07-09 11:07:25 +08:00
parent 3790b2dabb
commit 8ba3327074
2 changed files with 28 additions and 7 deletions

View File

@@ -138,7 +138,7 @@ export class SyncService {
}
try {
const username = u.mobile || `dd_${u.dingUserId}`;
const username = `dd_${u.dingUserId}`;
const passwordHash = await bcrypt.hash('123456', 10);
const user = this.userRepo.create({
@@ -155,10 +155,18 @@ export class SyncService {
if (role) {
user.roles = [role];
await this.userRepo.save(user);
teacherCount++;
} else {
this.logger.warn(`角色 id=${u.roleId} 不存在,用户 ${u.name} 未分配角色`);
this.logger.warn(`角色 id=${u.roleId} 不存在,用户 ${u.name} 转为学生`);
const student = this.studentRepo.create({
name: u.name,
phone: u.mobile || undefined,
userId: user.id,
status: 'active',
});
await this.studentRepo.save(student);
studentCount++;
}
teacherCount++;
} else {
// 学生:创建 Student 记录
const student = this.studentRepo.create({