2021-08-19 12:27:01 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2021-09-22 19:52:38 +00:00
|
|
|
|
2021-09-03 20:51:21 +00:00
|
|
|
import { getSession } from "@lib/auth";
|
2021-09-22 19:52:38 +00:00
|
|
|
import prisma from "@lib/prisma";
|
2021-06-05 22:53:33 +00:00
|
|
|
|
|
|
|
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
|
2021-08-19 12:27:01 +00:00
|
|
|
const session = await getSession({ req: req });
|
2021-06-05 22:53:33 +00:00
|
|
|
if (!session) {
|
2021-08-19 12:27:01 +00:00
|
|
|
return res.status(401).json({ message: "Not authenticated" });
|
2021-06-05 22:53:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DELETE /api/teams/{team}
|
|
|
|
if (req.method === "DELETE") {
|
2021-09-22 10:43:32 +00:00
|
|
|
if (!session.user?.id) {
|
|
|
|
console.log("Received session token without a user id.");
|
|
|
|
return res.status(500).json({ message: "Something went wrong." });
|
|
|
|
}
|
|
|
|
|
|
|
|
const membership = await prisma.membership.findFirst({
|
|
|
|
where: {
|
|
|
|
userId: session.user.id,
|
|
|
|
teamId: parseInt(req.query.team as string),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!membership || membership.role !== "OWNER") {
|
|
|
|
console.log(`User ${session.user.id} tried deleting an organization they don't own.`);
|
|
|
|
return res.status(403).json({ message: "Forbidden." });
|
|
|
|
}
|
|
|
|
|
2021-08-19 12:27:01 +00:00
|
|
|
await prisma.membership.delete({
|
2021-06-05 22:53:33 +00:00
|
|
|
where: {
|
2021-08-19 12:27:01 +00:00
|
|
|
userId_teamId: { userId: session.user.id, teamId: parseInt(req.query.team) },
|
|
|
|
},
|
2021-06-05 22:53:33 +00:00
|
|
|
});
|
2021-08-19 12:27:01 +00:00
|
|
|
await prisma.team.delete({
|
2021-06-05 22:53:33 +00:00
|
|
|
where: {
|
|
|
|
id: parseInt(req.query.team),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return res.status(204).send(null);
|
|
|
|
}
|
2021-08-19 12:27:01 +00:00
|
|
|
}
|