mirror of
https://github.com/Sendouc/sendou.ink.git
synced 2026-04-26 01:09:02 -05:00
36 lines
889 B
TypeScript
36 lines
889 B
TypeScript
import { getMySession } from "lib/api";
|
|
import { NextApiRequest, NextApiResponse } from "next";
|
|
import prisma from "prisma/client";
|
|
|
|
const leaveHandler = async (req: NextApiRequest, res: NextApiResponse) => {
|
|
switch (req.method) {
|
|
case "POST":
|
|
await postHandler(req, res);
|
|
break;
|
|
default:
|
|
res.status(405).end();
|
|
}
|
|
};
|
|
|
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|
const user = await getMySession(req);
|
|
if (!user) return res.status(401).end();
|
|
|
|
const userFromDb = await prisma.user.findUnique({
|
|
where: { id: user.id },
|
|
include: { team: true },
|
|
});
|
|
if (!userFromDb?.team || userFromDb.team.captainId === user.id) {
|
|
return res.status(400).end();
|
|
}
|
|
|
|
await prisma.user.update({
|
|
where: { id: user.id },
|
|
data: { team: { disconnect: true } },
|
|
});
|
|
|
|
res.status(200).end();
|
|
}
|
|
|
|
export default leaveHandler;
|