cal.pub0.org/pages/api/teams/[id]/edit.ts

64 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-30 12:17:55 +00:00
import type { NextApiRequest, NextApiResponse } from "next";
2022-03-30 12:17:55 +00:00
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { TeamResponse } from "@lib/types";
2022-03-30 12:17:55 +00:00
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team";
/**
* @swagger
* /api/teams/{id}/edit:
* patch:
* summary: Edits an existing team
* consumes:
* - application/json
* parameters:
* - in: body
* name: team
* description: The team to edit
* schema: 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.
*/
export async function editTeam(req: NextApiRequest, res: NextApiResponse<TeamResponse>) {
2022-03-30 12:17:55 +00:00
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaTeamBodyParams.safeParse(req.body);
2022-03-30 12:17:55 +00:00
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const team = await prisma.team.update({
2022-03-30 12:17:55 +00:00
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaTeamPublic.parse(team);
2022-03-30 12:17:55 +00:00
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidTeam(editTeam)));