diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 6b588a11..1b97b0c9 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2574,6 +2574,68 @@ exports.getOrdersByStoreId = async (req, reply) => { // } // }; +exports.getOrdersByInstallationId = 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 + const uniqueOrders = Array.from(uniqueCustomersMap.values()); + + // 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: "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; @@ -2582,47 +2644,46 @@ exports.getOrdersByStoreId = async (req, reply) => { // return reply.status(400).send({ error: "installationId is required" }); // } -// // Fetch orders with the matching installationId -// const orders = await Order.find({ installationId }); + + +// // Build query — do NOT filter by work_status yet +// const query = { installationId }; + +// const orders = await Order.find(query); // if (!orders.length) { // return reply.send({ // status_code: 200, -// message: "No orders found for this installation", +// message: "No orders found for this installation and customer", // data: [], // }); // } -// const uniqueCustomersMap = new Map(); +// const ordersWithDetails = []; -// // Build unique customerId-based map // for (const order of orders) { -// if (!uniqueCustomersMap.has(order.customerId)) { -// uniqueCustomersMap.set(order.customerId, order); +// // Ensure work_status is set +// if (!order.work_status || order.work_status.trim() === "") { +// order.work_status = "active"; +// await order.save(); // } -// } - -// // Only keep one order per customerId -// const uniqueOrders = Array.from(uniqueCustomersMap.values()); -// // Enrich with customer and sensor info -// const ordersWithDetails = await Promise.all( -// uniqueOrders.map(async (order) => { +// // ✅ Only push if work_status is "active" +// if (order.work_status === "active") { // const customer = await User.findOne({ customerId: order.customerId }).lean(); - // const allocatedSensors = await Insensors.find({ // storeId: order.storeId, // customerId: order.customerId, // status: "blocked", // }).lean(); -// return { +// ordersWithDetails.push({ // ...order.toObject(), // customer: customer || null, // allocated_sensors: allocatedSensors, -// }; -// }) -// ); +// }); +// } +// } // return reply.send({ // status_code: 200, @@ -2636,69 +2697,6 @@ exports.getOrdersByStoreId = async (req, reply) => { // } // }; -exports.getOrdersByInstallationId = async (req, reply) => { - try { - const { installationId, customerId } = req.params; - - if (!installationId) { - return reply.status(400).send({ error: "installationId is required" }); - } - - if (!customerId) { - return reply.status(400).send({ error: "customerId is required" }); - } - - // Build query — do NOT filter by work_status yet - const query = { installationId, customerId }; - - const orders = await Order.find(query); - - if (!orders.length) { - return reply.send({ - status_code: 200, - message: "No orders found for this installation and customer", - data: [], - }); - } - - const ordersWithDetails = []; - - for (const order of orders) { - // Ensure work_status is set - if (!order.work_status || order.work_status.trim() === "") { - order.work_status = "active"; - await order.save(); - } - - // ✅ Only push if work_status is "active" - if (order.work_status === "active") { - const customer = await User.findOne({ customerId: order.customerId }).lean(); - const allocatedSensors = await Insensors.find({ - storeId: order.storeId, - customerId: order.customerId, - status: "blocked", - }).lean(); - - ordersWithDetails.push({ - ...order.toObject(), - customer: customer || null, - allocated_sensors: allocatedSensors, - }); - } - } - - 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" }); - } -}; - diff --git a/src/routes/storeRoute.js b/src/routes/storeRoute.js index 44801216..3f4c0dfe 100644 --- a/src/routes/storeRoute.js +++ b/src/routes/storeRoute.js @@ -1969,7 +1969,7 @@ fastify.get("/api/ordersofstore/:storeId", { }); -fastify.get("/api/ordersofinstall/:installationId/:customerId", { +fastify.get("/api/ordersofinstall/:installationId", { schema: { tags: ["Installation"], description: "Fetches orders based on installationId", @@ -1979,7 +1979,7 @@ fastify.get("/api/ordersofinstall/:installationId/:customerId", { properties: { installationId: { type: "string" }, //work_status: { type: "string"}, - customerId: { type: "string"}, + //customerId: { type: "string"}, }, // required: ["installationId"], },