Skip to content

Commit 7679f0b

Browse files
committed
feat: add support for import id
1 parent 83a9203 commit 7679f0b

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

bin/importProfileFromJSON.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { QueryFailedError, type DataSource } from 'typeorm';
77
import { readFile, stat, readdir } from 'node:fs/promises';
88
import { importUserExperienceFromJSON } from '../src/common/profile/import';
99
import path from 'node:path';
10+
import { randomUUID } from 'node:crypto';
1011

1112
/**
1213
* Import profile from JSON to user by id
@@ -28,16 +29,22 @@ const main = async () => {
2829
type: 'string',
2930
short: 'l',
3031
},
32+
uid: {
33+
type: 'string',
34+
},
3135
},
3236
});
3337

3438
const paramsSchema = z.object({
3539
path: z.string().nonempty(),
3640
limit: z.coerce.number().int().positive().default(10),
41+
uid: z.string().nonempty().default(randomUUID()),
3742
});
3843

3944
const params = paramsSchema.parse(values);
4045

46+
console.log('Starting import with ID:', params.uid);
47+
4148
con = await createOrGetConnection();
4249

4350
const pathStat = await stat(params.path);
@@ -79,6 +86,7 @@ const main = async () => {
7986
con: con.manager,
8087
dataJson: dataJSON,
8188
userId: 'testuser',
89+
importId: params.uid,
8290
});
8391
} catch (error) {
8492
failedImports += 1;

src/common/profile/import.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,12 @@ export const importUserExperienceWork = async ({
141141
skills,
142142
ended_at: endedAt,
143143
location,
144+
flags,
144145
} = userExperience;
145146

146147
const insertResult = await con.getRepository(UserExperienceWork).insert(
147148
con.getRepository(UserExperienceWork).create({
149+
flags,
148150
userId: userId,
149151
...(await resolveUserCompanyPart({
150152
name: company,
@@ -202,10 +204,12 @@ export const importUserExperienceEducation = async ({
202204
ended_at: endedAt,
203205
location,
204206
subtitle,
207+
flags,
205208
} = userExperience;
206209

207210
const insertResult = await con.getRepository(UserExperienceEducation).insert(
208211
con.getRepository(UserExperienceEducation).create({
212+
flags,
209213
userId: userId,
210214
...(await resolveUserCompanyPart({
211215
name: company,
@@ -250,12 +254,14 @@ export const importUserExperienceCertification = async ({
250254
title,
251255
started_at: startedAt,
252256
ended_at: endedAt,
257+
flags,
253258
} = userExperience;
254259

255260
const insertResult = await con
256261
.getRepository(UserExperienceCertification)
257262
.insert(
258263
con.getRepository(UserExperienceCertification).create({
264+
flags,
259265
userId: userId,
260266
...(await resolveUserCompanyPart({
261267
name: company,
@@ -291,10 +297,12 @@ export const importUserExperienceProject = async ({
291297
started_at: startedAt,
292298
ended_at: endedAt,
293299
skills,
300+
flags,
294301
} = userExperience;
295302

296303
const insertResult = await con.getRepository(UserExperienceProject).insert(
297304
con.getRepository(UserExperienceProject).create({
305+
flags,
298306
userId: userId,
299307
title,
300308
description,
@@ -318,10 +326,12 @@ export const importUserExperienceFromJSON = async ({
318326
con,
319327
dataJson,
320328
userId,
329+
importId,
321330
}: {
322331
con: EntityManager;
323332
dataJson: unknown;
324333
userId: string;
334+
importId?: string;
325335
}) => {
326336
if (!userId) {
327337
throw new Error('userId is required');
@@ -352,26 +362,31 @@ export const importUserExperienceFromJSON = async ({
352362

353363
await con.transaction(async (entityManager) => {
354364
for (const item of data) {
355-
switch (item.type) {
365+
const importData = {
366+
...item,
367+
flags: importId ? { import: importId } : undefined,
368+
};
369+
370+
switch (importData.type) {
356371
case UserExperienceType.Work:
357372
await importUserExperienceWork({
358-
data: item,
373+
data: importData,
359374
con: entityManager,
360375
userId,
361376
});
362377

363378
break;
364379
case UserExperienceType.Education:
365380
await importUserExperienceEducation({
366-
data: item,
381+
data: importData,
367382
con: entityManager,
368383
userId,
369384
});
370385

371386
break;
372387
case UserExperienceType.Certification:
373388
await importUserExperienceCertification({
374-
data: item,
389+
data: importData,
375390
con: entityManager,
376391
userId,
377392
});
@@ -381,14 +396,14 @@ export const importUserExperienceFromJSON = async ({
381396
case UserExperienceType.OpenSource:
382397
case UserExperienceType.Volunteering:
383398
await importUserExperienceProject({
384-
data: item,
399+
data: importData,
385400
con: entityManager,
386401
userId,
387402
});
388403

389404
break;
390405
default:
391-
throw new Error(`Unsupported experience type: ${item.type}`);
406+
throw new Error(`Unsupported experience type: ${importData.type}`);
392407
}
393408
}
394409
});

src/common/schema/profile.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ export const userExperienceWorkImportSchema = z.object({
121121
country: z.string().nullish(),
122122
})
123123
.nullish(),
124+
flags: z.object({ import: z.string() }).partial().optional(),
124125
});
125126

126127
export const userExperienceEducationImportSchema = z.object({
@@ -145,6 +146,7 @@ export const userExperienceEducationImportSchema = z.object({
145146
.nullish()
146147
.transform((n) => (n === null ? undefined : n)),
147148
subtitle: z.string().nullish(),
149+
flags: z.object({ import: z.string() }).partial().optional(),
148150
});
149151

150152
export const userExperienceCertificationImportSchema = z.object({
@@ -157,6 +159,7 @@ export const userExperienceCertificationImportSchema = z.object({
157159
.default('Certification'),
158160
started_at: z.coerce.date().default(() => new Date()),
159161
ended_at: z.coerce.date().nullish().default(null),
162+
flags: z.object({ import: z.string() }).partial().optional(),
160163
});
161164

162165
export const userExperienceProjectImportSchema = z.object({
@@ -173,4 +176,5 @@ export const userExperienceProjectImportSchema = z.object({
173176
.array(z.string())
174177
.nullish()
175178
.transform((n) => (n === null ? undefined : n)),
179+
flags: z.object({ import: z.string() }).partial().optional(),
176180
});

0 commit comments

Comments
 (0)