()
| 53 | }; |
| 54 | |
| 55 | async function run() { |
| 56 | if (process.argv.length < 4) { |
| 57 | throw 'missing params, try: npm run script:import-user -- "accountId" "/full/path/file.csv"'; |
| 58 | } |
| 59 | const accountId = process.argv[2]; |
| 60 | const file = fs.readFileSync(process.argv[3]); |
| 61 | |
| 62 | const { data } = Papa.parse(file.toString(), { |
| 63 | header: true, |
| 64 | skipEmptyLines: true, |
| 65 | }); |
| 66 | const users = data as Users[]; |
| 67 | |
| 68 | const toUpsert: Prisma.usersCreateInput[] = users.map((user) => { |
| 69 | return { |
| 70 | account: { connect: { id: accountId } }, |
| 71 | anonymousAlias: generateRandomWordSlug(), |
| 72 | ...(shouldCreateAuth(user.status) && { |
| 73 | auth: { |
| 74 | connectOrCreate: { |
| 75 | create: { |
| 76 | email: user.email, |
| 77 | password: v4(), |
| 78 | salt: v4(), |
| 79 | accountId, |
| 80 | }, |
| 81 | where: { email: user.email }, |
| 82 | }, |
| 83 | }, |
| 84 | }), |
| 85 | displayName: user.displayname || user.username, |
| 86 | externalUserId: user.userid, |
| 87 | role: mapRole(user.status), |
| 88 | isAdmin: isAdmin(user.status), |
| 89 | isBot: isBot(user.status), |
| 90 | }; |
| 91 | }); |
| 92 | |
| 93 | await Promise.all( |
| 94 | toUpsert.map((data) => |
| 95 | prisma.users.upsert({ |
| 96 | create: data, |
| 97 | update: data, |
| 98 | where: { |
| 99 | externalUserId_accountsId: { |
| 100 | accountsId: accountId, |
| 101 | externalUserId: data.externalUserId!, |
| 102 | }, |
| 103 | }, |
| 104 | }) |
| 105 | ) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | run().catch(console.error); |
no test coverage detected