-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
153 lines (140 loc) · 4.7 KB
/
bamazonCustomer.js
File metadata and controls
153 lines (140 loc) · 4.7 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
const mysql = require("mysql");
const inquirer = require("inquirer");
const colors = require('colors');
// let methods = require("./bamazonSQLQueries.js");
let connection = mysql.createConnection({
host: "localhost",
port: 3306,
user: "root",
password: "Fodder165*",
database: "inventory_db"
});
connection.connect(function (err) {
if (err) throw (err);
handleDisplayInventory();
})
const handleDisplayInventory = () => {
connection.query("SELECT * FROM products", function (err, res) {
if (err) throw (err);
console.log('\nAvailable Inventory\n'.bold.underline.cyan)
res.forEach((product) => {
console.log(`Product ID: ${product.item_id} || Product Name: ${product.product_name} || Price: $${product.price.toFixed(2)}`);
})
handleInquirer();
})
};
const handleInquirer = () => {
console.log('\n')
inquirer
.prompt([
{
type: 'input',
name: 'id',
message: 'To purchase an item, please select a Product ID number.',
validate: function (value) {
return value !== '';
}
}
])
.then(answers => {
handleProductIdSearch(answers.id)
});
};
const handleProductIdSearch = (id) => {
connection.query("SELECT * FROM products WHERE item_id=?", [id], function (err, res) {
if (err) throw (err);
if (res.length === 0) {
console.log(`Product ID: ${id} does not exists. Please select a different Product ID.`.bold.cyan.underline);
// Rerun inventory list and prompt select product id question
return handleDisplayInventory()
} else {
const productName = res[0].product_name;
return handlePurchaseQuantity(id, productName)
}
})
};
const handlePurchaseQuantity = (id, productName) => {
console.log('\n');
inquirer
.prompt([
{
type: 'input',
name: 'quantity',
message: `You selected ${productName}.\n Enter the quantity you want to purchase?`,
validate: function (value) {
let valid = !isNaN(parseFloat(value));
return valid || "Please enter a number".red.bold;
}
}
])
.then(answers => {
handleQuantityInput(id, answers.quantity);
});
};
const handleQuantityInput = (id, userQuantity) => {
connection.query("SELECT * FROM products WHERE item_id=?", [id], function (err, res) {
if (err) throw (err);
// Get product quantity
const productQuantity = res[0].stock_quantity
// Get unit price
const productName = res[0].product_name;
const productPrice = res[0].price;
// Get product salse
const productSales = res[0].product_sales;
if (userQuantity > productQuantity) {
console.log(colors.cyan.bold(`\nSorry. Insufficient quantity!\nOnly ${productQuantity} ${productName} currently in stock\n`));
handleExitStore();
} else {
console.log('\nPurchase Completed:\n'.bold.green)
handleTransaction(id, productName, productPrice, productQuantity, userQuantity, productSales);
}
});
};
const handleTransaction = (id, productName, productPrice, productQuantity, userQuantity, productSales) => {
// Calculate sale amount;
const saleAmount = (productPrice * userQuantity).toFixed(2);
console.log(`Your purchase of ${userQuantity} ${productName} at $${productPrice.toFixed(2)} per item total: $${saleAmount}\n`.bold.green);
// Update Inventory
handleUpdateInventory(id, productQuantity, userQuantity);
// Calculate product_sales column
handleProductSalesColumn(id, saleAmount, productSales);
};
const handleUpdateInventory = (id, productQuantity, userQuantity) => {
const newQuantity = (productQuantity - userQuantity);
connection.query("UPDATE products SET ? WHERE ?", [{ stock_quantity: newQuantity }, { item_id: id }], function (err, res) {
});
// handleCheckInventory()
handleExitStore();
}
const handleExitStore = () => {
inquirer
.prompt([
{
type: 'list',
choices: ['Yes', 'No'],
name: 'continue',
message: 'Do you want to continue shopping?'
}
])
.then(answers => {
console.log(answers.continue);
if (answers.continue === 'No') {
console.log('\nThank you for shopping!!!\n'.green.bold);
connection.end();
} else {
handleDisplayInventory();
}
});
};
// For test purpose only
const handleCheckInventory = () => {
connection.query("SELECT * FROM products", function (err, res) {
console.log('Updated Inventory', res)
})
};
const handleProductSalesColumn = (id, saleAmount, productSales) => {
saleAmount = parseFloat(saleAmount)
updatedProductSales = (saleAmount + productSales).toFixed(2)
connection.query("UPDATE products SET ? WHERE ?", [{ product_sales: updatedProductSales }, { item_id: id }], function (err, res) {
})
};