-
Notifications
You must be signed in to change notification settings - Fork 1
/
bamazonCustomer.js
211 lines (183 loc) · 6.07 KB
/
bamazonCustomer.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
// Declare global constants and variables
const mysql = require('mysql');
const Table = require('cli-table');
const number = require('accounting')
const inquirer = require('inquirer');
const colors = require('colors');
var item, qty, updateQTY, total, grandTotal = 0, itemsPurchased = 0;
//create object containing mysql connection values
var connection = mysql.createConnection ({
host: 'localhost',
user: 'root',
port: '3306',
password: '',
database: 'bamazon'
})
//connect to database
connection.connect(function(err){
if(err){
console.log('Error connecting to Db');
return;
};
console.log('Connected to Bamazon');
displayProducts();
});
//method to display a table of all products
function displayProducts() {
connection.query('SELECT * FROM products', function (error, response){
let table = new Table ({
head: ['Item ID', 'Product Name', 'Department', 'Price (billions)', 'Quantity']
});
for (let i = 0; i < response.length; i++) {
table.push([response[i].item_id, response[i].product_name, response[i].department_name, '$' + response[i].product_price, response[i].stock_quantity])
}
console.reset();
console.log(table.toString());
selectProduct(); //call method
})
}
//method to prompt customer to select item for puchase
function selectProduct () {
inquirer.prompt([
{
name: "item",
type: "input",
message: "What is the ID of the item you would like to purchase?".yellow,
validate: function(value) {
var valid = !isNaN(parseInt(value));
return valid || 'Please enter the item id ';
}
}
])
.then(function(answer) {
item = parseInt(answer.item);
console.log('You chose ' + item);
let query = ("SELECT item_id, product_name, stock_quantity FROM products WHERE item_id =?");
connection.query(query, [item], function(err, response) {
if (err) {
console.log(err);
selectProduct();
}
else if(response.length < 1) {
console.log("That is not an item for sale:".green)
selectProduct();
}
else if (response[0].stock_quantity <= 1) {
console.log ("Sorry, we are currently out of stock on ".green + response[0].product_name .magenta)
selectProduct();
}
else {
console.log ("We have ".green + response[0].stock_quantity + " " + response[0].product_name .magenta + "'s ".magenta + "for sale" .green);
selectQuantity(); //call method
}
})
})
}
//method to prompt customer to select quantity for purchase, error check input,
//output selection to a table, and input selection to an invoice table
function selectQuantity () {
inquirer.prompt([
{
name: "qty",
type: "input",
message: "How many would you like to purchase?".yellow,
validate: function(value) {
var valid = !isNaN(parseInt(value));
return valid || 'Please enter the item id ';
}
}
])
.then(function(answer) {
qty = answer.qty;
let query = ("SELECT * FROM products WHERE item_id =?");
connection.query(query, [item], function(err, response) {
if (err) {
console.log ("Please input the quantity you would like to purchase".green);
selectQuantity();
}
else if (qty > response[0].stock_quantity) {
console.log ("We currently only have ".green + response[0].stock_quantity + " available".green);
selectQuantity();
}
else {
itemsPurchased++;
var price = parseInt(response[0].product_price);
total = qty * price
grandTotal = grandTotal + total;
updateQTY = response[0].stock_quantity - qty;
connection.query(query, [item], function (error, response){
purchase = new Table ({
head: ['Product Name', 'Price (Billions)', 'Quantity', 'Total']
});
purchase.push([response[0].product_name, response[0].product_price, qty, '$' + total]);
console.reset();
console.log(purchase.toString());
updateStock(); //call method
});
connection.query("INSERT INTO invoice SET ?",
{
product_name: response[0].product_name,
product_price: price,
quantity: qty,
total: total
},
function(err) {
if (err) throw err;
});
}
})
})
}
//method to update stock quanities in products table
function updateStock () {
connection.query(
"UPDATE products SET ? WHERE ?",
[
{
stock_quantity: updateQTY, product_sales: total
},
{
item_id: item
}
],
function(err, res) {
console.log(" inventory reduced to ".blue + updateQTY);
replay();
})
}
//prompt user to purchase another product or quit
//If quit display invoice table and order grand total and reset invoice table
function replay () {
inquirer.prompt([
{
name: "replay",
type: "list",
message: "Would you like to make an additional purchase?".yellow,
choices: ['Yes', 'No']
}
])
.then(function(answer) {
if (answer.replay === 'Yes') {
displayProducts();
}
else {
connection.query('SELECT * FROM invoice', function (error, response){
let receipt = new Table ({
head: ['Product Name', 'Price (Billions)', 'Quantity', 'Total']
})
for (let i = 0; i < response.length; i++) {
receipt.push([response[i].product_name, response[i].product_price, response[i].quantity, '$' + response[i].total]);
}
console.reset();
console.log(receipt.toString());
console.log('Grand Total: '+ number.formatMoney(grandTotal));
connection.query('DELETE FROM invoice', function (error, response){
})
connection.end();
})
}
})
}
console.reset = function () {
return process.stdout.write('\033c');
}