Skip to content

Commit

Permalink
feat/382
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-80 committed Jul 1, 2024
1 parent cd99a88 commit 185dc9d
Showing 1 changed file with 37 additions and 11 deletions.
48 changes: 37 additions & 11 deletions src/app/api/teams/members/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,49 @@ 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 })
);

const results = await Promise.all(memberDataPromises);

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 });

}

0 comments on commit 185dc9d

Please sign in to comment.