-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
692 lines (650 loc) · 20.7 KB
/
index.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
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
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
//Require dependencies
const inquirer = require("inquirer"); //Inquirer package to interact with the user via the command line
const connection = require("./config/connection");
const figlet = require("figlet"); // Figlet to add style to text in the terminal
const chalk = require("chalk"); // Chalk to style the console with colors
const validate = require("./validate"); // Validator contains functions that can be used to validate user input
// const { errorMonitor } = require("mysql2/typings/mysql/lib/Connection");
require("console.table"); // Console.table to print MySQL rows to the console
//Establish connection to MySQL database, console log to the console error or success message
connection.connect((error) => {
if (error) throw error;
console.log(
chalk.rgb(255, 105, 180).bold(
figlet.textSync("Employee\nManager", {
font: "Standard",
lineHeight: 3,
})
)
);
// Call function userChoices
userChoices();
});
// Function to prompt the user to select an option from the list of choices
const userChoices = () => {
inquirer
.prompt([
{
name: "choices",
type: "list",
message: "What would you like to do?",
choices: [
"View All Employees",
"Add Employee",
"Update Employee Role",
"View All Roles",
"Add Role",
"View All Departments",
"Add Department",
//Extra functionality
"Update Employee Manager",
"View Employees By Department",
"Remove Department",
"Remove Role",
"Remove Employee",
"View Department Budget",
"Close",
],
},
])
//Switch statements to check the value of the answers and call corresponding function for each case
.then((answers) => {
const { choices } = answers;
switch (choices) {
case "View All Employees":
viewAllEmployees();
break;
case "Add Employee":
addEmployee();
break;
case "Update Employee Role":
updateEmployeeRole();
break;
case "View All Roles":
viewAllRoles();
break;
case "Add Role":
addRole();
break;
case "View All Departments":
viewAllDepartments();
break;
case "Add Department":
addDepartment();
break;
case "Update Employee Manager":
updateEmployeeManager();
break;
case "View Employees By Department":
viewEmployeesByDepartment();
break;
case "Remove Department":
removeDepartment();
break;
case "Remove Role":
removeRole();
break;
case "Remove Employee":
removeEmployee();
break;
case "View Department Budget":
viewDepartmentBudget();
break;
case "Close":
connection.end();
break;
default:
console.log("Invalid choice");
}
});
};
//Retrieve data from database using Structured Query Language
//The promise() will wait until the connection is done before moving in to the next step and allows to hanlde the result of the query async
//The query () method takes the sql query as a parameter to be executed and a callback function to handle the result of the sql query
// Function to View All Employees
const viewAllEmployees = async () => {
try {
const [rows] = await connection.promise().query(
`SELECT e.id, e.first_name, e.last_name, r.title, d.department_name, r.salary, CONCAT(m.first_name, ' ', m.last_name) AS manager
FROM employee e
JOIN role r ON r.id = e.role_id
JOIN department d ON d.id = r.department_id
LEFT JOIN employee m ON m.id = e.manager_id
ORDER BY e.id;
`
);
console.log("");
console.log(chalk.rgb(255, 105, 180).bold(`View all Employees`));
console.log("");
console.table(rows);
userChoices();
} catch (error) {
console.error(error);
}
};
//Function to Add Employee
const addEmployee = () => {
inquirer
.prompt([
{
type: "input",
name: "firstName",
message: "What is the employee's first name?",
validate: (firstName) => {
if (firstName) {
return true;
} else {
console.log("Please enter first name");
return false;
}
},
},
{
type: "input",
name: "lastName",
message: "What is the employee's last name?",
validate: (lastName) => {
if (lastName) {
return true;
} else {
console.log("Please enter last name");
return false;
}
},
},
])
.then((answer) => {
const newEmployee = [answer.firstName, answer.lastName];
//Prompt user to select role for new employee
const roleNewEmployee = `Select role.id, role.title
FROM role
`;
connection
.promise()
.query(roleNewEmployee)
.then(([rows, fields]) => {
const roles = rows.map(({ id, title }) => ({
name: title,
value: id,
}));
inquirer
.prompt([
{
type: "list",
name: "role",
message: "Select the employee's role:",
choices: roles,
},
])
.then((answer) => {
const role = answer.role;
newEmployee.push(role);
//Prompt user to select manager for new employee
const managerNewEmployee = `SELECT *
FROM employee`;
connection
.promise()
.query(managerNewEmployee)
.then(([rows, fields]) => {
const managers = rows.map(
({ id, first_name, last_name }) => ({
name: first_name + " " + last_name,
value: id,
})
);
inquirer
.prompt([
{
type: "list",
name: "manager",
message: "Select the employee's manager:",
choices: managers,
},
])
.then((answer) => {
const manager = answer.manager;
newEmployee.push(manager);
// Insert the new employee's information into the database
const insertSql = `INSERT INTO employee (first_name, last_name, role_id, manager_id)
VALUES (?, ?, ?, ?)`;
// Call the connection.query() method with the SQL query and the newEmployee array as parameters
connection
.promise()
.query(insertSql, newEmployee)
.then(([rows, fields]) => {
console.log(``);
console.log(
"A new employee has been added to the database"
);
console.log(``);
//Call function viewAllEmployees to see the updated list
viewAllEmployees();
})
.catch((error) => {
console.log(error);
});
});
});
});
})
.catch((error) => {
console.log(error);
});
});
};
// Function to Update Employee Role
const updateEmployeeRole = () => {
// Define SQL query to retrieve employee and role information
let query = `
SELECT e.id, e.first_name, e.last_name, r.id AS role_id, r.title
FROM employee e
INNER JOIN role r ON e.role_id = r.id
INNER JOIN department d ON r.department_id = d.id
GROUP BY e.id, r.id;
`;
// Execute the query using the promise-based method
connection
.promise()
.query(query)
.then((response) => {
// Extract the results array from the response object
const results = response[0];
// Create arrays of employees and roles
let arrayOfEmployees = [];
let arrayOfRoles = [];
// Iterate over each object in the response array and push first name + last name to the array of employees
results.forEach((employee) => {
arrayOfEmployees.push(`${employee.first_name} ${employee.last_name}`);
arrayOfRoles.push({ id: employee.role_id, title: employee.title });
});
// Define SQL query to retrieve a distinct list of role titles
let rolesQuery = `
SELECT DISTINCT title
FROM role
`;
// Execute the roles query using the promise-based method
return connection
.promise()
.query(rolesQuery)
.then((response) => {
// Extract the role titles from the response object
const roleTitles = response[0].map((row) => row.title);
// Prompt user to select what employee has a new role and what is their new role
return inquirer
.prompt([
{
name: "updateEmployee",
type: "list",
message: "Select employee to update role",
choices: arrayOfEmployees,
},
{
name: "updateRole",
type: "list",
message: "Select new role",
choices: roleTitles,
},
])
.then((answer) => {
// Find the employee and role ids based on the user's input
let employeeId, newRoleId;
results.forEach((employee) => {
if (
answer.updateEmployee ===
`${employee.first_name} ${employee.last_name}`
) {
employeeId = employee.id;
}
});
arrayOfRoles.forEach((role) => {
if (answer.updateRole === role.title) {
newRoleId = role.id;
}
});
// Update the employee role in the database
let query = `
UPDATE employee
SET role_id = ?
WHERE id = ?
`;
return connection.promise().query(query, [newRoleId, employeeId]);
})
.then(() => {
console.log(``);
console.log(chalk.rgb(255, 105, 180)("Employee role updated!"));
console.log(``);
userChoices();
})
.catch((error) => {
console.error(error);
});
});
});
};
// Function to View All ROles
const viewAllRoles = () => {
console.log(``);
console.log(chalk.rgb(255, 105, 180)("Current Roles:"));
console.log(``);
const query = `SELECT r.id, r.title, d.department_name AS department
FROM role r
INNER JOIN department d ON r.department_id = d.id`;
connection
.promise()
.query(query)
.then((response) => {
// Extract the results array from the response object
const results = response[0];
results.forEach((role) => {
console.log(role.title);
});
console.log("");
userChoices();
})
.catch((error) => {
console.log(error);
});
};
// Function to Add Role
const addRole = async () => {
// Select to which department does the new role belong to
const query = `SELECT *
FROM department`;
try {
const response = await connection.promise().query(query);
const arrayOfDepartments = response[0].map(
(department) => department.department_name
);
arrayOfDepartments.push("Create Department");
const answer = await inquirer.prompt([
{
name: "departmentName",
type: "list",
message: "Which department is this new role in?",
choices: arrayOfDepartments,
},
]);
let departmentName;
if (answer.departmentName === "Create Department") {
departmentName = await addDepartment();
} else {
departmentName = answer.departmentName;
}
const newRoleQuestions = [
{
name: "newRole",
type: "input",
message: "What is the name of your new role?",
validate: validate.validateString,
},
{
name: "salary",
type: "input",
message: "What is the salary of this new role?",
validate: validate.validateSalary,
},
];
const roleAnswers = await inquirer.prompt(newRoleQuestions);
const departmentIdSql = `SELECT * FROM department
WHERE department_name = ?`;
const departmentIdResponse = await connection
.promise()
.query(departmentIdSql, departmentName);
const departmentId = departmentIdResponse[0][0].id;
const insertRoleSql = `INSERT INTO role (title, salary, department_id) VALUES (?, ?, ?)`;
await connection
.promise()
.query(insertRoleSql, [
roleAnswers.newRole,
roleAnswers.salary,
departmentId,
]);
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Role successfully created!`));
viewAllRoles();
} catch (error) {
console.log(chalk.whiteBright.bgRed.bold(`An error occurred: ${error}`));
userChoices();
}
};
// Function to View All Departments
const viewAllDepartments = () => {
const query = `SELECT d.id AS id, d.department_name AS department
FROM department d`;
connection
.promise()
.query(query)
.then((response) => {
console.log(``);
console.log(chalk.rgb(255, 105, 180)`All Departments:`);
console.log(``);
console.table(response[0]); // Pass only the rows to console.table
userChoices();
});
};
// Function to Add Department
const addDepartment = async () => {
try {
const answer = await inquirer.prompt([
{
name: "newDepartment",
type: "input",
message: "What is the Department's name?",
validate: validate.validateString,
},
]);
const query = `INSERT INTO department (department_name)
VALUES (?)`;
await connection.promise().query(query, answer.newDepartment);
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Department added!`));
console.log(``);
console.log(chalk.rgb(255, 105, 180).bold(answer.newDepartment));
viewAllDepartments();
return answer.newDepartment;
} catch (error) {
console.log(chalk.whiteBright.bgRed.bold(`An error occurred: ${error}`));
userChoices();
}
};
// Function to Update Employee Manager
const updateEmployeeManager = () => {
let query = `SELECT e.id, e.first_name, e.last_name, e.manager_id
FROM employee e`;
connection
.promise()
.query(query)
.then((response) => {
let arrayOfEmployees = [];
for (const employee of response[0]) {
arrayOfEmployees.push(`${employee.first_name} ${employee.last_name}`);
}
inquirer
.prompt([
{
name: "selectEmployee",
type: "list",
message: " Select employee to update their manager:",
choices: arrayOfEmployees,
},
{
name: "newManager",
type: "list",
message: "Select manager:",
choices: arrayOfEmployees,
},
])
.then((answer) => {
let employeeId, managerId;
response.forEach((employee) => {
if (
answer.selectEmployee ===
`${employee.first_name} ${employee.last_name}`
) {
employeeId = employee.id;
}
if (
answer.newManager ===
`${employee.first_name} ${employee.last_name}`
) {
managerId = employee.id;
}
});
if (validate.isSame(answer.selectEmployee, answer.newManager)) {
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Invalid Manager Selection`));
console.log(``);
userChoices();
} else {
let query = `UPDATE employee
SET employee.manager_id = ?
WHERE employee.id = ?`;
connection
.promise()
.query(query, [managerId, employeeId])
.then(() => {
console.log(``);
console.log(
chalk.rgb(255, 105, 180)(`Employee Manager updated!`)
);
console.log(``);
userChoices();
})
.catch((error) => {
throw error;
});
}
});
})
.catch((error) => {
throw error;
});
};
// Function to View Employees By Department
const viewEmployeesByDepartment = () => {
const query = `SELECT e.first_name, e.last_name, department.department_name AS deparment
FROM employee e
LEFT JOIN role ON e.role_id = role.id
LEFT JOIN department ON role.department_id = department.id`;
connection.query(query, (error, response) => {
if (error) throw error;
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Employees by Department:`));
console.log(``);
console.table(response);
userChoices();
});
};
// Function to Remove Department
const removeDepartment = () => {
const query = `SELECT d.id, d.department_name
FROM department d`;
connection
.promise()
.query(query)
.then((response) => {
const arrayOfDepartments = response[0].map(
(department) => department.department_name
);
return inquirer.prompt([
{
name: "departmentName",
type: "list",
message: "Select Department to be removed:",
choices: arrayOfDepartments,
},
]);
})
.then((answer) => {
const departmentName = answer.departmentName;
const departmentIdSql = `SELECT * FROM department WHERE department_name = ?`;
return connection.promise().query(departmentIdSql, departmentName);
})
.then((response) => {
const departmentId = response[0][0].id;
const deleteDepartmentSql = `DELETE FROM department WHERE id = ?`;
return connection.promise().query(deleteDepartmentSql, departmentId);
})
.then(() => {
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Department removed!`));
return viewAllDepartments();
})
.catch((error) => {
console.log(chalk.whiteBright.bgRed.bold(`An error occurred: ${error}`));
return userChoices();
});
};
//Function to Remove Role
const removeRole = async () => {
try {
const [rows, fields] = await connection
.promise()
.query(`SELECT r.id, r.title FROM role r`);
const arrayOfRoles = rows.map((row) => row.title);
const answer = await inquirer.prompt([
{
name: "roleTitle",
type: "list",
message: "Select Role to be removed:",
choices: arrayOfRoles,
},
]);
const role = rows.find((row) => row.title === answer.roleTitle);
const [result] = await connection
.promise()
.query(`DELETE FROM role WHERE id = ?`, [role.id]);
console.log(``);
console.log(chalk.rgb(255, 105, 180)("Role removed!"));
viewAllRoles();
} catch (error) {
console.error(error);
}
};
// Function to Remove Employee
const removeEmployee = async () => {
try {
const [rows] = await connection
.promise()
.query(`SELECT e.id, e.first_name, e.last_name FROM employee e`);
const arrayOfEmployees = rows.map(
(employee) => `${employee.first_name} ${employee.last_name}`
);
const answer = await inquirer.prompt([
{
name: "selectEmployee",
type: "list",
message: "Select employee to remove:",
choices: arrayOfEmployees,
},
]);
const selectedEmployee = rows.find(
(employee) =>
`${employee.first_name} ${employee.last_name}` === answer.selectEmployee
);
await connection
.promise()
.query(`DELETE FROM employee WHERE id = ?`, [selectedEmployee.id]);
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Employee Removed!`));
viewAllEmployees();
} catch (error) {
console.error(error);
}
};
// Function to View Department Budget
const viewDepartmentBudget = () => {
console.log(``);
console.log(chalk.rgb(255, 105, 180)(`Budget by Department:`));
console.log(``);
const query = `SELECT department_id AS id, department.department_name AS department,
SUM (salary) AS budget
FROM role
INNER JOIN department ON role.department_id = department.id
GROUP BY role.department_id`;
connection.query(query, (error, response) => {
if (error) throw error;
console.table(response);
userChoices();
});
};