master^2
Bhaskar 5 months ago
parent d09b951966
commit 8f82de0ddf

@ -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" });

Loading…
Cancel
Save