-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
305 lines (280 loc) · 10.3 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
// Import packages
const inquirer = require('inquirer');
const host = "http://localhost:3001";
const cTable = require('console.table');
var departmentChoiceArray = [];
const logo = require('asciiart-logo');
const config = require('./package.json');
// User is prompted with what they want to select
const openingPrompt = async () => {
// GET fetch is made to constantly retrieve the updated database information every prompt- for when new departments, roles, employees are made or any updated information
const updateArrays = await fetch(`${host}/api/queries`,
{
method: 'GET',
});
const json = await updateArrays.json();
const rolesArray = checkRoleTitles(json.dataRol);
const departmentArray = checkDepartmentNames(json.dataDep);
const employeesArray = json.dataEmp;
// User is prompted with selections
const inq = await inquirer.prompt([{
type: "list",
message: "What would you like to do?",
name: "selectionPrompt",
choices: ["View All Departments", "View All Roles", "View All Employees", "Add A Department", "Add A Role", "Add An Employee", "Update An Employee Role", "Quit"]
}]);
if(inq.selectionPrompt === "Quit") {
console.log("Thank you for using the Employee Tracker, goodbye.");
return;
} else {
promptChecker(inq.selectionPrompt, rolesArray, departmentArray, employeesArray);
};
};
// Utility function to remove any falsey items
const checkRoleTitles = (array) => {
let newArray = [];
for(let i = 0; i < array.length; i++) {
if(array[i]) {
newArray.push(array[i]);
};
};
return newArray;
};
// Utility function to remove any duplicates for departments
const checkDepartmentNames = (array) => {
let newArray = [];
for(let i = 0; i < array.length; i++){
if(newArray.includes(array[i]) === false) {
newArray.push(array[i]);
};
};
return newArray;
};
// Function that chooses what selection was made in the prompt
const promptChecker = (selection, rolesArray, departmentArray, employeesArray) => {
const removedDuplicates = removeDuplicates(rolesArray);
if(selection === "View All Departments") {
viewAllDepartments();
} else if(selection === "View All Roles") {
viewAllRoles();
} else if(selection === "View All Employees") {
viewAllEmployees();
} else if(selection === "Add A Department") {
addDepartment();
} else if(selection === "Add A Role") {
addRole(departmentArray);
} else if(selection === "Add An Employee") {
addEmployee(removedDuplicates);
} else if(selection === "Update An Employee Role") {
updateEmployeeRole(employeesArray, removedDuplicates);
};
};
// Function that removes duplicates for roles
const removeDuplicates = (array) => {
let newArray = [];
for(let i = 0; i < array.length; i++){
if(newArray.includes(array[i]) === false) {
newArray.push(array[i]);
};
};
return newArray;
};
// Async function that makes a GET request to the database to view the departments table information
const viewAllDepartments = async () => {
try {
const result = await fetch(`${host}/api/departments`, {
method: 'GET',
});
const json = await result.json();
console.table(json.data);
} catch(err) {
console.log(err);
};
openingPrompt();
};
// Async function to call a GET request to the database to view the roles table information
const viewAllRoles = async () => {
try {
const result = await fetch(`${host}/api/roles`, {
method: 'GET',
});
const json = await result.json();
console.table(json.data);
} catch(err) {
console.log(err);
};
openingPrompt();
};
// Async function that send a GET request to view the employees table information from the database
const viewAllEmployees = async () => {
try{
const result = await fetch(`${host}/api/employees`, {
method: 'GET',
});
const json = await result.json();
console.table(json.data);
} catch(err) {
console.log(err);
}
openingPrompt();
};
// Async function that sends a POST request, adding in a new department to the department table in the database
const addDepartment = async () => {
var returnNewDepartment;
try {
const inq = await inquirer.prompt([{
type: "input",
message: "What is the name of the department you would like to add?",
name: "newDepartment"
}]);
const newDepartment = await inq;
console.log(`The new department of ${newDepartment.name} has been added to the database.`);
const result = await fetch(`${host}/api/departments`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify(newDepartment),
});
const json = await result.json();
returnNewDepartment = json.data;
} catch(err) {
console.log(err);
}
departmentChoiceArray.push(returnNewDepartment);
openingPrompt();
};
// Async function that calls a POST request to add a new role to the roles table in the database
const addRole = async (departmentsArray) => {
try {
const inq = await inquirer.prompt([{
type: "input",
message: "What is the name of the new role?",
name: "newRoleName"
}, {
type: "number",
message: "What is the salary of the new role?",
name: "newRoleSalary"
}, {
type: "list",
message: "Which department does the new role belong to?",
choices: departmentsArray,
name: "newRoleDepartment"
}]);
const {newRoleName, newRoleSalary, newRoleDepartment} = await inq;
console.log(`The new role ${newRoleName} with the salary of $${newRoleSalary} within the department ${newRoleDepartment} has been added to the database.`);
const result = await fetch(`${host}/api/roles`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({newRoleName, newRoleSalary, newRoleDepartment, departmentsArray})
});
const json = await result.json();
} catch(err) {
console.log(err);
};
openingPrompt();
};
// Async function that makes a POST request to add an employee to the employees table in the database
const addEmployee = async (rolesArray) => {
try {
const inq = await inquirer.prompt([{
type: "input",
message: "What is the new employees first name?",
name: "newEmployeeFirstName"
}, {
type: "input",
message: "What is the new employees last name?",
name: "newEmployeeLastName"
}, {
type: "list",
message: "What is the new employees role?",
choices: rolesArray,
name: "newEmployeeRole"
}, {
type: "list",
message: "Who is the new employees manager?",
choices: ["Isabella Stefan", "Cyrus Sigal", "Jessica Urbonas", "Gary Ryan"],
name: "newEmployeeManager"
}]);
const {newEmployeeFirstName, newEmployeeLastName, newEmployeeRole, newEmployeeManager} = await inq;
console.log(`New Employee: ${newEmployeeFirstName} ${newEmployeeLastName} with the role of ${newEmployeeRole} and manager ${newEmployeeManager} has been added to the database.`);
const newEmployeeManagerId = await checkNewEmployeeManager(newEmployeeManager);
const result = await fetch(`${host}/api/employees`, {
method: 'POST',
headers: {
'Content-type': 'application/json',
},
body: JSON.stringify({newEmployeeFirstName, newEmployeeLastName, newEmployeeRole, newEmployeeManagerId, rolesArray})
});
const json = await result.json();
} catch(err) {
console.log(err);
};
openingPrompt();
};
// Utility function that checks for employee manager and returns their id
const checkNewEmployeeManager = (newEmployeeManager) => {
if(newEmployeeManager === "Isabella Stefan") {
return 1;
} else if(newEmployeeManager === "Cyrus Sigal") {
return 4;
} else if(newEmployeeManager === "Jessica Urbonas") {
return 6;
} else if(newEmployeeManager === "Gary Ryan") {
return 8;
};
};
// Async function that updates an employees role and adds the new information into the database
const updateEmployeeRole = async (employeesArray, rolesArray) => {
try {
const inq = await inquirer.prompt([{
type: "list",
message: "Which employee do you want to update?",
choices: employeesArray,
name: "updateEmployeeName"
}, {
type: "list",
message: "Which role do you want to assign to the updated employee?",
choices: rolesArray,
name: "updatedEmployeeRole"
}]);
const {updateEmployeeName, updatedEmployeeRole} = await inq;
console.log(`The employee ${updateEmployeeName} updated role to ${updatedEmployeeRole} has been updated in the database.`);
const result = await fetch(`${host}/api/employees`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({updateEmployeeName, updatedEmployeeRole, rolesArray})
});
const json = await result.json();
} catch(err) {
console.log(err);
};
openingPrompt();
};
// Init function that sends an ascii logo in the console and prompts the user with selection options
const init = () => {
console.log(
logo({
name: 'Employee Tracker',
font: 'Shadow',
lineChars: 10,
padding: 2,
margin: 3,
borderColor: 'grey',
logoColor: 'white',
textColor: 'green',
})
.emptyLine()
.right('Backend Tech Used: Node.js, Express.js, MySql')
.emptyLine()
.center("By Ben Smerd")
.render()
);
openingPrompt();
};
// Initialise the init function
init();