Get orders by installationId chnages

master^2
Bhaskar 3 months ago
parent 6a1f78e172
commit 4f4eca0e14

@ -2574,6 +2574,68 @@ 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 orders with the matching installationId
const orders = await Order.find({ installationId });
if (!orders.length) {
return reply.send({
status_code: 200,
message: "No orders found for this installation",
data: [],
});
}
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(
uniqueOrders.map(async (order) => {
const customer = await User.findOne({ customerId: order.customerId }).lean();
const allocatedSensors = await Insensors.find({
storeId: order.storeId,
customerId: order.customerId,
status: "blocked",
}).lean();
return {
...order.toObject(),
customer: customer || null,
allocated_sensors: allocatedSensors,
};
})
);
return reply.send({
status_code: 200,
message: "Orders fetched successfully",
data: ordersWithDetails,
});
} catch (err) {
console.error("Error fetching orders:", err);
return reply.status(500).send({ error: "Internal server error" });
}
};
// exports.getOrdersByInstallationId = async (req, reply) => { // exports.getOrdersByInstallationId = async (req, reply) => {
// try { // try {
// const { installationId } = req.params; // const { installationId } = req.params;
@ -2582,47 +2644,46 @@ exports.getOrdersByStoreId = async (req, reply) => {
// return reply.status(400).send({ error: "installationId is required" }); // return reply.status(400).send({ error: "installationId is required" });
// } // }
// // Fetch orders with the matching installationId
// const orders = await Order.find({ installationId });
// // Build query — do NOT filter by work_status yet
// const query = { installationId };
// const orders = await Order.find(query);
// if (!orders.length) { // if (!orders.length) {
// return reply.send({ // return reply.send({
// status_code: 200, // status_code: 200,
// message: "No orders found for this installation", // message: "No orders found for this installation and customer",
// data: [], // data: [],
// }); // });
// } // }
// const uniqueCustomersMap = new Map(); // const ordersWithDetails = [];
// // Build unique customerId-based map
// for (const order of orders) { // for (const order of orders) {
// if (!uniqueCustomersMap.has(order.customerId)) { // // Ensure work_status is set
// uniqueCustomersMap.set(order.customerId, order); // if (!order.work_status || order.work_status.trim() === "") {
// order.work_status = "active";
// await order.save();
// } // }
// }
// // Only keep one order per customerId
// const uniqueOrders = Array.from(uniqueCustomersMap.values());
// // Enrich with customer and sensor info // // ✅ Only push if work_status is "active"
// const ordersWithDetails = await Promise.all( // if (order.work_status === "active") {
// uniqueOrders.map(async (order) => {
// const customer = await User.findOne({ customerId: order.customerId }).lean(); // const customer = await User.findOne({ customerId: order.customerId }).lean();
// const allocatedSensors = await Insensors.find({ // const allocatedSensors = await Insensors.find({
// storeId: order.storeId, // storeId: order.storeId,
// customerId: order.customerId, // customerId: order.customerId,
// status: "blocked", // status: "blocked",
// }).lean(); // }).lean();
// return { // ordersWithDetails.push({
// ...order.toObject(), // ...order.toObject(),
// customer: customer || null, // customer: customer || null,
// allocated_sensors: allocatedSensors, // allocated_sensors: allocatedSensors,
// }; // });
// }) // }
// ); // }
// return reply.send({ // return reply.send({
// status_code: 200, // status_code: 200,
@ -2636,69 +2697,6 @@ exports.getOrdersByStoreId = async (req, reply) => {
// } // }
// }; // };
exports.getOrdersByInstallationId = async (req, reply) => {
try {
const { installationId, customerId } = req.params;
if (!installationId) {
return reply.status(400).send({ error: "installationId is required" });
}
if (!customerId) {
return reply.status(400).send({ error: "customerId is required" });
}
// Build query — do NOT filter by work_status yet
const query = { installationId, customerId };
const orders = await Order.find(query);
if (!orders.length) {
return reply.send({
status_code: 200,
message: "No orders found for this installation and customer",
data: [],
});
}
const ordersWithDetails = [];
for (const order of orders) {
// Ensure work_status is set
if (!order.work_status || order.work_status.trim() === "") {
order.work_status = "active";
await order.save();
}
// ✅ Only push if work_status is "active"
if (order.work_status === "active") {
const customer = await User.findOne({ customerId: order.customerId }).lean();
const allocatedSensors = await Insensors.find({
storeId: order.storeId,
customerId: order.customerId,
status: "blocked",
}).lean();
ordersWithDetails.push({
...order.toObject(),
customer: customer || null,
allocated_sensors: allocatedSensors,
});
}
}
return reply.send({
status_code: 200,
message: "Orders fetched successfully",
data: ordersWithDetails,
});
} catch (err) {
console.error("Error fetching orders:", err);
return reply.status(500).send({ error: "Internal server error" });
}
};

@ -1969,7 +1969,7 @@ fastify.get("/api/ordersofstore/:storeId", {
}); });
fastify.get("/api/ordersofinstall/:installationId/:customerId", { fastify.get("/api/ordersofinstall/:installationId", {
schema: { schema: {
tags: ["Installation"], tags: ["Installation"],
description: "Fetches orders based on installationId", description: "Fetches orders based on installationId",
@ -1979,7 +1979,7 @@ fastify.get("/api/ordersofinstall/:installationId/:customerId", {
properties: { properties: {
installationId: { type: "string" }, installationId: { type: "string" },
//work_status: { type: "string"}, //work_status: { type: "string"},
customerId: { type: "string"}, //customerId: { type: "string"},
}, },
// required: ["installationId"], // required: ["installationId"],
}, },

Loading…
Cancel
Save