From dc32a8dd3169f6d8c1f01fb554143d88022a30bd Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 10 Jul 2025 15:08:22 +0530 Subject: [PATCH] get the complete orders list --- src/controllers/storeController.js | 74 ++++++++++++++++++++++++++++++ src/routes/storeRoute.js | 23 ++++++++++ 2 files changed, 97 insertions(+) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 381f5609..f724c4f9 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2785,6 +2785,80 @@ exports.getPendingOrdersByInstallationId = async (req, reply) => { } }; +exports.getPendingOrdersByInstallationId = async (req, reply) => { + try { + const { installationId } = req.params; + + if (!installationId) { + return reply.status(400).send({ error: "installationId is required" }); + } + + // Fetch orders with the matching installationId + const orders = await Order.find({ installationId }); + + if (!orders.length) { + return reply.send({ + status_code: 200, + message: "No orders found for this installation", + data: [], + }); + } + + const uniqueCustomersMap = new Map(); + + // Build unique customerId-based map + for (const order of orders) { + if (!uniqueCustomersMap.has(order.customerId)) { + uniqueCustomersMap.set(order.customerId, order); + } + } + + // Only keep one order per customerId + let uniqueOrders = Array.from(uniqueCustomersMap.values()); + + // ✅ Filter: keep only those where work_status is "pending" + uniqueOrders = uniqueOrders.filter(order => order.work_status === 'complete'); + + // If nothing left after filtering, return empty + if (!uniqueOrders.length) { + return reply.send({ + status_code: 200, + message: "No pending orders found for this installation", + data: [], + }); + } + + // Enrich with customer and sensor info + const ordersWithDetails = await Promise.all( + uniqueOrders.map(async (order) => { + const customer = await User.findOne({ customerId: order.customerId }).lean(); + + const allocatedSensors = await Insensors.find({ + storeId: order.storeId, + customerId: order.customerId, + status: "blocked", + }).lean(); + + return { + ...order.toObject(), + customer: customer || null, + allocated_sensors: allocatedSensors, + }; + }) + ); + + return reply.send({ + status_code: 200, + message: "Complete orders fetched successfully", + data: ordersWithDetails, + }); + + } catch (err) { + console.error("Error fetching orders:", err); + return reply.status(500).send({ error: "Internal server error" }); + } +}; + // exports.getOrdersByInstallationId = async (req, reply) => { // try { // const { installationId } = req.params; diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 39aeb500..c83b29fc 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -2016,6 +2016,29 @@ fastify.get("/api/Pendingordersofinstall/:installationId", { }); +fastify.get("/api/Completeordersofinstall/:installationId", { + schema: { + tags: ["Installation"], + description: "Fetches orders based on installationId", + summary: "Get Complete orders by installationId", + params: { + type: "object", + properties: { + installationId: { type: "string" }, + //work_status: { type: "string"}, + //customerId: { type: "string"}, + }, + // required: ["installationId"], + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: storeController.getPendingOrdersByInstallationId, +}); + fastify.post( '/api/orders/:installationId/:customerId', {