-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathemployeeTracker.js
381 lines (370 loc) · 10 KB
/
employeeTracker.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
var mysql = require("mysql");
var inquirer = require("inquirer");
const cTable = require("console.table");
let deptArr = [];
let roleArr = [];
let emplArr = [];
let managerArr = [];
// create the connection information for the sql database
var connection = mysql.createConnection({
host: "localhost",
// Your port; if not 3306
port: 3306,
// Your username
user: "root",
// Your password
password: "password",
database: "employee_DB",
});
const mainMenu = [
{
type: "list",
name: "firstChoice",
message: "What would you like to do?",
choices: [
"Add Employee",
"Add Role",
"Add Department",
"View All Employees",
"View All Employees By Role",
"View All Employees By Department",
"View All Roles",
"View All Departments",
"Update An Employee Role",
"Exit",
],
},
];
// connect to the mysql server and sql database
connection.connect(function (err) {
if (err) throw err;
console.log("\n WELCOME TO EMPLOYEE TRACKER \n");
// run the start function after the connection is made to prompt the user
init();
});
// Offer main menu then prompt next function based on response
function init() {
inquirer.prompt(mainMenu).then((response) => {
switch (response.firstChoice) {
case "Add Employee":
employee();
break;
case "Add Role":
role();
break;
case "Add Department":
department();
break;
case "View All Employees":
viewEmployees();
break;
case "View All Employees By Role":
viewByRole();
break;
case "View All Employees By Department":
viewByDepartment();
break;
case "View All Roles":
viewRoles();
break;
case "View All Departments":
viewDepartments();
break;
case "Update An Employee Role":
updateEmployee();
break;
case "Exit":
connection.end();
break;
default:
connection.end();
}
});
// update arrays each time the init function is called
getDepts();
getRoles();
getManagers();
}
// Functions to update Employee, Department and Role arrays
// Get all departments
function getDepts() {
connection.query(`SELECT department_name FROM department`, function (
err,
departments
) {
if (err) throw err;
deptArr = [];
for (i = 0; i < departments.length; i++) {
deptArr.push(departments[i].department_name);
}
// console.log(deptArr);
});
}
// Get all roles
function getRoles() {
connection.query(`SELECT title FROM role`, function (err, roles) {
if (err) throw err;
roleArr = [];
for (i = 0; i < roles.length; i++) {
roleArr.push(roles[i].title);
}
// console.log(roleArr);
});
}
// Get all potential managers by last name
function getManagers() {
connection.query(`SELECT employee.last_name FROM employee`, function (
err,
managers
) {
if (err) throw err;
emplArr = [];
for (i = 0; i < managers.length; i++) {
managerArr.push(managers[i].last_name);
}
// console.log(managerArr);
});
}
// Functions to execute main menu selections
// Add Employee
function employee() {
connection.query("SELECT * FROM role", function (err, res) {
if (err) throw err;
connection.query("SELECT * FROM employee", function (err, res2) {
if (err) throw err;
inquirer
.prompt([
{
name: "first_name",
type: "input",
message: "What is the employee's first name?",
},
{
name: "last_name",
type: "input",
message: "What is the employee's last name?",
},
{
name: "roleName",
type: "list",
message: "What is the employee's role?",
choices: roleArr,
},
{
name: "managerName",
type: "list",
message: "Who is this employee's Manager?",
choices: managerArr,
},
])
.then(function (answer) {
let roleID;
for (let r = 0; r < res.length; r++) {
if (res[r].title == answer.roleName) {
roleID = res[r].role_id;
}
}
let managerID;
for (let m = 0; m < res2.length; m++) {
if (res2[m].last_name == answer.managerName) {
managerID = res2[m].employee_id;
}
}
// when finished prompting, insert a new item into the db with that info
connection.query(
"INSERT INTO employee SET ?",
{
first_name: answer.first_name,
last_name: answer.last_name,
role_id: roleID,
manager_id: managerID,
},
function (err) {
if (err) throw err;
}
);
init();
});
});
});
}
// Add Role
function role() {
connection.query("SELECT * FROM department", function (err, res) {
if (err) throw err;
inquirer
.prompt([
{
name: "title",
type: "input",
message: "What is your role title?",
},
{
name: "salary",
type: "input",
message: "What is the salary for this role?",
default: "0.00",
},
{
name: "departmentName",
type: "list",
message: "What is your department is this role in?",
choices: deptArr,
},
])
.then(function (answer) {
// set corresponding department ID to variable
let deptID;
for (let d = 0; d < res.length; d++) {
if (res[d].department_name == answer.departmentName) {
deptID = res[d].department_id;
}
}
// when finished prompting, insert a new item into the db with that info
connection.query(
"INSERT INTO role SET ?",
{
title: answer.title,
salary: answer.salary,
department_id: deptID,
},
function (err) {
if (err) throw err;
}
);
init();
});
});
}
// Add Department
function department() {
inquirer
.prompt([
{
name: "department",
type: "input",
message: "What is your department name?",
},
])
.then(function (answer) {
// when finished prompting, insert a new item into the db with that info
connection.query(
"INSERT INTO department SET ?",
{
department_name: answer.department,
},
function (err) {
if (err) throw err;
}
);
init();
});
}
// View all employees by department
function viewByDepartment() {
connection.query(
`SELECT employee.employee_id, employee.first_name, employee.last_name, department.department_name FROM employee
LEFT JOIN role ON employee.role_id = role.role_id
LEFT JOIN department ON role.department_id = department.department_id
ORDER BY department.department_name`,
function (err, data) {
if (err) throw err;
console.table(data);
init();
}
);
}
// View all employees by role
function viewByRole() {
connection.query(
`SELECT employee.employee_id, employee.first_name, employee.last_name, role.title, role.salary, department.department_name FROM employee
LEFT JOIN role ON employee.role_id = role.role_id
LEFT JOIN department ON role.department_id = department.department_id
ORDER BY role.title`,
function (err, data) {
if (err) throw err;
console.table(data);
init();
}
);
}
// View all roles
function viewRoles() {
connection.query(`SELECT * FROM role`, function (err, data) {
if (err) throw err;
console.table(data);
init();
});
}
// View all departments
function viewDepartments() {
connection.query(`SELECT * FROM department`, function (err, data) {
if (err) throw err;
console.table(data);
init();
});
}
// View all employees
function viewEmployees() {
connection.query(
`SELECT employee.employee_id, employee.first_name, employee.last_name, role.title,
department.department_name AS department,role.salary,CONCAT(a.first_name, " ", a.last_name) AS manager
FROM employee
LEFT JOIN role ON employee.role_id = role.role_id
LEFT JOIN department ON role.department_id = department.department_id
LEFT JOIN employee a ON a.employee_id = employee.manager_id`,
function (err, data) {
if (err) throw err;
console.table(data);
init();
}
);
}
// Update an employee
function updateEmployee() {
connection.query(
`SELECT concat(employee.first_name, ' ' , employee.last_name) AS Name FROM employee`,
function (err, employees) {
if (err) throw err;
emplArr = [];
for (i = 0; i < employees.length; i++) {
emplArr.push(employees[i].Name);
}
connection.query("SELECT * FROM role", function (err, res2) {
if (err) throw err;
inquirer
.prompt([
{
name: "employeeChoice",
type: "list",
message: "Which employee would you like to update?",
choices: emplArr,
},
{
name: "roleChoice",
type: "list",
message: "What is the employee's new role?",
choices: roleArr,
},
])
.then(function (answer) {
let roleID;
for (let r = 0; r < res2.length; r++) {
if (res2[r].title == answer.roleChoice) {
roleID = res2[r].role_id;
}
}
// when finished prompting, update the db with that info
connection.query(
`UPDATE employee SET role_id = ? WHERE employee_id = (SELECT employee_id FROM(SELECT employee_id FROM employee WHERE CONCAT(first_name," ",last_name) = ?)AS NAME)`,
[roleID, answer.employeeChoice],
function (err) {
if (err) throw err;
}
);
init();
});
});
}
);
}