-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathorderController.js
More file actions
143 lines (115 loc) · 3.54 KB
/
orderController.js
File metadata and controls
143 lines (115 loc) · 3.54 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
const Order = require('../models/orderModel')
const catchAsync = require('../middleware/catchAsyncError');
const Product = require('../models/productModel');
const ErrorHandler = require('../utils/errorHandler');
// Create new Order
exports.newOrder = catchAsync(async (req, res, next) => {
const { shippingInfo, orderItems, paymentInfo, itemsPrice, taxPrice, shippingPrice, totalPrice } = req.body;
const order = await Order.create({
shippingInfo,
orderItems,
paymentInfo,
itemsPrice,
taxPrice,
shippingPrice,
totalPrice,
paidAt: Date.now(),
user: req.user._id,
})
res.status(200).json({
success: true,
order
})
})
// @route GET /api/order/:id
// @desc Get order details by ID
// @access Private
exports.getSingeOrder = catchAsync(async (req, res) => {
try {
const order = await Order.findById(req.params.id).populate(
"user",
"name email",
);
if (!order) {
return res.status(404).json({ message: "Order not found" })
}
//Return the full order details
res.json(order)
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server Error" })
}
})
// @route GET /api/orders/my-orders
// @desc Get logged-in user's orders
// @access Private
exports.myOrders = catchAsync(async (req, res,) => {
try {
// Find order for authenticated user
const order = await Order.find({ user: req.user._id }).sort({
createdAt: -1,
}); // sort most recent orders
res.json(order)
} catch (error) {
console.error(error);
res.status(500).json({ message: "Server Error" })
}
});
// Get all orders -- (Admin)
exports.getAllOrders = catchAsync(async (req, res, next) => {
const orders = await Order.find()
let totalAmount = 0;
orders.forEach((order) => {
totalAmount += order.totalPrice
});
res.status(200).json({
success: true,
totalAmount,
orders,
})
})
// Update Order Status -- (Admin)
exports.updateOrder = catchAsync(async (req, res, next) => {
const order = await Order.findById(req.params.id);
if (!order) {
return next(new ErrorHandler("Order not found with this ID", 404));
}
if (!order) {
return next(new ErrorHandler("Order not found with this ID", 404));
}
if (order.orderStatus === "Delivered") {
return next(new ErrorHandler("You have already delivered this order", 400));
}
order.orderItems.forEach(async (item) => {
await updateStock(item.product, item.quantity);
});
order.orderStatus = req.body.status;
if (req.body.status === "Delivered") {
order.deliveredAt = Date.now();
}
await order.save({ validateBeforeSave: false });
res.status(200).json({
success: true,
order,
});
});
async function updateStock(id, quantity) {
const product = await Product.findById(id);
if (!product) {
throw new ErrorHandler("Product not found", 404);
}
product.Stock -= quantity;
await product.save({ validateBeforeSave: false });
}
// Delete orders -- (Admin)
exports.deleteOrder = catchAsync(async (req, res, next) => {
const order = await Order.findById(req.params.id);
if (!order) {
return next(new ErrorHandler("Order not found with this ID", 404));
}
await order.deleteOne();
res.status(200).json({
success: true,
message: "Order deleted successfully"
});
});