2022-10-11 02:25:47 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
2022-06-29 22:01:14 +00:00
|
|
|
|
|
|
|
import { HttpError } from "@calcom/lib/http-error";
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
|
|
|
import { schemaQueryTeamId } from "@lib/validations/shared/queryTeamId";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /users/{teamId}:
|
|
|
|
* delete:
|
|
|
|
* operationId: removeTeamById
|
|
|
|
* summary: Remove an existing team
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: teamId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: ID of the team to delete
|
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 201:
|
2022-10-11 02:25:47 +00:00
|
|
|
* description: OK, team removed successfully
|
2022-06-29 22:01:14 +00:00
|
|
|
* 400:
|
|
|
|
* description: Bad request. Team id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
2022-10-11 02:25:47 +00:00
|
|
|
export async function deleteHandler(req: NextApiRequest) {
|
2022-10-11 20:09:22 +00:00
|
|
|
const { prisma, query } = req;
|
2022-10-11 02:25:47 +00:00
|
|
|
const { teamId } = schemaQueryTeamId.parse(query);
|
2022-10-11 20:09:22 +00:00
|
|
|
await checkPermissions(req);
|
|
|
|
await prisma.team.delete({ where: { id: teamId } });
|
|
|
|
return { message: `Team with id: ${teamId} deleted successfully` };
|
|
|
|
}
|
|
|
|
|
|
|
|
async function checkPermissions(req: NextApiRequest) {
|
|
|
|
const { userId, prisma, isAdmin } = req;
|
|
|
|
const { teamId } = schemaQueryTeamId.parse(req.query);
|
|
|
|
if (isAdmin) return;
|
2022-10-11 02:25:47 +00:00
|
|
|
/** Only OWNERS can delete teams */
|
|
|
|
const _team = await prisma.team.findFirst({
|
|
|
|
where: { id: teamId, members: { some: { userId, role: "OWNER" } } },
|
2022-06-29 22:01:14 +00:00
|
|
|
});
|
2022-10-11 02:25:47 +00:00
|
|
|
if (!_team) throw new HttpError({ statusCode: 401, message: "Unauthorized: OWNER required" });
|
2022-06-29 22:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(deleteHandler);
|