-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_super_user.js
More file actions
108 lines (104 loc) · 2.96 KB
/
Copy pathcreate_super_user.js
File metadata and controls
108 lines (104 loc) · 2.96 KB
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
const inquirer = require("inquirer");
const { PrismaClient } = require("@prisma/client");
const bcrypt = require("bcrypt");
const prisma = new PrismaClient();
let adminInfo = {};
const questions = [
{
type: "input",
name: "first",
message: "first name?",
},
{
type: "input",
name: "last",
message: "last name?",
},
{
type: "input",
name: "email",
message: "email?",
validate: async (answer) => {
if (!answer) return "email required";
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(answer)) {
return "invalid email";
}
const user = await prisma.user.findUnique({
where: {
email: answer,
},
});
if (user) return "user with this email exist";
return true;
},
},
{
type: "input",
name: "phone_number",
message: "phone_number?",
validate: async (answer) => {
if (!answer) return "phone_number required";
if (isNaN(answer)) return "invalid phone_number";
const user = await prisma.user.findUnique({
where: {
phone_number: answer,
},
});
if (user) return "user with this email exist";
return true;
},
},
{
type: "password",
name: "password1",
message: "password?",
mask: "*",
validate: (answer) => {
if (!answer) return "password required";
adminInfo.password1 = answer;
return true;
},
},
{
type: "password",
name: "password2",
message: "password(again)?",
mask: "*",
validate: (answer) => {
if (!answer) return "confirm password required";
adminInfo.password2 = answer;
if (answer !== adminInfo.password1) {
return "passwords not match";
}
return true;
},
},
];
inquirer.prompt(questions).then(async (answers) => {
adminInfo = { ...answers, ...adminInfo };
const salt = await bcrypt.genSalt(10);
const hashPassword = await bcrypt.hash(adminInfo.password1, salt);
const newAdmin = await prisma.user.create({
data: {
first: adminInfo.first,
last: adminInfo.last,
password: hashPassword,
email: adminInfo.email,
phone_number: adminInfo.phone_number,
is_active: true,
is_admin: true,
is_staff: true,
},
});
const newcostomer = await prisma.costomer.create({
data: {
id: newAdmin.id,
first: newAdmin.first,
last: newAdmin.last,
email: newAdmin.email,
phone_number: newAdmin.phone_number,
},
});
console.log(newAdmin);
});