-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadmin.js
162 lines (111 loc) · 4.02 KB
/
admin.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
const express = require("express");
const admin = require("../middlewares/admin");
const {Product} = require("../model/product");
const Order = require("../model/order");
const adminRouter = express.Router();
adminRouter.post("/admin/add-product", admin , async (req, res) => {
try {
const {name, description , images, quantity, price, category} = req.body;
let product = new Product({name, description, images, quantity, price, category});
product = await product.save();
res.json(product);
} catch (err) {
res.status(500).json({error: err.message});
}
});
adminRouter.get("/admin/get-products", admin, async (req, res) => {
try {
const products = await Product.find({});
res.json(products);
} catch (err) {
res.status(500).json({error : err.message});
}
});
adminRouter.get("/admin/get-category-product/:category", admin, async (req, res) => {
try {
const { category } = req.params;
let products = await Product.find({
'category' : category,
});
res.json(products);
} catch (e) {
res.status(500).json({error : e.message});
}
});
adminRouter.post("/admin/delete-product", admin , async (req, res) => {
try {
const {id} = req.body;
let product = await Product.findByIdAndDelete(id);
res.json(product);
} catch (err) {
res.status(500).json({error : err.message});
}
});
adminRouter.get("/admin/get-orders", admin , async (req, res) => {
try {
const orders = await Order.find({});
res.json(orders);
} catch (e) {
res.status(500).json({error : e.message});
}
});
adminRouter.post("/admin/change-order-status", admin , async (req, res) => {
try {
const { status, id } = req.body;
let order = await Order.findById(id);
order.status = status;
order = await order.save();
res.json(order);
} catch (e) {
res.status(500).json({error : e.message});
}
});
adminRouter.get("/admin/analytics", admin, async ( req, res) => {
try {
const orders = await Order.find({});
let totalEarnings = 0;
for( let i = 0; i< orders.length; i++){
for( let j = 0; j < orders[i].products.length ; j++){
totalEarnings += orders[i].products[j].quantity * orders[i].products[j].product.price;
}
}
// CATEGORY WISE ORDERS FETCHING
let mobileEarnings = await fetchCategoryWiseProduct('Mobiles');
let fashionEarnings = await fetchCategoryWiseProduct('Fashion');
let electronicsEarnings = await fetchCategoryWiseProduct('Electronics');
let homeEarnings = await fetchCategoryWiseProduct('Home');
let beautyEarnings = await fetchCategoryWiseProduct('Beauty');
let appliancesEarnings = await fetchCategoryWiseProduct('Appliances');
let groceryEarnings = await fetchCategoryWiseProduct('Grocery');
let booksEarnings = await fetchCategoryWiseProduct('Books');
let essentialsEarnings = await fetchCategoryWiseProduct('Essentials');
let earnings = {
totalEarnings,
mobileEarnings,
fashionEarnings,
electronicsEarnings,
homeEarnings,
beautyEarnings,
appliancesEarnings,
groceryEarnings,
booksEarnings,
essentialsEarnings,
}
res.json(earnings);
} catch (e) {
res.status(500).json({error : e.message});
}
})
async function fetchCategoryWiseProduct(category){
let earnings = 0;
let categoryOrders = await Order.find({
'products.product.category' : category,
});
for(let i=0; i<categoryOrders.length; i++){
for (let j=0; j< categoryOrders[i].products.length; j++){
earnings += categoryOrders[i].products[j].quantity * categoryOrders[i].products[j].product.price;
}
}
return earnings;
}
module.exports = adminRouter;