-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup-app.js
55 lines (47 loc) · 1.08 KB
/
setup-app.js
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
import argon2 from "argon2";
import { PrismaClient } from "@prisma/client";
const prisma = new PrismaClient();
const vendorData = {
vendorName: "LOGIKINNEPAL",
vendorAddress: "Thapathali",
vendorEmail: "[email protected]"
};
const userData = {
role: "DEVELOPER",
name: "Pukar Sharma",
username: "sharmapukar217",
password: "12122121",
email: "[email protected]"
};
prisma
.$transaction(async function (tx) {
let vendor;
vendor = await tx.vendor.findFirst({
select: { id: true },
where: { vendorName: vendorData.vendorName },
});
if (!vendor) {
vendor = await tx.vendor.create({
data: vendorData
});
}
const userExists = await tx.user.count({
where: { username: userData.username }
});
let user;
if (!userExists) {
user = await tx.user.create({
data: {
...userData,
vendorId: vendor.id,
password: await argon2.hash(userData.password),
picture: encodeURI(
`https://ui-avatars.com/api/?name=${userData.name}`
)
}
});
}
return { vendor, user };
})
.then(console.log)
.catch(console.error);