-
Notifications
You must be signed in to change notification settings - Fork 0
/
bamazonManager.js
237 lines (237 loc) · 8.95 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
#!/usr/bin/env node
/**
* BAMazon is a command line application created with NodeJS and MySQL. There three separate
* programs included in this project. Each program is designed for a different BAMazon user,
* one for customers, one for managers, and one for supervisors. Data about the store's
* inventory is stored in a local MySQL database which can be recreated by any user with the
* provided sql files in this project.
*
* @summary BAMazon manager interface used for performing various manager roles.
* @since 1.0.0
* @version 1.0.0
*
* Inquirer npm module
* @link https://www.npmjs.com/package/inquirer
* MySQL npm module
* @link https://www.npmjs.com/package/mysql
*
* @author Aaron Michael McNulty
*/
// Include depended files and libraries.
require('dotenv').config();
require('console.table');
const inquirer = require('inquirer');
const MYSQL = require('mysql');
/**
* All properties and methods involving manager interactions are stored in this object.
*
* @since 1.0.0
*/
var manager = {
/**
* A handle for the connection to the MySQL database.
*/
connection: MYSQL.createConnection({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASS,
database: "bamazon"
}),
/**
* A map for different user interactions gathered from user input.
* @type {Object}
*/
actionsMap: {
'View Products for Sale': 'showProducts',
'View Low Inventory': 'showLowInventory',
'Add to Inventory': 'addToInventory',
'Add New Product': 'addNewProduct',
'Exit App': 'exitApp'
},
/**
* Displays the main menu to the user and prompts for input.
*
* @since 1.0.0
*/
showMenu: function() {
inquirer.prompt([
{
type: 'list',
message: '\nBAMazon Management Protal\n\nPlease choose an action from the list below.',
choices: [
'View Products for Sale',
'View Low Inventory',
'Add to Inventory',
'Add New Product',
'Exit App'
],
name: 'action'
}
]).then(function(answers) {
manager[manager.actionsMap[answers.action]]();
});
},
/**
* Displays the products that are in the database to the console. Then shows the main menu.
*
* @since 1.0.0
* @fires manager.showMenu()
*/
showProducts: function() {
manager.connection.query("SELECT * FROM products", function(err, results) {
if (err) throw err;
var myTable = [];
for (var i = 0; i < results.length; i++) {
myTable.push(
{
"Product ID": results[i].item_id,
"Product Name": results[i].product_name,
"Price": results[i].price.toFixed(2),
"Stock Quantity": results[i].stock_quantity
}
);
}
console.table(myTable);
manager.showMenu();
});
},
/**
* Displays all products that have an stock quantity lower than 5 to the console.
*
* @since 1.0.0
* @fires manager.showMenu()
*/
showLowInventory: function() {
manager.connection.query("SELECT * FROM products WHERE stock_quantity < 5", function(err, results) {
if (err) throw err;
var myTable = [];
for (var i = 0; i < results.length; i++) {
myTable.push(
{
"Product ID": results[i].item_id,
"Product Name": results[i].product_name,
"Price": results[i].price.toFixed(2),
"Stock Quantity": results[i].stock_quantity
}
);
// console.log("\nProduct ID: " + results[i].item_id + "\nProduct Name: " + results[i].product_name + "\nPrice: " + results[i].price + "\nStock Quantity: " + results[i].stock_quantity);
}
console.table(myTable);
manager.showMenu();
});
},
/**
* Adds a new user generated product to the store.
*
* @since 1.0.0
* @fires manager.showMenu()
*/
addToInventory: function() {
manager.connection.query("SELECT * FROM products", function(err, results) {
if (err) throw err;
var myTable = [];
for (var i = 0; i < results.length; i++) {
myTable.push(
{
"Product ID": results[i].item_id,
"Product Name": results[i].product_name,
"Price": results[i].price.toFixed(2),
"Stock Quantity": results[i].stock_quantity
}
);
}
console.table(myTable);
inquirer.prompt([
{
type: 'input',
message: '\n\nEnter the ID of the product you would like to add more of.',
validate: function(id) {
if (Number.isInteger(parseFloat(id))) {
if (parseFloat(id) > 0 && parseFloat(id) < results.length + 1) return true;
else return "\nChoose a number between 0 - 10.";
}
else return "\nThat is not a valid number! Please choose a number between 0 - 10.";
},
name: 'productID'
},
{
type: 'input',
message: 'How many units would you like to add to the inventory?',
validate: function(quantity) {
if (Number.isInteger(parseFloat(quantity))) {
if (parseFloat(quantity) > 0) return true;
else return "\nChoose a number greater than 0.";
}
else return "\nPlease enter a valid number greater than 0."
},
name: 'productQuantity'
}
]).then(function(answers) {
manager.connection.query("UPDATE products SET stock_quantity = stock_quantity + " + answers.productQuantity + " WHERE item_id = " + answers.productID, function(err, results) {
if (err) throw err;
console.log("\n" + answers.productQuantity + " units added to inventory.");
manager.showMenu();
});
});
});
},
addNewProduct: function() {
inquirer.prompt([
{
type: 'input',
message: "\nPlease enter the name of the new product.",
name: 'productName'
},
{
type: 'input',
message: "\nEnter the department this product belongs in.",
name: 'productDepartment'
},
{
type: 'input',
message: "\nEnter the price for this product.",
validate: function(price) {
if (Number.isNaN(parseFloat(price)) || parseFloat(price) <= 0) return "\nPlease enter a valid number greater than 0.";
else return true;
},
name: 'productPrice'
},
{
type: 'input',
message: "\nEnter the stock quantity for this product.",
validate: function(quantity) {
if (Number.isInteger(parseFloat(quantity))) {
if (parseFloat(quantity) > 0) return true;
else return "\nChoose a number greater than 0.";
}
else return "\nPlease enter a valid number greater than 0."
},
name: 'productStock'
}
]).then(function(answers) {
manager.connection.query("INSERT INTO products (product_name, department_name, price, stock_quantity) VALUES('" + answers.productName + "', '" + answers.productDepartment + "', " + answers.productPrice + ", " + answers.productStock + ")", function(err, results) {
if (err) throw err;
console.log("\nYou have added " + answers.productName + " to the store!");
manager.showMenu();
});
});
},
/**
* Ends database connection, thanks customer, and exits application.
*
* @since 1.0.0
*/
exitApp: function() {
manager.connection.end(function(err) {
if (err) throw err;
console.log("\nThank you for using BAMazon!")
process.exit();
});
}
};
// Create connection to MySQL database then show main menu.
manager.connection.connect(function(err) {
if (err) throw err;
manager.showMenu();
});