diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 81dbb0bb..17a031e0 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -593,6 +593,85 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { // }; +// exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { +// try { +// const { installationId, teamMemberId } = request.params; + +// if (!installationId || !teamMemberId) { +// return reply.status(400).send({ +// simplydata: { +// error: true, +// message: "Both installationId and teamMemberId are required", +// }, +// }); +// } + +// // 🔹 Find quotations matching installationId and assignedTeamMembers +// let quotations = await Order.find({ +// installationId, +// assignedTeamMembers: teamMemberId, +// }).lean(); + +// if (!quotations || quotations.length === 0) { +// return reply.status(404).send({ +// simplydata: { +// error: true, +// message: "No quotations found for this installation and team member", +// }, +// }); +// } + +// // ✅ Filter: keep only quotations where at least one master_connections.work_status === 'active' +// quotations = quotations.filter(q => +// Array.isArray(q.master_connections) && +// q.master_connections.some(mc => mc.work_status === 'active') +// ); + +// // If no quotations left after filtering, return empty list +// if (!quotations.length) { +// return reply.send({ +// simplydata: { +// error: false, +// message: "No active quotations found for this installation and team member", +// quotations: [], +// }, +// }); +// } + +// // 🔹 Enrich each quotation with customer details +// const enrichedQuotations = await Promise.all( +// quotations.map(async (quotation) => { +// const customer = await User.findOne({ customerId: quotation.customerId }).lean(); +// // 🔹 Keep only active master_connections +// const activeMasters = quotation.master_connections?.filter(mc => mc.work_status === 'active') || []; + +// return { +// ...quotation, +// master_connections: activeMasters, +// customer: customer || null, +// }; +// }) +// ); + + +// return reply.send({ +// simplydata: { +// error: false, +// message: "Active quotations fetched successfully", +// quotations: enrichedQuotations, +// }, +// }); +// } catch (err) { +// console.error("Error fetching quotations:", err); +// return reply.status(500).send({ +// simplydata: { +// error: true, +// message: "Internal server error", +// }, +// }); +// } +// }; + exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { try { const { installationId, teamMemberId } = request.params; @@ -610,7 +689,7 @@ exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { let quotations = await Order.find({ installationId, assignedTeamMembers: teamMemberId, - }).lean(); + }); if (!quotations || quotations.length === 0) { return reply.status(404).send({ @@ -621,8 +700,32 @@ exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { }); } + // ✅ Step 1: update master_connections where work_status is missing to 'active' + for (const order of quotations) { + let updated = false; + + if (Array.isArray(order.master_connections)) { + for (const mc of order.master_connections) { + if (!mc.work_status) { + mc.work_status = 'active'; + updated = true; + } + } + } + + if (updated) { + await order.save(); + } + } + + // Re-fetch quotations as lean after update + quotations = await Order.find({ + installationId, + assignedTeamMembers: teamMemberId, + }).lean(); + // ✅ Filter: keep only quotations where at least one master_connections.work_status === 'active' - quotations = quotations.filter(q => + quotations = quotations.filter(q => Array.isArray(q.master_connections) && q.master_connections.some(mc => mc.work_status === 'active') ); @@ -638,21 +741,22 @@ exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { }); } - // 🔹 Enrich each quotation with customer details - const enrichedQuotations = await Promise.all( - quotations.map(async (quotation) => { - const customer = await User.findOne({ customerId: quotation.customerId }).lean(); - // 🔹 Keep only active master_connections - const activeMasters = quotation.master_connections?.filter(mc => mc.work_status === 'active') || []; + // 🔹 Enrich each quotation with customer details & keep only active master_connections + const enrichedQuotations = await Promise.all( + quotations.map(async (quotation) => { + const customer = await User.findOne({ customerId: quotation.customerId }).lean(); - return { - ...quotation, - master_connections: activeMasters, - customer: customer || null, - }; - }) -); + const activeMasters = quotation.master_connections?.filter(mc => + mc.work_status === 'active' + ) || []; + return { + ...quotation, + master_connections: activeMasters, + customer: customer || null, + }; + }) + ); return reply.send({ simplydata: { @@ -673,6 +777,7 @@ exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { }; + exports.getDepartmentByFirstName = async (req, reply) => { try { let { firstName } = req.params;