Skip to content

Commit c9dd9ee

Browse files
committed
fix: Insufficient error handling - missing data validation on user creationtype change error
1 parent 0fe3f40 commit c9dd9ee

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

scripts/utils/data-importer.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -87,26 +87,34 @@ export class DataImporter {
8787
// 如果有联系人用户,先创建用户
8888
if (cleanOrgData.contactUser) {
8989
try {
90-
const createdUser = await this.api.createUser(
91-
cleanOrgData.contactUser,
92-
);
93-
console.log(
94-
`✓ 成功创建联系人用户: ${cleanOrgData.contactUser.username}`,
95-
);
90+
// 验证用户数据
91+
const contactUser = cleanOrgData.contactUser as any;
92+
if (!contactUser.email || !contactUser.username) {
93+
throw new Error('用户数据缺少必需字段:email 或 username');
94+
}
9695

97-
// 设置组织与用户的关联
98-
const userId = (createdUser as any).id;
99-
if (!userId) {
100-
throw new Error(
101-
`创建的用户缺少ID: ${cleanOrgData.contactUser.username}`,
102-
);
96+
// 检查用户是否已存在
97+
const existingUser = await this.api.findUserByEmail(
98+
contactUser.email,
99+
);
100+
if (existingUser) {
101+
console.log(`✓ 使用现有用户: ${contactUser.username}`);
102+
cleanOrgData.contactUser = (existingUser as any).id;
103+
} else {
104+
const createdUser = await this.api.createUser(contactUser);
105+
console.log(`✓ 成功创建联系人用户: ${contactUser.username}`);
106+
107+
// 设置组织与用户的关联
108+
const userId = (createdUser as any).id;
109+
if (!userId) {
110+
throw new Error(`创建的用户缺少ID: ${contactUser.username}`);
111+
}
112+
cleanOrgData.contactUser = userId;
103113
}
104-
cleanOrgData.contactUser = userId;
105114
} catch (userError: any) {
106-
console.error(
107-
`✗ 创建用户失败: ${cleanOrgData.contactUser.username}`,
108-
userError.message,
109-
);
115+
const username =
116+
(cleanOrgData.contactUser as any)?.username || 'unknown';
117+
console.error(`✗ 用户操作失败: ${username}`, userError.message);
110118
this.logger.logFailed(org, userError);
111119
this.stats.failed++;
112120
continue;

0 commit comments

Comments
 (0)