-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
578 lines (503 loc) · 17.3 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
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
const mysql = require("mysql");
const inquirer = require("inquirer");
const asql = require("./Employee");
require("console.table");
let role;
const dbconfig = {
host: 'localhost',
port: 3306,
user: 'root',
password: 'password',
database: 'employees_DB'
};
let db = mysql.createConnection(dbconfig);
let adb = asql.makeDb(dbconfig);
db.connect((err) => {
if (err) {
throw err;
}
console.log(`connected at: ${db.threadId}`);
runSearch()
});
function addEmployees() {
db.query('SELECT title,id, salary, department_id FROM ROLES', function (err, response) {
if (err) {
throw err;
}
//console.log(response);
inquirer.prompt([
{
type: "input",
name: "first_name",
message: "Enter first name of employee:"
}, {
type: "input",
name: "Last_name",
message: "Enter last name of employee:"
}, {
type: "list",
message: "Add employee role:",
name: "role",
choices() {
const choiceArray = [];
response.forEach(({ title }) => {
choiceArray.push(title);
});
//console.log({choiceArray});
return choiceArray;
}
},
])
.then((answer) => {
let chosenItem;
response.forEach((item) => {
if (item.title === answer.role) {
chosenItem = item;
}
});
//console.log("Choice: " + answer);
// console.log("Role: " + answer.role);
//response.forEach(item => {
// console.log("Item: " + item.title + " " + item.id);
//})
//console.log("ChosenItem: " + chosenItem.title + " " + chosenItem.id);
const query = `INSERT INTO EMPLOYEE (first_name,Last_name,roles_id) VALUE(?,?,?) `
db.query(
query, [answer.first_name, answer.Last_name, chosenItem.id], function (err, res) {
if (err) throw err;
console.table(`${answer.first_name, answer.last_name}added`, res);
runSearch()
})
})
})
//console.log({query})c
}
function addNewRole() {
db.query('SELECT dept_name,id FROM DEPARTMENT', function (err, response) {
if (err) {
throw err;
}
//console.log(response);
inquirer.prompt([
{
type: "input",
message: "Enter title of new role:",
name: "title"
}, {
type: "input",
message: "Enter salary of new role:",
name: "salary"
}, {
type: "list",
message: "Assign new role to an present department:",
name: "department",
choices() {
const choiceArray = [];
response.forEach(({ dept_name }) => {
choiceArray.push(dept_name);
});
//console.log({choiceArray});
return choiceArray;
}
},
])
.then((answer) => {
let chosenItem;
response.forEach((item) => {
if (item.dept_name === answer.department) {
chosenItem = item;
}
});
const query = `INSERT INTO ROLES (title,salary,department_id) VALUE(?,?,?) `
db.query(
query, [answer.title, answer.salary, chosenItem.id], function (err, res) {
if (err) throw err;
console.table(`${answer.title, answer.roles}added`, res);
runSearch()
})
})
})
//console.log({query})c
}
function addNewDepartment() {
db.query('SELECT title,id, salary, department_id FROM ROLES', function (err, response) {
if (err) {
throw err;
}
console.log(response);
inquirer.prompt([
{
type: "input",
message: "Enter new department name:",
name: "dept_name"
}, {
type: "list",
message: "Assign new department to an present roles:",
name: "role",
choices() {
const choiceArray = [];
response.forEach(({ title }) => {
if(title !== null) {
choiceArray.push(title);
}
});
console.log({choiceArray});
return choiceArray;
}
},
])
.then((answer) => {
let chosenItem;
response.forEach((item) => {
if (item.title === answer.role) {
chosenItem = item;
}
});
const query = `INSERT INTO DEPARTMENT (dept_name) VALUE(?) `
db.query(
query, [answer.dept_name, chosenItem.id], function (err, res) {
if (err) throw err;
console.table(`${answer.title, answer.roles}added`, res);
runSearch()
})
})
})
//console.log({query})c
}
function runSearch() {
inquirer.prompt([
{
name: "VeiwEmployee",
type: "list",
message: "Which devision you want to see?",
choices: [
"Veiw all departments.",
"View all employees.",
"Veiw all roles.",
"Veiw all employees by roles.",
"Veiw all employees by manager.",
"Add employee.",
"Add New role.",
"Add New Department.",
"Update employee role.",
"Upadte employee manager.",
"Remove.",
"View total budget of Department.", "End session."]
}])
.then(function (answer) {
switch (answer.VeiwEmployee) {
case "Veiw all departments.":
viewDepartments();
break;
case "View all employees.":
veiwAllEmployees();
break;
case "Veiw all employees by roles.":
viewEmployeeByRoles();
break;
case "Veiw all employees by manager.":
viewEmployeeByManager();
break;
case "Veiw all roles.":
veiwAllRoles();
break;
case "Add employee.":
addEmployees();
break;
case "Add New role.":
addNewRole();
break;
case "Add New Department.":
addNewDepartment();
break;
case "Remove.":
Remove();
break;
case "Update employee role.":
updateEmployeeRole();
break;
case "Upadte employee manager.":
UpadateEmployeesManager();
break;
case "View total budget of Department.":
VeiwTotalBudget();
break;
case "End session.":
db.end();
//adb.end();
break;
default:
console.log("No method mateches with selection.");
break;
}
});
}
function viewDepartments() {
let query = ` SELECT dept_name, department.id, roles.title, roles.salary FROM DEPARTMENT LEFT JOIN roles ON roles.department_id = department.id `;
//console.log({query})
db.query(query, function (err, res) {
if (err) throw err;
//console.log({res})
console.table('Employees By Department', res);
runSearch()
})
}
function veiwAllEmployees() {
let query = `SELECT employee.id, employee.first_name, employee.last_name, roles.title, department.dept_name AS Department, roles.salary,CONCAT(manager.first_name," " ,manager.last_name) AS manager
FROM EMPLOYEE
LEFT JOIN roles ON employee.roles_id = roles.id
LEFT JOIN department ON roles.department_id = department.id
LEFT JOIN employee manager ON manager.id = employee.manager_id`;
//console.log({query})
db.query(query, function (err, res) {
if (err) throw err;
//console.log({res})
console.table('All Employees', res);
runSearch()
})
}
function veiwAllRoles() {
let query = `SELECT roles.id, roles.title, roles.salary, department.dept_name AS Department
FROM ROLES
LEFT JOIN department ON roles.department_id = department.id`
db.query(query, function (err, res) {
if (err) throw err;
//console.log({res})
console.table('All ROLES', res);
runSearch()
})
}
function viewEmployeeByRoles() {
let query = " SELECT roles.title ,roles.salary, employee.id, employee.first_name, employee.last_name ";
query += " FROM EMPLOYEE ";
query += " LEFT JOIN roles ON employee.roles_id = roles.id "
query += " LEFT JOIN department ON roles.department_id = department.id ";
query += " ORDER BY roles.title ";
//console.log({query})
db.query(query, function (err, res) {
if (err) throw err;
//console.log({res})
console.table('Employees By Department', res);
runSearch()
})
}
function viewEmployeeByManager() {
let query = `SELECT CONCAT(m.first_name, ' ', m.last_name) AS Manager, e.id AS EmployeeID, CONCAT(e.first_name, ' ', e.last_name) AS EmployeeName, roles.title AS Role, roles.salary AS Salary,
department.dept_name AS Department
FROM employee e
INNER JOIN employee m ON
e.manager_id = m.id
LEFT JOIN roles ON e.roles_id = roles.id
LEFT JOIN department ON roles.department_id = department.id
ORDER BY Manager`;
//console.log({query})
db.query(query, function (err, res) {
if (err) throw err;
//console.log({res})
console.table('All Employees', res);
runSearch()
})
}
async function updateEmployeeRole() {
const queryResult1 = await adb.query('SELECT first_name,last_name, employee.id FROM EMPLOYEE');
let employeeList = [];
queryResult1.forEach(({ first_name, last_name, id }) => {
employeeList.push({
name: first_name + " " + last_name,
value: {
first_name,
last_name,
id
}
})
});
const queryResult2 = await adb.query('SELECT title, id, salary, department_id FROM ROLES');
let roleList = [];
queryResult2.forEach(({ title, id, salary, department_id }) => {
roleList.push({
name: title,
value: {
title,
id,
salary,
department_id
}
})
});
console.log({ employeeList });
console.log({ roleList });
let answer = await inquirer.prompt([
{
type: "list",
message: "Which employee you want updated?",
name: "employee",
choices: employeeList
}, {
type: "list",
message: "Which role you want to assign to this employee?",
name: "role",
choices: roleList
}
]);
//console.log({answer});
//console.log(`Chosen employee: ${answer.employee.id} ${answer.employee.first_name} ${answer.employee.last_name}`);
//console.log(`Chosen role: ${answer.role.id} ${answer.role.title} ${answer.role.salary}`);
const updateQueryString = `UPDATE EMPLOYEE SET roles_id = ${answer.role.id} WHERE id = ${answer.employee.id}`;
//console.log(`Delete query: ${updateQueryString}`);
const queryResult3 = await adb.query(updateQueryString);
//console.table(`Employee table has been updated`);
runSearch();
}
function Remove() {
inquirer.prompt([
{
type: "list",
message: "What do you want to remove:",
name: "remove",
choices: ["Remove departments", "Remove employees", "Remove roles"]
},
])
.then((answer) => {
switch (answer.remove) {
case "Remove departments":
removeDepartment();
break;
case "Remove employees":
removeEmployees();
break;
case "Remove roles":
removeRoles();
break;
default:
console.log("Response did not match any option.");
}
})
};
async function removeDepartment() {
const queryResult1 = await adb.query('SELECT dept_name, department.id FROM DEPARTMENT');
let departmentList = [];
queryResult1.forEach(({ id, dept_name }) => {
departmentList.push({
name: `${id} ${dept_name}`,
value: {
dept_name,
id
}
})
});
console.log({ departmentList });
let answer = await inquirer.prompt([
{
type: "list",
message: "Which Department you want remove?",
name: "department",
choices: departmentList
}
]);
const removeQueryString = `DELETE FROM DEPARTMENT WHERE id = ${answer.department.id}`;
console.log("Remove query: " + removeQueryString);
const queryResult3 = await adb.query(removeQueryString);
runSearch();
}
async function removeEmployees() {
const queryResult1 = await adb.query('SELECT employee.first_name, employee.last_name, employee.id, employee.roles_id, employee.manager_id FROM EMPLOYEE');
let employeeList = [];
queryResult1.forEach(({ first_name, last_name, id, roles_id, manager_id }) => {
employeeList.push({
name: first_name + " " + last_name,
value: {
first_name,
last_name,
id,
roles_id,
manager_id
}
})
});
console.log({ employeeList });
let answer = await inquirer.prompt([
{
type: "list",
message: "Which employee you want to remove?",
name: "employee",
choices: employeeList
}
]);
const removeQueryString = `DELETE FROM EMPLOYEE WHERE id = ${answer.employee.id}`;
const queryResult3 = await adb.query(removeQueryString);
runSearch();
}
async function removeRoles() {
const queryResult1 = await adb.query('SELECT roles.title, roles.salary, roles.id FROM ROLES');
let rolesList = [];
queryResult1.forEach(({ title, salary, id }) => {
rolesList.push({
name: title,
value: {
title,
salary,
id
}
})
});
console.log({ rolesList });
let answer = await inquirer.prompt([
{
type: "list",
message: "Which role you want to remove?",
name: "roles",
choices: rolesList
}
]);
const removeQueryString = `DELETE FROM ROLES WHERE id = ${answer.roles.id}`;
//console.log("Remove query: " + removeQueryString);
const queryResult3 = await adb.query(removeQueryString);
runSearch();
}
async function UpadateEmployeesManager() {
const queryResult1 = await adb.query('SELECT first_name,last_name, employee.id FROM EMPLOYEE');
let employeeList = [];
queryResult1.forEach(({ first_name, last_name, id }) => {
employeeList.push({
name: first_name + " " + last_name,
value: {
first_name,
last_name,
id
}
})
});
console.log({ employeeList });
// console.log({ roleList });
let answer = await inquirer.prompt([
{
type: "list",
message: "Which employee you want updated?",
name: "employee",
choices: employeeList
}, {
type: "list",
message: "Which manager you want to assign to this employee?",
name: "manager",
choices: employeeList
}
]);
console.log({ answer });
if (answer.manager.id != answer.employee.id) {
const updateQueryString = `UPDATE EMPLOYEE SET manager_id = ${answer.manager.id} WHERE id = ${answer.employee.id}`;
console.log(`Update query: ${updateQueryString}`);
const queryResult3 = await adb.query(updateQueryString);
} else {
console.log("Employee can't be his/her own manager.");
}
runSearch();
}
function VeiwTotalBudget() {
let query = `SELECT SUM(salary) TotalBudget FROM ROLES;`;
db.query(query, function (err, res) {
if (err) throw err;
//console.log({res})
console.table('All Employees', res);
runSearch()
})
}