2022-03-26 00:53:56 +00:00
|
|
|
import type { NextApiRequest, NextApiResponse } from "next";
|
|
|
|
|
2022-03-29 01:59:57 +00:00
|
|
|
import prisma from "@calcom/prisma";
|
2022-03-30 12:17:55 +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";
|
|
|
|
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
|
|
|
|
* /api/teams/new:
|
|
|
|
* post:
|
|
|
|
* description: Creates a new team
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, team created
|
|
|
|
* model: Team
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Team body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
*/
|
|
|
|
async function createTeam(req: NextApiRequest, res: NextApiResponse<TeamResponse>) {
|
|
|
|
const safe = schemaTeamBodyParams.safeParse(req.body);
|
|
|
|
if (!safe.success) throw new Error("Invalid request body", safe.error);
|
2022-03-26 00:53:56 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
const team = await prisma.team.create({ data: safe.data });
|
|
|
|
const data = schemaTeamPublic.parse(team);
|
2022-03-30 12:17:55 +00:00
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
if (data) res.status(201).json({ data, message: "Team created successfully" });
|
2022-03-30 12:17:55 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
2022-03-30 14:56:24 +00:00
|
|
|
message: "Could not create new team",
|
|
|
|
error,
|
2022-03-30 12:17:55 +00:00
|
|
|
});
|
2022-03-26 00:53:56 +00:00
|
|
|
}
|
|
|
|
|
2022-03-30 14:56:24 +00:00
|
|
|
export default withMiddleware("HTTP_POST")(withValidTeam(createTeam));
|