-
Notifications
You must be signed in to change notification settings - Fork 0
/
dummy.ts
71 lines (65 loc) · 1.89 KB
/
dummy.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import { Prisma, PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
async function fillQuestionUser() {
const questions = await prisma.question.findMany();
const user = await prisma.user.findFirst();
if (!user)
return questions && console.log("임시 데이터로 사용할 유저가 없음");
for (const question of questions) {
await prisma.question.update({
where: { id: question.id },
data: { user: { connect: { id: user.id } } },
});
}
}
async function fillAnswerUser() {
const answers = await prisma.answer.findMany();
const user = await prisma.user.findFirst();
if (user === null)
return answers && console.log("임시 데이터로 사용할 유저가 없음");
for (const answer of answers) {
await prisma.answer.update({
where: { id: answer.id },
data: { user: { connect: { id: user.id } } },
});
}
}
async function fillUserName() {
const users = await prisma.user.findMany({
select: {
id: true,
accounts: { select: { provider: true, providerAccountId: true } },
},
});
for (const user of users) {
const credentialProvider = user.accounts.find(
({ provider }) => provider === "credentials"
);
if (credentialProvider) {
const { providerAccountId: id } = credentialProvider;
const credential = await prisma.credential.findUnique({ where: { id } });
if (credential) {
await prisma.user.update({
where: { id: user.id },
data: { name: credential.username },
});
} else {
await prisma.user.update({
where: { id: user.id },
data: { name: id },
});
}
} else {
await prisma.user.update({
where: { id: user.id },
data: { name: user.id },
});
}
}
}
async function main() {
await fillQuestionUser();
await fillAnswerUser();
await fillUserName();
}
main();