-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
534 lines (518 loc) · 19.8 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
// variables function
const connection = require('./config/connection');
const inquirer = require('inquirer');
const chalk = require('chalk');
const figlet = require('figlet');
const validate = require('./utils/validate');
// start title
connection.connect((error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`==========================================================================================`));
console.log(chalk.blueBright.bold(`= =`));
console.log(chalk.blueBright.bold(figlet.textSync('Employees Tracker')));
console.log(chalk.blueBright.bold(`= =`));
console.log(chalk.blueBright.bold(`==========================================================================================`));
promptUser();
});
// choices menu
const promptUser = () => {
inquirer.prompt([
{
name: 'choices',
type: 'list',
message: 'Please select an option:',
choices: [
'View All Employees',
'View All Roles',
'View All Departments',
'View All Employees By Department',
'Update Employee Role',
'Update Employee Manager',
'Add Employee',
'Add Role',
'Add Department',
'Remove Employee',
'Remove Role',
'Remove Department',
'Exit'
]
}
])
.then((answers) => {
const { choices } = answers;
if (choices === 'View All Employees') {
viewAllEmployees();
}
if (choices === 'View All Departments') {
viewAllDepartments();
}
if (choices === 'View All Employees By Department') {
viewEmployeesByDepartment();
}
if (choices === 'Add Employee') {
addEmployee();
}
if (choices === 'Remove Employee') {
removeEmployee();
}
if (choices === 'Update Employee Role') {
updateEmployeeRole();
}
if (choices === 'Update Employee Manager') {
updateEmployeeManager();
}
if (choices === 'View All Roles') {
viewAllRoles();
}
if (choices === 'Add Role') {
addRole();
}
if (choices === 'Remove Role') {
removeRole();
}
if (choices === 'Add Department') {
addDepartment();
}
if (choices === 'Remove Department') {
removeDepartment();
}
if (choices === 'Exit') {
connection.end();
}
});
};
// function view all employees
const viewAllEmployees = () => {
let sql = `SELECT employee.id,
employee.first_name,
employee.last_name,
role.title,
department.department_name AS 'department',
role.salary
FROM employee, role, department
WHERE department.id = role.department_id
AND role.id = employee.role_id
ORDER BY employee.id ASC`;
connection.promise().query(sql, (error, response) => {
if (error) throw error;
console.log(` ` + chalk.blueBright.bold(`All Current Employees:`));
console.table(response);
promptUser();
});
};
// function view roles
const viewAllRoles = () => {
console.log(` ` + chalk.blueBright.bold(`All Current Employee Roles:`));
const sql = `SELECT
role.id,
role.title,
department.department_name AS department
FROM role INNER JOIN department ON role.department_id = department.id`;
connection.promise().query(sql, (error, response) => {
if (error) throw error;
response.forEach((role) => { console.log(role.title); });
promptUser();
});
};
// function view all departments
const viewAllDepartments = () => {
const sql = `SELECT
department.id AS id,
department.department_name AS department FROM department`;
connection.promise().query(sql, (error, response) => {
if (error) throw error;
console.log(` ` + chalk.blueBright.bold(`All Current Departments:`));
console.table(response);
promptUser();
});
};
// function view all employees by department
const viewEmployeesByDepartment = () => {
const sql = `SELECT
employee.first_name,
employee.last_name,
department.department_name AS department
FROM employee
LEFT JOIN role ON employee.role_id = role.id
LEFT JOIN department ON role.department_id = department.id`;
connection.query(sql, (error, response) => {
if (error) throw error;
console.log(` ` + chalk.blueBright.bold(`All Employees by Department:`));
console.table(response);
promptUser();
});
};
// function add new employee
const addEmployee = () => {
inquirer.prompt([
{
type: 'input',
name: 'fistName',
message: "What is the employee's first name?",
validate: addFirstName => {
if (addFirstName) {
return true;
} else {
console.log('Please enter a first name');
return false;
}
}
},
{
type: 'input',
name: 'lastName',
message: "What is the employee's last name?",
validate: addLastName => {
if (addLastName) {
return true;
} else {
console.log('Please enter a last name');
return false;
}
}
}
])
.then(answer => {
const crit = [answer.fistName, answer.lastName]
const roleSql = `SELECT
role.id,
role.title FROM role`;
connection.query(roleSql, (error, data) => {
if (error) throw error;
const roles = data.map(({ id, title }) => ({ name: title, value: id }));
inquirer.prompt([
{
type: 'list',
name: 'role',
message: "What is the employee's role?",
choices: roles
}
])
.then(roleChoice => {
const role = roleChoice.role;
crit.push(role);
const managerSql = `SELECT * FROM employee`;
connection.query(managerSql, (error, data) => {
if (error) throw error;
const managers = data.map(({ id, first_name, last_name }) => ({ name: first_name + " " + last_name, value: id }));
inquirer.prompt([
{
type: 'list',
name: 'manager',
message: "Who is the employee's manager?",
choices: managers
}
])
.then(managerChoice => {
const manager = managerChoice.manager;
crit.push(manager);
const sql = `INSERT INTO employee (first_name, last_name, role_id, manager_id)
VALUES (?, ?, ?, ?)`;
connection.query(sql, crit, (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Employee has been added!`));
viewAllEmployees();
});
});
});
});
});
});
};
// funtion add new role
const addRole = () => {
const sql = 'SELECT * FROM department'
connection.query(sql, (error, response) => {
if (error) throw error;
let deptNamesArray = [];
response.forEach((department) => { deptNamesArray.push(department.department_name); });
deptNamesArray.push('Create Department');
inquirer
.prompt([
{
name: 'departmentName',
type: 'list',
message: 'Which department is this new role in?',
choices: deptNamesArray
}
])
.then((answer) => {
if (answer.departmentName === 'Create Department') {
this.addDepartment();
} else {
addRoleResume(answer);
}
});
const addRoleResume = (departmentData) => {
inquirer
.prompt([
{
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
}
])
.then((answer) => {
let createdRole = answer.newRole;
let departmentId;
response.forEach((department) => {
if (departmentData.departmentName === department.department_name) { departmentId = department.id; }
});
let sql = `INSERT INTO role (title, salary, department_id) VALUES (?, ?, ?)`;
let crit = [createdRole, answer.salary, departmentId];
connection.query(sql, crit, (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Role successfully created!`));
viewAllRoles();
});
});
};
});
};
// funtion add new department
const addDepartment = () => {
inquirer
.prompt([
{
name: 'newDepartment',
type: 'input',
message: 'What is the name of your new Department?',
validate: validate.validateString
}
])
.then((answer) => {
let sql = `INSERT INTO department (department_name) VALUES (?)`;
connection.query(sql, answer.newDepartment, (error, response) => {
if (error) throw error;
console.log(chalk.blueBright.bold(answer.newDepartment + `Department successfully created!`));
viewAllDepartments();
});
});
};
// function update employee role
const updateEmployeeRole = () => {
let sql = `SELECT
employee.id,
employee.first_name,
employee.last_name,
role.id AS "role_id"
FROM employee, role,
department WHERE department.id = role.department_id AND role.id = employee.role_id`;
connection.query(sql, (error, response) => {
if (error) throw error;
let employeeNamesArray = [];
response.forEach((employee) => { employeeNamesArray.push(`${employee.first_name} ${employee.last_name}`); });
let sql = `SELECT
role.id, role.title FROM role`;
connection.query(sql, (error, response) => {
if (error) throw error;
let rolesArray = [];
response.forEach((role) => { rolesArray.push(role.title); });
inquirer
.prompt([
{
name: 'chosenEmployee',
type: 'list',
message: 'Which employee has a new role?',
choices: employeeNamesArray
},
{
name: 'chosenRole',
type: 'list',
message: 'What is their new role?',
choices: rolesArray
}
])
.then((answer) => {
let newTitleId, employeeId;
response.forEach((role) => {
if (answer.chosenRole === role.title) {
newTitleId = role.id;
}
});
response.forEach((employee) => {
if (
answer.chosenEmployee ===
`${employee.first_name} ${employee.last_name}`
) {
employeeId = employee.id;
}
});
let sqls = `UPDATE employee SET employee.role_id = ? WHERE employee.id = ?`;
connection.query(sqls, [newTitleId, employeeId], (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Employee Role Updated`));
promptUser();
}
);
});
});
});
};
// function update employee manager
const updateEmployeeManager = () => {
let sql = `SELECT
employee.id,
employee.first_name,
employee.last_name,
employee.manager_id FROM employee`;
connection.query(sql, (error, response) => {
let employeeNamesArray = [];
response.forEach((employee) => { employeeNamesArray.push(`${employee.first_name} ${employee.last_name}`); });
inquirer
.prompt([
{
name: 'chosenEmployee',
type: 'list',
message: 'Which employee has a new manager?',
choices: employeeNamesArray
},
{
name: 'newManager',
type: 'list',
message: 'Who is their manager?',
choices: employeeNamesArray
}
])
.then((answer) => {
let employeeId, managerId;
response.forEach((employee) => {
if (
answer.chosenEmployee === `${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.chosenEmployee, answer.newManager)) {
console.log(chalk.redBright.bold(`Invalid Manager`));
promptUser();
} else {
let sql = `UPDATE employee SET employee.manager_id = ? WHERE employee.id = ?`;
connection.query(sql, [managerId, employeeId], (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Employee Manager Updated`));
promptUser();
}
);
}
});
});
};
// function delete employee
const removeEmployee = () => {
let sql = `SELECT
employee.id,
employee.first_name,
employee.last_name FROM employee`;
connection.query(sql, (error, response) => {
if (error) throw error;
let employeeNamesArray = [];
response.forEach((employee) => { employeeNamesArray.push(`${employee.first_name} ${employee.last_name}`); });
inquirer
.prompt([
{
name: 'chosenEmployee',
type: 'list',
message: 'Which employee would you like to remove?',
choices: employeeNamesArray
}
])
.then((answer) => {
let employeeId;
response.forEach((employee) => {
if (
answer.chosenEmployee ===
`${employee.first_name} ${employee.last_name}`
) {
employeeId = employee.id;
}
});
let sql = `DELETE FROM employee WHERE employee.id = ?`;
connection.query(sql, [employeeId], (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Employee Has Successfully Removed`));
viewAllEmployees();
});
});
});
};
// function delete role
const removeRole = () => {
let sql = `SELECT
role.id,
role.title FROM role`;
connection.query(sql, (error, response) => {
if (error) throw error;
let roleNamesArray = [];
response.forEach((role) => { roleNamesArray.push(role.title); });
inquirer
.prompt([
{
name: 'chosenRole',
type: 'list',
message: 'Which role would you like to remove?',
choices: roleNamesArray
}
])
.then((answer) => {
let roleId;
response.forEach((role) => {
if (answer.chosenRole === role.title) {
roleId = role.id;
}
});
let sql = `DELETE FROM role WHERE role.id = ?`;
connection.query(sql, [roleId], (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Role Has Successfully Removed`));
viewAllRoles();
});
});
});
};
// function delete department
const removeDepartment = () => {
let sql = `SELECT
department.id,
department.department_name FROM department`;
connection.query(sql, (error, response) => {
if (error) throw error;
let departmentNamesArray = [];
response.forEach((department) => { departmentNamesArray.push(department.department_name); });
inquirer
.prompt([
{
name: 'chosenDept',
type: 'list',
message: 'Which department would you like to remove?',
choices: departmentNamesArray
}
])
.then((answer) => {
let departmentId;
response.forEach((department) => {
if (answer.chosenDept === department.department_name) {
departmentId = department.id;
}
});
let sql = `DELETE FROM department WHERE department.id = ?`;
connection.query(sql, [departmentId], (error) => {
if (error) throw error;
console.log(chalk.blueBright.bold(`Department Has Successfully Removed`));
viewAllDepartments();
});
});
});
};