diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index d47afff5..e88fda62 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -488,51 +488,110 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { } }; - exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { - try { - const { installationId, teamMemberId } = request.params; + // 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", - }, - }); - } + // if (!installationId || !teamMemberId) { + // return reply.status(400).send({ + // simplydata: { + // error: true, + // message: "Both installationId and teamMemberId are required", + // }, + // }); + // } - // 🔹 Fetch quotations where installationId matches and teamMemberId is assigned - const quotations = await Order.find({ - installationId, - assignedTeamMembers: teamMemberId, - }); + // // 🔹 Fetch quotations where installationId matches and teamMemberId is assigned + // const quotations = await Order.find({ + // installationId, + // assignedTeamMembers: teamMemberId, + // }); - if (!quotations || quotations.length === 0) { - return reply.status(404).send({ - simplydata: { - error: true, - message: "No quotations found for this installation and team member", - }, - }); - } + // if (!quotations || quotations.length === 0) { + // return reply.status(404).send({ + // simplydata: { + // error: true, + // message: "No quotations found for this installation and team member", + // }, + // }); + // } - return reply.send({ + // return reply.send({ + // simplydata: { + // error: false, + // message: "Quotations fetched successfully", + // quotations, + // }, + // }); + // } catch (err) { + // console.error("Error fetching quotations:", err); + // reply.status(500).send({ + // simplydata: { + // error: true, + // message: "Internal server error", + // }, + // }); + // } + // }; + +exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => { + try { + const { installationId, teamMemberId } = request.params; + + if (!installationId || !teamMemberId) { + return reply.status(400).send({ simplydata: { - error: false, - message: "Quotations fetched successfully", - quotations, + error: true, + message: "Both installationId and teamMemberId are required", }, }); - } catch (err) { - console.error("Error fetching quotations:", err); - reply.status(500).send({ + } + + // 🔹 Find quotations matching installationId and assignedTeamMembers + const quotations = await Order.find({ + installationId, + assignedTeamMembers: teamMemberId, + }).lean(); // use lean() for performance, since we'll enrich manually + + if (!quotations || quotations.length === 0) { + return reply.status(404).send({ simplydata: { error: true, - message: "Internal server error", + message: "No quotations found for this installation and team member", }, }); } - }; + + // 🔹 Enrich each quotation with customer details + const enrichedQuotations = await Promise.all( + quotations.map(async (quotation) => { + const customer = await User.findOne({ customerId: quotation.customerId }).lean(); + + return { + ...quotation, + customer: customer || null, // attach under 'customer' + }; + }) + ); + + return reply.send({ + simplydata: { + error: false, + message: "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.getDepartmentByFirstName = async (req, reply) => { try {