-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonClient.js
More file actions
383 lines (360 loc) · 11.6 KB
/
bamazonClient.js
File metadata and controls
383 lines (360 loc) · 11.6 KB
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
//initialize dependencies
require("dotenv").config();
var mysql = require("mysql");
var inquirer = require("inquirer");
var colors = require("colors");
var Table = require("cli-table");
//create connection to sql db
var connection = mysql.createConnection({
host: process.env.MYSQL_HOST,
port: 3306,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASS,
database: "bamazon"
})
//on connection, run the menu function
connection.connect(function(err){
if(err) throw err;
menu();
})
//-------------Functions----------------//
/*
menu function runs an inquirer prompt to allow the user to login as a different user type
*/
function menu(){
inquirer.prompt([
{
type: "list",
message: "How do you want to login?\n",
choices: ["Customer", "Manager", "Supervisor", "Exit"],
name: "choice"
}
]).then(function(answer){
//on the different cases, runs the appropriate functions
switch (answer.choice) {
case "Customer":
console.log("Opening customer portal...\n".bgBlue.yellow);
setTimeout(customerMenu, 1500);
break;
case "Manager":
console.log("Opening manager portal...\n".bgGreen.white);
setTimeout(managerMenu, 1500);
break;
case "Supervisor":
console.log("Opening supervisor portal...\n".bgMagenta.grey);
console.log("We're sorry, the supervisor portal is currently under construction.".inverse);
menu();
break;
case "Exit":
console.log("Goodbye!".inverse);
return;
break;
default:
console.log("I'm sorry, I don't understand that command.");
console.log("Goodbye.");
return;
break;
}//end switch statement
})//end inquirer prompt
}//end menu function
/*
customerMenu is the menu that will display if a user chooses the Customer user type
It queries our db and returns all products available to purchase, in a table format
User is prompted to select which item to buy and the quantity. The db is updated
with the new quantity number once the user confirms his/her purchase
*/
function customerMenu(){
var table = new Table({
head: ["ID", "Name", "Department", "Price"]
}) //create table object
//query database and pull back all results
connection.query("SELECT * FROM products", function(err, res){
if (err) throw (err);
for (var i = 0; i < res.length; i++) {
table.push([
res[i].item_ID, res[i].product_name, res[i].department_name, res[i].price
])
}//end for loop to create table
//display talbe to customer
console.log(table.toString());
inquirer.prompt([
{
name: "choice",
type: "input",
message: "Choose which item you would like to buy: ",
validate: function(value){
if(isNaN(value) === false){
return true;
}
return false;
}, //end validate statement
},
{
name: "quant",
type: "input",
message: "How many would you like to buy?",
validate: function(value){
if(isNaN(value) === false){
return true;
}
return false;
}, //end validate statement
}
]).then(function(answer){
var query = "SELECT stock_quantity FROM products WHERE item_ID = ?";
connection.query(query, [answer.choice], function(err, res){
for (var i = 0; i < res.length; i++) {
if(answer.quant > res[i].stock_quantity){
//if the user chooses too many items, send them back to the customer menu
console.log("There isn't enough in stock to accomodate that request.");
setTimeout(customerMenu, 1000);
} else {
//otherwise run the reduceInv function to update the db
console.log("Updating the quantity of item " + answer.choice + "...");
reduceInv(answer.choice, answer.quant, res[i].stock_quantity);
}
}//end for loop
})
})
})//end connection query
}//end customerMenu function
/*
managerMenu displays the options that are available to a manager. On the different cases, run the appropriate
mananger functions
*/
function managerMenu(){
inquirer.prompt([
{
type: "list",
message: "Hey boss, what would you like to do?\n",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Product", "Back to Main Menu", "Exit"],
name: "choice"
}
]).then(function(ans){
switch (ans.choice) {
case "View Products for Sale":
console.log("Items for sale...");
setTimeout(prodList, 1500);
break;
case "View Low Inventory":
console.log("Low inventory...");
setTimeout(lowInv, 1500);
break;
case "Add to Inventory":
console.log("Okay boss, let's add some inventory...");
setTimeout(addInv, 1500);
break;
case "Add New Product":
console.log("Alright, a brand new product!");
setTimeout(addProd, 1500);
break;
case "Back to Main Menu":
menu();
break;
case "Exit":
console.log("Later, jefecito!");
return;
break;
default:
console.log("Sorry boss, I don't recognize that command");
setTimeout(menu, 1000)
break;
}//end switch statement
})//end inquirer prompt
}//end managerMenu function
/*
prodList is a function that queries the db and returns all items from the produts table
*/
function prodList(){
connection.query("SELECT * FROM products", function(err, res){
if (err) throw err;
var table = new Table({
head: ["Item ID", "Product Name", "Price", "Quantity Available"]
});
for (var i = 0; i < res.length; i++) {
table.push([res[i].item_ID, res[i].product_name, res[i].price, res[i].stock_quantity]);
}
console.log(table.toString());
backToMenu();
})//end query
}//end prodList function
/*
lowInv function queries the db and returns all items from the products table,
where the stock_quantity is less than 5. If it is less than five, create a table object
and display the details in the appropriate columns.
*/
function lowInv(){
connection.query("SELECT * FROM products GROUP BY stock_quantity HAVING count(*) < 5", function(err, res){
if (err) throw err;
var table = new Table({
head: ["Item ID", "Product Name", "Price", "Inventory"]
});
for (var i = 0; i < res.length; i++) {
if (res[i].stock_quantity < 5) {
table.push([res[i].item_ID, res[i].product_name, res[i].price, res[i].stock_quantity]);
}
}
if (table.length === 0) {
console.log("No items with a low inventory");
} else {
console.log(table.toString());
}
backToMenu();
})//end query
}//end lowInv function
/*
addInv function updates the stock_quantity of a specific product name
*/
function addInv(){
var table = new Table({
head: ["ID", "Name", "Department", "Price", "Inventory"]
}) //create table object
//build table with all products
connection.query("SELECT * FROM products", function(err, res){
if (err) throw (err);
for (var i = 0; i < res.length; i++) {
table.push([
res[i].item_ID, res[i].product_name, res[i].department_name, res[i].price, res[i].stock_quantity
])
}//end for loop to create table
//display that table to the manager
console.log(table.toString());
inquirer.prompt([
{
type: "input",
message: "What would you like to add?",
name: "id"
},
{
type: "input",
message: "How much inventory would you like to add?",
name: "amount"
}
]).then(function(answer){
connection.query("SELECT stock_quantity FROM products WHERE item_ID = ?", [answer.id], function(err, res){
if(err) throw err;
var newAmount = 0;
for (var i = 0; i <= res.length; i++) {
var newQuant = res[0].stock_quantity;
}
//hold the existing quantity + our added quantity in a variable
newAmount = parseInt(newQuant) + parseInt(answer.amount);
var query = "UPDATE products SET stock_quantity = ? WHERE item_ID = ?";
//update the id chosen with our new quantity amount
connection.query(query, [newAmount, answer.id], function(err, res){
console.log("Added " + answer.amount + " to item number: " + answer.id + ". New inventory: " + newAmount);
backToMenu();
})
})
})//end inquirer prompt
})//end first query to get all prods
}//end addInv function
/*
addProd function allows a manager to add a new product to our product table
Prompts with inquirer to fill in all applicable column data and then use Insert Into
to add the new product to our table
*/
function addProd(){
inquirer.prompt([
{
type: "input",
message: "Enter the name of your product: ",
name: "product"
},
{
type: "input",
message: "Enter product department: ",
name: "department"
},
{
type: "input",
message: "Set the intial price: ",
validate: function(value){
if(isNaN(value) === false){
return true;
}
return false;
}, //end validate statement
name: "price"
},
{
type: "input",
message: "Set the product inventory: ",
validate: function(value){
if(isNaN(value) === false){
return true;
}
return false;
}, //end validate statement
name: "inventory"
}
]).then(function(answers){
connection.query("INSERT INTO products SET ?",
{
product_name: answers.product,
department_name: answers.department,
price: answers.price,
stock_quantity: answers.inventory
},
function(err){
if (err) throw err;
console.log(answers.product + " has been added to the database.");
inquirer.prompt([
{
type: "list",
message: "Any more products to add?\n",
choices: ["Yes", "No"],
name: "choice"
}
]).then(function(choice){
if(choice.choice === "Yes"){
addProd();
} else {
backToMenu();
}
})
})
})//end first inquirer prompt
}//end addProd function
/*
reduceInv function takes 3 parameters and reduces the stock_quantity in our products table
by the amount purchased by the customer.
@id = the value assigned to answer.choice in customerMenu function
@change = value assigned to answer.quant in customerMenu function
*/
function reduceInv(id, change, stock){
var newQuant = stock - change;
var query = "UPDATE products SET stock_quantity = ? WHERE item_ID = ?";
connection.query(query, [newQuant, id], function(err, res){
if(err) throw err;
})//end first connection query
var query2 = "SELECT price FROM products WHERE item_ID = ?";
connection.query(query2, [id], function(err, res){
if(err) throw err;
for (var i = 0; i < res.length; i++) {
var total = res[i].price * change;
console.log("Total amount owed: $" + total);
}
backToMenu();
})//end second connection query
}//end custUpdate function
/*
backToMenu function asks the user if he/she wants to return to menu function
*/
function backToMenu(){
inquirer.prompt([
{
type: "list",
message: "Would you like to go back to the main menu?",
choices: ["Yes","No"],
name: "answer"
}
]).then(function(ans){
if (ans.answer === "Yes") {
menu();
}else {
console.log("Goodbye!");
return;
}
})
}//end backToMenu function