This repository has been archived by the owner on Mar 3, 2023. It is now read-only.
/
bamazonCustomer.js
173 lines (152 loc) · 4.55 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
// Check for the connected js file
// console.log('BamazonCustomer.js Connected!');
// Load dotenv package to store mysql variables
require('dotenv').config();
// Require npm module mysql
const mysql = require('mysql');
// Require the inquirer npm package
const inquirer = require('inquirer');
// Set the global variables
let prodID = 0;
let prodQuant = 0;
// Create a connection
const connection = mysql.createConnection({
host: process.env.HOST,
port: process.env.PORT,
user: process.env.USER,
password: process.env.PASSWORD,
database: process.env.DATABASE,
});
// Make connection
connection.connect((err) => {
if (err) throw err;
// console.log(`connected as id ${connection.threadId}`);
// Call the Read Products function
readProducts();
});
// Function to get all the products
function readProducts() {
console.log('\nSelecting all products...\n');
connection.query('SELECT * FROM products', (err, res) => {
if (err) throw err;
// Log all results of the SELECT statement
console.table(res);
// Call the Initial Buy Inquirer
inqBuy();
});
}// End readProducts
// Function to start the questions
function inqBuy() {
inquirer
// First prompt if want to exit or continue
.prompt([
{
type: 'confirm',
name: 'exit',
message: 'Would you like to Exit?',
},
])
.then((answers) => {
// If choose exit, show message and end connection
if (answers.exit === true) {
console.log('\nThank you for shopping, come again!\n');
connection.end();
return false;
}
// If want to continue, prompt for what to buy
inquirer
.prompt([
{
type: 'input',
message: 'Type the ID of the product you would like to buy?',
name: 'id',
// Validate that the user enters a Number
validate(value) {
if (isNaN(value) === false) {
return true;
}
// Prompt for a Number
console.log('Please select a Number');
return false;
},
},
{
type: 'input',
message: 'How many units of the product would you like to buy?',
name: 'units',
// Validate that the user enters a Number
validate(value) {
if (isNaN(value) === false) {
return true;
}
// Prompt for a Number
console.log('Please select a Number');
return false;
},
},
])
.then((answers) => {
// Call makePurchase function, pass the answers object
makePurchase(answers);
});
});
}// End inqBuy
// Function to make the purchase
function makePurchase(answers) {
// First check if there is sufficient quantity
console.log('\nChecking Product Stock Level...\n');
prodID = answers.id;
prodQuant = answers.units;
// Make the query based on the prodID
const sql = `SELECT stock_quantity FROM products Where item_id = ${connection.escape(prodID)}`;
connection.query(sql, (err, res) => {
if (err) throw err;
// Get the stock quantity
const result = res[0].stock_quantity;
// Make sure there is enough stock
if (result > prodQuant) {
// Update quantity
const updatedQuant = result - prodQuant;
// Calculate Cost and complete the purchase
cost(prodID, prodQuant, updatedQuant);
} else {
console.log(
'Stock Quantity Insufficient, unable to complete the purchase.\nTry again tomorrow, thank you.',
);
// Restart
readProducts();
}
});
}// End makePurchase
// Get the price and calculate cost
function cost(prodID, prodQuant, updatedQuant) {
// Get the item by id
const sql = `SELECT price FROM products Where item_id = ${connection.escape(prodID)}`;
connection.query(sql, (err, res) => {
if (err) throw err;
// Get the price from the result
const { price } = res[0];
// Calculate the cost
const calcCost = price * prodQuant;
// Make the purchase
console.log(`Completing the Purchase...\nYour total cost is $${calcCost}`);
// Update the db
const query = connection.query(
'UPDATE products SET ? WHERE ?',
[
{
stock_quantity: updatedQuant,
},
{
item_id: prodID,
},
],
(err, res) => {
if (err) throw err;
console.log(`${res.affectedRows} product(s) updated!\n`);
// Show updated quant
readProducts();
},
);
});
}// End cost