diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index bc41b75d..a942a318 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -3024,6 +3024,72 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { } }; +// exports.getManagerPendingOrdersByInstallationId = 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 === 'pending'); + +// 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: "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.getManagerPendingOrdersByInstallationId = async (req, reply) => { try { const { installationId } = req.params; @@ -3043,7 +3109,6 @@ exports.getManagerPendingOrdersByInstallationId = async (req, reply) => { }); } - // 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) { @@ -3060,10 +3125,14 @@ exports.getManagerPendingOrdersByInstallationId = async (req, reply) => { status: "blocked", }).lean(); - // Build response object + // Take work_status from the first pending master (since they all have 'pending') + const work_status = pendingMasters[0].work_status; + + // Build the order object and add work_status beside surveyId ordersWithPendingMasters.push({ ...order.toObject(), - master_connections: pendingMasters, // keep only pending masters + master_connections: pendingMasters, // keep only pending masters + work_status, // add work_status beside surveyId (top-level field) customer: customer || null, allocated_sensors: allocatedSensors, }); @@ -3090,7 +3159,6 @@ exports.getManagerPendingOrdersByInstallationId = async (req, reply) => { } }; - exports.getWaitingManagerPendingOrdersByInstallationId = async (req, reply) => { try { const { installationId } = req.params;