Varun 7 months ago
commit 76cb85ee76

@ -2334,37 +2334,41 @@ exports.getOrdersByInstallationId = async (req, reply) => {
const { installationId } = req.params; const { installationId } = req.params;
if (!installationId) { if (!installationId) {
return reply.status(400).send({ error: "storeId is required" }); return reply.status(400).send({ error: "installationId is required" });
} }
// Fetch orders with the matching storeId
const orders = await Order.find({ installationId }); const orders = await Order.find({ installationId });
if (!orders.length) { if (!orders.length) {
return reply.send({ return reply.send({
status_code: 200, status_code: 200,
message: "No orders found for this store", message: "No orders found for this installationId",
data: [], data: [],
}); });
} }
// Fetch customer details & allocated sensors for each order
const ordersWithDetails = await Promise.all( const ordersWithDetails = await Promise.all(
orders.map(async (order) => { orders.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 console.log("Fetching allocated sensors for:", {
storeId: order.storeId,
customerId: order.customerId,
status: "blocked",
});
const allocatedSensors = await Insensors.find({ const allocatedSensors = await Insensors.find({
installationId, 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();
console.log("✅ Found Sensors:", allocatedSensors);
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, // Now it should return the correct sensors
}; };
}) })
); );
@ -2375,11 +2379,12 @@ exports.getOrdersByInstallationId = async (req, reply) => {
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" });
} }
}; };
exports.getallocatedsensorstouser= async (req, reply) => { exports.getallocatedsensorstouser= async (req, reply) => {
try { try {
const { customerId } = req.params; const { customerId } = req.params;

Loading…
Cancel
Save