cal.pub0.org/pages/api/teams/[teamId]/_get.ts

43 lines
1.3 KiB
TypeScript
Raw Normal View History

2022-10-11 02:25:47 +00:00
import type { Prisma } from "@prisma/client";
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) };
}
export default defaultResponder(getHandler);