Add GET /teams/{id}/event-types endpoint (#251)

Allows fetching team event types - GET /event-types only lists the user
event types - currently fetching the team event types isn't possible.
This commit is contained in:
Alex van Andel
2023-03-13 12:24:10 +00:00
committed by GitHub
parent 870b2df538
commit cb08bba351
2 changed files with 74 additions and 0 deletions
@@ -0,0 +1,65 @@
import type { Prisma } from "@prisma/client";
import type { NextApiRequest } from "next";
import { z } from "zod";
import { defaultResponder } from "@calcom/lib/server";
import { schemaEventTypeReadPublic } from "~/lib/validations/event-type";
const querySchema = z.object({
teamId: z.coerce.number(),
});
/**
* @swagger
* /teams/{teamId}/event-types:
* get:
* summary: Find all event types that belong to teamId
* operationId: listEventTypesByTeamId
* parameters:
* - in: query
* name: apiKey
* schema:
* type: string
* required: true
* description: Your API Key
* - in: path
* name: teamId
* schema:
* type: number
* required: true
* tags:
* - event-types
* externalDocs:
* url: https://docs.cal.com/core-features/event-types
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No event types were found
*/
async function getHandler(req: NextApiRequest) {
const { userId, isAdmin, prisma } = req;
const { teamId } = querySchema.parse(req.query);
const args: Prisma.EventTypeFindManyArgs = {
where: {
team: isAdmin
? {
id: teamId,
}
: {
id: teamId,
members: { some: { userId } },
},
},
};
const data = await prisma.eventType.findMany(args);
return { event_types: data.map((attendee) => schemaEventTypeReadPublic.parse(attendee)) };
}
export default defaultResponder(getHandler);
@@ -0,0 +1,9 @@
import { defaultHandler } from "@calcom/lib/server";
import { withMiddleware } from "~/lib/helpers/withMiddleware";
export default withMiddleware()(
defaultHandler({
GET: import("./_get"),
})
);