diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 7f1936f6..0be9b09a 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2295,6 +2295,56 @@ exports.getOrdersByStoreId = async (req, reply) => { } }; +exports.getOrdersByInstallationId = async (req, reply) => { + try { + const { installationId } = req.params; + + if (!installationId) { + return reply.status(400).send({ error: "storeId is required" }); + } + + // Fetch orders with the matching storeId + const orders = await Order.find({ installationId }); + + if (!orders.length) { + return reply.send({ + status_code: 200, + message: "No orders found for this store", + data: [], + }); + } + + // Fetch customer details & allocated sensors for each order + const ordersWithDetails = await Promise.all( + orders.map(async (order) => { + // Fetch customer details + const customer = await User.findOne({ customerId: order.customerId }).lean(); + + // Fetch allocated sensors for this customer + const allocatedSensors = await Insensors.find({ + installationId, + customerId: order.customerId, // Match only sensors allocated to this customer + status: "blocked", // Only fetch sensors that are allocated (blocked) + }).lean(); + + return { + ...order.toObject(), + customer: customer || null, // Include customer details or null if not found + allocated_sensors: allocatedSensors, // List of allocated sensors + }; + }) + ); + + return reply.send({ + status_code: 200, + message: "Orders fetched successfully", + data: ordersWithDetails, + }); + } catch (err) { + console.error("Error fetching orders:", err); + return reply.status(500).send({ error: "Internal server error" }); + } +}; exports.getallocatedsensorstouser= async (req, reply) => { try { diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 0aed3c70..ef932c60 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1804,6 +1804,26 @@ fastify.get("/api/ordersofstore/:storeId", { }); +fastify.get("/api/ordersofstore/:installationId", { + schema: { + tags: ["Installation"], + description: "Fetches orders based on installationId", + summary: "Get orders by installationId", + params: { + type: "object", + properties: { + storeId: { type: "string" }, + }, + required: ["storeId"], + }, + security: [ + { + basicAuth: [], + }, + ], + }, + handler: storeController.getOrdersByInstallationId, +}); fastify.get("/api/getallocatedsensors/:customerId", {