-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazon-customer.js
286 lines (253 loc) · 10.5 KB
/
bamazon-customer.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
// file packaged as a function for export
function bamazonCustomer() {
// variable declarations for required packages and imported files/functions
require("dotenv").config();
let mysql = require("mysql");
let inquirer = require("inquirer");
let colors = require('colors');
let table = require("table");
let Menu = require("./index.js");
// variable declarations for table package
let config,
data,
output;
// declare variable for mysql connection to database
let connection = mysql.createConnection({
host: "localhost",
port: 3306,
// username
user: "root",
// password
password: process.env.MYSQLPASSWORD,
database: "bamazon"
});
// create connection to database and call customer menu
connection.connect(function (err) {
if (err) throw err;
console.log("\033c");
customerMenu();
});
// customer menu
function customerMenu() {
console.log("\033c");
inquirer.prompt([
{
type: "rawlist",
name: "functions",
message: "What would you like to do?",
choices: [
"View products for sale",
"Make a purchase",
"Back to role menu",
"Exit menu"
]
}
]).then(function (userChoice) {
// switch to call customer functions
switch (userChoice.functions) {
case "View products for sale":
displayAllItems();
break;
case "Make a purchase":
purchaseItem();
break;
case "Back to role menu": // end connection and return to main bamazon menu
connection.end();
Menu.bamazonMenu();
break;
case "Exit menu": // leave app
exitMenu();
break;
}
});
}
// function to show all items available to purchase
function displayAllItems() {
console.log("\033c");
// query database for all products in products table
connection.query("SELECT * FROM products", function (err, res) {
// create headings for table package and place that array in the table array
data = [
["Item #".bold, "Product".bold, "Price".bold]
];
// temporary array to hold each product's information
let temp = [];
// loop through each product and push the temporary array into the table array
for (let i = 0; i < res.length; i++) {
temp = [res[i].item_id, res[i].product_name, "$" + res[i].price.toFixed(2)];
data.push(temp);
}
// set up configuration for table package
config = {
columns: {
0: {
alignment: 'left',
minWidth: 10
},
1: {
alignment: 'left',
minWidth: 10
},
2: {
alignment: 'right',
minWidth: 10
}
},
border: {
topBody: `─`,
topJoin: `┬`,
topLeft: `┌`,
topRight: `┐`,
bottomBody: `─`,
bottomJoin: `┴`,
bottomLeft: `└`,
bottomRight: `┘`,
bodyLeft: `│`,
bodyRight: `│`,
bodyJoin: `│`,
joinBody: `─`,
joinLeft: `├`,
joinRight: `┤`,
joinJoin: `┼`
}
};
// print the table
output = table.table(data, config);
console.log(output.white.bgBlue);
console.log("\nPress any key to return to the menu");
// wait until the user presses any key before returning to the customer menu
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.once('data', function () {
customerMenu();
process.exit.bind(process, 0);
});
});
}
// function for user to purchase item
function purchaseItem() {
inquirer.prompt([
// take user input for id and quantity and validate input
{
name: "productId",
message: "Please enter the ID of the product you'd like to purchase",
validate: answer => {
let pass = answer.match(
/^[1-9]\d*$/
);
if (pass) {
return true;
}
return "Please enter a positive number greater than zero.";
}
},
{
name: "quantity",
message: "How many would you like to purchase?",
validate: answer => {
let pass = answer.match(
/^[1-9]\d*$/
);
if (pass) {
return true;
}
return "Please enter a positive number greater than zero.";
}
}
]).then(function (input) {
// query database for item with id matching user input
let query = connection.query("SELECT * FROM products WHERE item_id=?", input.productId, function (err, res) {
// if no results are returned, send user back to re-enter input
if (!res.length) {
console.log("\nInvalid product ID. Please choose again\n");
purchaseItem();
} else {
// otherwise, check to see if the user's input for quantity exceeds the qunatity in stock
if (input.quantity > res[0].stock_quantity) {
// if the user quantity exceeds the quantity on hand, print how many they can purchase and send user back to re-enter input
console.log("We don't have that many in stock. You can order up to " + res[0].stock_quantity);
purchaseItem();
} else { // otherwise, decrease the quantity and increase the total sales for that product
// variable to hold the total sales amount for the user's transaction
let salesTotal = (input.quantity * res[0].price).toFixed(2)
// query to update the product's quantity and total sales
let query = connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: res[0].stock_quantity - input.quantity,
product_sales: parseFloat(res[0].product_sales) + parseFloat(salesTotal)
},
{
item_id: input.productId
}
]
);
// array to hold table package information. Displays the purchased item and sales total to the user
data = [
["Product".bold, "Price".bold, "Quantity".bold, "Total".bold],
[res[0].product_name, "$" + res[0].price.toFixed(2), input.quantity, "$" + salesTotal]
];
// set up configuration for table package
config = {
columns: {
0: {
alignment: 'left',
minWidth: 10
},
1: {
alignment: 'left',
minWidth: 10
},
2: {
alignment: 'left',
minWidth: 10
},
3: {
alignment: 'right',
minWidth: 10
}
},
border: {
topBody: `─`,
topJoin: `┬`,
topLeft: `┌`,
topRight: `┐`,
bottomBody: `─`,
bottomJoin: `┴`,
bottomLeft: `└`,
bottomRight: `┘`,
bodyLeft: `│`,
bodyRight: `│`,
bodyJoin: `│`,
joinBody: `─`,
joinLeft: `├`,
joinRight: `┤`,
joinJoin: `┼`
}
};
console.log("\nYou bought the following:\n");
// print the table
output = table.table(data, config);
console.log(output.white.bgBlue);
// wait until the user presses any key before returning to the customer menu
console.log("\nPress any key to return to the menu");
process.stdin.setRawMode(true);
process.stdin.resume();
process.stdin.once('data', function () {
customerMenu();
process.exit.bind(process, 0);
});
}
}
});
});
}
// function to leave app
function exitMenu() {
console.log("\033c");
connection.end();
}
}
// export the file as a function to be accessed by the index
module.exports.bamazonCustomer = bamazonCustomer;