diff --git a/resources/PD_Orders/pdOrderController.js b/resources/PD_Orders/pdOrderController.js index 5497e9e..4b46bdd 100644 --- a/resources/PD_Orders/pdOrderController.js +++ b/resources/PD_Orders/pdOrderController.js @@ -655,7 +655,67 @@ export const getPlacedNewOrderAdmin = async (req, res) => { return res.status(500).json({ error: "Internal Server Error" }); } }; +export const getPlacedPendingOrderAdmin = 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 pending orders + const totalOrders = await PdOrder.countDocuments({ status: "pending" }); + + // Fetch paginated new orders + const placedOrders = await PdOrder.find({ status: "pending" }) + .sort({ createdAt: -1 }) + .skip(skip) + .limit(limit) + .populate({ path: "orderItem.productId" }) + .populate({ path: "invoices" }) + .populate({ path: "addedBy" }); + + if (placedOrders.length > 0) { + return res.status(200).json({ placedOrders, totalOrders }); + } + return res.status(404).json({ return_msg: "No new orders placed yet" }); + } catch (error) { + console.error("Error fetching new orders:", error); + return res.status(500).json({ error: "Internal Server Error" }); + } +}; + +export const getCancelledOrdersAdmin = 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 cancelled orders + const totalOrders = await PdOrder.countDocuments({ status: "cancelled" }); + + // Fetch paginated cancelled orders + const cancelledOrders = await PdOrder.find({ status: "cancelled" }) + .sort({ createdAt: -1 }) + .skip(skip) + .limit(limit) + .populate({ path: "orderItem.productId" }) + .populate({ path: "invoices" }) + .populate({ path: "addedBy" }); + + if (cancelledOrders.length > 0) { + return res.status(200).json({ cancelledOrders, totalOrders }); + } + return res.status(404).json({ return_msg: "No cancelled orders yet" }); + } catch (error) { + console.error("Error fetching cancelled orders:", error); + return res.status(500).json({ error: "Internal Server Error" }); + } +}; export const getDispatchedOrdersAdmin = async (req, res) => { try { // Extract page and limit from query parameters @@ -685,37 +745,6 @@ export const getDispatchedOrdersAdmin = async (req, res) => { return res.status(500).json({ error: "Internal Server Error" }); } }; - -export const getCancelledOrdersAdmin = 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 cancelled orders - const totalOrders = await PdOrder.countDocuments({ status: "cancelled" }); - - // Fetch paginated cancelled orders - const cancelledOrders = await PdOrder.find({ status: "cancelled" }) - .sort({ createdAt: -1 }) - .skip(skip) - .limit(limit) - .populate({ path: "orderItem.productId" }) - .populate({ path: "addedBy" }); - - if (cancelledOrders.length > 0) { - return res.status(200).json({ cancelledOrders, totalOrders }); - } - return res.status(404).json({ return_msg: "No cancelled orders yet" }); - } catch (error) { - console.error("Error fetching cancelled orders:", error); - return res.status(500).json({ error: "Internal Server Error" }); - } -}; - export const getProcessingOrdersAdmin = async (req, res) => { try { // Extract page and limit from query parameters diff --git a/resources/PD_Orders/pdOrderRoute.js b/resources/PD_Orders/pdOrderRoute.js index 0854cbf..4b71d3b 100644 --- a/resources/PD_Orders/pdOrderRoute.js +++ b/resources/PD_Orders/pdOrderRoute.js @@ -13,6 +13,7 @@ import { updateOrderStatusById, processOrder, cancelOrderController, + getPlacedPendingOrderAdmin, } from "./pdOrderController.js"; const router = express.Router(); @@ -28,10 +29,10 @@ router router .route("/processing-order") .post(isAuthenticatedUser, authorizeRoles("admin"), processOrder); - // Define the route for cancel an order +// Define the route for cancel an order router -.route("/cancel-order") -.post(isAuthenticatedUser, authorizeRoles("admin"), cancelOrderController); + .route("/cancel-order") + .post(isAuthenticatedUser, authorizeRoles("admin"), cancelOrderController); router .route("/get-placed-order-pd") .get( @@ -46,11 +47,18 @@ router .route("/get-new-order-admin") .get(isAuthenticatedUser, authorizeRoles("admin"), getPlacedNewOrderAdmin); router - .route("/get-dispatched-order-admin") - .get(isAuthenticatedUser, authorizeRoles("admin"), getDispatchedOrdersAdmin); + .route("/get-pending-order-admin") + .get( + isAuthenticatedUser, + authorizeRoles("admin"), + getPlacedPendingOrderAdmin + ); router .route("/get-cancelled-order-admin") .get(isAuthenticatedUser, authorizeRoles("admin"), getCancelledOrdersAdmin); +router + .route("/get-dispatched-order-admin") + .get(isAuthenticatedUser, authorizeRoles("admin"), getDispatchedOrdersAdmin); router .route("/get-processing-order-admin") .get(isAuthenticatedUser, authorizeRoles("admin"), getProcessingOrdersAdmin);