-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsupervisor.js
266 lines (237 loc) · 8.06 KB
/
supervisor.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
/*Environment Variables & NPM Packages
-------------------------------------------------------------------------------
*/
//add code to read and set any environment variables with the dotenv package:
require("dotenv").config();
//Add other node packages
var mysql = require("mysql");
var inquirer = require("inquirer");
var chalkPipe = require('chalk-pipe');
const { table, getBorderCharacters } = require('table');
//MySQL Database Connection
const connection = mysql.createConnection({
host: process.env.MYSQL_HOST,
port: process.env.MYSQL_PORT,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
});
/*END Environment Variables & NPM Packages
-------------------------------------------------------------------------------
*/
// connect to the mysql server and sql database
connection.connect(function (err) {
if (err) throw err;
// run the start function after the connection is made to prompt the user
start();
});
function start() {
inquirer
.prompt([
{
type: 'list',
name: 'menuOptions',
message: 'Supervisor Menu Options:',
choices: [
'View Sales by Department',
'Create New Department'
]
}
])
.then(answers => {
//console.log(JSON.stringify(answers, null, ' '));
switch (answers.menuOptions) {
case "View Sales by Department":
str = "List of Sales by Department:";
displayDeptProfit(str);
break;
case "Create New Department":
//construct inquirer questions
var questions = [
{
type: 'input',
name: 'departmentName',
message: "Department Name:"
},
{
type: 'input',
name: 'displayName',
message: "Display Name:"
},
{
type: 'input',
name: 'overheadCosts',
message: "Overhead Costs:",
validate: function (value) {
var n = value;
var regex = /^[1-9]\d*((\.\d{0,2})?)$/;
if (regex.test(n)) {
return true;
}
return (chalkPipe('red.bold')('Please enter a valid amount. Overhead must be a number. Up to 2 decimal places allowd'));
}
},
{
type: 'input',
name: 'comments',
message: "Comments:"
}
]; //end questions array
/* Start Questions via inquirer NPM package */
//customer questions via inquirer
inquirer.prompt(questions).then(answers => {
console.log(JSON.stringify(answers, null, ' '));
//construct query from object
var qry = "INSERT INTO `bambizon_db`.`departments`(";
var qry1 = "";
for (var i = 0; i < Object.keys(answers).length; i++) {
qry += "`" + Object.keys(answers)[i] + "`,";
qry1 += "'" + Object.values(answers)[i] + "',";
} //end loop
//strip last character of query & complete remaining query
qry1 = qry1.slice(0, -1) + ",'Kent Supervisor');";
qry = qry.slice(0, -1) + ",`recAddedBy`) VALUES (" + qry1;
//console.log(qry);
addDepartment(qry);
});
break;
default:
console.log("Default");
}
});
} //end start function
function addDepartment(sql) {
console.log(chalkPipe('cyan')("Adding Product...\n"));
console.log(sql);
connection.query(sql, function (err, res) {
if (err) throw err;
console.log("New Department Added");
console.log("-----------------------------------");
readDepartments();
});
} //end addProduct function
function readDepartments() {
/* These variables are required by the NPM Tables library */
let config,
data,
output;
/* Create array to hold table rows. but we can use the data variable created needed by the NPM Table library
* It is an array in an array. The first row is the header and it is predefined/hard-coded here.
* Also data is required by
/*/
data = [['ID', 'Department', 'Display Name', 'Overhead Costs', 'Comments']];
/* Configure table
* This configuration allows us to wrap text in each cell as well as truncate long data.
*/
config = {
columns: {
0: {
width: 3
},
1: {
width: 20,
wrapWord: true
},
2: {
width: 35,
wrapWord: true
},
3: {
width: 15
}
},
4: {
width: 35,
truncate: 50,
wrapWord: true
}
};
/* Create connection string to mySQL Database to return list of all Departments */
connection.query(
"SELECT `departmentID`, `departmentName`, `displayName`, format(`overheadCosts`,2) as `Overhead Costs`, `comments` as `Comments` FROM `departments` ORDER BY `departmentID`;",
function (err, res) {
if (err) throw err;
/* The data is return as an array of objects, but we need an array of rows in an array.
* Loop thru the array of object (in this case call 'res')
* Then we will push each row into it's own array.
* This statement would give me a particular field from the query: console.log("Product Name: " +res[i].productName);
*/
for (var i = 0; i < res.length; i++) {
//set up our empty row array
var row = [];
//loop through the array
for (keys in res[i]) {
//push each item in the array into the row array
row.push(res[i][keys]);
}
//after the loop is finished, push each row array into the data array.
data.push(row);
}
//populate the output variable and output as a table;
output = table(data, config);
console.log("Department Added:");
console.log(output);
connection.end();
});
}
function displayDeptProfit(str) {
console.log(str);
/* These variables are required by the NPM Tables library */
let config,
data,
output;
/* Create array to hold table rows. but we can use the data variable created needed by the NPM Table library
* It is an array in an array. The first row is the header and it is predefined/hard-coded here.
* Also data is required by
/*/
data = [['ID', 'Department', 'Overhead Costs', 'Product Sales', 'Total Profit']];
/* Configure table
* This configuration allows us to wrap text in each cell as well as truncate long data.
*/
config = {
columns: {
0: {
width: 3
},
1: {
width: 35,
wrapWord: true
},
2: {
minwidth: 15
},
3: {
minwidth: 15
},
4: {
minwidth: 15
}
}
};
/* Create connection string to mySQL Database to return list of all Departments */
connection.query(
"SELECT p.`departmentID`,d.`displayName`,d.`overheadCosts`,sum(p.`productSales`) as `productSales`,sum(p.`productSales`) - d.`overheadCosts` as `totalProfit`FROM `products` p INNER JOIN `departments` d ON p.`departmentID` = d.`departmentID` GROUP BY p.`departmentID`, d.`departmentName` ORDER BY `totalProfit`;",
function (err, res) {
if (err) throw err;
/* The data is return as an array of objects, but we need an array of rows in an array.
* Loop thru the array of object (in this case call 'res')
* Then we will push each row into it's own array.
* This statement would give me a particular field from the query: console.log("Product Name: " +res[i].productName);
*/
for (var i = 0; i < res.length; i++) {
//set up our empty row array
var row = [];
//loop through the array
for (keys in res[i]) {
//push each item in the array into the row array
row.push(res[i][keys]);
}
//after the loop is finished, push each row array into the data array.
data.push(row);
}
//populate the output variable and output as a table;
output = table(data, config);
console.log(output);
connection.end();
});
}