master^2
Bhaskar 7 months ago
parent f77dac00f8
commit 7069317d93

@ -2592,23 +2592,33 @@ exports.getOrdersByInstallationId = async (req, reply) => {
}); });
} }
// Fetch customer details & allocated sensors for each order 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( const ordersWithDetails = await Promise.all(
orders.map(async (order) => { uniqueOrders.map(async (order) => {
// Fetch customer details
const customer = await User.findOne({ customerId: order.customerId }).lean(); const customer = await User.findOne({ customerId: order.customerId }).lean();
// Fetch allocated sensors for this customer
const allocatedSensors = await Insensors.find({ const allocatedSensors = await Insensors.find({
storeId: order.storeId, storeId: order.storeId,
customerId: order.customerId, // Match only sensors allocated to this customer customerId: order.customerId,
status: "blocked", // Only fetch sensors that are allocated (blocked) status: "blocked",
}).lean(); }).lean();
return { return {
...order.toObject(), ...order.toObject(),
customer: customer || null, // Include customer details or null if not found customer: customer || null,
allocated_sensors: allocatedSensors, // List of allocated sensors allocated_sensors: allocatedSensors,
}; };
}) })
); );
@ -2618,6 +2628,7 @@ exports.getOrdersByInstallationId = async (req, reply) => {
message: "Orders fetched successfully", message: "Orders fetched successfully",
data: ordersWithDetails, data: ordersWithDetails,
}); });
} catch (err) { } catch (err) {
console.error("Error fetching orders:", err); console.error("Error fetching orders:", err);
return reply.status(500).send({ error: "Internal server error" }); return reply.status(500).send({ error: "Internal server error" });
@ -2627,6 +2638,7 @@ exports.getOrdersByInstallationId = async (req, reply) => {
exports.getallocatedsensorstouser= async (req, reply) => { exports.getallocatedsensorstouser= async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;

Loading…
Cancel
Save