diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 1658bf1f..39dc86b7 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -203,86 +203,93 @@ exports.createTeamMember = async (request, reply) => { 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", - }, - }); - } + const { installationId } = request.params; + const { teamMemberId, quotationId } = request.body; - // Find installation by installationId - const installation = await Install.findOne({ installationId }); + if (!teamMemberId || !quotationId) { + return reply.status(400).send({ + simplydata: { + error: true, + message: "Both teamMemberId and quotationId are required", + }, + }); + } - if (!installation) { - return reply.status(404).send({ - simplydata: { - error: true, - message: "Installation not found", - }, - }); - } + // 🔹 Find installation by installationId + const installation = await Install.findOne({ installationId }); - // Extract team members list - const teamMembers = installation.team_member?.team_member || []; + if (!installation) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Installation not found", + }, + }); + } - // Check if the provided teamMemberId exists in the installation's team - const assignedTeamMember = teamMembers.find(member => member.teamMemberId === teamMemberId); + // 🔹 Extract team members list + const teamMembers = installation.team_member?.team_member || []; - if (!assignedTeamMember) { - return reply.status(404).send({ - simplydata: { - error: true, - message: "Team member not found in this installation", - }, - }); - } + // 🔹 Check if the provided teamMemberId exists in the installation's team + const assignedTeamMember = teamMembers.find( + (member) => member.teamMemberId === teamMemberId + ); - // Find or create the quotation for the given installationId - let quotation = await Order.findOne({ installationId, quatationId: quotationId }); + if (!assignedTeamMember) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Team member not found in this installation", + }, + }); + } - if (!quotation) { - quotation = new Order({ - installationId, - quatationId: quotationId, - // assignedTeamMembers: [], - }); - } + // 🔹 Find or create the quotation for the given installationId + let quotation = await Order.findOne({ installationId, quatationId: quotationId }); - // Assign the team member to the quotation - if (!quotation.assignedTeamMembers) { - quotation.assignedTeamMembers = []; - } + if (!quotation) { + quotation = new Order({ + installationId, + quatationId: quotationId, + assignedTeamMembers: [], + status: "Pending", // Default status when created + }); + } - if (!quotation.assignedTeamMembers.includes(teamMemberId)) { - quotation.assignedTeamMembers.push(teamMemberId); - } + // 🔹 Assign the team member to the quotation + if (!quotation.assignedTeamMembers) { + quotation.assignedTeamMembers = []; + } - // Save the updated quotation - await quotation.save(); + if (!quotation.assignedTeamMembers.includes(teamMemberId)) { + quotation.assignedTeamMembers.push(teamMemberId); + } - return reply.send({ - simplydata: { - error: false, - message: "Team member assigned to quotation successfully", - quotation, - }, - }); + // 🔹 Update order status when a team member is assigned + quotation.status = "Assigned"; // Update status + + // 🔹 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", - }, - }); + 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