2022-04-03 22:49:05 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
|
|
|
import type { TeamResponse } from "@lib/types";
|
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
|
|
|
import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-08 23:29:26 +00:00
|
|
|
* /v1/teams/{id}:
|
2022-04-03 22:49:05 +00:00
|
|
|
* get:
|
|
|
|
* summary: Get a team by ID
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the team to get
|
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: Team was not found
|
|
|
|
* patch:
|
|
|
|
* summary: Edit an existing team
|
|
|
|
* consumes:
|
|
|
|
* - application/json
|
|
|
|
* parameters:
|
|
|
|
* - in: body
|
|
|
|
* name: team
|
|
|
|
* description: The team to edit
|
|
|
|
* schema:
|
|
|
|
* type: object
|
|
|
|
* $ref: '#/components/schemas/Team'
|
|
|
|
* required: true
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the team to edit
|
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, team edited successfuly
|
|
|
|
* model: Team
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Team body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* delete:
|
|
|
|
* summary: Remove an existing team
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: id
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: Numeric ID of the team to delete
|
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, team removed successfuly
|
|
|
|
* model: Team
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Team id is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
export async function teamById(req: NextApiRequest, res: NextApiResponse<TeamResponse>) {
|
|
|
|
const { method, query, body } = req;
|
2022-04-08 23:29:26 +00:00
|
|
|
const safeQuery = schemaQueryIdParseInt.safeParse(query);
|
|
|
|
const safeBody = schemaTeamBodyParams.safeParse(body);
|
2022-04-03 22:49:05 +00:00
|
|
|
if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error);
|
|
|
|
|
|
|
|
switch (method) {
|
|
|
|
case "GET":
|
|
|
|
await prisma.team
|
|
|
|
.findUnique({ where: { id: safeQuery.data.id } })
|
|
|
|
.then((data) => schemaTeamPublic.parse(data))
|
2022-04-04 00:02:11 +00:00
|
|
|
.then((data) => res.status(200).json({ data }))
|
2022-04-03 22:49:05 +00:00
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error })
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "PATCH":
|
|
|
|
if (!safeBody.success) throw new Error("Invalid request body");
|
|
|
|
await prisma.team
|
|
|
|
.update({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
|
|
|
})
|
|
|
|
.then((team) => schemaTeamPublic.parse(team))
|
2022-04-04 00:02:11 +00:00
|
|
|
.then((data) => res.status(200).json({ data }))
|
2022-04-03 22:49:05 +00:00
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error })
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "DELETE":
|
|
|
|
await prisma.team
|
|
|
|
.delete({ where: { id: safeQuery.data.id } })
|
|
|
|
.then(() =>
|
|
|
|
res.status(200).json({ message: `Team with id: ${safeQuery.data.id} deleted successfully` })
|
|
|
|
)
|
|
|
|
.catch((error: Error) =>
|
|
|
|
res.status(404).json({ message: `Team with id: ${safeQuery.data.id} not found`, error })
|
|
|
|
);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
res.status(405).json({ message: "Method not allowed" });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withMiddleware("HTTP_GET_DELETE_PATCH")(withValidQueryIdTransformParseInt(teamById));
|