ashok 2 months ago
commit 232c667f80

@ -3589,6 +3589,72 @@ exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => {
}
};
// exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) => {
// try {
// const { installationId } = req.params;
// if (!installationId) {
// return reply.status(400).send({ error: "installationId is required" });
// }
// // Step 1: Fetch orders 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: [],
// });
// }
// // Step 2: Filter orders to keep only those having at least one master_connection with work_status === 'pending'
// const ordersWithPendingMasters = [];
// for (const order of orders) {
// const pendingMasters = (order.master_connections || []).filter(mc => mc.work_status === 'complete');
// if (pendingMasters.length) {
// // Fetch customer details
// const customer = await User.findOne({ customerId: order.customerId }).lean();
// // Fetch allocated sensors (status blocked)
// const allocatedSensors = await Insensors.find({
// storeId: order.storeId,
// customerId: order.customerId,
// status: "blocked",
// }).lean();
// // Build response object
// ordersWithPendingMasters.push({
// ...order.toObject(),
// master_connections: pendingMasters, // keep only pending masters
// customer: customer || null,
// allocated_sensors: allocatedSensors,
// });
// }
// }
// if (!ordersWithPendingMasters.length) {
// return reply.send({
// status_code: 200,
// message: "No pending master connections found for this installation",
// data: [],
// });
// }
// return reply.send({
// status_code: 200,
// message: "Complete orders fetched successfully",
// data: ordersWithPendingMasters,
// });
// } catch (err) {
// console.error("Error fetching pending orders:", err);
// return reply.status(500).send({ error: "Internal server error" });
// }
// };
exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) => {
try {
const { installationId } = req.params;
@ -3608,13 +3674,13 @@ exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) =>
});
}
// Step 2: Filter orders to keep only those having at least one master_connection with work_status === 'pending'
// Step 2: Filter orders to keep only those having at least one master_connection with work_status === 'complete'
const ordersWithPendingMasters = [];
for (const order of orders) {
const pendingMasters = (order.master_connections || []).filter(mc => mc.work_status === 'complete');
const completeMasters = (order.master_connections || []).filter(mc => mc.work_status === 'complete');
if (pendingMasters.length) {
if (completeMasters.length) {
// Fetch customer details
const customer = await User.findOne({ customerId: order.customerId }).lean();
@ -3625,10 +3691,14 @@ exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) =>
status: "blocked",
}).lean();
// Add work_status at top level (from the first complete master)
const work_status = completeMasters[0]?.work_status || null;
// Build response object
ordersWithPendingMasters.push({
...order.toObject(),
master_connections: pendingMasters, // keep only pending masters
master_connections: completeMasters, // keep only complete masters
work_status, // add top-level work_status
customer: customer || null,
allocated_sensors: allocatedSensors,
});
@ -3638,7 +3708,7 @@ exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) =>
if (!ordersWithPendingMasters.length) {
return reply.send({
status_code: 200,
message: "No pending master connections found for this installation",
message: "No complete master connections found for this installation",
data: [],
});
}
@ -3650,7 +3720,7 @@ exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) =>
});
} catch (err) {
console.error("Error fetching pending orders:", err);
console.error("Error fetching complete orders:", err);
return reply.status(500).send({ error: "Internal server error" });
}
};

Loading…
Cancel
Save