2022-03-30 12:17:55 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
2022-03-26 00:53:56 +00:00
|
|
|
|
2022-03-30 12:17:55 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-26 00:53:56 +00:00
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-03-30 14:56:24 +00:00
|
|
|
import type { TeamResponse } from "@lib/types";
|
2022-03-30 12:17:55 +00:00
|
|
|
import {
|
|
|
|
schemaQueryIdParseInt,
|
|
|
|
withValidQueryIdTransformParseInt,
|
|
|
|
} from "@lib/validations/shared/queryIdTransformParseInt";
|
2022-03-30 14:56:24 +00:00
|
|
|
import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team";
|
2022-03-26 00:53:56 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
/**
|
|
|
|
* @swagger
|
2022-04-03 15:47:18 +00:00
|
|
|
* /api/teams/{id}/edit:
|
2022-03-30 14:56:24 +00:00
|
|
|
* patch:
|
2022-03-31 20:14:37 +00:00
|
|
|
* summary: Edits an existing team
|
2022-04-03 15:47:18 +00:00
|
|
|
* 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
|
2022-03-31 20:14:37 +00:00
|
|
|
* tags:
|
|
|
|
* - teams
|
2022-03-30 14:56:24 +00:00
|
|
|
* 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);
|
2022-03-30 14:56:24 +00:00
|
|
|
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");
|
2022-03-30 14:56:24 +00:00
|
|
|
const team = await prisma.team.update({
|
2022-03-30 12:17:55 +00:00
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
|
|
|
});
|
2022-03-30 14:56:24 +00:00
|
|
|
const data = schemaTeamPublic.parse(team);
|
2022-03-26 00:53:56 +00:00
|
|
|
|
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,
|
|
|
|
});
|
2022-03-26 00:53:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidTeam(editTeam)));
|