-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
350 lines (310 loc) · 10.5 KB
/
Copy pathadmin.js
File metadata and controls
350 lines (310 loc) · 10.5 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
const { createPool } = require('mysql');
const readline = require('readline');
const { promisify } = require('util');
const pool = createPool({
host: 'localhost',
port:3306,
user: 'aditya',
password: "12345678",
database: "blinkit",
connectionLimit: 10
});
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Promisify rl.question
const questionAsync = promisify(rl.question).bind(rl);
// Promisify pool.query for async/await support
const query = promisify(pool.query).bind(pool);
// Function to begin a transaction
async function startTransaction() {
await query('START TRANSACTION');
}
// Function to commit a transaction
async function commitTransaction() {
await query('COMMIT');
}
// Function to rollback a transaction
async function rollbackTransaction() {
await query('ROLLBACK');
}
async function adminmenu() {
while (true) {
const choice = await questionAsync(
'\nAdmin Menu:' +
'\nPress 0 to Exit' +
'\nPress 1 to Assign delivery' +
'\nPress 2 to view customerAnalysis' +
'\nPress 3 to Manage Products' +
'\nPress 4 to View Sales Reports' +
'\nPress 5 to View Feedback ' +
'\nEnter your choice: '
);
switch (choice) {
case '0':
console.log("Exiting Admin Menu...");
return;
case '1':
await assignDelivery();
break;
case '2':
await customerAnalysis();
break;
case '3':
await manageProducts();
break;
case '4':
await viewSalesReports();
break;
case '5':
await viewFeedback();
break;
default:
console.log("\nInvalid input");
console.log("Please enter a valid choice");
}
}
}
async function assignDelivery() {
const delivery_partner_id = await questionAsync('Enter the delivery partner ID: ');
const order_id = await questionAsync('Enter the order ID: ');
const duration = await questionAsync('Enter the duration: ');
const response = await new Promise((resolve, reject) => {
pool.query(
'INSERT INTO delivery (delivery_partner_id, order_id, duration) VALUES (?, ?, ?)',
[delivery_partner_id, order_id, duration],
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
);
});
console.log('Delivery assigned successfully');
}
async function customerAnalysis() {
// total no of customers
const response1 = await new Promise((resolve, reject) => {
pool.query(
'SELECT COUNT(*) FROM customer',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
);
});
console.log('Total number of customers: ', response1[0]['COUNT(*)']);
// total no of customers who have placed orders
const response2 = await new Promise((resolve, reject) => {
pool.query(
'SELECT COUNT(DISTINCT user_id) FROM orders',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
);
});
console.log('Total number of customers who have placed orders: ', response2[0]['COUNT(DISTINCT user_id)']);
// av cost of an order
const response3 = await new Promise((resolve, reject) => {
pool.query(
'SELECT AVG(total_amount) FROM orders',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
);
});
console.log('Average cost of an order: ', response3[0]['AVG(total_amount)']);
}
// Function to manage products
async function manageProducts() {
while (true) {
const choice = await questionAsync(
'\nProduct Management:' +
'\nPress 0 to Exit' +
'\nPress 1 to Add Product' +
'\nPress 2 to Update Product' +
'\nPress 3 to Delete Product' +
'\nEnter your choice: '
);
switch (choice) {
case '0':
console.log("Exiting Product Management...");
return;
case '1':
await addProduct();
break;
case '2':
await updateProduct();
break;
case '3':
await deleteProduct();
break;
default:
console.log("\nInvalid input");
console.log("Please enter a valid choice");
}
}
}
// Function to add a new product
async function addProduct() {
try {
await startTransaction();
const productName = await questionAsync("Enter product name: ");
const stock = parseInt(await questionAsync("Enter stock quantity: "));
const category = await questionAsync("Enter category: ");
const subcategory = await questionAsync("Enter subcategory: ");
const description = await questionAsync("Enter description: ");
const price = parseFloat(await questionAsync("Enter price: "));
// Insert product details into the database within the transaction
const insertQuery = `
INSERT INTO Products (product_name, stock, category, subcategory, description, price)
VALUES (?, ?, ?, ?, ?, ?)
`;
await query(insertQuery, [productName, stock, category, subcategory, description, price]);
await commitTransaction();
console.log("Product added successfully.");
} catch (error) {
await rollbackTransaction();
console.error("Error adding product:", error);
}
}
// Function to update an existing product
async function updateProduct() {
try {
await startTransaction();
const productId = parseInt(await questionAsync("Enter product ID to update: "));
const newStock = parseInt(await questionAsync("Enter new stock quantity: "));
const newPrice = parseFloat(await questionAsync("Enter new price: "));
// Update product details in the database within the transaction
const updateQuery = `
UPDATE Products
SET stock = ?, price = ?
WHERE product_id = ?
`;
await query(updateQuery, [newStock, newPrice, productId]);
await commitTransaction();
console.log("Product updated successfully.");
} catch (error) {
await rollbackTransaction();
console.error("Error updating product:", error);
}
}
// Function to delete an existing product
async function deleteProduct() {
try {
await startTransaction();
const productId = parseInt(await questionAsync("Enter product ID to delete: "));
// Delete the product from the database within the transaction
const deleteQuery = `
DELETE FROM Products
WHERE product_id = ?
`;
await query(deleteQuery, [productId]);
await commitTransaction();
console.log("Product deleted successfully.");
} catch (error) {
await rollbackTransaction();
console.error("Error deleting product:", error);
}
}
async function viewSalesReports() {
// total sales
const response1 = await new Promise((resolve, reject) =>
pool.query(
'SELECT SUM(total_amount) FROM payment',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
)
);
console.log('Total sales: ', response1[0]['SUM(total_amount)']);
// out of stock products
const response2 = await new Promise((resolve, reject) =>
pool.query(
'SELECT product_name FROM Products WHERE stock = 0',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
)
);
console.log('Out of stock products: ');
response2.forEach((product) => {
console.log(product['product_name']);
});
}
// review_id int AI PK
// comments varchar(200)
// rating int
// user_id int
// _date date
// product_id int
// Related Tables:
// Target products (product_id → product_id)
// On Update RESTRICT
// On Delete RESTRICT
// Target customer (user_id → user_id)
// On Update RESTRICT
// On Delete RESTRICT
async function viewFeedback() {
// average rating
const response1 = await new Promise((resolve, reject) =>
pool.query(
'SELECT AVG(rating) FROM review',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
)
);
console.log('Average rating: ', response1[0]['AVG(rating)']);
while(true){
const choice = await questionAsync("Press 1 to view all feedbacks, 0 to exit: ");
if(choice === '0'){
break;
}
else if(choice === '1'){
const response2 = await new Promise((resolve, reject) =>
pool.query(
'SELECT * FROM review',
(error, result, fields) => {
if (error) {
reject(error);
}
resolve(result);
}
)
);
console.log('All feedbacks: ');
response2.forEach((feedback) => {
console.log('Review ID: ', feedback['review_id']);
console.log('Comments: ', feedback['comments']);
console.log('Rating: ', feedback['rating']);
console.log('User ID: ', feedback['user_id']);
console.log('Date: ', feedback['_date']);
console.log('Product ID: ', feedback['product_id']);
});
}
else{
console.log("Enter a valid choice");
}
}
}
adminmenu();