-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
569 lines (533 loc) · 15.5 KB
/
app.js
File metadata and controls
569 lines (533 loc) · 15.5 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
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
const mysql = require("mysql");
const inquirer = require("inquirer");
const chalk = require("chalk");
const clear = require("clear");
const figlet = require("figlet");
const consoleTables = require("console.table");
require("dotenv").config();
const { allowedNodeEnvironmentFlags, exit } = require("process");
const { setupMaster } = require("cluster");
// Function to validate for salaries
const validateNumber = async (input) => {
if (isNaN(input) == true) {
return "Please input a number.";
}
return true;
};
let connection = mysql.createConnection({
multipleStatements: true,
host: process.env.DB_HOST,
// Your port; if not 3306
port: process.env.DB_PORT,
// Your username
user: process.env.DB_USER,
// Your password
password: process.env.DB_PASS,
database: process.env.DB_NAME,
});
connection.connect(function (err) {
if (err) throw err;
clear();
console.log(
chalk.blueBright(
figlet.textSync("Employee", {
horizontalLayout: "full",
})
)
);
console.log(
chalk.blueBright(
figlet.textSync("Management", {
horizontalLayout: "full",
})
)
);
console.log(
chalk.blueBright(
figlet.textSync("System", {
horizontalLayout: "full",
})
)
);
start();
});
// Begin app
function start() {
inquirer
.prompt({
name: "action",
type: "list",
message: "What would you like to do?",
choices: [
"Add a department.",
"Add a role.",
"Add an employee.",
"View departments.",
"View roles.",
"View employees.",
"View total budget utilization by department.",
"View employees with the same manager.",
"Update a role's department.",
"Update an employee's role.",
"Update an employee's manager.",
"Delete a department.",
"Delete a role.",
"Delete an employee.",
"Exit.",
],
})
.then(function (answer) {
switch (answer.action) {
case "Add a department.":
addDepartment();
break;
case "Add a role.":
addRole();
break;
case "Add an employee.":
addEmployee();
break;
case "View departments.":
viewDepartment();
break;
case "View roles.":
viewRoles();
break;
case "View employees.":
viewEmployees();
break;
case "View total budget utilization by department.":
budgetUtil();
break;
case "View employees with the same manager.":
employeesByManager();
break;
case "Update a role's department.":
roleUpdate();
break;
case "Update an employee's role.":
employeeRole();
break;
case "Update an employee's manager.":
employeeManagers();
break;
case "Delete a department.":
deleteDepartment();
break;
case "Delete a role.":
deleteRole();
break;
case "Delete an employee.":
deleteEmployee();
break;
case "Exit.":
connection.end();
break;
}
});
function addDepartment() {
inquirer
.prompt({
name: "department",
type: "input",
message: "What is the name of the department you would like to add?",
})
.then(function (answer) {
connection.query(
"INSERT INTO departments SET ?",
{
name: answer.department,
},
function (err) {
if (err) throw err;
console.log(
answer.department + " successfully added to the departments"
);
// Return to the beginning
start();
}
);
});
}
function addRole() {
inquirer
.prompt([
{
name: "role",
type: "input",
message: "What is the title of the role you like to add?",
},
{
name: "salary",
type: "input",
message: "What is the salary, without separating with commas.",
validate: validateNumber,
},
])
.then(function (answers) {
connection.query(
"INSERT INTO role SET ?",
{
title: answers.role,
salary: answers.salary,
},
function (err) {
if (err) throw err;
console.log(answers.role + " successfully add to roles.");
// Return to the beginning
start();
}
);
});
}
function addEmployee() {
connection.query("SELECT * FROM role", (err, results) => {
if (err) throw err;
inquirer
.prompt([
{
name: "firstName",
type: "input",
message: "What is the first name of the employee?",
},
{
name: "lastName",
type: "input",
message: "What is the last name of the employee?",
},
{
name: "role",
type: "list",
message: "What is the employee's role?",
choices: () => results.map((result) => result.title),
},
])
.then((answers) => {
answers.role = results.filter(
(result) => result.title === answers.role
)[0].id;
connection.query(
"INSERT INTO employee SET ?",
{
first_name: answers.firstName,
last_name: answers.lastName,
role_id: answers.role,
},
(err) => {
if (err) throw err;
console.log(
answers.firstName +
" " +
answers.lastName +
" successfully added to employee with the role of " +
answers.role
);
// Return to the beginning
start();
}
);
});
});
}
function viewDepartment() {
// Render department table
connection.query("SELECT * FROM departments", (err, results) => {
if (err) throw err;
console.table(results);
start();
});
}
function viewRoles() {
// Render employee table
connection.query("SELECT * FROM role", (err, results) => {
if (err) throw err;
console.table(results);
start();
});
}
function viewEmployees() {
connection.query(
`SELECT
e.id,
e.first_name AS "First Name",
e.last_name AS "Last Name",
r.title,
d.name AS "Department",
IFNULL(r.salary, 'No Data') AS "Salary",
CONCAT(m.first_name," ",m.last_name) AS "Manager"
FROM employee e
LEFT JOIN role r
ON r.id = e.role_id
LEFT JOIN departments d
ON d.id = r.department_id
LEFT JOIN employee m ON m.id = e.manager_id
ORDER BY e.id;`,
(err, results) => {
if (err) throw err;
console.table(results);
start();
}
);
}
function budgetUtil() {
connection.query(
`SELECT departments.id, departments.name, role.salary as Budget
FROM employee
LEFT JOIN
role on employee.role_id = role.id
LEFT JOIN
departments on role.department_id = departments.id
`,
(err, results) => {
if (err) throw err;
console.table(results);
start();
}
);
}
function employeesByManager() {
connection.query("SELECT * FROM employee", (err, results) => {
if (err) throw err;
inquirer
.prompt({
name: "manager",
type: "list",
message: "Which manager's team would you like to view?",
choices: () =>
results.map((result) => result.first_name + " " + result.last_name),
})
.then((answer) => {
let managerID = results.filter(
(result) =>
result.first_name + " " + result.last_name === answer.manager
)[0].role_id;
connection.query(
`SELECT * FROM employee WHERE manager_id = ${managerID}`,
(err, filteredResults) => {
if (err) throw err;
console.table(answer.manager + "'s Team: ", filteredResults);
start();
}
);
});
});
}
function roleUpdate() {
connection.query(
"SELECT * FROM role; SELECT * FROM departments",
(err, results) => {
if (err) throw err;
inquirer
.prompt([
{
name: "role",
type: "list",
message:
"What role would you like to assign to a new department?",
choices: () => results[0].map((result) => result.title),
},
{
name: "department",
type: "list",
message: "What department does this role belong to?",
choices: () => results[1].map((result) => result.name),
},
])
.then((answers) => {
let roleID = results[0].filter(
(result) => result.title === answers.role
)[0].id;
let newDepartment = results[1].filter(
(result) => result.name === answers.department
)[0].id;
connection.query(`UPDATE role set department_id = ? WHERE id = ?`, [
newDepartment,
roleID,
]);
console.log(
answers.role +
" now belongs to the " +
answers.department +
" department."
);
start();
});
}
);
}
// This one's really cool, check this out...
function employeeRole() {
connection.query(
// Must include line 19 in order for this to work. This is two queries in one, returned as an array of data
// This needed to be done because in previous attempts I was unable to map the names AND the roles
// that existed within my database without them interacting negatively with one another.
"SELECT * FROM employee; SELECT * FROM role",
(err, results) => {
if (err) throw err;
inquirer
.prompt([
{
name: "employee",
type: "list",
message: "What employee's role would you like to update?",
choices: () =>
// Like every other .map function used in my other functions, except we're interested in
// the zeroth point of the array "results" because that corresponds with employee
results[0].map(
(result) => result.first_name + " " + result.last_name
),
},
{
name: "role",
type: "list",
message: "What is this employee's new role?",
// results[1] here corresponds to role
choices: () => results[1].map((result) => result.title),
},
])
.then((answers) => {
// The two filter functions are present in other functions, these are unique because like the
// .map functions above, we need to specify the zeroth or first point of the array
let employeeID = results[0].filter(
(result) =>
result.first_name + " " + result.last_name === answers.employee
)[0].id;
let newRole = results[1].filter(
(result) => result.title === answers.role
)[0].id;
connection.query(`UPDATE employee SET role_id = ? WHERE id = ?`, [
newRole,
employeeID,
]);
console.log(
answers.employee + "'s new role is now " + answers.role
);
start();
});
}
);
}
function employeeManagers() {
connection.query("SELECT * FROM employee", (err, results) => {
if (err) throw err;
inquirer
.prompt([
{
name: "employee",
type: "list",
message: "Which employee's manager would you like to update?",
choices: () =>
results.map(
(result) => result.first_name + " " + result.last_name
),
},
{
name: "manager",
type: "list",
message: "Who is this employee's manager?",
choices: () =>
results.map(
(result) => result.first_name + " " + result.last_name
),
},
])
.then((answers) => {
let employeeID = results.filter(
(result) =>
result.first_name + " " + result.last_name === answers.employee
)[0].id;
let managerID = results.filter(
(result) =>
result.first_name + " " + result.last_name === answers.manager
)[0].id;
connection.query(`UPDATE employee SET manager_id = ? WHERE id = ? `, [
managerID,
employeeID,
]);
console.log(
answers.manager + " is now the manager of " + answers.employee
);
start();
});
});
}
function deleteDepartment() {
connection.query(
"SET FOREIGN_KEY_CHECKS=0; SELECT * FROM departments",
(err, results) => {
if (err) throw err;
inquirer
.prompt({
name: "department",
type: "list",
message: "What department would you like to delete?",
choices: () => results[1].map((result) => result.name),
})
.then((answer) => {
let departmentID = results[1].filter(
(result) => result.name === answer.department
)[0].id;
connection.query(`DELETE FROM departments WHERE ? `, {
id: departmentID,
});
console.log(
"Successfully deleted the " + answer.department + " department."
);
start();
});
}
);
}
function deleteRole() {
connection.query(
"SET FOREIGN_KEY_CHECKS=0; SELECT * FROM role",
(err, results) => {
if (err) throw err;
inquirer
.prompt({
name: "role",
type: "list",
message: "What role would you like to delete?",
choices: () => results[1].map((result) => result.title),
})
.then((answer) => {
connection.query(`DELETE FROM role WHERE ? `, {
title: answer.role,
});
console.log("Successfully deleted the role of: " + answer.role);
start();
});
}
);
}
function deleteEmployee() {
connection.query("SELECT * FROM employee", (err, results) => {
if (err) throw err;
inquirer
.prompt({
name: "employee",
type: "list",
message: "What employee would you like to delete?",
choices: () =>
results.map(
(results) => results.first_name + " " + results.last_name
),
})
.then((answer) => {
let employeeID = results.filter(
(result) =>
result.first_name + " " + result.last_name === answer.employee
)[0].id;
connection.query(`DELETE FROM employee WHERE ? `, {
id: employeeID,
});
console.log(
"Successfully deleted " +
answer.employee +
" from the employee database"
);
start();
});
});
}
}