Merge pull request #15 from calcom/docs/improve-swagger

This commit is contained in:
Agusti Fernandez
2022-04-02 02:10:50 +02:00
committed by GitHub
89 changed files with 2347 additions and 974 deletions
+23
View File
@@ -0,0 +1,23 @@
// Make a middleware that adds a cost to running the request
// by calling stripeSubscription addCost() * pricePerBooking
// Initially to test out 0,5 cent per booking via API call
// withCost(5)(endpoint)
// Should add a charge of 0.5 cent per booking to the subscription of the user making the request
import { NextMiddleware } from "next-api-middleware";
export const withCost = (priceInCents: number): NextMiddleware => {
return async function (req, res, next) {
console.log(req.headers);
if (
priceInCents > 0
// && stripeCustomerId && stripeSubscriptionId
) {
console.log(priceInCents);
// if (req.method === allowedHttpMethod || req.method == "OPTIONS") {
await next();
} else {
res.status(405).json({ message: `We weren't able to process the payment for this transaction` });
res.end();
}
};
};
+2 -3
View File
@@ -2,7 +2,7 @@ import { label } from "next-api-middleware";
import { addRequestId } from "./addRequestid";
import { captureErrors } from "./captureErrors";
import { HTTP_POST, HTTP_DELETE, HTTP_PATCH, HTTP_GET, httpMethod } from "./httpMethods";
import { HTTP_POST, HTTP_DELETE, HTTP_PATCH, HTTP_GET } from "./httpMethods";
import { verifyApiKey } from "./verifyApiKey";
const withMiddleware = label(
@@ -14,9 +14,8 @@ const withMiddleware = label(
addRequestId,
verifyApiKey,
sentry: captureErrors,
httpMethod: httpMethod("GET" || "DELETE" || "PATCH" || "POST"),
},
["sentry", "verifyApiKey", "httpMethod", "addRequestId"] // <-- Provide a list of middleware to call automatically
["sentry", "verifyApiKey", "addRequestId"] // <-- Provide a list of middleware to call automatically
);
export { withMiddleware };
+12
View File
@@ -2,8 +2,10 @@ import {
User,
ApiKey,
Team,
Credential,
SelectedCalendar,
EventType,
EventTypeCustomInput,
Attendee,
Availability,
BookingReference,
@@ -21,10 +23,12 @@ export type BaseResponse = {
message?: string;
error?: Error;
};
// User
export type UserResponse = BaseResponse & {
data?: Partial<User>;
};
export type UsersResponse = BaseResponse & {
data?: Partial<User>[];
};
@@ -117,6 +121,14 @@ export type MembershipsResponse = BaseResponse & {
data?: Partial<Membership>[];
};
// EventTypeCustomInput
export type EventTypeCustomInputResponse = BaseResponse & {
data?: Partial<EventTypeCustomInput>;
};
export type EventTypeCustomInputsResponse = BaseResponse & {
data?: Partial<EventTypeCustomInput>[];
};
// EventType
export type EventTypeResponse = BaseResponse & {
data?: Partial<EventType>;
+1 -1
View File
@@ -2,7 +2,7 @@ import { withValidation } from "next-validations";
import { _ApiKeyModel as ApiKey } from "@calcom/prisma/zod";
export const schemaApiKeyBodyParams = ApiKey.omit({ id: true, userId: true, createdAt: true });
export const schemaApiKeyBodyParams = ApiKey.omit({ id: true, userId: true, createdAt: true }).partial();
export const schemaApiKeyPublic = ApiKey.omit({
id: true,
+10 -1
View File
@@ -1,11 +1,20 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _AttendeeModel as Attendee } from "@calcom/prisma/zod";
export const schemaAttendeeBodyParams = Attendee.omit({ id: true });
export const schemaAttendeeBaseBodyParams = Attendee.omit({ id: true }).partial();
export const schemaAttendeePublic = Attendee.omit({});
const schemaAttendeeRequiredParams = z.object({
email: z.string().email(),
name: z.string(),
timeZone: z.string(),
});
export const schemaAttendeeBodyParams = schemaAttendeeBaseBodyParams.merge(schemaAttendeeRequiredParams);
export const withValidAttendee = withValidation({
schema: schemaAttendeeBodyParams,
type: "Zod",
+11 -1
View File
@@ -1,11 +1,21 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _AvailabilityModel as Availability } from "@calcom/prisma/zod";
export const schemaAvailabilityBodyParams = Availability.omit({ id: true });
export const schemaAvailabilityBaseBodyParams = Availability.omit({ id: true }).partial();
export const schemaAvailabilityPublic = Availability.omit({});
const schemaAvailabilityRequiredParams = z.object({
startTime: z.string(),
endTime: z.string(),
});
export const schemaAvailabilityBodyParams = schemaAvailabilityBaseBodyParams.merge(
schemaAvailabilityRequiredParams
);
export const withValidAvailability = withValidation({
schema: schemaAvailabilityBodyParams,
type: "Zod",
+11 -1
View File
@@ -1,11 +1,21 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _BookingReferenceModel as BookingReference } from "@calcom/prisma/zod";
export const schemaBookingReferenceBodyParams = BookingReference.omit({ id: true });
export const schemaBookingReferenceBaseBodyParams = BookingReference.omit({ id: true }).partial();
export const schemaBookingReferencePublic = BookingReference.omit({});
const schemaBookingReferenceRequiredParams = z.object({
type: z.string(),
uid: z.string(),
});
export const schemaBookingReferenceBodyParams = schemaBookingReferenceBaseBodyParams.merge(
schemaBookingReferenceRequiredParams
);
export const withValidBookingReference = withValidation({
schema: schemaBookingReferenceBodyParams,
type: "Zod",
+11 -1
View File
@@ -1,8 +1,18 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _BookingModel as Booking } from "@calcom/prisma/zod";
export const schemaBookingBodyParams = Booking.omit({ id: true });
const schemaBookingBaseBodyParams = Booking.omit({ id: true }).partial();
const schemaBookingRequiredParams = z.object({
uid: z.string(),
title: z.string(),
startTime: z.date(),
endTime: z.date(),
});
export const schemaBookingBodyParams = schemaBookingBaseBodyParams.merge(schemaBookingRequiredParams);
export const schemaBookingPublic = Booking.omit({});
+13 -1
View File
@@ -1,8 +1,20 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _CredentialModel as Credential } from "@calcom/prisma/zod";
export const schemaCredentialBodyParams = Credential.omit({ id: true });
import { jsonSchema } from "./shared/jsonSchema";
const schemaCredentialBaseBodyParams = Credential.omit({ id: true, userId: true }).partial();
const schemaCredentialRequiredParams = z.object({
type: z.string(),
key: jsonSchema,
});
export const schemaCredentialBodyParams = schemaCredentialBaseBodyParams.merge(
schemaCredentialRequiredParams
);
export const schemaCredentialPublic = Credential.omit({});
@@ -0,0 +1,37 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _EventTypeCustomInputModel as EventTypeCustomInput } from "@calcom/prisma/zod";
export const schemaEventTypeCustomInputBaseBodyParams = EventTypeCustomInput.omit({
id: true,
eventTypeId: true,
}).partial();
export const schemaEventTypeCustomInputPublic = EventTypeCustomInput.omit({});
const schemaEventTypeCustomInputRequiredParams = z.object({
label: z.string(),
required: z.boolean(),
// eventType: EventTypeModel.optional(),
type: z.enum(["TEXT", "TEXTLONG", "NUMBER", "BOOL"]),
eventType: z.object({
connect: z.object({
id: z.number().optional(),
// username: z.string().optional(),
// email: z.string().optional(),
}),
// FIXME: Provide valid EventTypeModel schema here, but not sure how yet.
create: z.any(),
}),
});
export const schemaEventTypeCustomInputBodyParams = schemaEventTypeCustomInputBaseBodyParams.merge(
schemaEventTypeCustomInputRequiredParams
);
export const withValidEventTypeCustomInput = withValidation({
schema: schemaEventTypeCustomInputBodyParams,
type: "Zod",
mode: "body",
});
+9 -1
View File
@@ -1,8 +1,16 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _ScheduleModel as Schedule } from "@calcom/prisma/zod";
export const schemaScheduleBodyParams = Schedule.omit({ id: true });
const schemaScheduleBaseBodyParams = Schedule.omit({ id: true }).partial();
const schemaScheduleRequiredParams = z.object({
userId: z.number(),
name: z.string(),
});
export const schemaScheduleBodyParams = schemaScheduleBaseBodyParams.merge(schemaScheduleRequiredParams);
export const schemaSchedulePublic = Schedule.omit({});
+21 -2
View File
@@ -1,10 +1,29 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _SelectedCalendarModel as SelectedCalendar } from "@calcom/prisma/zod";
export const schemaSelectedCalendarBodyParams = SelectedCalendar.omit({});
export const schemaSelectedCalendarBaseBodyParams = SelectedCalendar.omit({ userId: true }).partial();
export const schemaSelectedCalendarPublic = SelectedCalendar.omit({ userId: true });
export const schemaSelectedCalendarPublic = SelectedCalendar.omit({});
const schemaSelectedCalendarRequiredParams = z.object({
externalId: z.string(),
integration: z.string(),
user: z.object({
connect: z.object({
id: z.number().optional(),
username: z.string().optional(),
email: z.string().optional(),
}),
// FIXME: Provide valid UserModel schema here, but not sure how yet.
create: z.any(),
}),
});
export const schemaSelectedCalendarBodyParams = schemaSelectedCalendarBaseBodyParams.merge(
schemaSelectedCalendarRequiredParams
);
export const withValidSelectedCalendar = withValidation({
schema: schemaSelectedCalendarBodyParams,
+9
View File
@@ -0,0 +1,9 @@
import { z } from "zod";
// Helper schema for JSON fields
type Literal = boolean | number | string;
type Json = Literal | { [key: string]: Json } | Json[];
const literalSchema = z.union([z.string(), z.number(), z.boolean()]);
export const jsonSchema: z.ZodSchema<Json> = z.lazy(() =>
z.union([literalSchema, z.array(jsonSchema), z.record(jsonSchema)])
);
+1 -1
View File
@@ -2,7 +2,7 @@ import { withValidation } from "next-validations";
import { _TeamModel as Team } from "@calcom/prisma/zod";
export const schemaTeamBodyParams = Team.omit({ id: true });
export const schemaTeamBodyParams = Team.omit({ id: true }).partial();
export const schemaTeamPublic = Team.omit({});
+8 -1
View File
@@ -1,15 +1,22 @@
import { withValidation } from "next-validations";
import { z } from "zod";
import { _UserModel as User } from "@calcom/prisma/zod";
export const schemaUserBodyParams = User.omit({
export const schemaUserBaseBodyParams = User.omit({
id: true,
createdAt: true,
password: true,
twoFactorEnabled: true,
twoFactorSecret: true,
}).partial();
const schemaUserRequiredParams = z.object({
email: z.string().email(),
});
export const schemaUserBodyParams = schemaUserBaseBodyParams.merge(schemaUserRequiredParams);
export const schemaUserPublic = User.omit({
identityProvider: true,
identityProviderId: true,
+1 -1
View File
@@ -2,7 +2,7 @@ import { withValidation } from "next-validations";
import { _WebhookModel as Webhook } from "@calcom/prisma/zod";
export const schemaWebhookBodyParams = Webhook.omit({ id: true });
export const schemaWebhookBodyParams = Webhook.omit({ id: true }).partial();
export const schemaWebhookPublic = Webhook.omit({});
+1 -2
View File
@@ -38,7 +38,6 @@
"next-swagger-doc": "^0.2.1",
"next-transpile-modules": "^9.0.0",
"next-validations": "^0.1.11",
"typescript": "^4.6.3",
"zod": "^3.14.2"
"typescript": "^4.6.3"
}
}
+1 -1
View File
@@ -9,7 +9,7 @@ export default async function requireApiKeyAsQueryParams({ nextUrl }: NextReques
const apiKey = nextUrl.searchParams.get("apiKey");
if (apiKey) return response;
// if no apiKey is passed, we throw early a 401 unauthorized
// if no apiKey is passed, we throw early a 401 unauthorized asking for a valid apiKey
else
new NextResponse(
JSON.stringify({
+38 -16
View File
@@ -2,27 +2,49 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/attendees/{id}/delete:
* delete:
* summary: Remove an existing attendee
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to delete
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee removed successfuly
* model: Attendee
* 400:
* description: Bad request. Attendee id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteAttendee(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const attendee = await prisma.attendee.delete({ where: { id: safe.data.id } });
// We only remove the attendee type from the database if there's an existing resource.
if (attendee) res.status(200).json({ message: `attendee with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.attendee.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.attendee.delete({ where: { id: safe.data.id } });
if (data) res.status(200).json({ message: `Attendee with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `Attendee with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(attendee);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteAttendee));
+44 -27
View File
@@ -1,41 +1,58 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Attendee } from "@calcom/prisma/client";
import { schemaAttendee, withValidAttendee } from "@lib/validations/attendee";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AttendeeResponse } from "@lib/types";
import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Attendee;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/attendees/{id}/edit:
* patch:
* summary: Edit an existing attendee
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to edit
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee edited successfuly
* model: Attendee
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editAttendee(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaAttendeeBodyParams.safeParse(req.body);
export async function editAttendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaAttendee.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const attendee = await prisma.attendee.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaAttendeePublic.parse(attendee);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
await prisma.attendee
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((attendee) => {
res.status(200).json({ data: attendee });
})
.catch((error) => {
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating attendees" });
}
export default withValidQueryIdTransformParseInt(withValidAttendee(editAttendee));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidAttendee(editAttendee))
);
+38 -17
View File
@@ -1,30 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Attendee } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AttendeeResponse } from "@lib/types";
import { schemaAttendeePublic } from "@lib/validations/attendee";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Attendee;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/attendees/{id}:
* get:
* summary: Get a attendee by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the attendee to get
* tags:
* - attendees
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Attendee was not found
*/
export async function attendeeById(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
const attendee = await prisma.attendee.findUnique({ where: { id: safe.data.id } });
const data = schemaAttendeePublic.parse(attendee);
if (method === "GET" && safe.success) {
const attendee = await prisma.attendee.findUnique({ where: { id: safe.data.id } });
if (attendee) res.status(200).json({ data: attendee });
if (!attendee) res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (attendee) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Attendee was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(attendee);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(attendeeById));
+31 -13
View File
@@ -1,19 +1,37 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Attendee } from "@calcom/prisma/client";
type ResponseData = {
data?: Attendee[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { AttendeesResponse } from "@lib/types";
import { schemaAttendeePublic } from "@lib/validations/attendee";
export default async function attendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
try {
const data = await prisma.attendee.findMany();
res.status(200).json({ data });
} catch (error) {
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
/**
* @swagger
* /api/attendees:
* get:
* summary: Get all attendees
* tags:
* - attendees
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No attendees were found
*/
async function allAttendees(_: NextApiRequest, res: NextApiResponse<AttendeesResponse>) {
const attendees = await prisma.attendee.findMany();
const data = attendees.map((attendee) => schemaAttendeePublic.parse(attendee));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Attendees were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allAttendees);
+39 -18
View File
@@ -1,27 +1,48 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Attendee } from "@calcom/prisma/client";
import { schemaAttendee, withValidAttendee } from "@lib/validations/attendee";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AttendeeResponse } from "@lib/types";
import { schemaAttendeeBodyParams, schemaAttendeePublic, withValidAttendee } from "@lib/validations/attendee";
type ResponseData = {
data?: Attendee;
message?: string;
error?: string;
};
/**
* @swagger
* /api/attendees/new:
* post:
* summary: Creates a new attendee
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Attendee'
* tags:
* - attendees
* responses:
* 201:
* description: OK, attendee created
* model: Attendee
* 400:
* description: Bad request. Attendee body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createAttendee(req: NextApiRequest, res: NextApiResponse<AttendeeResponse>) {
const safe = schemaAttendeeBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createAttendee(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaAttendee.safeParse(body);
const attendee = await prisma.attendee.create({ data: safe.data });
const data = schemaAttendeePublic.parse(attendee);
if (method === "POST" && safe.success) {
await prisma.attendee
.create({ data: safe.data })
.then((attendee) => res.status(201).json({ data: attendee }))
.catch((error) => res.status(400).json({ message: "Could not create attendee type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
if (data) res.status(201).json({ data, message: "Attendee created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new attendee",
error,
});
}
export default withValidAttendee(createAttendee);
export default withMiddleware("HTTP_POST")(withValidAttendee(createAttendee));
+38 -17
View File
@@ -2,28 +2,49 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/availabilites/{id}/delete:
* delete:
* summary: Remove an existing availability
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to delete
* tags:
* - availabilites
* responses:
* 201:
* description: OK, availability removed successfuly
* model: Availability
* 400:
* description: Bad request. Availability id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteAvailability(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function availability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const availability = await prisma.availability.delete({ where: { id: safe.data.id } });
// We only remove the availability type from the database if there's an existing resource.
if (availability)
res.status(200).json({ message: `availability with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.availability.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.availability.delete({ where: { id: safe.data.id } });
if (data) res.status(200).json({ message: `Availability with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `Availability with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(availability);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteAvailability));
+48 -27
View File
@@ -1,41 +1,62 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Availability } from "@calcom/prisma/client";
import { schemaAvailability, withValidAvailability } from "@lib/validations/availability";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AvailabilityResponse } from "@lib/types";
import {
schemaAvailabilityBodyParams,
schemaAvailabilityPublic,
withValidAvailability,
} from "@lib/validations/availability";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Availability;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/availabilites/{id}/edit:
* patch:
* summary: Edit an existing availability
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to edit
* tags:
* - availabilites
* responses:
* 201:
* description: OK, availability edited successfuly
* model: Availability
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editAvailability(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaAvailabilityBodyParams.safeParse(req.body);
export async function editAvailability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaAvailability.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const availability = await prisma.availability.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaAvailabilityPublic.parse(availability);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
await prisma.availability
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((availability) => {
res.status(200).json({ data: availability });
})
.catch((error) => {
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
// Reject any other HTTP method than PATCH
} else res.status(405).json({ message: "Only PATCH Method allowed for updating availabilities" });
}
export default withValidQueryIdTransformParseInt(withValidAvailability(editAvailability));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidAvailability(editAvailability))
);
+38 -17
View File
@@ -1,30 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Availability } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AvailabilityResponse } from "@lib/types";
import { schemaAvailabilityPublic } from "@lib/validations/availability";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Availability;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/availabilites/{id}:
* get:
* summary: Get a availability by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the availability to get
* tags:
* - availabilites
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Availability was not found
*/
export async function availabilityById(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function availability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
const availability = await prisma.availability.findUnique({ where: { id: safe.data.id } });
const data = schemaAvailabilityPublic.parse(availability);
if (method === "GET" && safe.success) {
const availability = await prisma.availability.findUnique({ where: { id: safe.data.id } });
if (availability) res.status(200).json({ data: availability });
if (!availability) res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (availability) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Availability was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(availability);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(availabilityById));
+31 -13
View File
@@ -1,19 +1,37 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Availability } from "@calcom/prisma/client";
type ResponseData = {
data?: Availability[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { AvailabilitysResponse } from "@lib/types";
import { schemaAvailabilityPublic } from "@lib/validations/availability";
export default async function availability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
try {
const data = await prisma.availability.findMany();
res.status(200).json({ data });
} catch (error) {
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
/**
* @swagger
* /api/availabilites:
* get:
* summary: Get all availabilites
* tags:
* - availabilites
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No availabilites were found
*/
async function allAvailabilitys(_: NextApiRequest, res: NextApiResponse<AvailabilitysResponse>) {
const availabilites = await prisma.availability.findMany();
const data = availabilites.map((availability) => schemaAvailabilityPublic.parse(availability));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Availabilitys were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allAvailabilitys);
+44 -24
View File
@@ -1,32 +1,52 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Availability } from "@calcom/prisma/client";
import { schemaAvailability, withValidAvailability } from "@lib/validations/availability";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { AvailabilityResponse } from "@lib/types";
import {
schemaAvailabilityBodyParams,
schemaAvailabilityPublic,
withValidAvailability,
} from "@lib/validations/availability";
type ResponseData = {
data?: Availability;
message?: string;
error?: string;
};
/**
* @swagger
* /api/availabilites/new:
* post:
* summary: Creates a new availability
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Availability'
* tags:
* - availabilites
* responses:
* 201:
* description: OK, availability created
* model: Availability
* 400:
* description: Bad request. Availability body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createAvailability(req: NextApiRequest, res: NextApiResponse<AvailabilityResponse>) {
const safe = schemaAvailabilityBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createAvailability(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
if (method === "POST") {
const safe = schemaAvailability.safeParse(body);
if (safe.success && safe.data) {
await prisma.availability
.create({ data: safe.data })
.then((availability) => res.status(201).json({ data: availability }))
.catch((error) =>
res.status(400).json({ message: "Could not create availability type", error: error })
);
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ error: "Only POST Method allowed" });
}
const availability = await prisma.availability.create({ data: safe.data });
const data = schemaAvailabilityPublic.parse(availability);
if (data) res.status(201).json({ data, message: "Availability created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new availability",
error,
});
}
export default withValidAvailability(createAvailability);
export default withMiddleware("HTTP_POST")(withValidAvailability(createAvailability));
+39 -17
View File
@@ -2,28 +2,50 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/booking-references/{id}/delete:
* delete:
* summary: Remove an existing bookingReference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the bookingReference to delete
* tags:
* - bookingReferences
* responses:
* 201:
* description: OK, bookingReference removed successfuly
* model: BookingReference
* 400:
* description: Bad request. BookingReference id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteBookingReference(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteBookingReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const bookingReference = await prisma.bookingReference.delete({ where: { id: safe.data.id } });
// We only remove the bookingReference type from the database if there's an existing resource.
if (bookingReference)
res.status(200).json({ message: `bookingReference with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.bookingReference.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.bookingReference.delete({ where: { id: safe.data.id } });
if (data)
res.status(200).json({ message: `BookingReference with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `BookingReference with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteBookingReference);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteBookingReference));
+52 -25
View File
@@ -1,38 +1,65 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingReferenceResponse } from "@lib/types";
import {
schemaBookingReferenceBodyParams,
schemaBookingReferencePublic,
withValidBookingReference,
} from "@lib/validations/booking-reference";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: BookingReference;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/booking-references/{id}/edit:
* patch:
* summary: Edit an existing bookingReference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the bookingReference to edit
* tags:
* - bookingReferences
* responses:
* 201:
* description: OK, bookingReference edited successfuly
* model: BookingReference
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editBookingReference(
req: NextApiRequest,
res: NextApiResponse<BookingReferenceResponse>
) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaBookingReferenceBodyParams.safeParse(req.body);
export async function editBookingReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaBookingReference.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const bookingReference = await prisma.bookingReference.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaBookingReferencePublic.parse(bookingReference);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.bookingReference.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating bookingReferences" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidBookingReference(editBookingReference));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidBookingReference(editBookingReference))
);
+41 -16
View File
@@ -1,29 +1,54 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingReferenceResponse } from "@lib/types";
import { schemaBookingReferencePublic } from "@lib/validations/booking-reference";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: BookingReference;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/booking-references/{id}:
* get:
* summary: Get a bookingReference by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the bookingReference to get
* tags:
* - bookingReferences
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: BookingReference was not found
*/
export async function bookingReferenceById(
req: NextApiRequest,
res: NextApiResponse<BookingReferenceResponse>
) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function bookingReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.bookingReference.findUnique({ where: { id: safe.data.id } });
const bookingReference = await prisma.bookingReference.findUnique({ where: { id: safe.data.id } });
const data = schemaBookingReferencePublic.parse(bookingReference);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (bookingReference) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "BookingReference was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(bookingReference);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(bookingReferenceById));
+32 -8
View File
@@ -1,15 +1,39 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
type ResponseData = {
data?: BookingReference[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { BookingReferencesResponse } from "@lib/types";
import { schemaBookingReferencePublic } from "@lib/validations/booking-reference";
/**
* @swagger
* /api/booking-references:
* get:
* summary: Get all bookingReferences
* tags:
* - bookingReferences
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No bookingReferences were found
*/
async function allBookingReferences(_: NextApiRequest, res: NextApiResponse<BookingReferencesResponse>) {
const bookingReferences = await prisma.bookingReference.findMany();
const data = bookingReferences.map((bookingReference) =>
schemaBookingReferencePublic.parse(bookingReference)
);
export default async function bookingReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.bookingReference.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No BookingReferences were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allBookingReferences);
+44 -20
View File
@@ -1,28 +1,52 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import { schemaBookingReference, withValidBookingReference } from "@lib/validations/booking-reference";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingReferenceResponse } from "@lib/types";
import {
schemaBookingReferenceBodyParams,
schemaBookingReferencePublic,
withValidBookingReference,
} from "@lib/validations/booking-reference";
type ResponseData = {
data?: BookingReference;
message?: string;
error?: string;
};
/**
* @swagger
* /api/booking-references/new:
* post:
* summary: Creates a new bookingReference
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/BookingReference'
* tags:
* - bookingReferences
* responses:
* 201:
* description: OK, bookingReference created
* model: BookingReference
* 400:
* description: Bad request. BookingReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createBookingReference(req: NextApiRequest, res: NextApiResponse<BookingReferenceResponse>) {
const safe = schemaBookingReferenceBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createBookingReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaBookingReference.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.bookingReference
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) =>
res.status(400).json({ message: "Could not create bookingReference type", error: error })
);
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const bookingReference = await prisma.bookingReference.create({ data: safe.data });
const data = schemaBookingReferencePublic.parse(bookingReference);
if (data) res.status(201).json({ data, message: "BookingReference created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new bookingReference",
error,
});
}
export default withValidBookingReference(createBookingReference);
export default withMiddleware("HTTP_POST")(withValidBookingReference(createBookingReference));
+38 -17
View File
@@ -2,28 +2,49 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/bookings/{id}/delete:
* delete:
* summary: Remove an existing booking
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking to delete
* tags:
* - bookings
* responses:
* 201:
* description: OK, booking removed successfuly
* model: Booking
* 400:
* description: Bad request. Booking id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteBooking(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const booking = await prisma.booking.delete({ where: { id: safe.data.id } });
// We only remove the booking type from the database if there's an existing resource.
if (booking) res.status(200).json({ message: `booking with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.booking.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else
res.status(405).json({ message: "Only DELETE Method allowed in /availabilities/[id]/delete endpoint" });
const data = await prisma.booking.delete({ where: { id: safe.data.id } });
if (data) res.status(200).json({ message: `Booking with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `Booking with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteBooking);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteBooking));
+43 -32
View File
@@ -1,45 +1,56 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import { schemaBooking, withValidBooking } from "@lib/validations/booking";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingResponse } from "@lib/types";
import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Booking;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/bookings/{id}/edit:
* patch:
* summary: Edit an existing booking
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking to edit
* tags:
* - bookings
* responses:
* 201:
* description: OK, booking edited successfuly
* model: Booking
* 400:
* description: Bad request. Booking body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editBooking(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaBookingBodyParams.safeParse(req.body);
export async function editBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaBooking.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const booking = await prisma.booking.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaBookingPublic.parse(booking);
if (method === "PATCH") {
if (safeQuery.success && safeBody.success) {
await prisma.booking
.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
})
.then((booking) => {
res.status(200).json({ data: booking });
})
.catch((error) => {
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
});
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only PATCH Method allowed for updating bookings" });
}
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidBooking(editBooking));
export default withMiddleware("HTTP_PATCH")(withValidQueryIdTransformParseInt(withValidBooking(editBooking)));
+38 -19
View File
@@ -1,32 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingResponse } from "@lib/types";
import { schemaBookingPublic } from "@lib/validations/booking";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Booking;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/bookings/{id}:
* get:
* summary: Get a booking by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the booking to get
* tags:
* - bookings
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Booking was not found
*/
export async function bookingById(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function booking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
const booking = await prisma.booking.findUnique({ where: { id: safe.data.id } });
const data = schemaBookingPublic.parse(booking);
if (method === "GET" && safe.success) {
const booking = await prisma.booking.findUnique({ where: { id: safe.data.id } });
if (booking) res.status(200).json({ data: booking });
if (!booking) res.status(404).json({ message: "Event type not found" });
} else {
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
}
if (booking) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Booking was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(booking);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(bookingById));
+31 -13
View File
@@ -1,19 +1,37 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
type ResponseData = {
data?: Booking[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { BookingsResponse } from "@lib/types";
import { schemaBookingPublic } from "@lib/validations/booking";
export default async function booking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
try {
const data = await prisma.booking.findMany();
res.status(200).json({ data });
} catch (error) {
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
/**
* @swagger
* /api/bookings:
* get:
* summary: Get all bookings
* tags:
* - bookings
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No bookings were found
*/
async function allBookings(_: NextApiRequest, res: NextApiResponse<BookingsResponse>) {
const bookings = await prisma.booking.findMany();
const data = bookings.map((booking) => schemaBookingPublic.parse(booking));
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "No Bookings were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allBookings);
+40 -22
View File
@@ -1,30 +1,48 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Booking } from "@calcom/prisma/client";
import { schemaBooking, withValidBooking } from "@lib/validations/booking";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BookingResponse } from "@lib/types";
import { schemaBookingBodyParams, schemaBookingPublic, withValidBooking } from "@lib/validations/booking";
type ResponseData = {
data?: Booking;
message?: string;
error?: string;
};
/**
* @swagger
* /api/bookings/new:
* post:
* summary: Creates a new booking
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Booking'
* tags:
* - bookings
* responses:
* 201:
* description: OK, booking created
* model: Booking
* 400:
* description: Bad request. Booking body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createBooking(req: NextApiRequest, res: NextApiResponse<BookingResponse>) {
const safe = schemaBookingBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createBooking(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
if (method === "POST") {
const safe = schemaBooking.safeParse(body);
if (safe.success && safe.data) {
await prisma.booking
.create({ data: safe.data })
.then((booking) => res.status(201).json({ data: booking }))
.catch((error) => res.status(400).json({ message: "Could not create booking type", error: error }));
}
} else {
// Reject any other HTTP method than POST
res.status(405).json({ error: "Only POST Method allowed" });
}
const booking = await prisma.booking.create({ data: safe.data });
const data = schemaBookingPublic.parse(booking);
if (data) res.status(201).json({ data, message: "Booking created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new booking",
error,
});
}
export default withValidBooking(createBooking);
export default withMiddleware("HTTP_POST")(withValidBooking(createBooking));
+38 -17
View File
@@ -2,28 +2,49 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/credentials/{id}/delete:
* delete:
* summary: Remove an existing credential
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the credential to delete
* tags:
* - credentials
* responses:
* 201:
* description: OK, credential removed successfuly
* model: Credential
* 400:
* description: Bad request. Credential id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteCredential(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteCredential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const credential = await prisma.credential.delete({ where: { id: safe.data.id } });
// We only remove the credential type from the database if there's an existing resource.
if (credential)
res.status(200).json({ message: `credential with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.credential.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.credential.delete({ where: { id: safe.data.id } });
if (data) res.status(200).json({ message: `Credential with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `Credential with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteCredential);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteCredential));
+49 -25
View File
@@ -1,38 +1,62 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import { schemaCredential, withValidCredential } from "@lib/validations/credential";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { CredentialResponse } from "@lib/types";
import {
schemaCredentialBodyParams,
schemaCredentialPublic,
withValidCredential,
} from "@lib/validations/credential";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Credential;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/credentials/{id}/edit:
* patch:
* summary: Edit an existing credential
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the credential to edit
* tags:
* - credentials
* responses:
* 201:
* description: OK, credential edited successfuly
* model: Credential
* 400:
* description: Bad request. Credential body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editCredential(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaCredentialBodyParams.safeParse(req.body);
export async function editCredential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaCredential.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const credential = await prisma.credential.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaCredentialPublic.parse(credential);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.credential.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating credentials" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidCredential(editCredential));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidCredential(editCredential))
);
+38 -16
View File
@@ -1,29 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { CredentialResponse } from "@lib/types";
import { schemaCredentialPublic } from "@lib/validations/credential";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Credential;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/credentials/{id}:
* get:
* summary: Get a credential by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the credential to get
* tags:
* - credentials
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Credential was not found
*/
export async function credentialById(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function credential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.credential.findUnique({ where: { id: safe.data.id } });
const credential = await prisma.credential.findUnique({ where: { id: safe.data.id } });
const data = schemaCredentialPublic.parse(credential);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (credential) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Credential was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(credential);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(credentialById));
+30 -8
View File
@@ -1,15 +1,37 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
type ResponseData = {
data?: Credential[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { CredentialsResponse } from "@lib/types";
import { schemaCredentialPublic } from "@lib/validations/credential";
/**
* @swagger
* /api/credentials:
* get:
* summary: Get all credentials
* tags:
* - credentials
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No credentials were found
*/
async function allCredentials(_: NextApiRequest, res: NextApiResponse<CredentialsResponse>) {
const credentials = await prisma.credential.findMany();
const data = credentials.map((credential) => schemaCredentialPublic.parse(credential));
export default async function credential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.credential.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No Credentials were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allCredentials);
+44 -18
View File
@@ -1,26 +1,52 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import { schemaCredential, withValidCredential } from "@lib/validations/credential";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { CredentialResponse } from "@lib/types";
import {
schemaCredentialBodyParams,
schemaCredentialPublic,
withValidCredential,
} from "@lib/validations/credential";
type ResponseData = {
data?: Credential;
message?: string;
error?: string;
};
/**
* @swagger
* /api/credentials/new:
* post:
* summary: Creates a new credential
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/Credential'
* tags:
* - credentials
* responses:
* 201:
* description: OK, credential created
* model: Credential
* 400:
* description: Bad request. Credential body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createCredential(req: NextApiRequest, res: NextApiResponse<CredentialResponse>) {
const safe = schemaCredentialBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createCredential(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaCredential.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.credential
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) => res.status(400).json({ message: "Could not create credential type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const credential = await prisma.credential.create({ data: safe.data });
const data = schemaCredentialPublic.parse(credential);
if (data) res.status(201).json({ data, message: "Credential created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new credential",
error,
});
}
export default withValidCredential(createCredential);
export default withMiddleware("HTTP_POST")(withValidCredential(createCredential));
+37 -18
View File
@@ -3,30 +3,49 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
/**
* @swagger
* /api/daily-event-references/{id}/delete:
* delete:
* summary: Remove an existing dailyEventReference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the dailyEventReference to delete
* tags:
* - dailyEventReferences
* responses:
* 201:
* description: OK, dailyEventReference removed successfuly
* model: DailyEventReference
* 400:
* description: Bad request. DailyEventReference id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (safe.success) {
const deletedDailyEventReference = await prisma.dailyEventReference.delete({
where: { id: safe.data.id },
});
// We only remove the dailyEventReference type from the database if there's an existing resource.
if (deletedDailyEventReference)
res.status(200).json({ message: `dailyEventReference with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.dailyEventReference.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
if (!safe.success) throw new Error("Invalid request query", safe.error);
const data = await prisma.dailyEventReference.delete({ where: { id: safe.data.id } });
if (data)
res.status(200).json({ message: `DailyEventReference with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `DailyEventReference with id: ${safe.data.id} was not able to be processed`,
error,
});
}
// export default withValidQueryIdTransformParseInt(deleteDailyEventReference);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteDailyEventReference));
+49 -25
View File
@@ -1,10 +1,12 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DailyEventReferenceResponse } from "@lib/types";
import {
schemaDailyEventReference,
schemaDailyEventReferenceBodyParams,
schemaDailyEventReferencePublic,
withValidDailyEventReference,
} from "@lib/validations/daily-event-reference";
import {
@@ -12,30 +14,52 @@ import {
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DailyEventReference;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/daily-event-references/{id}/edit:
* patch:
* summary: Edit an existing dailyEventReference
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the dailyEventReference to edit
* tags:
* - dailyEventReferences
* responses:
* 201:
* description: OK, dailyEventReference edited successfuly
* model: DailyEventReference
* 400:
* description: Bad request. DailyEventReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editDailyEventReference(
req: NextApiRequest,
res: NextApiResponse<DailyEventReferenceResponse>
) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaDailyEventReferenceBodyParams.safeParse(req.body);
export async function editDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaDailyEventReference.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const dailyEventReference = await prisma.dailyEventReference.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaDailyEventReferencePublic.parse(dailyEventReference);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.dailyEventReference.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating dailyEventReferences" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidDailyEventReference(editDailyEventReference));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidDailyEventReference(editDailyEventReference))
);
+41 -16
View File
@@ -1,29 +1,54 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DailyEventReferenceResponse } from "@lib/types";
import { schemaDailyEventReferencePublic } from "@lib/validations/daily-event-reference";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DailyEventReference;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/daily-event-references/{id}:
* get:
* summary: Get a dailyEventReference by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the dailyEventReference to get
* tags:
* - dailyEventReferences
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: DailyEventReference was not found
*/
export async function dailyEventReferenceById(
req: NextApiRequest,
res: NextApiResponse<DailyEventReferenceResponse>
) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function dailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } });
const dailyEventReference = await prisma.dailyEventReference.findUnique({ where: { id: safe.data.id } });
const data = schemaDailyEventReferencePublic.parse(dailyEventReference);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (dailyEventReference) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "DailyEventReference was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(dailyEventReference);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(dailyEventReferenceById));
+35 -8
View File
@@ -1,15 +1,42 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
type ResponseData = {
data?: DailyEventReference[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { DailyEventReferencesResponse } from "@lib/types";
import { schemaDailyEventReferencePublic } from "@lib/validations/daily-event-reference";
/**
* @swagger
* /api/daily-event-references:
* get:
* summary: Get all dailyEventReferences
* tags:
* - dailyEventReferences
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No dailyEventReferences were found
*/
async function allDailyEventReferences(
_: NextApiRequest,
res: NextApiResponse<DailyEventReferencesResponse>
) {
const dailyEventReferences = await prisma.dailyEventReference.findMany();
const data = dailyEventReferences.map((dailyEventReference) =>
schemaDailyEventReferencePublic.parse(dailyEventReference)
);
export default async function dailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.dailyEventReference.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No DailyEventReferences were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allDailyEventReferences);
+44 -20
View File
@@ -1,31 +1,55 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DailyEventReferenceResponse } from "@lib/types";
import {
schemaDailyEventReference,
schemaDailyEventReferenceBodyParams,
schemaDailyEventReferencePublic,
withValidDailyEventReference,
} from "@lib/validations/daily-event-reference";
type ResponseData = {
data?: DailyEventReference;
message?: string;
error?: string;
};
/**
* @swagger
* /api/daily-event-references/new:
* post:
* summary: Creates a new dailyEventReference
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DailyEventReference'
* tags:
* - dailyEventReferences
* responses:
* 201:
* description: OK, dailyEventReference created
* model: DailyEventReference
* 400:
* description: Bad request. DailyEventReference body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createDailyEventReference(
req: NextApiRequest,
res: NextApiResponse<DailyEventReferenceResponse>
) {
const safe = schemaDailyEventReferenceBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaDailyEventReference.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.dailyEventReference
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) =>
res.status(400).json({ message: "Could not create dailyEventReference type", error: error })
);
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const dailyEventReference = await prisma.dailyEventReference.create({ data: safe.data });
const data = schemaDailyEventReferencePublic.parse(dailyEventReference);
if (data) res.status(201).json({ data, message: "DailyEventReference created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new dailyEventReference",
error,
});
}
export default withValidDailyEventReference(createDailyEventReference);
export default withMiddleware("HTTP_POST")(withValidDailyEventReference(createDailyEventReference));
+39 -17
View File
@@ -2,28 +2,50 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/destination-calendars/{id}/delete:
* delete:
* summary: Remove an existing destinationCalendar
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the destinationCalendar to delete
* tags:
* - destinationCalendars
* responses:
* 201:
* description: OK, destinationCalendar removed successfuly
* model: DestinationCalendar
* 400:
* description: Bad request. DestinationCalendar id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteDestinationCalendar(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteDestinationCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const destinationCalendar = await prisma.destinationCalendar.delete({ where: { id: safe.data.id } });
// We only remove the destinationCalendar type from the database if there's an existing resource.
if (destinationCalendar)
res.status(200).json({ message: `destinationCalendar with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.destinationCalendar.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.destinationCalendar.delete({ where: { id: safe.data.id } });
if (data)
res.status(200).json({ message: `DestinationCalendar with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `DestinationCalendar with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteDestinationCalendar);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteDestinationCalendar));
+49 -25
View File
@@ -1,10 +1,12 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DestinationCalendarResponse } from "@lib/types";
import {
schemaDestinationCalendar,
schemaDestinationCalendarBodyParams,
schemaDestinationCalendarPublic,
withValidDestinationCalendar,
} from "@lib/validations/destination-calendar";
import {
@@ -12,30 +14,52 @@ import {
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DestinationCalendar;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/destination-calendars/{id}/edit:
* patch:
* summary: Edit an existing destinationCalendar
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the destinationCalendar to edit
* tags:
* - destinationCalendars
* responses:
* 201:
* description: OK, destinationCalendar edited successfuly
* model: DestinationCalendar
* 400:
* description: Bad request. DestinationCalendar body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editDestinationCalendar(
req: NextApiRequest,
res: NextApiResponse<DestinationCalendarResponse>
) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaDestinationCalendarBodyParams.safeParse(req.body);
export async function editDestinationCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaDestinationCalendar.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const destinationCalendar = await prisma.destinationCalendar.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaDestinationCalendarPublic.parse(destinationCalendar);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.destinationCalendar.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating destinationCalendars" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidDestinationCalendar(editDestinationCalendar));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidDestinationCalendar(editDestinationCalendar))
);
+41 -16
View File
@@ -1,29 +1,54 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DestinationCalendarResponse } from "@lib/types";
import { schemaDestinationCalendarPublic } from "@lib/validations/destination-calendar";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DestinationCalendar;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/destination-calendars/{id}:
* get:
* summary: Get a destinationCalendar by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the destinationCalendar to get
* tags:
* - destinationCalendars
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: DestinationCalendar was not found
*/
export async function destinationCalendarById(
req: NextApiRequest,
res: NextApiResponse<DestinationCalendarResponse>
) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function destinationCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } });
const destinationCalendar = await prisma.destinationCalendar.findUnique({ where: { id: safe.data.id } });
const data = schemaDestinationCalendarPublic.parse(destinationCalendar);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (destinationCalendar) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "DestinationCalendar was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(destinationCalendar);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(destinationCalendarById));
+35 -8
View File
@@ -1,15 +1,42 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
type ResponseData = {
data?: DestinationCalendar[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { DestinationCalendarsResponse } from "@lib/types";
import { schemaDestinationCalendarPublic } from "@lib/validations/destination-calendar";
/**
* @swagger
* /api/destination-calendars:
* get:
* summary: Get all destinationCalendars
* tags:
* - destinationCalendars
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No destinationCalendars were found
*/
async function allDestinationCalendars(
_: NextApiRequest,
res: NextApiResponse<DestinationCalendarsResponse>
) {
const destinationCalendars = await prisma.destinationCalendar.findMany();
const data = destinationCalendars.map((destinationCalendar) =>
schemaDestinationCalendarPublic.parse(destinationCalendar)
);
export default async function destinationCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.destinationCalendar.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No DestinationCalendars were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allDestinationCalendars);
+44 -20
View File
@@ -1,31 +1,55 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { DestinationCalendarResponse } from "@lib/types";
import {
schemaDestinationCalendar,
schemaDestinationCalendarBodyParams,
schemaDestinationCalendarPublic,
withValidDestinationCalendar,
} from "@lib/validations/destination-calendar";
type ResponseData = {
data?: DestinationCalendar;
message?: string;
error?: string;
};
/**
* @swagger
* /api/destination-calendars/new:
* post:
* summary: Creates a new destinationCalendar
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/DestinationCalendar'
* tags:
* - destinationCalendars
* responses:
* 201:
* description: OK, destinationCalendar created
* model: DestinationCalendar
* 400:
* description: Bad request. DestinationCalendar body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createDestinationCalendar(
req: NextApiRequest,
res: NextApiResponse<DestinationCalendarResponse>
) {
const safe = schemaDestinationCalendarBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createDestinationCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaDestinationCalendar.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.destinationCalendar
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) =>
res.status(400).json({ message: "Could not create destinationCalendar type", error: error })
);
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const destinationCalendar = await prisma.destinationCalendar.create({ data: safe.data });
const data = schemaDestinationCalendarPublic.parse(destinationCalendar);
if (data) res.status(201).json({ data, message: "DestinationCalendar created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new destinationCalendar",
error,
});
}
export default withValidDestinationCalendar(createDestinationCalendar);
export default withMiddleware("HTTP_POST")(withValidDestinationCalendar(createDestinationCalendar));
+4 -2
View File
@@ -1,12 +1,14 @@
import pjson from "@/package.json";
import { withSwagger } from "next-swagger-doc";
const swaggerHandler = withSwagger({
definition: {
openapi: "3.0.0",
info: {
title: "Cal.com Public API",
version: "1.0.0",
title: `${pjson.name}: ${pjson.description}`,
version: pjson.version,
},
tags: ["users", "teams"],
},
apiFolder: "pages/api",
});
@@ -2,28 +2,50 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/event-type-custom-inputs/{id}/delete:
* delete:
* summary: Remove an existing eventTypeCustomInput
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the eventTypeCustomInput to delete
* tags:
* - eventTypeCustomInputs
* responses:
* 201:
* description: OK, eventTypeCustomInput removed successfuly
* model: EventTypeCustomInput
* 400:
* description: Bad request. EventTypeCustomInput id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const eventTypeCustomInput = await prisma.eventTypeCustomInput.delete({ where: { id: safe.data.id } });
// We only remove the eventTypeCustomInput type from the database if there's an existing resource.
if (eventTypeCustomInput)
res.status(200).json({ message: `eventTypeCustomInput with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.eventTypeCustomInput.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.eventTypeCustomInput.delete({ where: { id: safe.data.id } });
if (data)
res.status(200).json({ message: `EventTypeCustomInput with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `EventTypeCustomInput with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteEventTypeCustomInput);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteEventTypeCustomInput));
+50 -26
View File
@@ -1,41 +1,65 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeCustomInputResponse } from "@lib/types";
import {
schemaEventTypeCustomInput,
schemaEventTypeCustomInputBodyParams,
schemaEventTypeCustomInputPublic,
withValidEventTypeCustomInput,
} from "@lib/validations/eventTypeCustomInput";
} from "@lib/validations/event-type-custom-input";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventTypeCustomInput;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/event-type-custom-inputs/{id}/edit:
* patch:
* summary: Edit an existing eventTypeCustomInput
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the eventTypeCustomInput to edit
* tags:
* - eventTypeCustomInputs
* responses:
* 201:
* description: OK, eventTypeCustomInput edited successfuly
* model: EventTypeCustomInput
* 400:
* description: Bad request. EventTypeCustomInput body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editEventTypeCustomInput(
req: NextApiRequest,
res: NextApiResponse<EventTypeCustomInputResponse>
) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaEventTypeCustomInputBodyParams.safeParse(req.body);
export async function editEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaEventTypeCustomInput.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const eventTypeCustomInput = await prisma.eventTypeCustomInput.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.eventTypeCustomInput.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating eventTypeCustomInputs" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidEventTypeCustomInput(editEventTypeCustomInput));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidEventTypeCustomInput(editEventTypeCustomInput))
);
@@ -1,29 +1,54 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeCustomInputResponse } from "@lib/types";
import { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-custom-input";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventTypeCustomInput;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/event-type-custom-inputs/{id}:
* get:
* summary: Get a eventTypeCustomInput by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the eventTypeCustomInput to get
* tags:
* - eventTypeCustomInputs
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: EventTypeCustomInput was not found
*/
export async function eventTypeCustomInputById(
req: NextApiRequest,
res: NextApiResponse<EventTypeCustomInputResponse>
) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function eventTypeCustomInput(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } });
const eventTypeCustomInput = await prisma.eventTypeCustomInput.findUnique({ where: { id: safe.data.id } });
const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (eventTypeCustomInput) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "EventTypeCustomInput was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(eventTypeCustomInput);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(eventTypeCustomInputById));
+35 -8
View File
@@ -1,15 +1,42 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
type ResponseData = {
data?: EventTypeCustomInput[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { EventTypeCustomInputsResponse } from "@lib/types";
import { schemaEventTypeCustomInputPublic } from "@lib/validations/event-type-custom-input";
/**
* @swagger
* /api/event-type-custom-inputs:
* get:
* summary: Get all eventTypeCustomInputs
* tags:
* - eventTypeCustomInputs
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No eventTypeCustomInputs were found
*/
async function allEventTypeCustomInputs(
_: NextApiRequest,
res: NextApiResponse<EventTypeCustomInputsResponse>
) {
const eventTypeCustomInputs = await prisma.eventTypeCustomInput.findMany();
const data = eventTypeCustomInputs.map((eventTypeCustomInput) =>
schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput)
);
export default async function eventTypeCustomInput(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.eventTypeCustomInput.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No EventTypeCustomInputs were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allEventTypeCustomInputs);
+45 -21
View File
@@ -1,31 +1,55 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { EventTypeCustomInputResponse } from "@lib/types";
import {
schemaEventTypeCustomInput,
schemaEventTypeCustomInputBodyParams,
schemaEventTypeCustomInputPublic,
withValidEventTypeCustomInput,
} from "@lib/validations/eventTypeCustomInput";
} from "@lib/validations/event-type-custom-input";
type ResponseData = {
data?: EventTypeCustomInput;
message?: string;
error?: string;
};
/**
* @swagger
* /api/event-type-custom-inputs/new:
* post:
* summary: Creates a new eventTypeCustomInput
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/EventTypeCustomInput'
* tags:
* - eventTypeCustomInputs
* responses:
* 201:
* description: OK, eventTypeCustomInput created
* model: EventTypeCustomInput
* 400:
* description: Bad request. EventTypeCustomInput body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createEventTypeCustomInput(
req: NextApiRequest,
res: NextApiResponse<EventTypeCustomInputResponse>
) {
const safe = schemaEventTypeCustomInputBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createEventTypeCustomInput(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaEventTypeCustomInput.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.eventTypeCustomInput
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) =>
res.status(400).json({ message: "Could not create eventTypeCustomInput type", error: error })
);
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const eventTypeCustomInput = await prisma.eventTypeCustomInput.create({ data: safe.data });
const data = schemaEventTypeCustomInputPublic.parse(eventTypeCustomInput);
if (data) res.status(201).json({ data, message: "EventTypeCustomInput created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new eventTypeCustomInput",
error,
});
}
export default withValidEventTypeCustomInput(createEventTypeCustomInput);
export default withMiddleware("HTTP_POST")(withValidEventTypeCustomInput(createEventTypeCustomInput));
+11 -2
View File
@@ -11,9 +11,18 @@ import {
/**
* @swagger
* /api/eventTypes/:id/delete:
* /api/eventTypes/{id}/delete:
* delete:
* description: Remove an existing eventType
* summary: Remove an existing eventType
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the eventType to delete
* tags:
* - eventTypes
* responses:
* 201:
* description: OK, eventType removed successfuly
+11 -2
View File
@@ -16,9 +16,18 @@ import {
/**
* @swagger
* /api/eventTypes/:id/edit:
* /api/eventTypes/{id}/edit:
* patch:
* description: Edits an existing eventType
* summary: Edits an existing eventType
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the eventType to delete
* tags:
* - eventTypes
* responses:
* 201:
* description: OK, eventType edited successfuly
+9 -2
View File
@@ -12,9 +12,16 @@ import {
/**
* @swagger
* /api/eventTypes/:id:
* /api/eventTypes/{id}:
* get:
* description: find eventType by ID
* summary: find eventType by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the eventType to get
* responses:
* 200:
* description: OK
+1 -1
View File
@@ -10,7 +10,7 @@ import { schemaEventTypePublic } from "@lib/validations/eventType";
* @swagger
* /api/eventTypes:
* get:
* description: Returns all eventTypes
* summary: Returns all eventTypes
* responses:
* 200:
* description: OK
+1 -1
View File
@@ -14,7 +14,7 @@ import {
* @swagger
* /api/eventTypes/new:
* post:
* description: Creates a new eventType
* summary: Creates a new eventType
* responses:
* 201:
* description: OK, eventType created
+46 -21
View File
@@ -2,28 +2,53 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/memberships/{userId}_{teamId}/delete:
* delete:
* summary: Remove an existing membership
* parameters:
* - in: path
* - name: userId
* schema:
* type: integer
* required: true
* description: Numeric ID of the user to get the membership of
* * - name: teamId
* schema:
* type: integer
* required: true
* description: Numeric ID of the team to get the membership of
* tags:
* - memberships
* responses:
* 201:
* description: OK, membership removed successfuly
* model: Membership
* 400:
* description: Bad request. Membership id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteMembership(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdAsString.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
const [userId, teamId] = safe.data.id.split("_");
const data = await prisma.membership.delete({
where: { userId_teamId: { userId: parseInt(userId), teamId: parseInt(teamId) } },
});
export async function deleteMembership(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const membership = await prisma.membership.delete({ where: { id: safe.data.id } });
// We only remove the membership type from the database if there's an existing resource.
if (membership)
res.status(200).json({ message: `membership with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.membership.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
if (data) res.status(200).json({ message: `Membership with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `Membership with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteMembership);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdString(deleteMembership));
+41 -28
View File
@@ -1,38 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import { schemaMembership, withValidMembership } from "@lib/validations/membership";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { MembershipResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
schemaMembershipBodyParams,
schemaMembershipPublic,
withValidMembership,
} from "@lib/validations/membership";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
data?: Membership;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/memberships/{id}/edit:
* patch:
* summary: Edits an existing membership
* tags:
* - memberships
* responses:
* 201:
* description: OK, membership edited successfuly
* model: Membership
* 400:
* description: Bad request. Membership body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editMembership(req: NextApiRequest, res: NextApiResponse<MembershipResponse>) {
const safeQuery = await schemaQueryIdAsString.safeParse(req.query);
const safeBody = await schemaMembershipBodyParams.safeParse(req.body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const [userId, teamId] = safeQuery.data.id.split("_");
export async function editMembership(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaMembership.safeParse(body);
const membership = await prisma.membership.update({
where: { userId_teamId: { userId: parseInt(userId), teamId: parseInt(teamId) } },
data: safeBody.data,
});
const data = schemaMembershipPublic.parse(membership);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.membership.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating memberships" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidMembership(editMembership));
export default withMiddleware("HTTP_PATCH")(withValidQueryIdString(withValidMembership(editMembership)));
+48 -20
View File
@@ -1,29 +1,57 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { MembershipResponse } from "@lib/types";
import { schemaMembershipPublic } from "@lib/validations/membership";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
type ResponseData = {
data?: Membership;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/memberships/{userId}_{teamId}:
* get:
* summary: find membership by userID and teamID
* parameters:
* - in: path
* name: userId
* schema:
* type: integer
* required: true
* description: Numeric userId of the membership to get
* - in: path
* name: teamId
* schema:
* type: integer
* required: true
* description: Numeric teamId of the membership to get
* tags:
* - memberships
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Membership was not found
*/
export async function membershipById(req: NextApiRequest, res: NextApiResponse<MembershipResponse>) {
const safe = await schemaQueryIdAsString.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
const [userId, teamId] = safe.data.id.split("_");
export async function membership(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.membership.findUnique({ where: { id: safe.data.id } });
const membership = await prisma.membership.findUnique({
where: { userId_teamId: { userId: parseInt(userId), teamId: parseInt(teamId) } },
});
const data = schemaMembershipPublic.parse(membership);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (membership) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Membership was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(membership);
export default withMiddleware("HTTP_GET")(withValidQueryIdString(membershipById));
+30 -8
View File
@@ -1,15 +1,37 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
type ResponseData = {
data?: Membership[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { MembershipsResponse } from "@lib/types";
import { schemaMembershipPublic } from "@lib/validations/membership";
/**
* @swagger
* /api/memberships:
* get:
* summary: Returns all memberships
* tags:
* - memberships
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No memberships were found
*/
async function allMemberships(_: NextApiRequest, res: NextApiResponse<MembershipsResponse>) {
const memberships = await prisma.membership.findMany();
const data = memberships.map((membership) => schemaMembershipPublic.parse(membership));
export default async function membership(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.membership.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No Memberships were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allMemberships);
+37 -18
View File
@@ -1,26 +1,45 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import { schemaMembership, withValidMembership } from "@lib/validations/membership";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { MembershipResponse } from "@lib/types";
import {
schemaMembershipBodyParams,
schemaMembershipPublic,
withValidMembership,
} from "@lib/validations/membership";
type ResponseData = {
data?: Membership;
message?: string;
error?: string;
};
/**
* @swagger
* /api/memberships/new:
* post:
* summary: Creates a new membership
* tags:
* - memberships
* responses:
* 201:
* description: OK, membership created
* model: Membership
* 400:
* description: Bad request. Membership body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createMembership(req: NextApiRequest, res: NextApiResponse<MembershipResponse>) {
const safe = schemaMembershipBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createMembership(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaMembership.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.membership
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) => res.status(400).json({ message: "Could not create membership type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const membership = await prisma.membership.create({ data: safe.data });
const data = schemaMembershipPublic.parse(membership);
if (data) res.status(201).json({ data, message: "Membership created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new membership",
error,
});
}
export default withValidMembership(createMembership);
export default withMiddleware("HTTP_POST")(withValidMembership(createMembership));
+31 -16
View File
@@ -2,27 +2,42 @@ import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/schedules/:id/delete:
* delete:
* summary: Remove an existing schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule removed successfuly
* model: Schedule
* 400:
* description: Bad request. Schedule id is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function deleteSchedule(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
export async function deleteSchedule(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const schedule = await prisma.schedule.delete({ where: { id: safe.data.id } });
// We only remove the schedule type from the database if there's an existing resource.
if (schedule) res.status(200).json({ message: `schedule with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.schedule.delete() if the resource is not found.
else res.status(400).json({ message: `Resource with id:${safe.data.id} was not found` });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only DELETE Method allowed" });
const data = await prisma.schedule.delete({ where: { id: safe.data.id } });
if (data) res.status(200).json({ message: `Schedule with id: ${safe.data.id} deleted successfully` });
else
(error: Error) =>
res.status(400).json({
message: `Schedule with id: ${safe.data.id} was not able to be processed`,
error,
});
}
export default withValidQueryIdTransformParseInt(deleteSchedule);
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteSchedule));
+38 -25
View File
@@ -1,38 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import { schemaSchedule, withValidSchedule } from "@lib/validations/schedule";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { ScheduleResponse } from "@lib/types";
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Schedule;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/schedules/:id/edit:
* patch:
* summary: Edits an existing schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule edited successfuly
* model: Schedule
* 400:
* description: Bad request. Schedule body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
export async function editSchedule(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeBody = await schemaScheduleBodyParams.safeParse(req.body);
export async function editSchedule(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaSchedule.safeParse(body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const schedule = await prisma.schedule.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
const data = schemaSchedulePublic.parse(schedule);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.schedule.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
});
if (data) res.status(200).json({ data });
else
res
.status(404)
.json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only PATCH Method allowed for updating schedules" });
if (data) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`,
error,
});
}
export default withValidQueryIdTransformParseInt(withValidSchedule(editSchedule));
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidSchedule(editSchedule))
);
+38 -16
View File
@@ -1,29 +1,51 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { ScheduleResponse } from "@lib/types";
import { schemaSchedulePublic } from "@lib/validations/schedule";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Schedule;
message?: string;
error?: unknown;
};
/**
* @swagger
* /api/schedules/{id}:
* get:
* summary: Get schedule by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the schedule to delete
* tags:
* - schedules
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: Schedule was not found
*/
export async function scheduleById(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
export async function schedule(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.schedule.findUnique({ where: { id: safe.data.id } });
const schedule = await prisma.schedule.findUnique({ where: { id: safe.data.id } });
const data = schemaSchedulePublic.parse(schedule);
if (data) res.status(200).json({ data });
else res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
} else res.status(405).json({ message: "Only GET Method allowed" });
if (schedule) res.status(200).json({ data });
else
(error: Error) =>
res.status(404).json({
message: "Schedule was not found",
error,
});
}
export default withValidQueryIdTransformParseInt(schedule);
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(scheduleById));
+30 -8
View File
@@ -1,15 +1,37 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
type ResponseData = {
data?: Schedule[];
error?: unknown;
};
import { withMiddleware } from "@lib/helpers/withMiddleware";
import { SchedulesResponse } from "@lib/types";
import { schemaSchedulePublic } from "@lib/validations/schedule";
/**
* @swagger
* /api/schedules:
* get:
* summary: Returns all schedules
* tags:
* - schedules
* responses:
* 200:
* description: OK
* 401:
* description: Authorization information is missing or invalid.
* 404:
* description: No schedules were found
*/
async function allSchedules(_: NextApiRequest, res: NextApiResponse<SchedulesResponse>) {
const schedules = await prisma.schedule.findMany();
const data = schedules.map((schedule) => schemaSchedulePublic.parse(schedule));
export default async function schedule(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.schedule.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
else
(error: Error) =>
res.status(404).json({
message: "No Schedules were found",
error,
});
}
export default withMiddleware("HTTP_GET")(allSchedules);
+33 -18
View File
@@ -1,26 +1,41 @@
import type { NextApiRequest, NextApiResponse } from "next";
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import { schemaSchedule, withValidSchedule } from "@lib/validations/schedule";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { ScheduleResponse } from "@lib/types";
import { schemaScheduleBodyParams, schemaSchedulePublic, withValidSchedule } from "@lib/validations/schedule";
type ResponseData = {
data?: Schedule;
message?: string;
error?: string;
};
/**
* @swagger
* /api/schedules/new:
* post:
* summary: Creates a new schedule
* tags:
* - schedules
* responses:
* 201:
* description: OK, schedule created
* model: Schedule
* 400:
* description: Bad request. Schedule body is invalid.
* 401:
* description: Authorization information is missing or invalid.
*/
async function createSchedule(req: NextApiRequest, res: NextApiResponse<ScheduleResponse>) {
const safe = schemaScheduleBodyParams.safeParse(req.body);
if (!safe.success) throw new Error("Invalid request body", safe.error);
async function createSchedule(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaSchedule.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.schedule
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) => res.status(400).json({ message: "Could not create schedule type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
const schedule = await prisma.schedule.create({ data: safe.data });
const data = schemaSchedulePublic.parse(schedule);
if (data) res.status(201).json({ data, message: "Schedule created successfully" });
else
(error: Error) =>
res.status(400).json({
message: "Could not create new schedule",
error,
});
}
export default withValidSchedule(createSchedule);
export default withMiddleware("HTTP_POST")(withValidSchedule(createSchedule));
+29 -10
View File
@@ -4,16 +4,27 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { BaseResponse } from "@lib/types";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
/**
* @swagger
* /api/selectedCalendars/:id/delete:
* /api/selected-calendars/{userId}_{teamId}/delete:
* delete:
* description: Remove an existing selectedCalendar
* summary: Remove an existing selectedCalendar
* parameters:
* - in: path
* - name: userId
* schema:
* type: integer
* required: true
* description: Numeric ID of the user to get the selectedCalendar of
* * - name: teamId
* schema:
* type: integer
* required: true
* description: Numeric ID of the team to get the selectedCalendar of
* tags:
* - selectedCalendars
* responses:
* 201:
* description: OK, selectedCalendar removed successfuly
@@ -24,10 +35,18 @@ import {
* description: Authorization information is missing or invalid.
*/
export async function deleteSelectedCalendar(req: NextApiRequest, res: NextApiResponse<BaseResponse>) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdAsString.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query", safe.error);
const data = await prisma.selectedCalendar.delete({ where: { id: safe.data.id } });
const [userId, integration, externalId] = safe.data.id.split("_");
const data = await prisma.selectedCalendar.delete({
where: {
userId_integration_externalId: {
userId: parseInt(userId),
integration: integration,
externalId: externalId,
},
},
});
if (data)
res.status(200).json({ message: `SelectedCalendar with id: ${safe.data.id} deleted successfully` });
@@ -39,4 +58,4 @@ export async function deleteSelectedCalendar(req: NextApiRequest, res: NextApiRe
});
}
export default withMiddleware("HTTP_DELETE")(withValidQueryIdTransformParseInt(deleteSelectedCalendar));
export default withMiddleware("HTTP_DELETE")(withValidQueryIdString(deleteSelectedCalendar));
+16 -10
View File
@@ -9,16 +9,15 @@ import {
schemaSelectedCalendarPublic,
withValidSelectedCalendar,
} from "@lib/validations/selected-calendar";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
/**
* @swagger
* /api/selectedCalendars/:id/edit:
* /api/selected-calendars/{id}/edit:
* patch:
* description: Edits an existing selectedCalendar
* summary: Edits an existing selectedCalendar
* tags:
* - selectedCalendars
* responses:
* 201:
* description: OK, selectedCalendar edited successfuly
@@ -32,12 +31,19 @@ export async function editSelectedCalendar(
req: NextApiRequest,
res: NextApiResponse<SelectedCalendarResponse>
) {
const safeQuery = await schemaQueryIdParseInt.safeParse(req.query);
const safeQuery = await schemaQueryIdAsString.safeParse(req.query);
const safeBody = await schemaSelectedCalendarBodyParams.safeParse(req.body);
if (!safeQuery.success || !safeBody.success) throw new Error("Invalid request");
const [userId, integration, externalId] = safeQuery.data.id.split("_");
const selectedCalendar = await prisma.selectedCalendar.update({
where: { id: safeQuery.data.id },
where: {
userId_integration_externalId: {
userId: parseInt(userId),
integration: integration,
externalId: externalId,
},
},
data: safeBody.data,
});
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
@@ -52,5 +58,5 @@ export async function editSelectedCalendar(
}
export default withMiddleware("HTTP_PATCH")(
withValidQueryIdTransformParseInt(withValidSelectedCalendar(editSelectedCalendar))
withValidQueryIdString(withValidSelectedCalendar(editSelectedCalendar))
);
+30 -9
View File
@@ -5,16 +5,28 @@ import prisma from "@calcom/prisma";
import { withMiddleware } from "@lib/helpers/withMiddleware";
import type { SelectedCalendarResponse } from "@lib/types";
import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar";
import {
schemaQueryIdParseInt,
withValidQueryIdTransformParseInt,
} from "@lib/validations/shared/queryIdTransformParseInt";
import { schemaQueryIdAsString, withValidQueryIdString } from "@lib/validations/shared/queryIdString";
/**
* @swagger
* /api/selectedCalendars/:id:
* /api/selected-calendars/{userId}_{teamId}:
* get:
* description: find selectedCalendar by ID
* summary: find selectedCalendar by userID and teamID
* parameters:
* - in: path
* name: userId
* schema:
* type: integer
* required: true
* description: Numeric userId of the selectedCalendar to get
* - in: path
* name: teamId
* schema:
* type: integer
* required: true
* description: Numeric teamId of the selectedCalendar to get
* tags:
* - selectedCalendars
* responses:
* 200:
* description: OK
@@ -27,10 +39,19 @@ export async function selectedCalendarById(
req: NextApiRequest,
res: NextApiResponse<SelectedCalendarResponse>
) {
const safe = await schemaQueryIdParseInt.safeParse(req.query);
const safe = await schemaQueryIdAsString.safeParse(req.query);
if (!safe.success) throw new Error("Invalid request query");
const [userId, integration, externalId] = safe.data.id.split("_");
const selectedCalendar = await prisma.selectedCalendar.findUnique({ where: { id: safe.data.id } });
const selectedCalendar = await prisma.selectedCalendar.findUnique({
where: {
userId_integration_externalId: {
userId: parseInt(userId),
integration: integration,
externalId: externalId,
},
},
});
const data = schemaSelectedCalendarPublic.parse(selectedCalendar);
if (selectedCalendar) res.status(200).json({ data });
@@ -42,4 +63,4 @@ export async function selectedCalendarById(
});
}
export default withMiddleware("HTTP_GET")(withValidQueryIdTransformParseInt(selectedCalendarById));
export default withMiddleware("HTTP_GET")(withValidQueryIdString(selectedCalendarById));
+1 -1
View File
@@ -10,7 +10,7 @@ import { schemaSelectedCalendarPublic } from "@lib/validations/selected-calendar
* @swagger
* /api/selectedCalendars:
* get:
* description: Returns all selected calendars
* summary: Returns all selected calendars
* responses:
* 200:
* description: OK
+1 -1
View File
@@ -14,7 +14,7 @@ import {
* @swagger
* /api/selectedCalendars/new:
* post:
* description: Creates a new selected calendar
* summary: Creates a new selected calendar
* responses:
* 201:
* description: OK, selected calendar created
+3 -1
View File
@@ -13,7 +13,9 @@ import {
* @swagger
* /api/teams/:id/delete:
* delete:
* description: Remove an existing team
* summary: Remove an existing team
* tags:
* - teams
* responses:
* 201:
* description: OK, team removed successfuly
+3 -1
View File
@@ -14,7 +14,9 @@ import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/vali
* @swagger
* /api/teams/:id/edit:
* patch:
* description: Edits an existing team
* summary: Edits an existing team
* tags:
* - teams
* responses:
* 201:
* description: OK, team edited successfuly
+11 -2
View File
@@ -12,9 +12,18 @@ import { schemaTeamPublic } from "@lib/validations/team";
/**
* @swagger
* /api/teams/:id:
* /api/teams/{id}:
* get:
* description: find team by ID
* summary: find team by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the team to get
* tags:
* - teams
* responses:
* 200:
* description: OK
+3 -1
View File
@@ -10,7 +10,9 @@ import { schemaTeamPublic } from "@lib/validations/team";
* @swagger
* /api/teams:
* get:
* description: Returns all teams
* summary: Returns all teams
* tags:
* - teams
* responses:
* 200:
* description: OK
+3 -1
View File
@@ -10,7 +10,9 @@ import { schemaTeamBodyParams, schemaTeamPublic, withValidTeam } from "@lib/vali
* @swagger
* /api/teams/new:
* post:
* description: Creates a new team
* summary: Creates a new team
* tags:
* - teams
* responses:
* 201:
* description: OK, team created
+11 -2
View File
@@ -11,9 +11,18 @@ import {
/**
* @swagger
* /api/users/:id/delete:
* /api/users/{id}/delete:
* delete:
* description: Remove an existing user
* summary: Remove an existing user
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the user to delete
* tags:
* - users
* responses:
* 201:
* description: OK, user removed successfuly
+11 -2
View File
@@ -12,9 +12,18 @@ import { schemaUserBodyParams, schemaUserPublic, withValidUser } from "@lib/vali
/**
* @swagger
* /api/users/:id/edit:
* /api/users/{id}/edit:
* patch:
* description: Edits an existing user
* summary: Edit an existing user
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the user to edit
* tags:
* - users
* responses:
* 201:
* description: OK, user edited successfuly
+11 -2
View File
@@ -12,9 +12,18 @@ import { schemaUserPublic } from "@lib/validations/user";
/**
* @swagger
* /api/users/:id:
* /api/users/{id}:
* get:
* description: find user by ID
* summary: Get a user by ID
* parameters:
* - in: path
* name: id
* schema:
* type: integer
* required: true
* description: Numeric ID of the user to get
* tags:
* - users
* responses:
* 200:
* description: OK
+3 -1
View File
@@ -10,7 +10,9 @@ import { schemaUserPublic } from "@lib/validations/user";
* @swagger
* /api/users:
* get:
* description: Returns all users
* summary: Get all users
* tags:
* - users
* responses:
* 200:
* description: OK
+10 -1
View File
@@ -10,7 +10,16 @@ import { schemaUserBodyParams, schemaUserPublic, withValidUser } from "@lib/vali
* @swagger
* /api/users/new:
* post:
* description: Creates a new user
* summary: Creates a new user
* requestBody:
* description: Optional description in *Markdown*
* required: true
* content:
* application/json:
* schema:
* $ref: '#/components/schemas/User'
* tags:
* - users
* responses:
* 201:
* description: OK, user created