From 55310b1407fc02f4e0ba1edbc4d0f5dbfbd99c6b Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Thu, 6 Mar 2025 16:04:27 +0530 Subject: [PATCH] installer assigned quation teamMember --- src/controllers/installationController.js | 195 +++++++++++++++------- src/routes/installationRoute.js | 6 +- 2 files changed, 143 insertions(+), 58 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 76d5f1b6..1658bf1f 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -3,7 +3,7 @@ const bcrypt = require('bcrypt'); const jwt = require('jsonwebtoken'); const customJwtAuth = require("../customAuthJwt"); const { Deparments } = require("../models/Department"); -const { Install } = require("../models/store"); +const { Install, SensorStock, SensorQuotation, Order } = require("../models/store"); const { Counter } = require("../models/User") const fastify = require("fastify")({ logger: true, @@ -135,73 +135,154 @@ exports.createTeamMember = async (request, reply) => { } }; - exports.assignTeamMemberToQuotation = async (request, reply) => { - try { - const { installationId } = request.params; // Get installationId from URL params - const { teamMemberId } = request.body; // Get teamMemberId from request body +// exports.assignTeamMemberToQuotation = async (request, reply) => { +// try { +// const { installationId } = request.params; // Get installationId from URL params +// const { teamMemberId } = request.body; // Get teamMemberId from request body - if (!teamMemberId) { - return reply.status(400).send({ - simplydata: { - error: true, - message: "teamMemberId is required", - }, - }); - } +// if (!teamMemberId) { +// return reply.status(400).send({ +// simplydata: { +// error: true, +// message: "teamMemberId is required", +// }, +// }); +// } - // Find installation by installationId - const installation = await Install.findOne({ installationId }); +// // Find installation by installationId +// const installation = await Install.findOne({ installationId }); - if (!installation) { - return reply.status(404).send({ - simplydata: { - error: true, - message: "Installation not found", - }, - }); - } +// if (!installation) { +// return reply.status(404).send({ +// simplydata: { +// error: true, +// message: "Installation not found", +// }, +// }); +// } - // Extract team members list - const teamMembers = installation.team_member.team_member; +// // Extract team members list +// const teamMembers = installation.team_member.team_member; - // Check if provided teamMemberId exists in the installation's team - const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId); +// // Check if provided teamMemberId exists in the installation's team +// const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId); - if (!assignedTeamMember) { - return reply.status(404).send({ - simplydata: { - error: true, - message: "Team member not found in this installation", - }, - }); - } +// if (!assignedTeamMember) { +// return reply.status(404).send({ +// simplydata: { +// error: true, +// message: "Team member not found in this installation", +// }, +// }); +// } - // Here, you would save the assigned team member to the quotation (modify as needed) - const quotation = { - installationId, - assignedTeamMember - }; +// // Here, you would save the assigned team member to the quotation (modify as needed) +// const quotation = { +// installationId, +// assignedTeamMember +// }; - return reply.send({ - simplydata: { - error: false, - message: "Team member assigned to quotation successfully", - quotation - }, - }); +// return reply.send({ +// simplydata: { +// error: false, +// message: "Team member assigned to quotation successfully", +// quotation +// }, +// }); - } catch (err) { - console.error("Error assigning team member to quotation:", err); - reply.status(500).send({ - simplydata: { - error: true, - message: "Internal server error", - }, - }); - } -}; +// } catch (err) { +// console.error("Error assigning team member to quotation:", err); +// reply.status(500).send({ +// simplydata: { +// error: true, +// message: "Internal server error", +// }, +// }); +// } +// }; + +exports.assignTeamMemberToQuotation = async (request, reply) => { + try { + const { installationId } = request.params; + const { teamMemberId, quotationId } = request.body; + + if (!teamMemberId || !quotationId) { + return reply.status(400).send({ + simplydata: { + error: true, + message: "Both teamMemberId and quotationId are required", + }, + }); + } + + // Find installation by installationId + const installation = await Install.findOne({ installationId }); + + if (!installation) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Installation not found", + }, + }); + } + + // Extract team members list + const teamMembers = installation.team_member?.team_member || []; + + // Check if the provided teamMemberId exists in the installation's team + const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId); + + if (!assignedTeamMember) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Team member not found in this installation", + }, + }); + } + // Find or create the quotation for the given installationId + let quotation = await Order.findOne({ installationId, quatationId: quotationId }); + if (!quotation) { + quotation = new Order({ + installationId, + quatationId: quotationId, + // assignedTeamMembers: [], + }); + } + + // Assign the team member to the quotation + if (!quotation.assignedTeamMembers) { + quotation.assignedTeamMembers = []; + } + + if (!quotation.assignedTeamMembers.includes(teamMemberId)) { + quotation.assignedTeamMembers.push(teamMemberId); + } + + // Save the updated quotation + await quotation.save(); + + return reply.send({ + simplydata: { + error: false, + message: "Team member assigned to quotation successfully", + quotation, + }, + }); + + } catch (err) { + console.error("Error assigning team member to quotation:", err); + reply.status(500).send({ + simplydata: { + error: true, + message: "Internal server error", + }, + }); + } +}; exports.getAllInstallers = async (request, reply) => { try { const { departmentName } = request.params; // Get installationId from request params diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 57e2a102..0e4f0fa0 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -67,9 +67,13 @@ module.exports = function (fastify, opts, next) { teamMemberId: { type: "string", description: "The team member ID to assign" + }, + quotationId: { + type: "string", + description: "The team member ID to assign" } }, - required: ["teamMemberId"] + // required: ["teamMemberId"] }, }, handler: installationController.assignTeamMemberToQuotation