-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
216 lines (202 loc) · 4.97 KB
/
main.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
var mysql = require('mysql');
var inquirer = require('inquirer');
require('console.table');
var connection = mysql.createConnection({
host: 'localhost',
port: 3307,
user: 'ClassUser',
password: 'password',
database: 'bamazon'
});
var user = '';
connection.connect(function(err) {
if (err) throw err;
console.log('Connection success!');
loginPrompt();
});
function loginPrompt() {
//Ask for UserName and Password
inquirer.prompt([
{
type: 'input',
name: 'user',
message: 'What is your username?'
},
{
type: 'input',
name: 'pass',
message: 'What is your password?'
}
]).then(function(res) {
//Switch sending the user down the appropriate view.
if (res.user === 'customer' && res.pass === 'password') {
user = 'customer';
showProducts();
}
else if (res.user === 'manager' && res.pass === 'password') {
user = 'manager';
managerPrompt();
}
else {
console.log('Unauthorized Account. Try again.');
loginPrompt();
}
});
}
function customerPrompt(inventory) {
inquirer.prompt([
{
type: 'input',
name: 'productChoice',
message: 'Type the ID of the product you would like to purchase!'
}
]).then(function(val) {
let choice = parseInt(val.productChoice);
let product = checkInventory(choice, inventory);
if (product) {
customerQuantityPrompt(product);
} else {
console.log('That product is not in our inventory. Please try again.');
showProducts();
}
});
}
function checkInventory(choice, inventory) {
for (let i = 0; i < inventory.length; i++) {
if (inventory[i].item_id === choice) {
return inventory[i];
}
}
return null;
}
function showProducts() {
let query = 'SELECT * from products';
connection.query(query, function(err, inventory) {
console.table(inventory);
if (user === 'customer') {
customerPrompt(inventory);
}
else if (user === 'manager') {
managerPrompt(inventory);
}
});
}
function customerQuantityPrompt(product) {
inquirer.prompt([
{
type: 'input',
name: 'quantity',
message: 'How many would you like to purchase?'
}
]).then(function(val) {
let quantity = parseInt(val.quantity);
if (quantity > product.stock_quantity) {
console.log('There aren\'t enough of that product in stock. Please try again.');
showProducts();
} else {
makePurchase(product, quantity);
}
});
}
function makePurchase(product, quantity) {
connection.query(
'UPDATE products SET stock_quantity = stock_quantity - ? WHERE item_id = ?',
[quantity, product.item_id],
function(err, res) {
console.log('Your purchase was a success!');
console.log('The total came to ' + (quantity * product.price));
showProducts();
}
);
}
function managerPrompt() {
inquirer.prompt([
{
type: 'list',
name: 'managerChoice',
message: 'What would you like to do?',
choices: ['Show Products', 'Low Inventory Check', 'Add to Inventory', 'New Product']
}
]).then(function(res) {
console.log(res.managerChoice);
if (res.managerChoice === 'Show Products') {
showProducts();
}
else if (res.managerChoice === 'Low Inventory Check') {
lowInventoryCheck();
}
else if (res.managerChoice === 'Add to Inventory') {
addToInventory();
}
else if (res.managerChoice === 'New Product') {
newItem();
} else {
console.log('Error Occurred in Choice.');
managerPrompt();
}
});
}
function lowInventoryCheck() {
connection.query(
'SELECT * FROM products WHERE stock_quantity <=5',
function (err, lowinventory) {
if (err) throw err;
console.table(lowinventory);
managerPrompt();
}
);
}
function addToInventory() {
inquirer.prompt([
{
type: 'input',
name: 'productID',
message: 'What is the ID of the product you would like to replenish?'
},
{
type: 'input',
name: 'qtyAdded',
message: 'How many would you like to add to inventory?'
}
]).then(function(res) {
let inputs = ['qtyAdded', 'productID'];
let query = 'UPDATE products SET stock_quantity = stock_quantity + ? WHERE item_id = ?';
connection.query(query, inputs, function(err, res) {
if (err) throw err;
console.log('Product replenished. Returning to the Main Menu!');
managerPrompt();
});
});
}
function newItem() {
inquirer.prompt([
{
type: 'input',
name: 'addedProduct',
message: 'What product would you like to add?'
},
{
type: 'input',
name: 'productDept',
message: 'What department is this product in?'
},
{
type: 'input',
name: 'qtyAdded',
message: 'How many of the product would you like to add?'
},
{
type: 'input',
name: 'addedProductPrice',
message: 'What is the price of the product you are adding?'
}
]).then(function(res) {
let inputs = [res.addedProduct, res.productDept, parseFloat(res.addedProductPrice), parseInt(res.qtyAdded)];
let query = 'INSERT INTO products SET product_name=?, department_name=?, price=?, stock_quantity=?';
connection.query(query, inputs, function(err, result) {
if (err) throw err;
console.log('Your product has been added to the database!');
managerPrompt();
});
});
}