2022-10-11 02:25:47 +00:00
|
|
|
import type { Prisma } from "@prisma/client";
|
2022-06-29 22:01:14 +00:00
|
|
|
import type { NextApiRequest } from "next";
|
|
|
|
|
|
|
|
import { defaultResponder } from "@calcom/lib/server";
|
|
|
|
|
|
|
|
import { schemaQueryTeamId } from "@lib/validations/shared/queryTeamId";
|
|
|
|
import { schemaTeamReadPublic } from "@lib/validations/team";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @swagger
|
|
|
|
* /teams/{teamId}:
|
|
|
|
* get:
|
|
|
|
* operationId: getTeamById
|
|
|
|
* summary: Find a team
|
|
|
|
* parameters:
|
|
|
|
* - in: path
|
|
|
|
* name: teamId
|
|
|
|
* schema:
|
|
|
|
* type: integer
|
|
|
|
* required: true
|
|
|
|
* description: ID of the team to get
|
|
|
|
* tags:
|
|
|
|
* - teams
|
|
|
|
* responses:
|
|
|
|
* 200:
|
|
|
|
* description: OK
|
|
|
|
* 401:
|
|
|
|
* description: Authorization information is missing or invalid.
|
|
|
|
* 404:
|
|
|
|
* description: Team was not found
|
|
|
|
*/
|
|
|
|
export async function getHandler(req: NextApiRequest) {
|
|
|
|
const { prisma, isAdmin, userId } = req;
|
2022-10-11 02:25:47 +00:00
|
|
|
const { teamId } = schemaQueryTeamId.parse(req.query);
|
|
|
|
const where: Prisma.TeamWhereInput = { id: teamId };
|
|
|
|
// Non-admins can only query the teams they're part of
|
|
|
|
if (!isAdmin) where.members = { some: { userId } };
|
|
|
|
const data = await prisma.team.findFirstOrThrow({ where });
|
|
|
|
return { team: schemaTeamReadPublic.parse(data) };
|
2022-06-29 22:01:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default defaultResponder(getHandler);
|