/
app.js
756 lines (705 loc) · 27.7 KB
/
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
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
require('dotenv').config();
var trueFalse = false;
const inquirer = require('inquirer');
const chalk = require('chalk');
const figlet = require('figlet');
const mysql = require("mysql");
const cTable = require('console.table');
const PASSWORD = process.env.PASSWORD;
const USER = process.env.USER;
var connection;
const proc = require('process');
if (process.env.JAWSDB_URL) {
connection = mysql.createConnection(process.env.JAWSDB_URL);
} else {
connection = mysql.createConnection({
host: "localhost",
port: 3306, // Your port; if not 3306
user: USER, // Your username
password: PASSWORD, // Your password
database: "companydb"
});
}
let PID = proc.pid;
var message = "Employee Manager";
let dividerType = "=";
let divider = dividerType.repeat(process.stdout.columns)
let role = [];
let listRoleArray = [];
let listManArray = [];
let listDeptArray = [];
let id;
const headingMsg = function (message) {
console.clear();
let messagelen = message.length;
console.log("Message Length = ",messagelen);
console.log('Terminal size: ' + process.stdout.columns + 'x' + process.stdout.rows);
if (process.stdout.columns > 100) {
console.log(
chalk.yellow.bold(
figlet.textSync(message, { horizontalLayout: 'fitted' })
)
);
} else {
console.log(chalk.yellowBright.bold.underline(message));
}
};
function init() {
console.clear()
//console.log(chalk.greenBright("Server up at PID: " + chalk.blue(PID)));
if (process.stdout.columns > 100) {
console.log(
chalk.blueBright.bold(
figlet.textSync(message, { horizontalLayout: 'fitted' })
)
);
} else {
console.log(chalk.blueBright.bold('Employee Manager'));
console.log(divider);
}
//afterConnection();
//start();
listRole(id)
}
//View All Employees
function viewAllEmp() {
let query = "SELECT employee.id AS Id, employee.first_name AS FirstName, employee.last_name AS LastName, role.title AS Title, department.name AS Department, role.salary AS Salary, CONCAT(manager.first_name, ' ', manager.last_name) AS Manager FROM employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id LEFT JOIN employee manager on manager.id = employee.manager_id;";
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
//console.log(divider.repeat(120));
console.table(res);
})
start();
};
//View All Employees by Department
function empDept(message) {
let query = "SELECT department.name AS Department, employee.id AS Id, employee.first_name AS FirstName, employee.last_name AS LastName, role.title AS Title FROM `employee`, `department`, `role` WHERE role.`department_id`= department.`id` AND employee.role_id=role.id ORDER BY department_id;"
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
})
start();
}
//View All Employees by Manager
function empMan(message) {
let query = "SELECT employee.id AS Id, employee.first_name AS FirstName, employee.last_name AS LastName, role.title AS Title, department.name AS Department, role.salary AS Salary, CONCAT(manager.first_name, ' ', manager.last_name) AS Manager FROM employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id LEFT JOIN employee manager on manager.id = employee.manager_id ORDER BY manager;"
query +=
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
})
start();
}
const checkEmp = function (first_name, last_name) {
let query = "SELECT EXISTS(SELECT * FROM employee WHERE ?);";
//let query = "SELECT * FROM employee;"
const searchEmp = connection.query(query,
{
first_name: first_name,
last_name: last_name
},
function (err, results) {
console.log("Attempting to find a match in the DB to :",first_name, "and", last_name);
results.forEach(el => {
//console.log("Element.first_name: ", el.first_name, "and Element.last_name:",el.last_name);
if (el.first_name === first_name && el.last_name === last_name) {
console.log(chalk.whiteBright("Employee: " + first_name + " " + last_name + " already Present in Database."));
trueFalse = true;
console.log("Line 131 ------->>>>>>The Value of result is: ",results.affectedRows)
}
});
trueFalse = results;
console.log("Line 135 ------->>>>>>The Value of trueFalse is: ",trueFalse.affectedRows)
return trueFalse;
});
console.log("searchEmp: (0/1):", searchEmp.affectedRows);
trueFalse = searchEmp;
console.log("Line 140 ------->>>>>>The Value of trueFalse is: ",results)
return trueFalse;
};
//ADD an Employee
function addEmployee(message, first_name, last_name, role_id, manager_id) {
// based on their answer
headingMsg(message);
role_id = parseInt(role_id);
manager_id = parseInt(manager_id);
console.log("Manager ID: ", manager_id, role_id);
console.log("Preparing to Add a new Employee: " + first_name + " " + last_name);
let trueFalse = checkEmp(first_name, last_name);
console.log("On Line 146 before checkEmp trueFalse",trueFalse.affectedRows);
// if (trueFalse === false) { //if false add to db
//let query = "INSERT INTO employee (first_name, last_name, role_id, manager_id) SELECT * FROM (SELECT ??) AS tmp WHERE NOT EXISTS (SELECT first_name, last_name FROM employee WHERE ??) LIMIT 1;"
let query = "INSERT INTO employee SET ?";
console.log("query: ", query);
connection.query(query,
{
first_name: first_name,
last_name: last_name,
role_id: role_id,
manager_id: manager_id
},
function (err, res) {
if (err) throw err;
console.log(divider);
console.log(res.affectedRows + " new Employee inserted!\n");
// Call updateProduct AFTER the INSERT completes
}
);
console.log("Query: ", query.sql);
console.log("A New Employee:\n");
console.log("addName: ", first_name + " " + last_name);
console.log("\naddRoleId: ", role_id);
console.log("\naddManager_Id: ", manager_id + "\nwas added to the Database.");
start();
// } else {
// console.log("Found Employee in Database. Not added.");
// start();
// }
};
//Delete Employee
const delEmployee = function (message,id, first_name, last_name) {
let query = "SELECT employee.id AS Id, employee.first_name AS FirstName, employee.last_name AS LastName, role.title AS Title, department.name AS Department, role.salary AS Salary, CONCAT(manager.first_name, ' ', manager.last_name) AS Manager FROM employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id LEFT JOIN employee manager on manager.id = employee.manager_id ORDER BY Id;";
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
inquirer.prompt([{
type: "list",
message: "Choose the Employee you wish to Delete",
name: "id",
choices: function () {
const choiceArr = [];
res.forEach(function (index) {
choiceArr.push(`${index.Id} ${index.FirstName} ${index.LastName} ${index.Title} ${index.Department}`);
})
return choiceArr;
}
}])
.then(function (answer) {
//console.log('answer', answer.id);
let idSplit = answer.id.split(" ");
//console.log("IdSplit: ",idSplit);
let id = parseInt(idSplit[0]);
connection.query(
"DELETE FROM employee WHERE ?",
{
id: id
},
function (err, res) {
if (err) throw err;
console.log(res.affectedRows + " Employee deleted!\n");
// Call AFTER the DELETE completes
console.log(divider);
console.table(res);
start();
}
);
})
.catch(error => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
console.log(error);
}
})
})
}
//Add a Role
function addRole(message, title, salary, department_id) {
// based on their answer
headingMsg(message);
console.log("Creating a new Role...\n");
console.log("Role Title: ", title);
department_id = parseInt(department_id); //just added
let query = connection.query(
"INSERT INTO role SET ?",
{
title: title,
salary: salary,
department_id: department_id
},
function (err, res) {
if (err) throw err;
console.log(res.affectedRows + " new Role created!\n");
// Call updateProduct AFTER the INSERT completes
start();
}
);
// logs the actual query being run
console.log(query.sql);
}
//Remove a Role
function delRole(message, id) {
// based on their answer
let query = "SELECT role.id AS Id, role.title AS Title, role.salary AS Salary, role.department_id FROM role;";
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
inquirer.prompt([{
type: "list",
message: "Choose the Role you wish to Delete",
name: "id",
choices: function () {
const choiceArr = [];
res.forEach(function (index) {
choiceArr.push(`${index.Id} ${index.Title}`)
})
return choiceArr;
}
}])
.then(function (answer) {
//console.log('answer', answer.id);
let idSplit = answer.id.split(" ");
//console.log("IdSplit: ",idSplit);
let id = parseInt(idSplit[0]);
connection.query(
"DELETE FROM role WHERE ?",
{
id: id
},
function (err, res) {
if (err) throw err;
console.log(res.affectedRows + " role deleted!\n");
// Call AFTER the DELETE completes
console.log(divider);
console.table(res);
start();
}
);
})
.catch(error => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
console.log(error);
}
})
})
}
// //List Roles for Choice in New Employee
function listRole(id) { // List of Roles from DB
let query = "SELECT role.id AS Id, role.title AS Title, role.salary AS Salary, role.department_id FROM role;";
connection.query(query, function (err, res) {
if (err) throw err;
//console.log("\n");
console.log(divider);
//console.log("Res: ", res)
for (const choice of res) {
listRoleArray.push(`${parseInt(choice.Id)} ${choice.Title}`)
}
//start();// function to start the menu and general program
listMan(id);
})
};
//Add a Department
function addDept(message, name) {
// based on their answer
headingMsg(message);
console.log(divider);
let query = "SELECT department.id AS Id, department.name AS Department FROM department;";
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
})
console.log("Creating a new Department...\n");
console.log("Department name: ", name);
query = connection.query(
"INSERT INTO department SET ?",
{
name: name
},
function (err, res) {
if (err) throw err;
console.log(res.affectedRows + " new Department created!\n");
// Call updateProduct AFTER the INSERT completes
start();
}
);
// logs the actual query being run
console.log(query.sql);
}
// //List Roles for Choice in New Employee
function listDept(id) { // List of Departments from DB
let query = "SELECT department.id AS Id, department.name AS Department FROM department;";
connection.query(query, function (err, res) {
if (err) throw err;
//console.log("\n");
//console.log(divider);
//console.log("Res: ", res)
for (const choice of res) {
listDeptArray.push(`${parseInt(choice.Id)} ${choice.Department}`)
}
//console.log("listDept: ",listDeptArray);
start();// function to start the menu and general program
//listMan(id);
})
};
//View All Departments
function viewDept(message) {
let query = "SELECT department.id AS Id, department.name AS Department FROM department;";
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
})
//listDept(id);
start();
};
//Remove a Department
function delDept(message, id) {
// based on their answer
let idSplit = "";
let query = "SELECT department.id AS Id, department.name AS Department FROM department;";
connection.query(query, function (err, res) {
if (err) throw err;
console.log("\n");
headingMsg(message);
console.log(divider);
console.log("Res: ", res)
inquirer.prompt([{
type: "list",
message: "Choose the Department you wish to Delete",
name: "id",
choices: function () {
const choiceArr = [];
res.forEach(function (index) {
choiceArr.push(`${index.Id} ${index.Department}`)
})
return choiceArr;
}
}])
.then(function (answer) {
//console.log('answer', answer.id);
idSplit = answer.id.split(" ");
//console.log("IdSplit: ",idSplit);
let id = parseInt(idSplit[0]);
connection.query(
"DELETE FROM department WHERE ?",
{
id: id
},
function (err, res) {
if (err) throw err;
console.log(res.affectedRows + " department deleted!\n");
// Call AFTER the DELETE completes
console.log(divider);
console.table(res);
}
);
})
.catch(error => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
console.log(error);
}
})
start();
})
}
//ADD a Manager
function addManager(message, first_name, last_name, role_id, manager_id) {
// based on their answer
headingMsg(message);
role_id = parseInt(role_id);
manager_id = null;
console.log("Manager ID: ", manager_id, role_id);
console.log("Preparing to Add a new Employee: " + first_name + " " + last_name);
//let trueFalse = checkEmp(first_name, last_name);
//console.log("On Line 146 before checkEmp trueFalse",trueFalse.affectedRows);
// if (trueFalse === false) { //if false add to db
//let query = "INSERT INTO employee (first_name, last_name, role_id, manager_id) SELECT * FROM (SELECT ??) AS tmp WHERE NOT EXISTS (SELECT first_name, last_name FROM employee WHERE ??) LIMIT 1;"
let query = "INSERT INTO employee SET ?";
console.log("query: ", query);
connection.query(query,
{
first_name: first_name,
last_name: last_name,
role_id: role_id,
manager_id: manager_id
},
function (err, res) {
if (err) throw err;
console.log(divider);
console.log(res.affectedRows + " new Manager inserted!\n");
// Call updateProduct AFTER the INSERT completes
}
);
console.log("Query: ", query.sql);
console.log("A New Manager:\n");
console.log("addName: ", first_name + " " + last_name);
console.log("\naddRoleId: ", role_id);
console.log("\naddManager_Id: ", manager_id + "\nwas added to the Database.");
start();
// } else {
// console.log("Found Employee in Database. Not added.");
// start();
// }
};
//List Managers for N
function listMan(message) { // List of Roles from DB
let query = "SELECT employee.id AS `Id`, employee.first_name AS FirstName, employee.last_name AS LastName, role.title AS Title, ";
query += "department.name AS Department FROM employee LEFT JOIN role on employee.role_id = role.id LEFT JOIN department on role.department_id = department.id ";
query += "LEFT JOIN employee manager on manager.id = employee.manager_id WHERE employee.manager_id IS NULL;";
connection.query(query, function (err, res) {
if (err) throw err;
//console.log("Line 377 listMan", message);
if (message === "View all Managers") {
headingMsg(message);
console.log(divider);
console.table(res);
};
let idSplit = JSON.stringify(res);
let id = idSplit.Id;
for (const choice of res) {
listManArray.push(`${choice.Id} ${choice.FirstName} ${choice.LastName} ${choice.Title} ${choice.Department}`)
}
//console.log("listManArr: ", listManArray);//<---------------------------Remove Please
listDept(id);
//start();// function to start the menu and general program
})
};
//Start the Menu and Program
const start = function () {
inquirer
.prompt([
{
type: "rawlist",
message: "What would you like to do? \n Choose 'Exit' to close the program.\n",
choices: [
"View ALL Employees",
"View ALL Employees by Department",
"View ALL Employees by Manager",
"View all Managers",
"View all Departments",
"View all Roles",
"Add a Department",
"Add a Role",
"Add Manager",
"ADD an Employee",
"Remove a Department",
"Remove a Role",
"Remove a Manager",
"REMOVE an Employee",
"UPDATE Employee Role",
"Update Employee Manager",
"Update Employee Department",
"Exit"
],
name: "start",
},
{
type: "input",
message: "What is the name of the new Role?",
name: "role",
when: (start) => start.start === "Add a Role",
validate: async (input) => {
if (await input.trim().length === 0) {
return "NOT a valid entry! Please try again.";
} else if (input.match(/^[a-zA-Z]+( [a-zA-Z]+)*$/i)) {
return true;
} else {
return "Not a valid entry!"
}
}
},
{
type: "number",
message: "What is the Salary for that Role(Numbers only)?",
name: "salary",
when: (start) => start.start === "Add a Role",
},
{
type: "rawlist",
message: "What is the Department ID of the new Role?\n(Remember you must have a Department to assign a role.)",
name: "department_id",
when: (start) => start.start === "Add a Role",
choices: listDeptArray,
},
{
type: "input",
message: "What is the name of the new Department?",
name: "name",
when: (start) => start.start === "Add a Department",
validate: async (input) => {
if (await input.trim().length === 0) {
return "NOT a valid entry! Please try again.";
} else if (input.match(/^[a-zA-Z]+( [a-zA-Z]+)*$/i)) {
return true;
} else {
return "Not a valid entry!"
}
}
},
{
type: "input",
message: "What is the Employee's first name?",
name: "first_name",
when: (start) => start.start === "ADD an Employee" || start.start === "Add Manager",
validate: async (input) => {
if (await input.trim().length === 0) {
return "NOT a valid entry! Please try again.";
} else if (input.match(/^[a-zA-Z]+( [a-zA-Z]+)*$/i)) {
return true;
} else {
return "Not a valid entry!"
}
}
},
{
type: "input",
message: "What is the Employee's last name?",
name: "last_name",
when: (start) => start.start === "ADD an Employee" || start.start === "Add Manager",
validate: async (input) => {
if (await input.trim().length === 0) {
return "NOT a valid entry! Please try again.";
} else if (input.match(/^[a-zA-Z]+( [a-zA-Z]+)*$/i)) {
return true;
} else {
return "Not a valid entry!"
}
}
},
{
type: "rawlist",
message: "What is the Employee's role?" ,
name: "role_id",
when: (start) => start.start === "ADD an Employee" || start.start === "Add Manager",
choices: listRoleArray,
},
{
type: "prompt",
message: "Is this Employee a manager?",
name: "manager_id",
when: (start) => start.start === "Add Manager",
//choices: listManArray,
},
{
type: "rawlist",
message: "Who is the Employee's manager?",
name: "manager_id",
when: (start) => start.start === "ADD an Employee",
choices: listManArray,
},
])
.then(function (answer) {
// based on their answer, either call the bid or the post functions
console.log("\n", answer);
message = answer.start;
switch (answer.start) {
case "======================================":
console.clear();
start(message);
break;
case "View ALL Employees":
viewAllEmp(message); //moved to empFunc.js
break;
case "View ALL Employees by Department":
empDept(message);
break;
case "View ALL Employees by Manager":
empMan(message);
break;
case "ADD an Employee":
addEmployee(message, answer.first_name, answer.last_name, answer.role_id, answer.manager_id);
break;
case "REMOVE an Employee":
delEmployee(message, answer.id, answer.first_name, answer.last_name);
break;
case "UPDATE Employee Role":
updateRole(message);
break;
case "Update Employee Manager":
rangeSearch();
break;
case "Update Employee Department":
songSearch();
break;
case "View all Roles":
getRole(message);
break;
case "Add a Role":
addRole(message, answer.role, answer.salary, answer.department_id);
break;
case "Remove a Role":
delRole(message); //Will only need the role primary id but just simplier to copy/paste ATM
break;
case "View all Managers":
listMan(message);
break;
case "Add Manager":
addManager(message, answer.first_name, answer.last_name, answer.role_id, answer.manager_id);
answer.manager_id = null;
break;
case "Remove a Manager":
delManager(message);
break;
case "View all Departments":
viewDept(message);
break;
case "Add a Department":
addDept(message, answer.name);
break;
case "Remove a Department":
delDept(message);
break;
case "Exit":
connection.end();
if (process.stdout.columns > 107) {
console.log(
chalk.yellow(
figlet.textSync('\nTHANK YOU for using \nEmployee Manager!\n', { horizontalLayout: 'fitted' })
)
);
} else {
console.log(chalk.yellowBright.bold.underline('\nTHANK YOU for using \nEmployee Manager!\n'));
}
break;
}
})
.catch(error => {
if (error.isTtyError) {
// Prompt couldn't be rendered in the current environment
} else {
console.log(error);
}
})
};
//DB Connection
connection.connect(function (err) {
if (err) throw err;
console.log("connected as id " + connection.threadId);
// start();
});
//List All Roles
function getRole(message) {
connection.query("SELECT role.id AS Id, role.title AS Title, role.salary AS Salary, role.department_id AS `Department Id` FROM role;", function (err, res) {
if (err) throw err;
//console.log("\n");
headingMsg(message);
console.log(divider);
console.table(res);
start();
})
};
init();