|
|
@ -488,51 +488,110 @@ exports.assignTeamMemberToQuotation = async (request, reply) => {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => {
|
|
|
|
// exports.getQuotationsByInstallationAndTeamMember = async (request, reply) => {
|
|
|
|
try {
|
|
|
|
// try {
|
|
|
|
const { installationId, teamMemberId } = request.params;
|
|
|
|
// const { installationId, teamMemberId } = request.params;
|
|
|
|
|
|
|
|
|
|
|
|
if (!installationId || !teamMemberId) {
|
|
|
|
// if (!installationId || !teamMemberId) {
|
|
|
|
return reply.status(400).send({
|
|
|
|
// return reply.status(400).send({
|
|
|
|
simplydata: {
|
|
|
|
// simplydata: {
|
|
|
|
error: true,
|
|
|
|
// error: true,
|
|
|
|
message: "Both installationId and teamMemberId are required",
|
|
|
|
// message: "Both installationId and teamMemberId are required",
|
|
|
|
},
|
|
|
|
// },
|
|
|
|
});
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
// 🔹 Fetch quotations where installationId matches and teamMemberId is assigned
|
|
|
|
// // 🔹 Fetch quotations where installationId matches and teamMemberId is assigned
|
|
|
|
const quotations = await Order.find({
|
|
|
|
// const quotations = await Order.find({
|
|
|
|
installationId,
|
|
|
|
// installationId,
|
|
|
|
assignedTeamMembers: teamMemberId,
|
|
|
|
// assignedTeamMembers: teamMemberId,
|
|
|
|
});
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
if (!quotations || quotations.length === 0) {
|
|
|
|
// if (!quotations || quotations.length === 0) {
|
|
|
|
return reply.status(404).send({
|
|
|
|
// return reply.status(404).send({
|
|
|
|
simplydata: {
|
|
|
|
// simplydata: {
|
|
|
|
error: true,
|
|
|
|
// error: true,
|
|
|
|
message: "No quotations found for this installation and team member",
|
|
|
|
// 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: {
|
|
|
|
simplydata: {
|
|
|
|
error: false,
|
|
|
|
error: true,
|
|
|
|
message: "Quotations fetched successfully",
|
|
|
|
message: "Both installationId and teamMemberId are required",
|
|
|
|
quotations,
|
|
|
|
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} 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: {
|
|
|
|
simplydata: {
|
|
|
|
error: true,
|
|
|
|
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) => {
|
|
|
|
exports.getDepartmentByFirstName = async (req, reply) => {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|