diff --git a/src/controllers/installationController.js b/src/controllers/installationController.js index 9a3c0e47..fd9890e8 100644 --- a/src/controllers/installationController.js +++ b/src/controllers/installationController.js @@ -97,4 +97,152 @@ exports.createTeamMember = async (request, reply) => { }; + exports.getTeamMembers = async (request, reply) => { + try { + const { installationId } = request.params; // Get installationId from request params + + // Check if installation exists + const installation = await Install.findOne({ installationId }); + + if (!installation) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Installation not found", + }, + }); + } + + // Extract team members + const teamMembers = installation.team_member.team_member; + + return reply.send({ + simplydata: { + error: false, + message: "Team members retrieved successfully", + teamMembers, // Return the list of team members + }, + }); + + } catch (err) { + console.error("Error fetching team members:", err); + reply.status(500).send({ + simplydata: { + error: true, + message: "Internal server error", + }, + }); + } + }; + + exports.editTeamMember = async (request, reply) => { + try { + const { installationId, teamMemberId } = request.params; + const updateData = request.body; + + // Find the installation + const installation = await Install.findOne({ installationId }); + + if (!installation) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Installation not found", + }, + }); + } + + // Find the team member + let teamMember = installation.team_member.team_member.find( + (member) => member.teamMemberId === teamMemberId + ); + + if (!teamMember) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Team member not found", + }, + }); + } + + // Update fields + Object.assign(teamMember, updateData); + + // Save changes + await installation.markModified("team_member.team_member"); + await installation.save(); + + return reply.send({ + simplydata: { + error: false, + message: "Team member updated successfully", + }, + }); + + } catch (err) { + console.error("Error updating team member:", err); + reply.status(500).send({ + simplydata: { + error: true, + message: "Internal server error", + }, + }); + } + }; + exports.deleteTeamMember = async (request, reply) => { + try { + const { installationId, teamMemberId } = request.params; + + // Find the installation + const installation = await Install.findOne({ installationId }); + + if (!installation) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Installation not found", + }, + }); + } + + // Find index of the team member + const memberIndex = installation.team_member.team_member.findIndex( + (member) => member.teamMemberId === teamMemberId + ); + + if (memberIndex === -1) { + return reply.status(404).send({ + simplydata: { + error: true, + message: "Team member not found", + }, + }); + } + + // Remove the team member from the array + installation.team_member.team_member.splice(memberIndex, 1); + + // Save changes + await installation.markModified("team_member.team_member"); + await installation.save(); + + return reply.send({ + simplydata: { + error: false, + message: "Team member deleted successfully", + }, + }); + + } catch (err) { + console.error("Error deleting team member:", 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 bd402d5f..1b665cd9 100644 --- a/src/routes/installationRoute.js +++ b/src/routes/installationRoute.js @@ -26,7 +26,126 @@ module.exports = function (fastify, opts, next) { handler: installationController.createTeamMember, }); + fastify.get("/api/getTeamMembers/:installationId", { + schema: { + description: "Get all team members under a specific installation", + tags: ["Installation"], + summary: "Get Team Members", + params: { + type: "object", + properties: { + installationId: { + type: "string", + description: "Installation ID to fetch team members from" + } + }, + required: ["installationId"] + }, + response: { + 200: { + type: "object", + properties: { + simplydata: { + type: "object", + properties: { + error: { type: "boolean" }, + message: { type: "string" }, + teamMembers: { + type: "array", + items: { + type: "object", + properties: { + teamMemberId: { type: "string" }, + name: { type: "string" }, + phone: { type: "string" }, + email: { type: "string" }, + alternativePhone: { type: "string" }, + installationTeamMemId: { type: "string" }, + status: { type: "string" } + } + } + } + } + } + } + } + } + }, + handler: installationController.getTeamMembers + }); + fastify.put("/api/editTeamMember/:installationId/:teamMemberId", { + schema: { + description: "Update an existing team member's details", + tags: ["Installation"], + summary: "Edit Team Member", + params: { + type: "object", + properties: { + installationId: { type: "string", description: "Installation ID" }, + teamMemberId: { type: "string", description: "Team Member ID" } + }, + required: ["installationId", "teamMemberId"] + }, + body: { + type: "object", + properties: { + name: { type: "string" }, + phone: { type: "string" }, + email: { type: "string" }, + alternativePhone: { type: "string" }, + status: { type: "string" } + } + }, + response: { + 200: { + type: "object", + properties: { + simplydata: { + type: "object", + properties: { + error: { type: "boolean" }, + message: { type: "string" } + } + } + } + } + } + }, + handler: installationController.editTeamMember + });fastify.delete("/api/deleteTeamMember/:installationId/:teamMemberId", { + schema: { + description: "Delete a team member from an installation", + tags: ["Installation"], + summary: "Delete Team Member", + params: { + type: "object", + properties: { + installationId: { type: "string", description: "Installation ID" }, + teamMemberId: { type: "string", description: "Team Member ID" } + }, + required: ["installationId", "teamMemberId"] + }, + response: { + 200: { + type: "object", + properties: { + simplydata: { + type: "object", + properties: { + error: { type: "boolean" }, + message: { type: "string" } + } + } + } + } + } + }, + handler: installationController.deleteTeamMember + }); + + + next(); } \ No newline at end of file