-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
339 lines (319 loc) · 10.2 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
const connection = require("./config/connection");
const inquirer = require("inquirer");
const cTable = require("console.table");
const Chalk = require("chalk");
prompt = inquirer.createPromptModule();
connection.connect((error) => {
if (error) throw error;
});
prompt([
{
type: "list",
message: `${Chalk.black.bgCyan(
"Welcome to Employee Tracker. Select continue to begin."
)}`,
choices: ["Continue", "Quit"],
name: "start",
},
]).then((response) => {
switch (response.start) {
case "Continue":
menu();
break;
case "Quit":
return console.log("Restart the application and try again.");
}
});
function menu() {
prompt([
{
name: "choices",
type: "list",
message: `${Chalk.black.bgGreen(
"Which action would you like to perform?"
)}`,
choices: [
"View All Employees",
"View All Roles",
"View All Departments",
"Update Employee Role",
"Add Employee",
"Add Role",
"Add Department",
"Exit",
],
},
]).then((answers) => {
const { choices } = answers;
if (choices === "View All Employees") {
viewAllEmployees();
}
if (choices === "View All Roles") {
viewAllRoles();
}
if (choices === "View All Departments") {
viewAllDepartments();
}
if (choices === "Update Employee Role") {
updateEmployeeRole();
}
if (choices === "Add Employee") {
addEmployee();
}
if (choices === "Add Role") {
addRole();
}
if (choices === "Add Department") {
addDepartment();
}
if (choices === "Exit") {
console.log("Thanks for using Employee Tracker. Until next time.");
connection.end();
}
});
}
// VIEW actions...
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.query(sql, (error, response) => {
if (error) throw error;
console.log(
"------------------------------------------------------------------"
);
console.log(`${Chalk.greenBright("All Employees:\n")}`);
console.table(response);
console.log(
"------------------------------------------------------------------"
);
menu();
});
};
const viewAllRoles = () => {
let sql = `SELECT role.id, role.title, department.department_name AS department
FROM role
INNER JOIN department ON role.department_id = department.id`;
connection.query(sql, (error, response) => {
if (error) throw error;
console.log(
"------------------------------------------------------------------"
);
console.log(`${Chalk.greenBright("List of Roles:\n")}`);
response.forEach((role) => {
console.log(role.title);
});
console.log(
"------------------------------------------------------------------"
);
menu();
});
};
const viewAllDepartments = () => {
let sql = `SELECT department.id AS id, department.department_name AS department FROM department`;
connection.query(sql, (error, response) => {
if (error) throw error;
console.log(
"------------------------------------------------------------------"
);
console.log(`${Chalk.greenBright("List of Departments:\n")}`);
console.table(response);
console.log(
"------------------------------------------------------------------"
);
menu();
});
};
// ADD actions...
const addEmployee = () => {
prompt([
{
type: "input",
name: "firstName",
message: "What is the employee's first name?",
},
{
type: "input",
name: "lastName",
message: "What is the employee's last name?",
},
]).then((answer) => {
const crit = [answer.firstName, 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 }));
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,
}));
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(
"------------------------------------------------------------------"
);
console.log("Employee added successfully!");
viewAllEmployees();
});
});
});
});
});
});
};
const addRole = () => {
const sql = "SELECT * FROM department";
connection.query(sql, (error, response) => {
if (error) throw error;
// Logic to add new dept for the new role...
let deptNamesArray = [];
response.forEach((department) => {
deptNamesArray.push(department.department_name);
});
deptNamesArray.push("Create Department");
prompt([
{
name: "departmentName",
type: "list",
message: "Which department will you add this role to?",
choices: deptNamesArray,
},
]).then((answer) => {
if (answer.departmentName === "Create Department") {
this.addDepartment();
} else {
addRoleResume(answer);
}
});
const addRoleResume = (departmentData) => {
prompt([
{
name: "newRole",
type: "input",
message: "What is the name of your new role?",
},
{
name: "salary",
type: "input",
message: "What is the salary of this new role?",
},
]).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(
"------------------------------------------------------------------"
);
console.log("Role created successfully!");
viewAllRoles();
});
});
};
});
};
const addDepartment = () => {
prompt([
{
name: 'newDepartment',
type: 'input',
message: 'Enter the name of the new department.'
}
])
.then((answer) => {
let sql = `INSERT INTO department (department_name) VALUES (?)`;
connection.query(sql, answer.newDepartment, (error, response) => {
if (error) throw error;
console.log(
"------------------------------------------------------------------"
);
console.log(answer.newDepartment + " department added successfully!");
viewAllDepartments();
});
});
};
// UPDATE action...
const updateEmployeeRole = () => {
let employeesArray = []
connection.query(
`SELECT first_name, last_name FROM employee`,
(err, res) => {
if (err) throw err;
prompt([
{
type: "list",
name: "employee",
message: "Which employee has a new role?",
choices() {
res.forEach(employee => {
employeesArray.push(`${employee.first_name} ${employee.last_name}`);
});
return employeesArray;
}
},
{
type: "input",
name: "role",
message: `Enter the new role ID from the choices below.${Chalk.greenBright('\nDesigner: 1\nSenior Designer: 2\nPresident: 3\nIntern: 4\nConsultant: 5\nPress: 6\nTemp: 7\n' + Chalk.cyan('Your Answer: '))}`
}
]).then( (answers) => {
const updateEmployeeRole = answers.employee.split(' ');
const updateEmployeeRoleFirstName = JSON.stringify(updateEmployeeRole[0]);
const updateEmployeeRoleLastName = JSON.stringify(updateEmployeeRole[1]);
connection.query(
`UPDATE employee
SET role_id = ${answers.role}
WHERE first_name = ${updateEmployeeRoleFirstName}
AND last_name = ${updateEmployeeRoleLastName}`,
(err, res) => {
if (err) throw err;
console.log(
"------------------------------------------------------------------"
);
console.log("Employee role updated successfully!");
viewAllEmployees();
}
);
});
}
);
};