2022-03-26 00:53:56 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
|
|
|
|
|
|
|
import { Team } from "@calcom/prisma/client";
|
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
|
|
|
import { schemaTeam, withValidTeam } from "@lib/validations/team";
|
2022-03-26 23:58:22 +00:00
|
|
|
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
|
2022-03-26 00:53:56 +00:00
|
|
|
|
|
|
|
type ResponseData = {
|
|
|
|
data?: Team;
|
|
|
|
message?: string;
|
|
|
|
error?: unknown;
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function editTeam(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
|
|
|
|
const { query, body, method } = req;
|
2022-03-26 23:58:22 +00:00
|
|
|
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
|
2022-03-26 00:53:56 +00:00
|
|
|
const safeBody = await schemaTeam.safeParse(body);
|
|
|
|
|
|
|
|
if (method === "PATCH") {
|
|
|
|
if (safeQuery.success && safeBody.success) {
|
|
|
|
await prisma.team.update({
|
|
|
|
where: { id: safeQuery.data.id },
|
|
|
|
data: safeBody.data,
|
|
|
|
}).then(team => {
|
|
|
|
res.status(200).json({ data: team });
|
|
|
|
}).catch(error => {
|
|
|
|
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Reject any other HTTP method than POST
|
2022-03-26 01:16:46 +00:00
|
|
|
res.status(405).json({ message: "Only PATCH Method allowed for updating teams" });
|
2022-03-26 00:53:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default withValidQueryIdTransformParseInt(withValidTeam(editTeam));
|