From 52051930f3c87d8b94f46ac98029096840035203 Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Fri, 11 Jul 2025 11:45:45 +0530 Subject: [PATCH] assigned team member detials added --- src/controllers/storeController.js | 86 +++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 8 deletions(-) diff --git a/src/controllers/storeController.js b/src/controllers/storeController.js index 3fea8be0..ae9faafa 100644 --- a/src/controllers/storeController.js +++ b/src/controllers/storeController.js @@ -2636,6 +2636,60 @@ exports.getOrdersByStoreId = async (req, reply) => { // } // }; +// exports.getOrdersByInstallationId = async (req, reply) => { +// try { +// const { installationId } = req.params; + +// if (!installationId) { +// return reply.status(400).send({ error: "installationId is required" }); +// } + +// // Step 1: Fetch orders by 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 ordersWithActiveMasters = []; + +// // Step 2: Process each order +// for (const order of orders) { +// // Filter master_connections to keep only those where work_status is 'active' or empty/null +// const activeMasters = (order.master_connections || []).filter(mc => +// mc.work_status === 'active' || mc.work_status === '' || mc.work_status == null +// ); + +// if (activeMasters.length) { +// // Fetch customer details +// const customer = await User.findOne({ customerId: order.customerId }).lean(); + +// // Build response object +// ordersWithActiveMasters.push({ +// ...order.toObject(), +// master_connections: activeMasters, // only active master_connections +// customer: customer || null, +// }); +// } +// } + +// // Step 3: Return response +// return reply.send({ +// status_code: 200, +// message: "Orders with active master connections fetched successfully", +// data: ordersWithActiveMasters, +// }); + +// } catch (err) { +// console.error("Error fetching orders:", err); +// return reply.status(500).send({ error: "Internal server error" }); +// } +// }; + exports.getOrdersByInstallationId = async (req, reply) => { try { const { installationId } = req.params; @@ -2655,11 +2709,14 @@ exports.getOrdersByInstallationId = async (req, reply) => { }); } - const ordersWithActiveMasters = []; + const ordersWithDetails = []; + + // Fetch installation document once (contains all team members) + const installationDoc = await Install.findOne({ installationId }).lean(); // Step 2: Process each order for (const order of orders) { - // Filter master_connections to keep only those where work_status is 'active' or empty/null + // Filter master_connections to keep active ones const activeMasters = (order.master_connections || []).filter(mc => mc.work_status === 'active' || mc.work_status === '' || mc.work_status == null ); @@ -2668,20 +2725,34 @@ exports.getOrdersByInstallationId = async (req, reply) => { // Fetch customer details const customer = await User.findOne({ customerId: order.customerId }).lean(); - // Build response object - ordersWithActiveMasters.push({ + // Find assigned team members from installation.team_member.team_member + let assignedTeamMembersDetails = []; + if ( + installationDoc && + installationDoc.team_member && + Array.isArray(installationDoc.team_member.team_member) && + order.assignedTeamMembers && + order.assignedTeamMembers.length > 0 + ) { + assignedTeamMembersDetails = installationDoc.team_member.team_member.filter(tm => + order.assignedTeamMembers.includes(tm.teamMemberId) + ); + } + + // Build response + ordersWithDetails.push({ ...order.toObject(), - master_connections: activeMasters, // only active master_connections + master_connections: activeMasters, customer: customer || null, + assignedTeamMembersDetails, }); } } - // Step 3: Return response return reply.send({ status_code: 200, message: "Orders with active master connections fetched successfully", - data: ordersWithActiveMasters, + data: ordersWithDetails, }); } catch (err) { @@ -2694,7 +2765,6 @@ exports.getOrdersByInstallationId = async (req, reply) => { - exports.getPendingOrdersByInstallationAndTeamMember = async (req, reply) => { try { const { installationId, teamMemberId } = req.params;