diff --git a/pages/api/index.ts b/pages/api/index.ts index 17d29ee64b..94461cedce 100644 --- a/pages/api/index.ts +++ b/pages/api/index.ts @@ -1,5 +1,5 @@ import type { NextApiRequest, NextApiResponse } from "next"; -export default async function CalcomApi(req: NextApiRequest, res: NextApiResponse) { +export default async function CalcomApi(_: NextApiRequest, res: NextApiResponse) { res.status(201).json({ message: "Welcome to Cal.com API - docs are at https://docs.cal.com/api" }); } diff --git a/pages/api/selected-calendars/[id].ts b/pages/api/selected-calendars/[id].ts index f8fb8c99b2..ed3af1e7b7 100644 --- a/pages/api/selected-calendars/[id].ts +++ b/pages/api/selected-calendars/[id].ts @@ -12,7 +12,7 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/ /** * @swagger - * /api/selected-calendars/{userId}_{teamId}: + * /v1/selected-calendars/{userId}_{integration}_{externalId}: * get: * summary: Get a selected-calendar by userID and teamID * parameters: @@ -21,13 +21,19 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/ * schema: * type: integer * required: true - * description: Numeric userId of the selected-calendar to get + * description: userId of the selected calendar to get * - in: path - * name: teamId + * name: externalId * schema: - * type: integer + * type: string * required: true - * description: Numeric teamId of the selected-calendar to get + * description: externalId of the selected calendar to get + * - in: path + * name: integration + * schema: + * type: string + * required: true + * description: integration of the selected calendar to get * tags: * - selected-calendars * responses: @@ -38,7 +44,7 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/ * 404: * description: SelectedCalendar was not found * patch: - * summary: Edit an existing selected-calendar + * summary: Edit an existing selected calendar * consumes: * - application/json * parameters: @@ -54,13 +60,19 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/ * schema: * type: integer * required: true - * description: Numeric userId of the selected-calendar to get + * description: userId of the selected calendar to get * - in: path - * name: teamId + * name: externalId * schema: - * type: integer + * type: string * required: true - * description: Numeric teamId of the selected-calendar to get + * description: externalId of the selected calendar to get + * - in: path + * name: integration + * schema: + * type: string + * required: true + * description: integration of the selected calendar to get * tags: * - selected-calendars * responses: @@ -79,13 +91,19 @@ import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/ * schema: * type: integer * required: true - * description: Numeric userId of the selected-calendar to get + * description: userId of the selected calendar to get * - in: path - * name: teamId + * name: externalId * schema: * type: integer * required: true - * description: Numeric teamId of the selected-calendar to get + * description: externalId of the selected-calendar to get + * - in: path + * name: integration + * schema: + * type: string + * required: true + * description: integration of the selected calendar to get * tags: * - selected-calendars * responses: @@ -105,7 +123,7 @@ export async function selectedCalendarById( const safeQuery = await schemaQueryIdAsString.safeParse(query); const safeBody = await schemaSelectedCalendarBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); - // This is how we set the userId and teamId in the query for managing compoundId. + // This is how we set the userId and externalId in the query for managing compoundId. const [userId, integration, externalId] = safeQuery.data.id.split("_"); switch (method) { diff --git a/pages/api/selected-calendars/index.ts b/pages/api/selected-calendars/index.ts index bfc6e7b834..faa2eb0e5c 100644 --- a/pages/api/selected-calendars/index.ts +++ b/pages/api/selected-calendars/index.ts @@ -3,14 +3,18 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { SelectedCalendarsResponse } from "@lib/types"; -import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar"; +import { SelectedCalendarResponse, SelectedCalendarsResponse } from "@lib/types"; +import { + schemaSelectedCalendarBodyParams, + schemaSelectedCalendarPublic, + withValidSelectedCalendar, +} from "@lib/validations/selected-calendar"; /** * @swagger - * /api/selected-calendars: + * /v1/selected-calendars: * get: - * summary: Returns all selected calendars + * summary: Get all selected calendars * tags: * - selected-calendars * responses: @@ -19,21 +23,54 @@ import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar * 401: * description: Authorization information is missing or invalid. * 404: - * description: No selectedCalendars were found + * description: No selected calendars were found + * post: + * summary: Creates a new selected calendar + * tags: + * - selected-calendars + * responses: + * 201: + * description: OK, selected calendar created + * model: SelectedCalendar + * 400: + * description: Bad request. SelectedCalendar body is invalid. + * 401: + * description: Authorization information is missing or invalid. */ -async function allSelectedCalendars(_: NextApiRequest, res: NextApiResponse) { - const selectedCalendars = await prisma.selectedCalendar.findMany(); - const data = selectedCalendars.map((selectedCalendar) => - schemaSelectedCalendarPublic.parse(selectedCalendar) - ); +async function createOrlistAllSelectedCalendars( + req: NextApiRequest, + res: NextApiResponse +) { + const { method } = req; + if (method === "GET") { + const selectedCalendars = await prisma.selectedCalendar.findMany(); + const data = selectedCalendars.map((selectedCalendar) => + schemaSelectedCalendarPublic.parse(selectedCalendar) + ); + if (data) res.status(200).json({ data }); + else + (error: Error) => + res.status(404).json({ + message: "No SelectedCalendars were found", + error, + }); + } else if (method === "POST") { + const safe = schemaSelectedCalendarBodyParams.safeParse(req.body); + if (!safe.success) throw new Error("Invalid request body"); - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "No SelectedCalendars were found", - error, - }); + const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data }); + const data = schemaSelectedCalendarPublic.parse(selectedCalendar); + + if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new selectedCalendar", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allSelectedCalendars); +export default withMiddleware("HTTP_GET_OR_POST")( + withValidSelectedCalendar(createOrlistAllSelectedCalendars) +); diff --git a/pages/api/selected-calendars/new.ts b/pages/api/selected-calendars/new.ts deleted file mode 100644 index 4534215c0c..0000000000 --- a/pages/api/selected-calendars/new.ts +++ /dev/null @@ -1,45 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { SelectedCalendarResponse } from "@lib/types"; -import { - schemaSelectedCalendarBodyParams, - schemaSelectedCalendarPublic, - withValidSelectedCalendar, -} from "@lib/validations/selected-calendar"; - -/** - * @swagger - * /api/selected-calendars/new: - * post: - * summary: Creates a new selected calendar - * tags: - * - selected-calendars - * responses: - * 201: - * description: OK, selected calendar created - * model: SelectedCalendar - * 400: - * description: Bad request. SelectedCalendar body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createSelectedCalendar(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaSelectedCalendarBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const selectedCalendar = await prisma.selectedCalendar.create({ data: safe.data }); - const data = schemaSelectedCalendarPublic.parse(selectedCalendar); - - if (data) res.status(201).json({ data, message: "SelectedCalendar created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new selectedCalendar", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidSelectedCalendar(createSelectedCalendar)); diff --git a/pages/api/selected-calendars/newIndex.ts b/pages/api/selected-calendars/newIndex.ts deleted file mode 100644 index 732eb013b2..0000000000 --- a/pages/api/selected-calendars/newIndex.ts +++ /dev/null @@ -1,68 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { PaymentResponse, PaymentsResponse } from "@lib/types"; -import { schemaPaymentBodyParams, schemaPaymentPublic, withValidPayment } from "@lib/validations/payment"; - -/** - * @swagger - * /api/payments: - * get: - * summary: Get all payments - * tags: - * - payments - * responses: - * 200: - * description: OK - * 401: - * description: Authorization information is missing or invalid. - * 404: - * description: No payments were found - * post: - * summary: Creates a new payment - * tags: - * - payments - * responses: - * 201: - * description: OK, payment created - * model: Payment - * 400: - * description: Bad request. Payment body is invalid. - * 401: - * description: Authorization information is missing or invalid. - */ -async function createOrlistAllPayments( - req: NextApiRequest, - res: NextApiResponse -) { - const { method } = req; - if (method === "GET") { - const payments = await prisma.payment.findMany(); - const data = payments.map((payment) => schemaPaymentPublic.parse(payment)); - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "No Payments were found", - error, - }); - } else if (method === "POST") { - const safe = schemaPaymentBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body"); - - const payment = await prisma.payment.create({ data: safe.data }); - const data = schemaPaymentPublic.parse(payment); - - if (data) res.status(201).json({ data, message: "Payment created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new payment", - error, - }); - } else res.status(405).json({ message: `Method ${method} not allowed` }); -} - -export default withMiddleware("HTTP_GET_OR_POST")(withValidPayment(createOrlistAllPayments)); diff --git a/pages/api/teams/[id].ts b/pages/api/teams/[id].ts index 21defc76b8..ef9f26845c 100644 --- a/pages/api/teams/[id].ts +++ b/pages/api/teams/[id].ts @@ -12,7 +12,7 @@ import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team"; /** * @swagger - * /api/teams/{id}: + * /v1/teams/{id}: * get: * summary: Get a team by ID * parameters: @@ -81,8 +81,8 @@ import { schemaTeamBodyParams, schemaTeamPublic } from "@lib/validations/team"; */ export async function teamById(req: NextApiRequest, res: NextApiResponse) { const { method, query, body } = req; - const safeQuery = await schemaQueryIdParseInt.safeParse(query); - const safeBody = await schemaTeamBodyParams.safeParse(body); + const safeQuery = schemaQueryIdParseInt.safeParse(query); + const safeBody = schemaTeamBodyParams.safeParse(body); if (!safeQuery.success) throw new Error("Invalid request query", safeQuery.error); switch (method) { diff --git a/pages/api/teams/index.ts b/pages/api/teams/index.ts index a4ed336c38..6184a5e1bd 100644 --- a/pages/api/teams/index.ts +++ b/pages/api/teams/index.ts @@ -3,14 +3,14 @@ import type { NextApiRequest, NextApiResponse } from "next"; import prisma from "@calcom/prisma"; import { withMiddleware } from "@lib/helpers/withMiddleware"; -import { TeamsResponse } from "@lib/types"; -import { schemaTeamPublic } from "@lib/validations/team"; +import { TeamResponse, TeamsResponse } from "@lib/types"; +import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team"; /** * @swagger - * /api/teams: + * /v1/teams: * get: - * summary: Returns all teams + * summary: Get all teams * tags: * - teams * responses: @@ -20,18 +20,46 @@ import { schemaTeamPublic } from "@lib/validations/team"; * description: Authorization information is missing or invalid. * 404: * description: No teams were found + * post: + * summary: Creates a new team + * 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. */ -async function allTeams(_: NextApiRequest, res: NextApiResponse) { - const teams = await prisma.team.findMany(); - const data = teams.map((team) => schemaTeamPublic.parse(team)); +async function createOrlistAllTeams(req: NextApiRequest, res: NextApiResponse) { + const { method } = req; + if (method === "GET") { + const teams = await prisma.team.findMany(); + const data = teams.map((team) => schemaTeamPublic.parse(team)); + if (data) res.status(200).json({ data }); + 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"); - if (data) res.status(200).json({ data }); - else - (error: Error) => - res.status(404).json({ - message: "No Teams were found", - error, - }); + const team = await prisma.team.create({ data: safe.data }); + const data = schemaTeamPublic.parse(team); + + if (data) res.status(201).json({ data, message: "Team created successfully" }); + else + (error: Error) => + res.status(400).json({ + message: "Could not create new team", + error, + }); + } else res.status(405).json({ message: `Method ${method} not allowed` }); } -export default withMiddleware("HTTP_GET")(allTeams); +export default withMiddleware("HTTP_GET_OR_POST")(withValidTeam(createOrlistAllTeams)); diff --git a/pages/api/teams/new.ts b/pages/api/teams/new.ts deleted file mode 100644 index 6100b5c42d..0000000000 --- a/pages/api/teams/new.ts +++ /dev/null @@ -1,41 +0,0 @@ -import type { NextApiRequest, NextApiResponse } from "next"; - -import prisma from "@calcom/prisma"; - -import { withMiddleware } from "@lib/helpers/withMiddleware"; -import type { TeamResponse } from "@lib/types"; -import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/validations/team"; - -/** - * @swagger - * /api/teams/new: - * post: - * summary: Creates a new team - * 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. - */ -async function createTeam(req: NextApiRequest, res: NextApiResponse) { - const safe = schemaTeamBodyParams.safeParse(req.body); - if (!safe.success) throw new Error("Invalid request body", safe.error); - - const team = await prisma.team.create({ data: safe.data }); - const data = schemaTeamPublic.parse(team); - - if (data) res.status(201).json({ data, message: "Team created successfully" }); - else - (error: Error) => - res.status(400).json({ - message: "Could not create new team", - error, - }); -} - -export default withMiddleware("HTTP_POST")(withValidTeam(createTeam)); diff --git a/pages/api/users/index.ts b/pages/api/users/index.ts index 17c975c32d..1d11d751d5 100644 --- a/pages/api/users/index.ts +++ b/pages/api/users/index.ts @@ -9,7 +9,7 @@ import { schemaUserPublic } from "@lib/validations/user"; /** * @swagger - * /api/users: + * /v1/users: * get: * summary: Get all users (admin only), returns your user if regular user. * tags: