From dd7aa1e24ea9f062de8d9772cdb58e9543562fed Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 6 Mar 2025 16:32:21 +0530 Subject: [PATCH] get all quations based on installation and team member --- src/controllers/installationController.js | 98 ++++++++++++++++++++++- src/routes/installationRoute.js | 42 ++++++++++ 2 files changed, 138 insertions(+), 2 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 39dc86b7..13a2e518 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -251,7 +251,7 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { quotation = new Order({ installationId, quatationId: quotationId, - assignedTeamMembers: [], + assignedTeamMembers: [], // Ensure assignedTeamMembers array is initialized status: "Pending", // Default status when created }); } @@ -268,9 +268,13 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { // 🔹 Update order status when a team member is assigned quotation.status = "Assigned"; // Update status - // 🔹 Save the updated quotation + // 🔹 Save the updated quotation in the Order schema await quotation.save(); + // 🔹 Update Installation schema with quotationId + installation.quatationId = quotationId; + await installation.save(); + return reply.send({ simplydata: { error: false, @@ -436,4 +440,94 @@ exports.assignTeamMemberToQuotation = async (request, reply) => { }); } }; + + + exports.getQuotationsByInstallationId = async (request, reply) => { + try { + const { installationId } = request.params; + + if (!installationId) { + return reply.status(400).send({ + simplydata: { + error: true, + message: "Installation ID is required", + }, + }); + } + + // 🔹 Fetch quotations based on installationId + const quotations = await Order.find({ installationId }); + + if (!quotations || quotations.length === 0) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "No quotations found for this installation ID", + }, + }); + } + + 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: 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", + }, + }); + } + }; \ No newline at end of file diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 0e4f0fa0..f6d2bf96 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -46,6 +46,48 @@ module.exports = function (fastify, opts, next) { handler: installationController.getTeamMembers }); + fastify.get("/api/getQuations/:installationId", { + schema: { + description: "Get all quatations under a specific installation", + tags: ["Installation"], + summary: "Get all quatations under a specific installation", + params: { + type: "object", + properties: { + installationId: { + type: "string", + description: "Installation ID to fetch team members from" + } + }, + required: ["installationId"] + }, + + }, + handler: installationController.getQuotationsByInstallationId + }); + fastify.get("/api/getQuations/:installationId/:teamMemberId", { + schema: { + description: "Get all quatations under a specific installation and team member", + tags: ["Installation"], + summary: "Get all quatations under a specific installation and team member", + params: { + type: "object", + properties: { + installationId: { + type: "string", + description: "Installation ID to fetch team members from" + }, + teamMemberId: { + type: "string", + description: "teamMember ID to fetch team members from" + } + }, + // required: ["installationId"] + }, + + }, + handler: installationController.getQuotationsByInstallationAndTeamMember + }); fastify.post("/api/assignTeammember/:installationId", { schema: { description: "Assign a team member to an installation's quotation",