-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimporter.ts
55 lines (49 loc) · 1.19 KB
/
importer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/* eslint-disable @typescript-eslint/no-unused-vars */
import getConfig from '@config'
import db from '@db'
import type { Sex } from '@prisma/client'
const system_tags = getConfig().system_tags
export async function importTags() {
await Promise.all(
system_tags.map(async (tag) => {
await db.tag.upsert({
where: {
name: tag
},
update: {},
create: {
name: tag
}
})
})
)
}
export async function importUsersFromCsv(file: string) {
const f = Bun.file(file)
const data = (await f.text()).trim()
const lines = data.split('\n')
await Promise.all(
lines.map(async (line) => {
const x = line.split(',')
const user = await db.user.findFirst({ where: { email: x[2] } })
if (!user) {
const team = await db.team.create({
data: {
name: x[0].trim(),
money: 0
}
})
await db.user.create({
data: {
name: x[0].trim(),
password: await Bun.password.hash(x[1]),
email: x[2],
sex: x[3] as Sex,
teamId: team.id,
admin: x[4] == '1'
}
})
}
})
)
}