inside the teammember customer details display

master^2
Bhaskar 3 months ago
parent db30d53392
commit 8659bb54a1

@ -488,6 +488,52 @@ 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",
// },
// });
// }
// // 🔹 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",
// },
// });
// }
// 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) => { exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => {
try { try {
const { installationId, teamMemberId } = request.params; const { installationId, teamMemberId } = request.params;
@ -501,11 +547,11 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
}); });
} }
// 🔹 Fetch quotations where installationId matches and teamMemberId is assigned // 🔹 Find quotations matching installationId and assignedTeamMembers
const quotations = await Order.find({ const quotations = await Order.find({
installationId, installationId,
assignedTeamMembers: teamMemberId, assignedTeamMembers: teamMemberId,
}); }).lean(); // use lean() for performance, since we'll enrich manually
if (!quotations || quotations.length === 0) { if (!quotations || quotations.length === 0) {
return reply.status(404).send({ return reply.status(404).send({
@ -516,16 +562,28 @@ exports.assignTeamMemberToQuotation = 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();
return {
...quotation,
customer: customer || null, // attach under 'customer'
};
})
);
return reply.send({ return reply.send({
simplydata: { simplydata: {
error: false, error: false,
message: "Quotations fetched successfully", message: "Quotations fetched successfully",
quotations, quotations: enrichedQuotations,
}, },
}); });
} catch (err) { } catch (err) {
console.error("Error fetching quotations:", err); console.error("Error fetching quotations:", err);
reply.status(500).send({ return reply.status(500).send({
simplydata: { simplydata: {
error: true, error: true,
message: "Internal server error", message: "Internal server error",
@ -534,6 +592,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
} }
}; };
exports.getDepartmentByFirstName = async (req, reply) => { exports.getDepartmentByFirstName = async (req, reply) => {
try { try {
let { firstName } = req.params; let { firstName } = req.params;

Loading…
Cancel
Save