From c45d1c023b8ae280b815a9c3085dea14914b60fd Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Mon, 21 Jul 2025 13:12:13 +0530 Subject: [PATCH] changes --- src/controllers/storeController.js | 106 ++++++++++++++++++++++++++--- 1 file changed, 98 insertions(+), 8 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index ba225f71..56c6a90f 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2939,6 +2939,91 @@ exports.getWaitingOrdersByInstallationAndTeamMember = async (req, reply) => { } }; +// exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { +// try { +// const { installationId, teamMemberId } = req.params; + +// if (!installationId) { +// return reply.status(400).send({ error: "installationId is required" }); +// } +// if (!teamMemberId) { +// return reply.status(400).send({ error: "teamMemberId is required" }); +// } + +// // Fetch orders matching installationId and assignedTeamMembers +// const orders = await Order.find({ +// installationId, +// assignedTeamMembers: teamMemberId +// }); + +// if (!orders.length) { +// return reply.send({ +// status_code: 200, +// message: "No orders found for this installation and team member", +// 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); +// } +// } + +// let uniqueOrders = Array.from(uniqueCustomersMap.values()); + +// // ✅ Filter orders that have at least one pending master_connection +// uniqueOrders = uniqueOrders.filter(order => +// Array.isArray(order.master_connections) && +// order.master_connections.some(mc => mc.work_status === 'complete') +// ); + +// if (!uniqueOrders.length) { +// return reply.send({ +// status_code: 200, +// message: "No pending orders found for this installation and team member", +// data: [], +// }); +// } + +// // Enrich and also filter master_connections inside each order +// 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(); + +// // Keep only master_connections with work_status === 'pending' +// const pendingMasters = order.master_connections.filter(mc => mc.work_status === 'complete'); + +// return { +// ...order.toObject(), +// master_connections: pendingMasters, +// customer: customer || null, +// allocated_sensors: allocatedSensors, +// }; +// }) +// ); + +// return reply.send({ +// status_code: 200, +// message: "Pending orders fetched successfully", +// data: ordersWithDetails, +// }); + +// } catch (err) { +// console.error("Error fetching pending orders:", err); +// return reply.status(500).send({ error: "Internal server error" }); +// } +// }; + exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { try { const { installationId, teamMemberId } = req.params; @@ -2975,7 +3060,7 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { let uniqueOrders = Array.from(uniqueCustomersMap.values()); - // ✅ Filter orders that have at least one pending master_connection + // ✅ Filter orders that have at least one master_connection with work_status === 'complete' uniqueOrders = uniqueOrders.filter(order => Array.isArray(order.master_connections) && order.master_connections.some(mc => mc.work_status === 'complete') @@ -2984,12 +3069,12 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { if (!uniqueOrders.length) { return reply.send({ status_code: 200, - message: "No pending orders found for this installation and team member", + message: "No complete orders found for this installation and team member", data: [], }); } - // Enrich and also filter master_connections inside each order + // Enrich orders and add work_status at top level const ordersWithDetails = await Promise.all( uniqueOrders.map(async (order) => { const customer = await User.findOne({ customerId: order.customerId }).lean(); @@ -3000,12 +3085,16 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { status: "blocked", }).lean(); - // Keep only master_connections with work_status === 'pending' - const pendingMasters = order.master_connections.filter(mc => mc.work_status === 'complete'); + // Keep only master_connections with work_status === 'complete' + const completeMasters = order.master_connections.filter(mc => mc.work_status === 'complete'); + + // Take work_status from the first matched master (which will be 'complete') + const work_status = completeMasters[0]?.work_status || null; return { ...order.toObject(), - master_connections: pendingMasters, + master_connections: completeMasters, + work_status, // add at top level customer: customer || null, allocated_sensors: allocatedSensors, }; @@ -3014,16 +3103,17 @@ exports.getCompleteOrdersByInstallationAndTeamMember = async (req, reply) => { return reply.send({ status_code: 200, - message: "Pending orders fetched successfully", + message: "Complete orders fetched successfully", data: ordersWithDetails, }); } 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" }); } }; + // exports.getManagerPendingOrdersByInstallationId = async (req, reply) => { // try { // const { installationId } = req.params;