-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonManager.js
executable file
·283 lines (233 loc) · 8.15 KB
/
bamazonManager.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
// Pull in required dependencies
var inquirer = require('inquirer');
var mysql = require('mysql');
// Define the MySQL connection parameters
var connection = mysql.createConnection({
host: 'localhost',
port: 3306,
// Your username
user: 'root',
// Your password
password: 'password',
database: 'Bamazon'
});
// promptManagerAction will present menu options to the manager and trigger appropriate logic
function promptManagerAction() {
// console.log('___ENTER promptManagerAction___');
// Prompt the manager to select an option
inquirer.prompt([
{
type: 'list',
name: 'option',
message: 'Please select an option:',
choices: ['View Products for Sale', 'View Low Inventory', 'Add to Inventory', 'Add New Product'],
filter: function (val) {
if (val === 'View Products for Sale') {
return 'sale';
} else if (val === 'View Low Inventory') {
return 'lowInventory';
} else if (val === 'Add to Inventory') {
return 'addInventory';
} else if (val === 'Add New Product') {
return 'newProduct';
} else {
// This case should be unreachable
console.log('ERROR: Unsupported operation!');
exit(1);
}
}
}
]).then(function(input) {
// console.log('User has selected: ' + JSON.stringify(input));
// Trigger the appropriate action based on the user input
if (input.option ==='sale') {
displayInventory();
} else if (input.option === 'lowInventory') {
displayLowInventory();
} else if (input.option === 'addInventory') {
addInventory();
} else if (input.option === 'newProduct') {
createNewProduct();
} else {
// This case should be unreachable
console.log('ERROR: Unsupported operation!');
exit(1);
}
})
}
// displayInventory will retrieve the current inventory from the database and output it to the console
function displayInventory() {
// console.log('___ENTER displayInventory___');
// Construct the db query string
queryStr = 'SELECT * FROM products';
// Make the db query
connection.query(queryStr, function(err, data) {
if (err) throw err;
console.log('Existing Inventory: ');
console.log('...................\n');
var strOut = '';
for (var i = 0; i < data.length; i++) {
strOut = '';
strOut += 'Item ID: ' + data[i].item_id + ' // ';
strOut += 'Product Name: ' + data[i].product_name + ' // ';
strOut += 'Department: ' + data[i].department_name + ' // ';
strOut += 'Price: $' + data[i].price + ' // ';
strOut += 'Quantity: ' + data[i].stock_quantity + '\n';
console.log(strOut);
}
console.log("---------------------------------------------------------------------\n");
// End the database connection
connection.end();
})
}
// displayLowInventory will display a list of products with the available quantity below 100
function displayLowInventory() {
// console.log('___ENTER displayLowInventory');
// Construct the db query string
queryStr = 'SELECT * FROM products WHERE stock_quantity < 100';
// Make the db query
connection.query(queryStr, function(err, data) {
if (err) throw err;
console.log('Low Inventory Items (below 100): ');
console.log('................................\n');
var strOut = '';
for (var i = 0; i < data.length; i++) {
strOut = '';
strOut += 'Item ID: ' + data[i].item_id + ' // ';
strOut += 'Product Name: ' + data[i].product_name + ' // ';
strOut += 'Department: ' + data[i].department_name + ' // ';
strOut += 'Price: $' + data[i].price + ' // ';
strOut += 'Quantity: ' + data[i].stock_quantity + '\n';
console.log(strOut);
}
console.log("---------------------------------------------------------------------\n");
// End the database connection
connection.end();
})
}
// validateInteger makes sure that the user is supplying only positive integers for their inputs
function validateInteger(value) {
var integer = Number.isInteger(parseFloat(value));
var sign = Math.sign(value);
if (integer && (sign === 1)) {
return true;
} else {
return 'Please enter a whole non-zero number.';
}
}
// validateNumeric makes sure that the user is supplying only positive numbers for their inputs
function validateNumeric(value) {
// Value must be a positive number
var number = (typeof parseFloat(value)) === 'number';
var positive = parseFloat(value) > 0;
if (number && positive) {
return true;
} else {
return 'Please enter a positive number for the unit price.'
}
}
// addInventory will guilde a user in adding additional quantify to an existing item
function addInventory() {
// console.log('___ENTER addInventory___');
// Prompt the user to select an item
inquirer.prompt([
{
type: 'input',
name: 'item_id',
message: 'Please enter the Item ID for stock_count update.',
validate: validateInteger,
filter: Number
},
{
type: 'input',
name: 'quantity',
message: 'How many would you like to add?',
validate: validateInteger,
filter: Number
}
]).then(function(input) {
// console.log('Manager has selected: \n item_id = ' + input.item_id + '\n additional quantity = ' + input.quantity);
var item = input.item_id;
var addQuantity = input.quantity;
// Query db to confirm that the given item ID exists and to determine the current stock_count
var queryStr = 'SELECT * FROM products WHERE ?';
connection.query(queryStr, {item_id: item}, function(err, data) {
if (err) throw err;
// If the user has selected an invalid item ID, data attay will be empty
// console.log('data = ' + JSON.stringify(data));
if (data.length === 0) {
console.log('ERROR: Invalid Item ID. Please select a valid Item ID.');
addInventory();
} else {
var productData = data[0];
// console.log('productData = ' + JSON.stringify(productData));
// console.log('productData.stock_quantity = ' + productData.stock_quantity);
console.log('Updating Inventory...');
// Construct the updating query string
var updateQueryStr = 'UPDATE products SET stock_quantity = ' + (productData.stock_quantity + addQuantity) + ' WHERE item_id = ' + item;
// console.log('updateQueryStr = ' + updateQueryStr);
// Update the inventory
connection.query(updateQueryStr, function(err, data) {
if (err) throw err;
console.log('Stock count for Item ID ' + item + ' has been updated to ' + (productData.stock_quantity + addQuantity) + '.');
console.log("\n---------------------------------------------------------------------\n");
// End the database connection
connection.end();
})
}
})
})
}
// createNewProduct will guide the user in adding a new product to the inventory
function createNewProduct() {
// console.log('___ENTER createNewProduct___');
// Prompt the user to enter information about the new product
inquirer.prompt([
{
type: 'input',
name: 'product_name',
message: 'Please enter the new product name.',
},
{
type: 'input',
name: 'department_name',
message: 'Which department does the new product belong to?',
},
{
type: 'input',
name: 'price',
message: 'What is the price per unit?',
validate: validateNumeric
},
{
type: 'input',
name: 'stock_quantity',
message: 'How many items are in stock?',
validate: validateInteger
}
]).then(function(input) {
// console.log('input: ' + JSON.stringify(input));
console.log('Adding New Item: \n product_name = ' + input.product_name + '\n' +
' department_name = ' + input.department_name + '\n' +
' price = ' + input.price + '\n' +
' stock_quantity = ' + input.stock_quantity);
// Create the insertion query string
var queryStr = 'INSERT INTO products SET ?';
// Add new product to the db
connection.query(queryStr, input, function (error, results, fields) {
if (error) throw error;
console.log('New product has been added to the inventory under Item ID ' + results.insertId + '.');
console.log("\n---------------------------------------------------------------------\n");
// End the database connection
connection.end();
});
})
}
// runBamazon will execute the main application logic
function runBamazon() {
// console.log('___ENTER runBamazon___');
// Prompt manager for input
promptManagerAction();
}
// Run the application logic
runBamazon();