-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
206 lines (202 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
// Declares initial required variables and functions from server.js
const inquirer = require('inquirer');
const { db, viewAllDepartments, viewAllRoles, viewAllEmployees, addDepartment, addRole, addEmployee, updateEmployeeRole } = require('./server');
// Object array with a list of options for the user to interact with the employee_db
const dbActions = [
{
type: 'list',
message: 'What would you like to do?',
name: 'actions',
choices: ["View All Departments", "View All Roles", "View All Employees", "Add a Department", "Add a Role", "Add an Employee", "Update an Employee's Role"]
}
];
// Prompts for the user to enter information when they wish to add a role
const addDeptAction = [
{
type: 'input',
message: 'Please enter a name for this new department.',
name: 'addDept'
}
];
// Runs the initialization function to start the prompt and execute the answers based on user input
function init() {
inquirer.prompt(dbActions)
.then(function (answers) {
switch (answers.actions) {
case "View All Departments":
viewAllDepartments();
setTimeout(init, 1000);
break;
case "View All Roles":
viewAllRoles();
setTimeout(init, 1000);
break;
case "View All Employees":
viewAllEmployees();
setTimeout(init, 1000);
break;
case "Add a Department":
inquirer.prompt(addDeptAction)
.then(function (answers) {
newDeptName = answers.addDept;
addDepartment(newDeptName);
})
.then(() => {
setTimeout(init, 1000);
})
break;
case "Add a Role":
db.query('SELECT * FROM department', function (err, results) {
if (err) {
console.log(err);
} else {
let departments = results.map(({ name, id }) => ({
'value': id,
'name': name
}));
inquirer.prompt([
{
type: 'input',
message: 'Please enter a title for this new role.',
name: 'addTitle'
},
{
type: 'input',
message: 'Please enter a salary amount for this new role.',
name: 'addSalary'
},
{
type: 'list',
message: 'Please choose a department for this new role.',
name: 'addDeptID',
choices: departments
}
])
.then(function (answers) {
newRole = {
title: answers.addTitle,
salary: answers.addSalary,
department_id: answers.addDeptID
};
addRole(newRole);
})
.then(() => {
setTimeout(init, 1000);
})
}
});
break;
case "Add an Employee":
db.query('SELECT role.id AS ID, role.title AS Title FROM role', function (err, results) {
if (err) {
console.log(err);
} else {
let roles = results.map(({ Title, ID }) => ({
'value': ID,
'name': Title
}));
db.query('SELECT employee.id AS ID, employee.first_name AS First_Name, employee.last_name AS Last_Name FROM employee WHERE manager_id IS NULL', function (err, results2) {
if (err) {
console.log(err);
} else {
let managers = results2.map(({ First_Name, Last_Name, ID }) => ({
'value': ID,
'name': `${First_Name} ${Last_Name}`
}));
inquirer.prompt([
{
type: 'input',
message: 'Please enter the first name of the new employee.',
name: 'newFirstName'
},
{
type: 'input',
message: 'Please enter the last name of the new employee.',
name: 'newLastName'
},
{
type: 'list',
message: 'Please choose a role for this new employee.',
name: 'newRole',
choices: roles
},
{
type: 'list',
message: 'Please assign a manager to this new employee if they have one.',
name: 'newManager',
choices: managers
}
])
.then(function (answers) {
newEmployee = {
first_name: answers.newFirstName,
last_name: answers.newLastName,
role_id: answers.newRole,
manager_id: answers.newManager
};
addEmployee(newEmployee);
})
.then(() => {
setTimeout(init, 1000);
})
}
}
)
}
})
break;
case "Update an Employee's Role":
db.query('SELECT employee.id AS ID, employee.first_name AS First_Name, employee.last_name AS Last_Name FROM employee', function (err, results) {
if (err) {
console.log(err);
} else {
let empList = results.map(({ First_Name, Last_Name, ID }) => ({
'value': ID,
'name': `${First_Name} ${Last_Name}`
}));
db.query('SELECT role.id AS ID, role.title AS Title FROM role', function (err, results2) {
if (err) {
console.log(err);
} else {
let roleList = results2.map(({ Title, ID }) => ({
'value': ID,
'name': Title
}));
inquirer.prompt([
{
type: 'list',
message: 'Please select an employee to grant a new position.',
name: 'shiftedEmp',
choices: empList
},
{
type: 'list',
message: 'Please choose a new role for this employee.',
name: 'grantedRole',
choices: roleList
}
])
.then(function (answers) {
updateEmpRole = {
role_id: answers.grantedRole,
id: answers.shiftedEmp,
};
updateEmployeeRole(updateEmpRole);
})
.then(() => {
setTimeout(init, 1000);
})
}
})
}
})
break;
default:
console.log("Error");
setTimeout(init, 1000);
break;
}
});
}
// Function call to initialize app after server.js finishes connecting to the database and showing which port it's running on
setTimeout(init, 1000);