Skip to content

Commit 9197e72

Browse files
committed
fix: simplify user association in organization
1 parent cca759b commit 9197e72

File tree

5 files changed

+40
-11
lines changed

5 files changed

+40
-11
lines changed

scripts/import-data.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ import { DataImporter } from './utils/data-importer';
1818
// Configuration
1919
const CONFIG: Config = {
2020
STRAPI_URL: process.env.STRAPI_URL || 'http://localhost:1337',
21-
STRAPI_TOKEN: process.env.STRAPI_TOKEN || '',
21+
STRAPI_TOKEN:
22+
process.env.STRAPI_TOKEN ||
23+
'd264463362ab5556b0365e75c9393126f70fc747a3581179cd30c46d5d6b7dede77bb8f212fd12d57da1039109402f335fd02e8f671fd188b8a01477453686d9e08cf1fa7ea5ed8dcf1a6e3710ea7bf5a0bbf16fc482cddf1e600f124c33273bf2eb3aa81d165e62d0a6b5a346f3abd7665a35dcafab371936bf6b805fa15395',
2224
EXCEL_FILE: process.env.EXCEL_FILE || '教育公益开放式数据库.xlsx',
2325
SHEET_NAME: process.env.SHEET_NAME || null,
2426
BATCH_SIZE: parseInt(process.env.BATCH_SIZE || '10'),
@@ -76,7 +78,14 @@ async function main(): Promise<void> {
7678
.map((row) => {
7779
try {
7880
const organization = DataTransformer.transformOrganization(row);
79-
(organization as any)._originalData = row;
81+
82+
// Attach original data as non-enumerable property to avoid serialization to API
83+
Object.defineProperty(organization, '_originalData', {
84+
value: row,
85+
enumerable: false,
86+
writable: false,
87+
configurable: false,
88+
});
8089

8190
return organization;
8291
} catch (error: any) {

scripts/transformers/data-transformer.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export class DataTransformer {
1010
static transformOrganization = (
1111
organization: Organization,
1212
): OrganizationData => ({
13+
contactUser: UserTransformer.transformUser(organization),
1314
name: organization['常用名称'] || organization.name || '',
1415
code: organization['机构信用代码'] || organization.code || '',
1516
entityType: DataUtils.transformEntityType(

scripts/transformers/user-transformer.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import { ExtendedUserData } from '../types';
22

33
export class UserTransformer {
4-
static transformUser = (
5-
organization: any,
6-
organizationId?: number,
7-
): ExtendedUserData | null => {
4+
static transformUser = (organization: any): ExtendedUserData | null => {
85
// 获取用户信息
96
const contactName = organization['机构联系人联系人姓名'] || '';
107
const contactPhone = organization['机构联系人联系人电话'] || '';
@@ -42,7 +39,6 @@ export class UserTransformer {
4239
blocked: true, // 阻止登录
4340
provider: 'local',
4441
phone: contactPhone || undefined,
45-
organizationName: organization['常用名称'] || organization.name,
4642
} as ExtendedUserData;
4743
};
4844

scripts/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ export {
2626
// 扩展的用户数据接口(包含自定义字段)
2727
export type ExtendedUserData = import('../types').UsersPermissionsUser & {
2828
phone?: string;
29-
organizationName?: string;
3029
};
3130

3231
// 组织数据接口

scripts/utils/data-importer.ts

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,6 @@ export class DataImporter {
7373

7474
// 创建新组织
7575
try {
76-
let createdOrganization: any = null;
77-
7876
if (this.dryRun) {
7977
console.log(`[DRY RUN] 将创建组织: ${nameKey}`);
8078
this.stats.success++;
@@ -86,7 +84,33 @@ export class DataImporter {
8684
const cleanOrgData = { ...org };
8785
delete (cleanOrgData as any)._originalData;
8886

89-
createdOrganization = await this.api.createOrganization(cleanOrgData);
87+
// 如果有联系人用户,先创建用户
88+
if (cleanOrgData.contactUser) {
89+
try {
90+
const createdUser = await this.api.createUser(
91+
cleanOrgData.contactUser,
92+
);
93+
console.log(
94+
`✓ 成功创建联系人用户: ${cleanOrgData.contactUser.username}`,
95+
);
96+
97+
// 设置组织与用户的关联
98+
cleanOrgData.contactUser = (createdUser as any).id || createdUser;
99+
} catch (userError: any) {
100+
console.error(
101+
`✗ 创建用户失败: ${cleanOrgData.contactUser.username}`,
102+
userError.message,
103+
);
104+
this.logger.logFailed(org, userError);
105+
this.stats.failed++;
106+
continue;
107+
}
108+
} else {
109+
// 如果没有联系人用户,设置为null
110+
cleanOrgData.contactUser = null;
111+
}
112+
113+
await this.api.createOrganization(cleanOrgData);
90114
console.log(`✓ 成功创建组织: ${nameKey}`);
91115
this.stats.success++;
92116
smallCache.add(nameKey);

0 commit comments

Comments
 (0)