-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
293 lines (279 loc) · 9.42 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
// Packages ============================
const mysql = require('mysql')
const inquirer = require('inquirer')
const { table } = require('table')
const CFonts = require('cfonts');
// =====================================
// MySQL Connection ====================
const db = mysql.createConnection({
host: 'localhost',
port: '3306',
user: 'root',
password: 'root',
database: 'cms_db'
});
// Connects and checks for error -------
db.connect((err) => {
if (err) throw err;
start();
});
// =====================================
// ASCII ===============================
CFonts.say('Employee Tracker', {
font: 'block',
align: 'left',
colors: ['blueBright', 'whiteBright'],
background: 'transparent',
letterSpacing: 1,
lineHeight: 1,
space: true,
maxLength: '8',
});
// =====================================
// Start ===============================
function start() {
inquirer
.prompt([
{
type: 'list',
message: 'What would you like to do?',
name: 'action',
choices: [
'Add department',
'Add employee',
'Add role',
'View by department',
'View roles',
'View employees',
'Update employee roles',
'Exit'
]
}
]).then((answer) => {
switch (answer.action) {
case 'Add department':
addDepartment();
break;
case 'Add employee':
addEmployee();
break;
case 'Add role':
addRole();
break;
case 'View by department':
viewByDepartment();
break;
case 'View roles':
viewRoles();
break;
case 'View employees':
viewEmployees();
break;
case 'Update employee roles':
updateEmpRoles();
break;
default:
db.end();
break;
}
})
};
// =====================================
// Add department function =============
function addDepartment() {
inquirer
.prompt({
name: 'name',
type: 'input',
message: 'Enter name of new department'
}).then(({ name }) => {
const query = 'insert into department (name) values (?)';
db.query(query, name, (err, res) => {
if (err) throw err;
start();
})
})
}
// =====================================
// Add employee function ===============
function addEmployee() {
db.query(
'select * from employee', (err, empRes) => {
const employees = empRes.map(employee => {
return employee.first_name + ' ' + employee.last_name;
});
db.query(
'select * from role', (err, roleRes) => {
const roles = roleRes.map(role => {
return role.title;
});
inquirer
.prompt([{
name: 'first_name',
type: 'input',
message: 'Enter the new employees first name: '
},
{
name: 'last_name',
type: 'input',
message: 'Enter the new employees last name: '
},
{
name: 'role_id',
type: 'list',
message: 'Choose the new employees role: ',
choices: roles
},
{
name: 'manager_id',
type: 'list',
message: 'Choose the new employees manager',
choices: employees
}
]).then((res) => {
const { first_name, last_name } = res;
const manager = empRes.filter(employee => {
return employee.first_name + ' ' + employee.last_name === res.manager;
})[0];
const role_id = roleRes.filter(role => {
return role.title === res.role;
})[0];
const manager_id = manager ? manager.id : null;
db.query(
'insert into employee set ?',
{ first_name, last_name, role_id, manager_id }, (err, result) => {
if (err) throw err;
}
)
start();
});
}
)
}
)
}
// =====================================
// Add role function ===================
function addRole() {
db.query(
'select * from department', (err, result) => {
if (err) throw err;
const departments = result.map(deparment => department.name);
inquirer
.prompt([
{
name: 'title',
type: 'input',
message: 'Enter title of new role: '
},
{
name: 'salary',
type: 'input',
message: 'Enter the salary of the new role: ',
validate: salary => {
if (isNaN(salary) || salary < 0) {
return 'Please enter a number'
}
return true;
}
},
{
name: 'department',
type: 'list',
message: 'What department is it in?',
choices: departments
}
]).then((res) => {
const { title } = res;
const salary = res.salary;
const department_id = res.department_id;
})[0];
db.query(
'insert into role set ?',
{
title, salary, department_id
},
(err, result) => {
if (err) throw err;
start();
}
)
}
)
}
// =====================================
// View department function ============
function viewByDepartment() {
const query = 'select * from department';
db.query(query, (err, res) => {
if (err) throw err;
console.log(table(toTableFormat(res)));
start();
});
};
// =====================================
// View roles function =================
function viewRoles() {
const query = 'select * from role';
db.query(query, (err, res) => {
if (err) throw err;
console.log(table(toTableFormat(res)));
start();
});
};
// =====================================
// View employees function =============
function viewEmployees() {
const query = 'select * from employee';
db.query(query, (err, res) => {
if (err) throw err;
console.log(table(toTableFormat(res)));
start();
});
};
// =====================================
// Update employee roles ===============
function updateEmpRoles() {
const query = 'select * from employee';
db.query(query, (err, res) => {
const employees = res.map(employee => {
return employee.first_name + ' ' + employee.last_name;
});
db.query('select * from role', (err, result) => {
const roles = result.map(role => {
return role.title;
});
inquirer
.prompt([
{
type: 'list',
name: 'employee',
message: 'Which employee\'s role do you want to update?',
choices: employees
},
{
type: 'list',
name: 'role',
message: 'What\'s the employee\'s new role?',
choices: roles
}
]).then(answer => {
const id = result.filter(employee => {
return employee.first_name + ' ' + employee.last_name === answer.employee;
})[0]
db.query(
'update employee set role_id = ? where id = ?', [role_id, id], (err, result) => {
if (err) throw err;
start();
}
)
})
})
})
}
// =====================================
function toTableFormat(arr) {
const header = Object.keys(arr[0]);
const rows = arr.map(obj => Object.values(obj));
return [header, ...rows];
}