From df70a70e60282b1998af59211b080255e37bb8cf Mon Sep 17 00:00:00 2001 From: Bhaskar Date: Wed, 24 Sep 2025 16:14:01 +0530 Subject: [PATCH] assign teammember for support --- src/controllers/installationController.js | 175 +++++++++++++++++----- src/models/store.js | 9 ++ src/routes/installationRoute.js | 4 +- 3 files changed, 149 insertions(+), 39 deletions(-) diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 24117629..f7191dab 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -6080,70 +6080,171 @@ exports.updateHardwareList = async (req, reply) => { +// exports.assignCategorizeIssue = async (request, reply) => { +// const { supportId } = request.params; +// const { support_teamMemberId, category, masterHardwareId } = request.body; + +// if (!support_teamMemberId || !category || !masterHardwareId) { +// return reply.code(400).send({ +// error: 'support_teamMemberId, category, and masterHardwareId are required' +// }); +// } + +// const support = await Support.findOne({ supportId }); +// if (!support) { +// return reply.code(404).send({ error: 'Support record not found' }); +// } + +// const teamMembers = support.team_member?.team_member || []; +// const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId); + +// if (!teamMember) { +// return reply.code(400).send({ error: `Team member ID ${support_teamMemberId} not found` }); +// } + +// const assignedAt = moment().format("DD-MM-YYYY HH:mm:ss"); +// const assignmentCode = Math.floor(100000 + Math.random() * 900000).toString(); // random 6-digit code +// let assignedCount = 0; + +// support.categorizedIssues.forEach(issue => { +// if ( +// issue.masterHardwareId === masterHardwareId && +// issue.category === category +// ) { +// issue.assignedTo = { +// name: teamMember.name, +// support_teamMemberId: teamMember.support_teamMemberId, +// phone: teamMember.phone, +// email: teamMember.email, +// assignedAt: assignedAt, +// assignmentCode: assignmentCode // ← Add this field +// }; +// assignedCount++; +// } +// }); + +// if (assignedCount === 0) { +// return reply.code(404).send({ message: 'No matching issues found for assignment' }); +// } + +// await support.save(); + +// return reply.send({ +// message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`, +// assignmentCode: assignmentCode, // ← Return the code in response +// assignedTo: { +// support_teamMemberId: teamMember.support_teamMemberId, +// name: teamMember.name, +// phone: teamMember.phone, +// email: teamMember.email, +// assignedAt: assignedAt, +// } +// }); +// }; + + exports.assignCategorizeIssue = async (request, reply) => { - const { supportId } = request.params; - const { support_teamMemberId, category, masterHardwareId } = request.body; + try { + const { supportId } = request.params; + const { support_teamMemberId, quationId } = request.body; - if (!support_teamMemberId || !category || !masterHardwareId) { - return reply.code(400).send({ - error: 'support_teamMemberId, category, and masterHardwareId are required' - }); - } + // Step 1: Validate input + if (!supportId || !support_teamMemberId || !quationId) { + return reply.code(400).send({ + error: 'supportId (path), support_teamMemberId, and quationId (body) are required' + }); + } - const support = await Support.findOne({ supportId }); - if (!support) { - return reply.code(404).send({ error: 'Support record not found' }); - } + // Step 2: Find support record + const support = await Support.findOne({ supportId }); + if (!support) { + return reply.code(404).send({ error: 'Support record not found' }); + } - const teamMembers = support.team_member?.team_member || []; - const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId); + // Step 3: Check team member exists + const teamMembers = support.team_member?.team_member || []; + const teamMember = teamMembers.find(m => m.support_teamMemberId === support_teamMemberId); - if (!teamMember) { - return reply.code(400).send({ error: `Team member ID ${support_teamMemberId} not found` }); - } + if (!teamMember) { + return reply.code(404).send({ error: `Team member ID ${support_teamMemberId} not found` }); + } - const assignedAt = moment().format("DD-MM-YYYY HH:mm:ss"); -const assignmentCode = Math.floor(100000 + Math.random() * 900000).toString(); // random 6-digit code - let assignedCount = 0; + // Step 4: Assign all categorized issues + const assignedAt = moment().format("DD-MM-YYYY HH:mm:ss"); + const assignmentCode = Math.floor(100000 + Math.random() * 900000).toString(); // random 6-digit code + let assignedCount = 0; - support.categorizedIssues.forEach(issue => { - if ( - issue.masterHardwareId === masterHardwareId && - issue.category === category - ) { + support.issues.forEach(issue => { issue.assignedTo = { name: teamMember.name, support_teamMemberId: teamMember.support_teamMemberId, phone: teamMember.phone, email: teamMember.email, assignedAt: assignedAt, - assignmentCode: assignmentCode // ← Add this field + assignmentCode: assignmentCode, + quationId: quationId }; assignedCount++; + }); + + if (assignedCount === 0) { + return reply.code(404).send({ message: 'No categorized issues found to assign' }); } - }); - if (assignedCount === 0) { - return reply.code(404).send({ message: 'No matching issues found for assignment' }); - } + // Step 5: Save support document + await support.save(); + + // Step 6: Update or create the Order document with assigned support team member + let order = await Order.findOne({ quatationId: quationId }); - await support.save(); + if (!order) { + order = new Order({ + quatationId: quationId, + assignedTeamMembers: [], + quatation_status: "Assigned", + }); + } - return reply.send({ - message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`, - assignmentCode: assignmentCode, // ← Return the code in response - assignedTo: { + // Save the assigned support team member info + order.assignedSupportTeamMember = { support_teamMemberId: teamMember.support_teamMemberId, name: teamMember.name, phone: teamMember.phone, email: teamMember.email, assignedAt: assignedAt, - } - }); -}; + assignmentCode: assignmentCode, + quationId: quationId, + }; + await order.save(); + // Step 7: Return response + return reply.send({ + simplydata: { + error: false, + message: `Assigned ${assignedCount} categorized issue(s) to ${teamMember.name}`, + assignmentCode: assignmentCode, + assignedTo: { + support_teamMemberId: teamMember.support_teamMemberId, + name: teamMember.name, + phone: teamMember.phone, + email: teamMember.email, + assignedAt: assignedAt, + quationId: quationId + } + } + }); + } catch (error) { + console.error("Error assigning categorized issues:", error); + return reply.status(500).send({ + simplydata: { + error: true, + message: "Internal Server Error" + } + }); + } +}; exports.getCategorizedIssue = async (request, reply) => { diff --git a/src/models/store.js b/src/models/store.js index a498237e..3d347141 100644 --- a/src/models/store.js +++ b/src/models/store.js @@ -914,6 +914,15 @@ const orderSchema = new mongoose.Schema({ datetime: { type: String, default: null }, updated_at: { type: String, default: null }, assignedTeamMembers: [{ type: String }], + assignedSupportTeamMember: { + support_teamMemberId: { type: String }, + name: { type: String }, + phone: { type: String }, + email: { type: String }, + assignedAt: { type: String }, + assignmentCode: { type: String }, + quationId: { type: String }, + }, tankhardwareId: [{ type: String,default: null }], master_connections: [ { diff --git a/src/routes/installationRoute.js b/src/routes/installationRoute.js index 7ab00c44..dae4daca 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -1325,8 +1325,8 @@ fastify.post( support_teamMemberId: { type: "string" }, // startDate: { type: "string" }, // endDate: { type: "string" }, - category: { type: "string" }, - masterHardwareId: { type: "string" }, + //category: { type: "string" }, + quationId: { type: "string" }, }, }, },