-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
322 lines (279 loc) · 11.4 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
//* Working orginal version */
// Dependencies
const { prompt } = require('inquirer'); // Load the prompt module only from the inquirer package //improves server speed
const cTable = require('console.table');
const connection = require('./config/connection'); // *NOTE: best practice in future - add classes and models rather than loading database //will effect server speed
// Inquirer Prompts to update, view, add, and delete (employees, roles + departments)
const questions = [{
type: 'list',
name: 'action',
message: 'What would you like to do?',
choices: [
'View All Employees',
'View All Roles',
'View All Departments',
'Add New Employee',
'Add New Role',
'Add New Department',
'Update Employee Role',
'Remove Employee',
'Finished']
}];
// Define initialization function // Pass questions object as param
const init = () => {
prompt(questions)
.then((answers) => {
switch (answers.action) {
case 'View All Employees':
searchEmp();
break;
case 'View All Roles':
searchRole();
break;
case 'View All Departments':
searchDept();
break;
case 'Add New Employee':
addEmp();
break;
case 'Add New Role':
addRole();
break;
case 'Add New Department':
addDept();
break;
case 'Update Employee Role':
updateEmpRole();
break;
case 'Remove Employee':
deleteEmp();
break;
case 'Finished':
connection.end();
break;
}
});
}
// Define queries
const queryEmps = 'SELECT * FROM employees;';
const queryRoles = 'SELECT * FROM roles;';
const queryDepts = 'SELECT * FROM departments;';
const queryEmpsInfo = 'SELECT employees.id, employees.first_name, employees.last_name, roles.title, roles.salary, departments.dept FROM employees LEFT JOIN roles ON employees.role_id = roles.id LEFT JOIN departments ON roles.dept_id = departments.id;';
const queryRolesInfo = 'SELECT roles.id, roles.title, roles.salary, departments.dept FROM roles LEFT JOIN departments ON roles.dept_id = departments.id;';
const queryEmpsBasic = 'SELECT id, first_name, last_name FROM employees;';
const queryRoleManagers = 'SELECT id, FROM roles';
const queryEmpChoices = 'SELECT CONCAT(employees.first_name, " ", employees.last_name) AS empName FROM employees;';
const queryAddEmp = 'INSERT INTO employees SET ?;';
const queryAddRole = 'INSERT INTO roles SET ?;';
const queryAddDept = 'INSERT INTO departments SET ?;';
const queryUpdateEmp = 'UPDATE employees SET ? WHERE ?;';
const queryUpdateRole = 'UPDATE roles SET ? WHERE ?;';
const queryDeleteEmp = 'DELETE FROM employees WHERE ?;';
//* Define functions for each switch case *//
const searchEmp = () => {
connection.query(queryEmpsInfo, (err, res) => {
if (err) throw err;
console.log(`\n\n\n
*** Viewing all ${res.length} Employees *** \n`);
console.table(res);
});
init();
};
const searchRole = () => {
connection.query(queryRolesInfo, (err, res) => {
if (err) throw err;
console.log(`\n\n\n
*** Viewing all ${res.length} Roles *** \n`);
console.table(res);
});
init();
};
const searchDept = () => {
connection.query(queryDepts, (err, res) => {
if (err) throw err;
console.log(`\n\n\n
*** Viewing all Departments *** \n`);
console.table(res);
});
init();
};
const addEmp = () => {
connection.query(queryRoles, (err, res) => {
let roleChoices = res.map(function (res) {
return res['title'];
});
let roles = res;
connection.query( `SELECT employees.first_name, employees.last_name, employees.role_id, roles.title FROM employees INNER JOIN roles ON employees.role_id = roles.id WHERE employees.is_mangr=TRUE;`, ( err, res ) => {
let managerChoices = res.map( function ( res ) {
return {name: res.first_name + ' ' + res.last_name+ ": " + res.title,value: res.role_id};
} );
prompt([
{ type: 'input', name: 'newEmpFirst', message: '\n New employee`s first name?' },
{ type: 'input', name: 'newEmpLast', message: 'New employee`s last name?' },
{
type: 'list',
name: 'newEmpRole',
message: 'New employee`s role?',
choices: roleChoices
},
{
type: 'list',
name: 'newRoleManager',
message: 'Employee`s manager?',
choices: managerChoices
}
])
.then( ( answers ) => {
let empRole = roles.find(role => role.title === answers.newEmpRole);
let empRoleManager = roles.find( role => role.id === answers.newRoleManager );
connection.query('INSERT INTO employees SET ?', {
id: empRole.id,
first_name: answers.newEmpFirst,
last_name: answers.newEmpLast,
mangr_id: answers.empRoleManager,
role_id: empRole.id
}, (err, res) => {
if (err) throw err;
console.log( ( '\n\n\n*** New Employee successfully added! ***\n' ) );
searchEmp();
});
});
});
});
};
const addRole = () => {
connection.query(queryDepts, (err, res) => {
let deptChoices = res.map(function (res) {
return res['dept'];
});
prompt([
{ type: 'input', name: 'newRoleTitle', message: 'Name of New Role?' },
{ type: 'input', name: 'newRoleSalary', message: 'Salary of New Role?' },
{
type: 'list',
name: 'newRoleDept',
message: 'Which department is this New Role under?',
choices: deptChoices
}
])
.then((answers) => {
connection.query(queryDepts, (err, res) => {
if (err) throw err;
const departments = res.find(departments => departments.dept === answers.newRoleDept);
connection.query(queryAddRole,
{
title: answers.newRoleTitle,
salary: answers.newRoleSalary,
dept_id: departments.id
}, (err, res) => {
if (err) throw err;
console.table(res);
console.log('\n\n\n*** New Role successfully added! ***\n')
searchRole();
});
});
});
});
};
const addDept = () => {
prompt([
{ type: 'input', name: 'newDept', message: 'Name of New Department?' }
])
.then((answers) => {
connection.query(queryAddDept,
{
dept: answers.newDept
}, (err, res) => {
if (err) throw err;
console.table(res);
console.log('*** New Department successfully added! ***');
searchDept();
});
});
};
const updateEmpRole = () => {
connection.query(queryEmpChoices, (err, res) => {
let empChoices = res.map(function (res) {
return res['empName'];
console.log(empChoices);
});
connection.query('SELECT roles.title FROM roles', (err, res) => {
let roleChoices = res.map(function (res) {
return res['title'];
console.log(roleChoices);
});
prompt([
{
type: 'list', name: 'updateEmp', message: 'Which employee would you like to update?',
choices: empChoices
},
{
type: 'list', name: 'updateRole', message: 'Employee`s new role?',
choices: roleChoices
}
])
.then((answers) => {
connection.query(queryRoles, (err, res) => {
if ( err ) throw err;
let roles = res;
let updatedRole = roles.find(role => role.title === answers.updateRole);
console.log(updatedRole);
connection.query(queryEmps, (err, res) => {
if (err) throw err;
let fullName = answers.updateEmp.split(' ');
const updatedEmp = res.find(employees => employees.first_name + " " + employees.last_name === answers.updateEmp);
console.log(updatedEmp);
connection.query(queryUpdateEmp,
[
{
role_id: updatedRole.id
},
{
id: updatedEmp.id,
}
], (err, res) => {
if (err) throw err;
console.log(err);
// console.table(res);
console.log('\n\n\n*** Employee Role successfully udpated! ***\n')
searchEmp();
}
);
});
});
});
});
});
};
const deleteEmp = () => {
connection.query(queryEmpChoices, (err, res) => {
let empChoices = res.map(function (res) {
return res['empName'];
});
prompt([
{
type: 'list', name: 'deleteEmp', message: 'Which employee would you like to remove?',
choices: empChoices
}
])
.then((answers) => {
connection.query(queryEmpsBasic, (err, res) => {
if (err) throw err;
let fullName = answers.deleteEmp.split(' ');
const employees = res.find(employees => employees.first_name === fullName[0] && employees.last_name === fullName[1]);
connection.query(queryDeleteEmp,
{
id: employees.id
},
(err, res) => {
if (err) throw err;
// console.table(res);
console.log('\n\n\n*** Employee successfully removed ***\n')
searchEmp();
});
});
});
});
};
//Call initialization function
init();