132 lines
3.4 KiB
JavaScript
132 lines
3.4 KiB
JavaScript
// Adjust the path to where your PdOrder model is located
|
|
|
|
import mongoose from "mongoose";
|
|
import { PdOrder } from "./pdOrderModal.js";
|
|
|
|
export const createOrder = async (req, res) => {
|
|
try {
|
|
const {
|
|
paymentMode,
|
|
shipTo,
|
|
billTo,
|
|
orderItems,
|
|
subtotal,
|
|
gstTotal,
|
|
grandTotal,
|
|
} = req.body;
|
|
|
|
if (
|
|
!paymentMode ||
|
|
!shipTo ||
|
|
!billTo ||
|
|
!orderItems ||
|
|
!subtotal ||
|
|
!gstTotal ||
|
|
!grandTotal
|
|
) {
|
|
return res.status(400).json({ error: "All fields are required." });
|
|
}
|
|
|
|
// Create the order
|
|
const addedBy = req.user._id;
|
|
const newOrder = new PdOrder({
|
|
paymentMode,
|
|
shipTo,
|
|
billTo,
|
|
orderItem: orderItems.map((item) => ({
|
|
productId: item._id,
|
|
quantity: item.count,
|
|
})),
|
|
subtotal,
|
|
gstTotal,
|
|
grandTotal,
|
|
addedBy,
|
|
});
|
|
|
|
// Save the order to the database
|
|
const savedOrder = await newOrder.save();
|
|
|
|
// Return the created order as a response
|
|
return res.status(201).json({ placedOrder: savedOrder });
|
|
} catch (error) {
|
|
console.error("Error creating order:", error);
|
|
return res.status(500).json({ error: "Internal Server Error" });
|
|
}
|
|
};
|
|
|
|
export const getPlacedOrder = async (req, res) => {
|
|
try {
|
|
// Extract page and limit from query parameters
|
|
const page = parseInt(req.query.page, 10) || 1;
|
|
const limit = parseInt(req.query.limit, 10) || 5;
|
|
|
|
// Calculate the number of documents to skip
|
|
const skip = (page - 1) * limit;
|
|
|
|
// Get the total count of orders
|
|
const totalOrders = await PdOrder.countDocuments({ addedBy: req.user._id });
|
|
|
|
// Fetch paginated orders
|
|
const plcaedOrders = await PdOrder.find({ addedBy: req.user._id })
|
|
.sort({ createdAt: -1 })
|
|
.skip(skip)
|
|
.limit(limit)
|
|
.populate({ path: "orderItem.productId" });
|
|
|
|
if (plcaedOrders.length > 0) {
|
|
return res.status(200).json({ plcaedOrders, totalOrders });
|
|
}
|
|
return res.status(404).json({ return_msg: "Not placed yet" });
|
|
} catch (error) {
|
|
console.error("Error fetching orders:", error);
|
|
return res.status(500).json({ error: "Internal Server Error" });
|
|
}
|
|
};
|
|
|
|
// Get single placed order api
|
|
|
|
export const getPlacedOrderById = async (req, res) => {
|
|
try {
|
|
const id = req.params.id;
|
|
if (!mongoose.Types.ObjectId.isValid(id)) {
|
|
return res
|
|
.status(404)
|
|
.json({ return_msg: "Not Valid id to search the doc " });
|
|
}
|
|
const doc = await PdOrder.findById(id).populate({
|
|
path: "orderItem.productId",
|
|
});
|
|
if (doc) {
|
|
return res
|
|
.status(200)
|
|
.json({ singleOrder: doc, return_msg: "Doc found" });
|
|
}
|
|
return res.status(404).json({ return_msg: "Not Found doc " });
|
|
} catch (error) {
|
|
console.log(error);
|
|
return res.status(500).json({ return_msg: "Internal Server Error" });
|
|
}
|
|
};
|
|
|
|
export const getPlacedOrderAdmin = async (req, res) => {
|
|
try {
|
|
const plcaedOrders = await PdOrder.find()
|
|
.sort({
|
|
createdAt: -1,
|
|
})
|
|
.populate({
|
|
path: "orderItem.productId",
|
|
})
|
|
.populate({
|
|
path: "addedBy",
|
|
});
|
|
if (plcaedOrders) {
|
|
return res.status(200).json({ plcaedOrders });
|
|
}
|
|
return res.status(404).json({ return_msg: "Not placed yet " });
|
|
} catch (error) {
|
|
console.error("Error creating order:", error);
|
|
return res.status(500).json({ error: "Internal Server Error" });
|
|
}
|
|
};
|