-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
247 lines (218 loc) · 6.24 KB
/
server.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
const inquirer = require("inquirer");
const dbUtil = require("./dbFiles/dbUtil.js");
const dbQueryUtil = require("./dbFiles/dbUtil.js");
init();
function init() {
inquirer
.prompt({
type: "list",
choices: [
"Add Department",
"Add Role",
"Add Employee",
"View Departments",
"View Roles",
"View Employees",
"Update Employee Role",
"Delete Employee",
"Delete Role",
"Delete Department",
"Quit",
],
message: "What would you like to do?",
name: "option",
})
.then((answer) => {
console.log(answer);
switch (answer.option) {
case "View Employees":
return viewAllEmployees();
case "View Roles":
return viewAllRoles();
case "View Departments":
return viewAllDepartments();
case "Add Employee":
return addEmployee();
case "Add Role":
return addRole();
case "Add Department":
return addDepartment();
case "Update Employee Role":
return updateEmployee();
case "Delete Employee":
return deleteEmployee();
case "Delete Role":
return deleteRole();
case "Delete Department":
return deleteDepartment();
case "Quit":
return quit();
}
});
}
async function viewAllEmployees() {
const employees = await dbQueryUtil.getAllEmployees();
console.table(employees);
init();
}
async function viewAllRoles() {
const role = await dbQueryUtil.viewAllRoles();
console.table(role);
init();
}
async function viewAllDepartments() {
const departments = await dbQueryUtil.viewAllDepartments();
console.table(departments);
init();
}
async function addDepartment() {
const department = await inquirer.prompt({
type: "input",
message: "What is the name of the department?",
name: "name",
});
await dbQueryUtil.createDepartment(department);
init();
}
async function addEmployee() {
const rolesOptions = await dbUtil.viewAllRoles();
const managerOptions = await dbUtil.getAllEmployees();
const employeeToAdd = await inquirer.prompt([
{
type: "input",
message: "What's the first name of the employee?",
name: "first_name",
},
{
type: "input",
message: "What's the last name of the employee?",
name: "last_name",
},
]);
var roleChoicesList = rolesOptions.map(({ id, title }) => ({ name: title, value: id }));
//console.log("role choicesList", rolesOptions)
const {roleId} = await inquirer.prompt({
type: "list",
name: "roleId",
message: "What is this new employees role?",
choices: roleChoicesList,
});
const managerChoicesList = managerOptions.map(({ first_name, last_name, id }) => ({ name: first_name + last_name, value: id }));
if (managerChoicesList && managerChoicesList.length > 0){
const { managerId } = await inquirer.prompt({
type: "list",
name: "managerId",
message: "Please select this new employees manager:",
choices: managerChoicesList,
});
employeeToAdd.manager_id = managerId;
}
employeeToAdd.role_id = roleId;
//employeeToAdd.manager_id = mana÷erId;
await dbUtil.createEmployee(employeeToAdd);
init();
}
async function addRole() {
const departments = await dbQueryUtil.viewAllDepartments();
const departmentsList = departments.map(({ id, name }) => ({ name: name, value: id }));
const roleToAdd = await inquirer.prompt([
{
type: "input",
message: "What's the name of the role?",
name: "title",
},
{
type: "input",
message: "What is the salary for this role?",
name: "salary",
},
{
type: "list",
message: "What is the department id number?",
name: "department_id",
choices: departmentsList,
},
]);
await dbQueryUtil.addRole(roleToAdd);
init();
}
async function updateEmployee() {
const employeeOptions = await dbUtil.getAllEmployees();
const rolesOptions = await dbUtil.viewAllRoles();
console.log(rolesOptions);
const employeeOptionsToChooseFrom = employeeOptions.map(({ id, first_name, last_name }) => ({
name: first_name + last_name,
value: id,
}));
const rolesOptionsToChooseFrom = rolesOptions.map(({ id, title }) => ({
name: title,
value: id,
}));
const { employeeId } = await inquirer.prompt([
{
type: "list",
name: "employeeId",
message: "Select the employee whose role you wish to change:",
choices: employeeOptionsToChooseFrom,
},
]);
const { roleId } = await inquirer.prompt([
{
type: "list",
name: "roleId",
message: "What new role would you like to assign to this employee?",
choices: rolesOptionsToChooseFrom,
},
]);
await dbUtil.updateEmployeeRole(employeeId, roleId);
init();
}
async function deleteEmployee() {
const employeeOptions = await dbUtil.getAllEmployees();
const employeeOptionsToChooseFrom = employeeOptions.map(({ id, first_name, last_name }) => ({
name: first_name + last_name,
value: id,
}));
const { employeeId } = await inquirer.prompt([
{
type: "list",
name: "employeeId",
message: "Which employee would you like to delete?",
choices: employeeOptionsToChooseFrom,
},
]);
await dbUtil.removeEmployee(employeeId);
init();
}
async function deleteRole() {
const rolesOptions = await dbUtil.viewAllRoles();
const rolesOptionsToChooseFrom = rolesOptions.map(({ id, title }) => ({
name: title,
value: id,
}));
const { roleId } = await inquirer.prompt([
{
type: "list",
name: "roleId",
message: "Which role would you like to delete?",
choices: rolesOptionsToChooseFrom,
},
]);
await dbUtil.removeRole(roleId);
init();
}
async function deleteDepartment() {
const departmentOptions = await dbUtil.viewAllDepartments();
const departmentOptionsToChooseFrom = departmentOptions.map(({ id, name }) => ({ name: name, value: id }));
const { departmentId } = await inquirer.prompt({
type: "list",
name: "departmentId",
message: "Which department would you like to delete?",
choices: departmentOptionsToChooseFrom,
});
await dbUtil.removeDepartment(departmentId);
init();
}
function quit() {
process.exit();
}