From fc35c1096a71ecfc5139bdef255bb9657d9ada75 Mon Sep 17 00:00:00 2001 From: minhd-vu Date: Sun, 25 Feb 2024 20:42:03 -0500 Subject: [PATCH] add kick player --- app/api/party/join/route.ts | 13 ++++- app/api/party/kick/route.ts | 57 +++++++++++++++++++ app/api/party/leave/route.ts | 18 +++--- .../{RemovePlayer.tsx => KickPlayer.tsx} | 14 +++-- components/Party.tsx | 4 +- 5 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 app/api/party/kick/route.ts rename components/{RemovePlayer.tsx => KickPlayer.tsx} (67%) diff --git a/app/api/party/join/route.ts b/app/api/party/join/route.ts index e589440..713e244 100644 --- a/app/api/party/join/route.ts +++ b/app/api/party/join/route.ts @@ -1,14 +1,23 @@ import prisma from "@/lib/prisma"; import { getServerSession } from "next-auth"; +type PartyJoinBody = { + code?: string; +}; + export async function POST(req: Request) { const session = await getServerSession(); if (!session?.user?.email) { return Response.json(null, { status: 401 }); } - const data = await req.json(); - const code = data.code; + const body: PartyJoinBody = await req.json(); + if (!body.code) { + return Response.json("No party code provided", { + status: 400, + }); + } + const code = body.code; let party = await prisma.party.findUnique({ where: { diff --git a/app/api/party/kick/route.ts b/app/api/party/kick/route.ts new file mode 100644 index 0000000..532e565 --- /dev/null +++ b/app/api/party/kick/route.ts @@ -0,0 +1,57 @@ +import { getServerSession } from "next-auth"; +import prisma from "@/lib/prisma"; +import { removePlayer } from "../leave/route"; + +type PartyKickBody = { + playerId?: string; +}; + +export async function POST(req: Request) { + const session = await getServerSession(); + if (!session?.user?.email) { + return Response.json("User is not authenticated", { status: 401 }); + } + + const body: PartyKickBody = await req.json(); + + if (!body.playerId) { + return Response.json("Must provide playerId in request body", { + status: 400, + }); + } + + const user = await prisma.user.findUniqueOrThrow({ + where: { + email: session.user.email, + }, + include: { + party: { + include: { + players: true, + }, + }, + targetedBy: true, + }, + }); + + if (!user.party) { + return Response.json("You are not part of a party", { + status: 400, + }); + } + + if (user.party.adminId !== user.id) { + return Response.json("You must be the party admin to kick a player", { + status: 403, + }); + } + + const player = user.party.players.find((e) => e.id === body.playerId); + if (!player) { + return Response.json("Player is not in your party", { + status: 400, + }); + } + + return removePlayer(player.email); +} diff --git a/app/api/party/leave/route.ts b/app/api/party/leave/route.ts index 4c9cf3e..4151e50 100644 --- a/app/api/party/leave/route.ts +++ b/app/api/party/leave/route.ts @@ -2,15 +2,10 @@ import prisma from "@/lib/prisma"; import _ from "lodash"; import { getServerSession } from "next-auth"; -export async function POST() { - const session = await getServerSession(); - if (!session?.user?.email) { - return Response.json(null, { status: 401 }); - } - +export async function removePlayer(email: string) { const user = await prisma.user.findUniqueOrThrow({ where: { - email: session.user.email, + email, }, include: { party: { @@ -97,3 +92,12 @@ export async function POST() { return Response.json(party); } + +export async function POST() { + const session = await getServerSession(); + if (!session?.user?.email) { + return Response.json(null, { status: 401 }); + } + + return removePlayer(session.user.email); +} diff --git a/components/RemovePlayer.tsx b/components/KickPlayer.tsx similarity index 67% rename from components/RemovePlayer.tsx rename to components/KickPlayer.tsx index e73dd53..662add6 100644 --- a/components/RemovePlayer.tsx +++ b/components/KickPlayer.tsx @@ -1,12 +1,15 @@ "use client"; +import { useContext } from "react"; import { useSWRConfig } from "swr"; +import { ErrorContext } from "./App"; -export default function RemovePlayer({ playerId }: { playerId: string }) { +export default function KickPlayer({ playerId }: { playerId: string }) { + const { setError } = useContext(ErrorContext); const { mutate } = useSWRConfig(); - async function removePlayer() { - const res = await fetch("/api/party/remove", { + async function onClick() { + const res = await fetch("/api/party/kick", { method: "POST", headers: { "Content-Type": "application/json", @@ -15,7 +18,8 @@ export default function RemovePlayer({ playerId }: { playerId: string }) { }); if (!res.ok) { - throw new Error(await res.json()); + setError(await res.json()); + return; } mutate("/api/user"); @@ -24,7 +28,7 @@ export default function RemovePlayer({ playerId }: { playerId: string }) { return (