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-29 01:59:57 +00:00
|
|
|
import { withMiddleware } from "@lib/helpers/withMiddleware";
|
2022-04-08 23:29:26 +00:00
|
|
|
import { TeamResponse, TeamsResponse } from "@lib/types";
|
2022-04-20 21:42:49 +00:00
|
|
|
import { schemaMembershipPublic } from "@lib/validations/membership";
|
2022-04-08 23:29:26 +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-08 23:29:26 +00:00
|
|
|
* /v1/teams:
|
2022-03-30 14:56:24 +00:00
|
|
|
* get:
|
2022-04-08 23:29:26 +00:00
|
|
|
* summary: Get all teams
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-03-31 20:14:37 +00:00
|
|
|
* tags:
|
|
|
|
* - teams
|
2022-03-30 14:56:24 +00:00
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: No teams were found
|
2022-04-08 23:29:26 +00:00
|
|
|
* post:
|
|
|
|
* summary: Creates a new team
|
2022-04-17 14:39:38 +00:00
|
|
|
* security:
|
|
|
|
* - ApiKeyAuth: []
|
2022-04-08 23:29:26 +00:00
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 201:
|
|
|
|
* description: OK, team created
|
|
|
|
* model: Team
|
|
|
|
* 400:
|
|
|
|
* description: Bad request. Team body is invalid.
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
2022-03-30 14:56:24 +00:00
|
|
|
*/
|
2022-04-08 23:29:26 +00:00
|
|
|
async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse<TeamsResponse | TeamResponse>) {
|
|
|
|
const { method } = req;
|
2022-04-22 01:36:33 +00:00
|
|
|
const userId = req.userId;
|
2022-04-08 23:29:26 +00:00
|
|
|
if (method === "GET") {
|
2022-04-11 10:03:15 +00:00
|
|
|
const userWithMemberships = await prisma.membership.findMany({
|
|
|
|
where: { userId: userId },
|
|
|
|
});
|
|
|
|
const teamIds = userWithMemberships.map((membership) => membership.teamId);
|
|
|
|
const teams = await prisma.team.findMany({ where: { id: { in: teamIds } } });
|
|
|
|
if (teams) res.status(200).json({ teams });
|
2022-04-08 23:29:26 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(404).json({
|
|
|
|
message: "No Teams were found",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else if (method === "POST") {
|
|
|
|
const safe = schemaTeamBodyParams.safeParse(req.body);
|
|
|
|
if (!safe.success) throw new Error("Invalid request body");
|
|
|
|
const team = await prisma.team.create({ data: safe.data });
|
2022-04-20 22:00:02 +00:00
|
|
|
// We're also creating the relation membership of team ownership in this call.
|
2022-04-20 21:42:49 +00:00
|
|
|
const membership = await prisma.membership
|
|
|
|
.create({
|
|
|
|
data: { userId, teamId: team.id, role: "OWNER", accepted: true },
|
|
|
|
})
|
|
|
|
.then((membership) => schemaMembershipPublic.parse(membership));
|
2022-04-08 23:29:26 +00:00
|
|
|
const data = schemaTeamPublic.parse(team);
|
2022-04-20 21:42:49 +00:00
|
|
|
// We are also returning the new ownership relation as owner besides team.
|
|
|
|
if (data)
|
|
|
|
res.status(201).json({
|
|
|
|
team: data,
|
|
|
|
owner: membership,
|
|
|
|
message: "Team created successfully, we also made you the owner of this team",
|
|
|
|
});
|
2022-04-08 23:29:26 +00:00
|
|
|
else
|
|
|
|
(error: Error) =>
|
|
|
|
res.status(400).json({
|
|
|
|
message: "Could not create new team",
|
|
|
|
error,
|
|
|
|
});
|
|
|
|
} else res.status(405).json({ message: `Method ${method} not allowed` });
|
2022-03-26 00:53:56 +00:00
|
|
|
}
|
2022-03-29 01:59:57 +00:00
|
|
|
|
2022-04-11 10:03:15 +00:00
|
|
|
export default withMiddleware("HTTP_GET_OR_POST")(createOrlistAllTeams);
|