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

Loading…
Cancel
Save