diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 59571852..133f277d 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; @@ -2603,7 +2665,19 @@ exports.getOrdersByInstallationId = async (req, reply) => { } // Only keep one order per customerId - const uniqueOrders = Array.from(uniqueCustomersMap.values()); + let uniqueOrders = Array.from(uniqueCustomersMap.values()); + + // ✅ Filter: keep only orders where work_status is 'active' + uniqueOrders = uniqueOrders.filter(order => order.work_status === 'active'); + + // If no orders left after filtering, return empty list + if (!uniqueOrders.length) { + return reply.send({ + status_code: 200, + message: "No active orders found for this installation", + data: [], + }); + } // Enrich with customer and sensor info const ordersWithDetails = await Promise.all( @@ -2626,7 +2700,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { return reply.send({ status_code: 200, - message: "Orders fetched successfully", + message: "Active orders fetched successfully", data: ordersWithDetails, }); @@ -2636,6 +2710,7 @@ exports.getOrdersByInstallationId = async (req, reply) => { } }; + exports.getPendingOrdersByInstallationId = async (req, reply) => { try { const { installationId } = req.params;