From 8f82de0ddf1cb5665f19e97ff9707733d31d137b Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Tue, 20 May 2025 15:40:25 +0530 Subject: [PATCH] changes --- src/controllers/storeController.js | 132 +++++++++++++++++++++-------- 1 file changed, 97 insertions(+), 35 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 25d2abc2..fd4deef1 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2507,6 +2507,72 @@ 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 all orders by installationId +// const orders = await Order.find({ installationId }); + +// if (!orders.length) { +// return reply.send({ +// status_code: 200, +// message: "No orders found for this installation", +// data: { +// customers: [], +// orders: [] +// } +// }); +// } + +// // Get unique customerIds from orders +// const uniqueCustomerIds = [...new Set(orders.map(order => order.customerId))]; + +// // Fetch all customers in a single query +// const customers = await User.find({ customerId: { $in: uniqueCustomerIds } }).lean(); + +// // Map customerId -> customer object +// const customerMap = {}; +// customers.forEach(c => { +// customerMap[c.customerId] = c; +// }); + +// // For each order, attach allocated sensors only +// const ordersWithSensors = await Promise.all( +// orders.map(async (order) => { +// const allocatedSensors = await Insensors.find({ +// storeId: order.storeId, +// customerId: order.customerId, +// status: "blocked" +// }).lean(); + +// return { +// ...order.toObject(), +// allocated_sensors: allocatedSensors +// }; +// }) +// ); + +// return reply.send({ +// status_code: 200, +// message: "Orders fetched successfully", +// data: customers +// // data: { +// // customers, +// // // orders: ordersWithSensors +// // } +// }); + +// } 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; @@ -2515,58 +2581,54 @@ exports.getOrdersByInstallationId = async (req, reply) => { return reply.status(400).send({ error: "installationId is required" }); } - // Fetch all orders by installationId - const orders = await Order.find({ installationId }); + // Fetch orders with the matching installationId + const orders = await Order.find({ installationId }).lean(); if (!orders.length) { return reply.send({ status_code: 200, message: "No orders found for this installation", - data: { - customers: [], - orders: [] - } + data: [], }); } - // Get unique customerIds from orders - const uniqueCustomerIds = [...new Set(orders.map(order => order.customerId))]; + // Group orders by customerId + const groupedByCustomer = {}; - // Fetch all customers in a single query - const customers = await User.find({ customerId: { $in: uniqueCustomerIds } }).lean(); + for (const order of orders) { + const { customerId, storeId } = order; - // Map customerId -> customer object - const customerMap = {}; - customers.forEach(c => { - customerMap[c.customerId] = c; - }); + // Fetch allocated sensors + const allocatedSensors = await Insensors.find({ + storeId, + customerId, + status: "blocked", + }).lean(); - // For each order, attach allocated sensors only - const ordersWithSensors = await Promise.all( - orders.map(async (order) => { - const allocatedSensors = await Insensors.find({ - storeId: order.storeId, - customerId: order.customerId, - status: "blocked" - }).lean(); + const enrichedOrder = { + ...order, + allocated_sensors: allocatedSensors, + }; - return { - ...order.toObject(), - allocated_sensors: allocatedSensors + if (!groupedByCustomer[customerId]) { + // Fetch customer once + const customer = await User.findOne({ customerId }).lean(); + groupedByCustomer[customerId] = { + customer: customer || null, + orders: [enrichedOrder], }; - }) - ); + } else { + groupedByCustomer[customerId].orders.push(enrichedOrder); + } + } + + const response = Object.values(groupedByCustomer); return reply.send({ status_code: 200, - message: "Orders fetched successfully", - data: customers - // data: { - // customers, - // // orders: ordersWithSensors - // } + message: "Orders grouped by customer fetched successfully", + data: response, }); - } catch (err) { console.error("Error fetching orders:", err); return reply.status(500).send({ error: "Internal server error" });