Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/382 #383

Merged
merged 1 commit into from
Jul 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 });

}
Loading