diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index a942a318..ba225f71 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -3159,71 +3159,71 @@ exports.getManagerPendingOrdersByInstallationId = async (req, reply) => { } }; -exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => { - try { - const { installationId } = req.params; +// exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => { +// try { +// const { installationId } = req.params; - if (!installationId) { - return reply.status(400).send({ error: "installationId is required" }); - } +// if (!installationId) { +// return reply.status(400).send({ error: "installationId is required" }); +// } - // Step 1: Fetch orders matching installationId - const orders = await Order.find({ installationId }); +// // 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: [], - }); - } +// 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 = []; +// // 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 === 'waiting'); +// for (const order of orders) { +// 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(); +// 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(); +// // 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, - }); - } - } +// // 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: [], - }); - } +// 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, - }); +// 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" }); - } -}; +// } catch (err) { +// console.error("Error fetching pending orders:", err); +// return reply.status(500).send({ error: "Internal server error" }); +// } +// }; // exports.getCompleteOrdersByInstallationId = async (req, reply) => { // 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) => { try { const { installationId } = req.params;