master^2
Bhaskar 2 months ago
parent cb4fd9b39a
commit fd37c7a272

@ -3159,71 +3159,71 @@ exports.getManagerPendingOrdersByInstallationId = async (req, reply) => {
} }
}; };
exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => { // exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => {
try { // try {
const { installationId } = req.params; // const { installationId } = req.params;
if (!installationId) { // if (!installationId) {
return reply.status(400).send({ error: "installationId is required" }); // return reply.status(400).send({ error: "installationId is required" });
} // }
// Step 1: Fetch orders matching installationId // // Step 1: Fetch orders matching installationId
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 installation", // message: "No orders found for this installation",
data: [], // data: [],
}); // });
} // }
// 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 === 'pending'
const ordersWithPendingMasters = []; // const ordersWithPendingMasters = [];
for (const order of orders) { // for (const order of orders) {
const pendingMasters = (order.master_connections || []).filter(mc => mc.work_status === 'waiting'); // const pendingMasters = (order.master_connections || []).filter(mc => mc.work_status === 'waiting');
if (pendingMasters.length) { // if (pendingMasters.length) {
// Fetch customer details // // Fetch customer details
const customer = await User.findOne({ customerId: order.customerId }).lean(); // const customer = await User.findOne({ customerId: order.customerId }).lean();
// Fetch allocated sensors (status blocked) // // Fetch allocated sensors (status blocked)
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();
// Build response object // // Build response object
ordersWithPendingMasters.push({ // ordersWithPendingMasters.push({
...order.toObject(), // ...order.toObject(),
master_connections: pendingMasters, // keep only pending masters // master_connections: pendingMasters, // keep only pending masters
customer: customer || null, // customer: customer || null,
allocated_sensors: allocatedSensors, // allocated_sensors: allocatedSensors,
}); // });
} // }
} // }
if (!ordersWithPendingMasters.length) { // if (!ordersWithPendingMasters.length) {
return reply.send({ // return reply.send({
status_code: 200, // status_code: 200,
message: "No pending master connections found for this installation", // message: "No pending master connections found for this installation",
data: [], // data: [],
}); // });
} // }
return reply.send({ // return reply.send({
status_code: 200, // status_code: 200,
message: "Pending orders fetched successfully", // message: "Pending orders fetched successfully",
data: ordersWithPendingMasters, // data: ordersWithPendingMasters,
}); // });
} catch (err) { // } catch (err) {
console.error("Error fetching pending orders:", err); // console.error("Error fetching pending orders:", err);
return reply.status(500).send({ error: "Internal server error" }); // return reply.status(500).send({ error: "Internal server error" });
} // }
}; // };
// exports.getCompleteOrdersByInstallationId = async (req, reply) => { // exports.getCompleteOrdersByInstallationId = async (req, reply) => {
// try { // try {
@ -3429,6 +3429,76 @@ exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => {
// } // }
// }; // };
exports.getWaitingManagerPendingOrdersByInstallationId = 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: [],
});
}
const ordersWithPendingMasters = [];
for (const order of orders) {
// Filter master_connections with work_status === 'waiting'
const pendingMasters = (order.master_connections || []).filter(mc => mc.work_status === 'waiting');
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();
// Take work_status from the first pending master
const work_status = pendingMasters[0].work_status;
// Build the response object
ordersWithPendingMasters.push({
...order.toObject(),
master_connections: pendingMasters, // keep only pending masters
work_status, // add at the top level
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: "Pending 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) => { exports.getCompleteManagerPendingOrdersByInstallationId = async (req, reply) => {
try { try {
const { installationId } = req.params; const { installationId } = req.params;

Loading…
Cancel
Save