-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbamazonCustomer.js
More file actions
106 lines (80 loc) · 4.07 KB
/
bamazonCustomer.js
File metadata and controls
106 lines (80 loc) · 4.07 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
// Required Dependencies
var mysql = require("mysql");
var inquirer = require("inquirer");
var colors = require('colors');
var Table = require('cli-table');
// Connection script
var connection = mysql.createConnection({
host: "localhost",
port: 3306,
// Username
user: "root",
// Credentials
password: "*******",
database: "bamazon"
});
connection.connect(function (err) {
if (err) throw err;
console.log(colors.cyan("Welcome! ...you are now connected to the Bamazon Store database as id " + connection.threadId));
//connection.end();
bamazon(); //Call main function
}); // End Connection Script
// BEGIN Display Inventory
function bamazon() {
connection.query('SELECT * FROM products', function (err, res) {
if (err) throw err;
// Cli-Table display code with Color
var table = new Table(
{
head: ["Product ID".cyan.bold, "Product Name".cyan.bold, "Department Name".cyan.bold, "Price".cyan.bold, "Quantity".cyan.bold],
colWidths: [12, 75, 20, 12, 12],
});
// Set/Style table headings and Loop through entire inventory
for (var i = 0; i < res.length; i++) {
table.push(
[res[i].id, res[i].product_name, res[i].department_name, parseFloat(res[i].price).toFixed(2), res[i].stock_quantity]
);
}
console.log(table.toString());
// END Display Inventory
// Prompt Customers Input
inquirer.prompt([
{
type: "number",
message: "Please enter the Product ID of the item that you would like to buy?".yellow,
name: "id"
},
{
type: "number",
message: "How many would you like to buy?".yellow,
name: "quantity"
},
])
// Ordering function
.then(function (cart) {
var quantity = cart.quantity;
var itemID = cart.id;
connection.query('SELECT * FROM products WHERE id=' + itemID, function (err, selectedItem) {
if (err) throw err;
// Varify item quantity desired is in inventory
if (selectedItem[0].stock_quantity - quantity >= 0) {
console.log("INVENTORY AUDIT: Quantity in Stock: ".green + selectedItem[0].stock_quantity + " Order Quantity: ".green + quantity.yellow);
console.log("Congratulations! Bamazon has suffiecient inventory of ".green + selectedItem[0].product_name.yellow + " to fill your order!".green);
// Calculate total sale, and fix 2 decimal places
console.log("Thank You for your purchase. Your order total will be ".green + (cart.quantity * selectedItem[0].price).toFixed(2).yellow + " dollars.".green, "\nThank you for shopping at Bamazon!".magenta);
// Query to remove the purchased item from inventory.
connection.query('UPDATE products SET stock_quantity=? WHERE id=?', [selectedItem[0].stock_quantity - quantity, itemID],
function (err, inventory) {
if (err) throw err;
bamazon(); // Runs the prompt again, so the customer can continue shopping.
}); // Ends the code to remove item from inventory.
}
// Low inventory warning
else {
console.log("INSUFFICIENT INVENTORY ALERT: \nBamazon only has ".red + selectedItem[0].stock_quantity + " " + selectedItem[0].product_name.cyan + " in stock at this moment. \nPlease make another selection or reduce your quantity.".red, "\nThank you for shopping at Bamazon!".magenta);
bamazon(); // Runs the prompt again, so the customer can continue shopping.
}
});
});
});
} // Closes bamazon function