From 185dc9da6b06a1f593937322fc89a7b47bd05829 Mon Sep 17 00:00:00 2001 From: Sid-80 Date: Mon, 1 Jul 2024 15:37:28 +0530 Subject: [PATCH] feat/382 --- src/app/api/teams/members/[id]/route.ts | 48 +++++++++++++++++++------ 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/src/app/api/teams/members/[id]/route.ts b/src/app/api/teams/members/[id]/route.ts index ccd9cd0..7e75e69 100644 --- a/src/app/api/teams/members/[id]/route.ts +++ b/src/app/api/teams/members/[id]/route.ts @@ -6,16 +6,17 @@ export async function GET( request: Request, { params }: { params: { id: string } } ) { - const {id} = params; - - - if (!id) return new Response('Parameters missing!!',{status: 401}); - - const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); - - const teamInfo = await client.query(api.teams.getTeamById,{_id:id as Id<"teams">}); - - const memberDataPromises = teamInfo.teamMembers.map((mem:string) => + const { id } = params; + + if (!id) return new Response("Parameters missing!!", { status: 401 }); + + const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + + const teamInfo = await client.query(api.teams.getTeamById, { + _id: id as Id<"teams">, + }); + + const memberDataPromises = teamInfo.teamMembers.map((mem: string) => client.query(api.user.getUser, { email: mem }) ); @@ -23,6 +24,31 @@ export async function GET( const memberData = results.flatMap((result) => result || []); - return Response.json({memberData}); + return Response.json({ memberData }); +} + +export async function DELETE( + request: Request, + { params }: { params: { id: string } } +) { + const { id } = params; + + const {email} = await request.json(); + + if (!id) return new Response("Parameters missing!!", { status: 401 }); + + const client = new ConvexHttpClient(process.env.NEXT_PUBLIC_CONVEX_URL!); + + const teamInfo = await client.query(api.teams.getTeamById,{_id:id as Id<"teams">}); + + if(!teamInfo.teamMembers.includes(email)) return new Response("Not the member of team!!", { status: 404 }); + + if(teamInfo.createdBy === email) return new Response("Owner of the team!!", { status: 404 }); + + const updatedTeamMembers = teamInfo.teamMembers.filter((writer: any) => writer !== email); + + await client.mutation(api.teams.addMember, { _id: id as Id<"teams">, memberArray:updatedTeamMembers }); + + return new Response("Member removed!!", { status: 200 }); }