-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonManager.js
More file actions
255 lines (206 loc) · 6.88 KB
/
bamazonManager.js
File metadata and controls
255 lines (206 loc) · 6.88 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
//************************************************//
//***** set required npm modules *****//
//************************************************//
//
//-- file system --//
var fs = require("fs");
//-- inquirer --//
var inquirer = require("inquirer");
//-- mysql --//
var mysql = require("mysql");
var keys = require("./keys.js");
var connection = mysql.createConnection(keys.connection);
connection.connect(function(err) {
if (err) throw err;
});
//-- table and formatting --//
var cliTable = require("cli-table");
var colors = require("colors");
//************************************************//
//***** declare global variables *****//
//************************************************//
//
var welcome = " **********************************************************************\n" +
" ********** BAMAZON MANAGER **********\n" +
" ********** Manage product inventories **********\n" +
" ********** **********\n" +
" **********************************************************************\n\r"
var inventoryMsg;
var goodbye = " **********************************************************************\n" +
" ********** EXTING BAMAZON MANAGER **********\n" +
" ********** Keep up the good work ! **********\n" +
" ********** **********\n" +
" **********************************************************************\n\r"
//************************************************//
//***** global functions *****//
//************************************************//
//
//---- Main menu with user input ----//
function managerMenu(){
inquirer.prompt([
{
type: "list",
message: "What do you want to do?",
choices: ["View Products for Sale", "View Low Inventory", "Add to Inventory", "Add New Products", "Exit"],
name: "mgrDoItem"
}
])
.then(function(manager_menu) {
switch(manager_menu.mgrDoItem){
case "View Products for Sale":
displayProducts();
break;
case "View Low Inventory":
lowInventory();
break;
case "Add to Inventory":
addInventory();
break;
case "Add New Products":
addProduct();
break;
case "Exit":
exitBamazonMgr();
break;
};
});
};
//---- Display table of inventory items ----//
function displayProducts() {
connection.query("SELECT * FROM products", function(err, res) {
if (err) throw err;
//console.log(" Reached first function")
var table = new cliTable({
head: ["Item Number".cyan, "Product Name".cyan, "Department".cyan, "Price".cyan, "Quantity".cyan],
colWidths: [13, 20, 20, 13, 13],
});
for(var i = 0; i < res.length; i++) {
table.push(
[res[i].item_id, res[i].product_name, res[i].department_name, parseFloat(res[i].price).toFixed(2), res[i].stock_quantity]
);
};
console.log(table.toString());
managerMenu();
});
};
//---- See inventory with low qty of instock items ----//
function lowInventory(){
connection.query("SELECT * FROM products WHERE stock_quantity < 5", function(err, res) {
if (err) throw err;
//console.log(" Reached first function")
var table = new cliTable({
head: ["Item Number".cyan, "Product Name".cyan, "Department".cyan, "Price".cyan, "Quantity".cyan],
colWidths: [13, 20, 20, 13, 13],
});
for(var i = 0; i < res.length; i++) {
table.push(
[res[i].item_id, res[i].product_name, res[i].department_name, parseFloat(res[i].price).toFixed(2), res[i].stock_quantity]
);
};
console.log(table.toString());
managerMenu();
});
};
//---- Reorder inventory items ----//
function addInventory(){
connection.query("SELECT * FROM products", function(err, res) {
if (err) throw err;
//console.log(" Reached first function")
var table = new cliTable({
head: ["Item Number".cyan, "Product Name".cyan, "Department".cyan, "Price".cyan, "Quantity".cyan],
colWidths: [13, 20, 20, 13, 13],
});
for(var i = 0; i < res.length; i++) {
table.push(
[res[i].item_id, res[i].product_name, res[i].department_name, parseFloat(res[i].price).toFixed(2), res[i].stock_quantity]
);
};
console.log(table.toString());
inquirer.prompt([
{
type: "input",
message: "Which item would you like to reorder? (Iten Number) ",
name: "itemNum"
},
{
type: "input",
message: "How many would you like to add to stock?",
name: "Qty"
}
])
.then(function (userOrder) {
var i = userOrder.itemNum - 1;
var updateQty = parseInt(res[i].stock_quantity) + parseInt(userOrder.Qty);
//-- Update the product table stock quantity --//
connection.query("UPDATE products SET ? WHERE ?",
[ {
stock_quantity: updateQty
},
{
item_id: userOrder.itemNum
}],
function(error, results) {
if(error) throw error;
inventoryMsg = " Your restock order for " + userOrder.Qty + " " + res[i].product_name + " has been placed. \n" ;
console.log(inventoryMsg);
managerMenu();
}
);
});
});
};
//---- Add a new inventory item ----//
function addProduct(){
//console.log("still working on this")
inquirer.prompt([
{
type: "input",
message: "What is the name of the product you would like to add? ",
name: "itemName"
},
{
type: "list",
message: "Which department will this item be under ?",
choices: ["Real Estate" , "Furniture" , "Accessories", "Appliances", "Electronics", "Outdoors", "Windows_Doors"],
name: "itemDept"
},
{
type: "input",
message: "At what price will this be offered?",
name: "itemPrice"
},
{
type: "input",
message: "How many will initially be stocked?",
name: "itemQty"
},
])
.then(function (addProd) {
connection.query("INSERT INTO products SET ?",
{
product_name: addProd.itemName,
department_name: addProd.itemDept,
price: addProd.itemPrice,
stock_quantity: addProd.itemQty
},
function(err, res) {
if(err) throw err;
inventoryMsg = " A quantity of " + addProd.itemQty + " " + addProd.itemName + "s have been added to inventory stock under the " +
addProd.itemDept + " department";
console.log(inventoryMsg);
managerMenu();
}
);
});
};
//---- Exit the program ----//
function exitBamazonMgr(){
connection.end();
console.log(goodbye);
};
//************************************************//
//***** Start the program *****//
//************************************************//
//
console.log(welcome);
managerMenu();