cal.pub0.org/pages/api/teams/_get.ts

41 lines
1.1 KiB
TypeScript
Raw Normal View History

2023-02-16 21:01:40 +00:00
import type { Prisma } from "@prisma/client";
import type { NextApiRequest } from "next";
import { defaultResponder } from "@calcom/lib/server";
2022-11-25 13:56:58 +00:00
import { schemaTeamsReadPublic } from "~/lib/validations/team";
/**
* @swagger
* /teams:
* get:
* operationId: listTeams
* summary: Find all teams
* parameters:
* - in: query
* name: apiKey
* required: true
* schema:
* type: string
* description: Your API key
* tags:
* - teams
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No teams were found
*/
async function getHandler(req: NextApiRequest) {
const { userId, prisma, isAdmin } = req;
2022-10-11 02:25:47 +00:00
const where: Prisma.TeamWhereInput = {};
// If user is not ADMIN, return only his data.
2022-10-11 02:25:47 +00:00
if (!isAdmin) where.members = { some: { userId } };
const data = await prisma.team.findMany({ where });
return { teams: schemaTeamsReadPublic.parse(data) };
}
export default defaultResponder(getHandler);