Merge pull request #9 from calcom/add-more-endpoints

Add more endpoints
pull/9078/head
Agusti Fernandez 2022-03-29 01:01:03 +02:00 committed by GitHub
commit 752a61c756
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
47 changed files with 1115 additions and 42 deletions

View File

@ -0,0 +1,47 @@
import { label, NextMiddleware } from "next-api-middleware";
import * as Sentry from "@sentry/nextjs";
import nanoid from "nanoid";
// 1 Create middleware functions
const captureErrors: NextMiddleware = async (req, res, next) => {
try {
// Catch any errors that are thrown in remaining
// middleware and the API route handler
await next();
} catch (err) {
const eventId = Sentry.captureException(err);
res.status(500);
res.json({ error: err });
}
};
const addRequestId: NextMiddleware = async (req, res, next) => {
// Let remaining middleware and API route execute
await next();
// Apply header
res.setHeader("X-Response-ID", nanoid());
};
// 2 Use `label` to assemble all middleware
const withMiddleware = label(
{
addRequestId,
sentry: captureErrors, // <-- Optionally alias middleware
},
["sentry"] // <-- Provide a list of middleware to call automatically
);
// 3 Define your API route handler
const apiRouteHandler = async (req, res) => {
res.status(200);
res.send("Hello world!");
};
// 4 Choose middleware to invoke for this API route
export default withMiddleware("addRequestId")(apiRouteHandler);

View File

@ -0,0 +1,36 @@
// https://github.com/htunnicliff/next-api-middleware
// import prisma from "@calcom/prisma";
// import { User } from "@calcom/prisma/client";
// import type { NextApiRequest, NextApiResponse } from "next";
// type ResponseData = {
// data?: User[];
// error?: unknown;
// };
// const dateInPast = function (firstDate: Date, secondDate: Date) {
// if (firstDate.setHours(0, 0, 0, 0) <= secondDate.setHours(0, 0, 0, 0)) {
// return true;
// }
// return false;
// };
// const today = new Date();
// export default async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
// const apiKey = req.query.apiKey as string;
// const apiInDb = await prisma.apiKey.findUnique({ where: { id: apiKey } });
// if (!apiInDb) throw new Error('API key not found');
// const { expiresAt } = apiInDb;
// // if (!apiInDb) res.status(400).json({ error: 'Your api key is not valid' });
// if (expiresAt && dateInPast(expiresAt, today)) {
// try {
// const data = await prisma.user.findMany();
// res.status(200).json({ data });
// } catch (error) {
// // FIXME: Add zod for validation/error handling
// res.status(400).json({ error: error });
// }
// } else res.status(400).json({ error: 'Your api key is not valid' });
// }

View File

@ -32,6 +32,7 @@
},
"dependencies": {
"next": "^12.1.0",
"next-api-middleware": "^1.0.1",
"next-transpile-modules": "^9.0.0",
"next-validations": "^0.1.11",
"typescript": "^4.6.3",

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
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" });
}
export default withValidQueryIdTransformParseInt(deleteBookingReference);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: BookingReference;
message?: string;
error?: unknown;
};
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 (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" });
}
export default withValidQueryIdTransformParseInt(withValidBookingReference(editBookingReference));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: BookingReference;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(bookingReference);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: BookingReference[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { BookingReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaBookingReference, withValidBookingReference } from "@lib/validations/bookingReference";
type ResponseData = {
data?: BookingReference;
message?: string;
error?: string;
};
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" });
}
export default withValidBookingReference(createBookingReference);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
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" });
}
export default withValidQueryIdTransformParseInt(deleteCredential);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaCredential, withValidCredential } from "@lib/validations/credential";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Credential;
message?: string;
error?: unknown;
};
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 (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" });
}
export default withValidQueryIdTransformParseInt(withValidCredential(editCredential));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Credential;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(credential);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: Credential[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { Credential } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaCredential, withValidCredential } from "@lib/validations/credential";
type ResponseData = {
data?: Credential;
message?: string;
error?: string;
};
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" });
}
export default withValidCredential(createCredential);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
export async function deleteDailyEventReference(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const dailyEventReference = 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 (dailyEventReference) 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" });
}
export default withValidQueryIdTransformParseInt(deleteDailyEventReference);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/dailyEventReference";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DailyEventReference;
message?: string;
error?: unknown;
};
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 (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" });
}
export default withValidQueryIdTransformParseInt(withValidDailyEventReference(editDailyEventReference));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DailyEventReference;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(dailyEventReference);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: DailyEventReference[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { DailyEventReference } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDailyEventReference, withValidDailyEventReference } from "@lib/validations/dailyEventReference";
type ResponseData = {
data?: DailyEventReference;
message?: string;
error?: string;
};
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" });
}
export default withValidDailyEventReference(createDailyEventReference);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
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" });
}
export default withValidQueryIdTransformParseInt(deleteDestinationCalendar);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destinationCalendar";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DestinationCalendar;
message?: string;
error?: unknown;
};
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 (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" });
}
export default withValidQueryIdTransformParseInt(withValidDestinationCalendar(editDestinationCalendar));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: DestinationCalendar;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(destinationCalendar);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: DestinationCalendar[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { DestinationCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaDestinationCalendar, withValidDestinationCalendar } from "@lib/validations/destinationCalendar";
type ResponseData = {
data?: DestinationCalendar;
message?: string;
error?: string;
};
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" });
}
export default withValidDestinationCalendar(createDestinationCalendar);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
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" });
}
export default withValidQueryIdTransformParseInt(deleteEventTypeCustomInput);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaEventTypeCustomInput, withValidEventTypeCustomInput } from "@lib/validations/eventTypeCustomInput";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventTypeCustomInput;
message?: string;
error?: unknown;
};
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 (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" });
}
export default withValidQueryIdTransformParseInt(withValidEventTypeCustomInput(editEventTypeCustomInput));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: EventTypeCustomInput;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(eventTypeCustomInput);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: EventTypeCustomInput[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { EventTypeCustomInput } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaEventTypeCustomInput, withValidEventTypeCustomInput } from "@lib/validations/eventTypeCustomInput";
type ResponseData = {
data?: EventTypeCustomInput;
message?: string;
error?: string;
};
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" });
}
export default withValidEventTypeCustomInput(createEventTypeCustomInput);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
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" });
}
export default withValidQueryIdTransformParseInt(deleteMembership);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaMembership, withValidMembership } from "@lib/validations/membership";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Membership;
message?: string;
error?: unknown;
};
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);
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" });
}
export default withValidQueryIdTransformParseInt(withValidMembership(editMembership));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Membership;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(membership);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: Membership[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { Membership } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaMembership, withValidMembership } from "@lib/validations/membership";
type ResponseData = {
data?: Membership;
message?: string;
error?: string;
};
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" });
}
export default withValidMembership(createMembership);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
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" });
}
export default withValidQueryIdTransformParseInt(deleteSchedule);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSchedule, withValidSchedule } from "@lib/validations/schedule";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Schedule;
message?: string;
error?: unknown;
};
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 (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" });
}
export default withValidQueryIdTransformParseInt(withValidSchedule(editSchedule));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: Schedule;
message?: string;
error?: unknown;
};
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 } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(schedule);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: Schedule[];
error?: unknown;
};
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" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { Schedule } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSchedule, withValidSchedule } from "@lib/validations/schedule";
type ResponseData = {
data?: Schedule;
message?: string;
error?: string;
};
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" });
}
export default withValidSchedule(createSchedule);

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
message?: string;
error?: unknown;
};
export async function deleteSelectedCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "DELETE" && safe.success && safe.data) {
const selectedCalendar = await prisma.selectedCalendar
.delete({ where: { id: safe.data.id } })
// We only remove the selectedCalendar type from the database if there's an existing resource.
if (selectedCalendar) res.status(200).json({ message: `selectedCalendar with id: ${safe.data.id} deleted successfully` });
// This catches the error thrown by prisma.selectedCalendar.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" });
}
export default withValidQueryIdTransformParseInt(deleteSelectedCalendar);

View File

@ -0,0 +1,32 @@
import prisma from "@calcom/prisma";
import { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selectedCalendar";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: SelectedCalendar;
message?: string;
error?: unknown;
};
export async function editSelectedCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, body, method } = req;
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaSelectedCalendar.safeParse(body);
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.selectedCalendar.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 selectedCalendars" });
}
export default withValidQueryIdTransformParseInt(withValidSelectedCalendar(editSelectedCalendar));

View File

@ -0,0 +1,27 @@
import prisma from "@calcom/prisma";
import { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaQueryIdParseInt, withValidQueryIdTransformParseInt } from "@lib/validations/shared/queryIdTransformParseInt";
type ResponseData = {
data?: SelectedCalendar;
message?: string;
error?: unknown;
};
export async function selectedCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const data = await prisma.selectedCalendar.findUnique({ where: { id: safe.data.id } });
if (data) res.status(200).json({ data });
if (!data) 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" });
}
export default withValidQueryIdTransformParseInt(selectedCalendar);

View File

@ -0,0 +1,15 @@
import prisma from "@calcom/prisma";
import { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
type ResponseData = {
data?: SelectedCalendar[];
error?: unknown;
};
export default async function selectedCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const data = await prisma.selectedCalendar.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
}

View File

@ -0,0 +1,26 @@
import prisma from "@calcom/prisma";
import { SelectedCalendar } from "@calcom/prisma/client";
import type { NextApiRequest, NextApiResponse } from "next";
import { schemaSelectedCalendar, withValidSelectedCalendar } from "@lib/validations/selectedCalendar";
type ResponseData = {
data?: SelectedCalendar;
message?: string;
error?: string;
};
async function createSelectedCalendar(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const { body, method } = req;
const safe = schemaSelectedCalendar.safeParse(body);
if (method === "POST" && safe.success) {
await prisma.selectedCalendar
.create({ data: safe.data })
.then((data) => res.status(201).json({ data }))
.catch((error) => res.status(400).json({ message: "Could not create selectedCalendar type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });
}
export default withValidSelectedCalendar(createSelectedCalendar);

View File

@ -17,21 +17,16 @@ export async function editUser(req: NextApiRequest, res: NextApiResponse<Respons
const safeQuery = await schemaQueryIdParseInt.safeParse(query);
const safeBody = await schemaUser.safeParse(body);
if (method === "PATCH") {
if (safeQuery.success && safeBody.success) {
await prisma.user.update({
if (method === "PATCH" && safeQuery.success && safeBody.success) {
const data = await prisma.user.update({
where: { id: safeQuery.data.id },
data: safeBody.data,
}).then(user => {
res.status(200).json({ data: user });
}).catch(error => {
res.status(404).json({ message: `Event type with ID ${safeQuery.data.id} not found and wasn't updated`, error })
});
}
} else {
})
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
res.status(405).json({ message: "Only PATCH Method allowed for updating users" });
}
} else res.status(405).json({ message: "Only PATCH Method allowed for updating users" });
}
export default withValidQueryIdTransformParseInt(withValidUser(editUser));

View File

@ -15,14 +15,12 @@ export async function user(req: NextApiRequest, res: NextApiResponse<ResponseDat
const { query, method } = req;
const safe = await schemaQueryIdParseInt.safeParse(query);
if (method === "GET" && safe.success) {
const user = await prisma.user.findUnique({ where: { id: safe.data.id } });
const data = await prisma.user.findUnique({ where: { id: safe.data.id } });
if (user) res.status(200).json({ data: user });
if (!user) res.status(404).json({ message: "Event type not found" });
} else {
if (data) res.status(200).json({ data });
if (!data) res.status(404).json({ message: "Event type not found" });
// Reject any other HTTP method than POST
res.status(405).json({ message: "Only GET Method allowed" });
}
} else res.status(405).json({ message: "Only GET Method allowed" });
}

View File

@ -7,29 +7,9 @@ type ResponseData = {
data?: User[];
error?: unknown;
};
const dateInPast = function (firstDate: Date, secondDate: Date) {
if (firstDate.setHours(0, 0, 0, 0) <= secondDate.setHours(0, 0, 0, 0)) {
return true;
}
return false;
};
const today = new Date();
export default async function user(req: NextApiRequest, res: NextApiResponse<ResponseData>) {
const apiKey = req.query.apiKey as string;
const apiInDb = await prisma.apiKey.findUnique({ where: { id: apiKey } });
if (!apiInDb) throw new Error('API key not found');
const { expiresAt } = apiInDb;
// if (!apiInDb) res.status(400).json({ error: 'Your api key is not valid' });
if (expiresAt && dateInPast(expiresAt, today)) {
try {
const data = await prisma.user.findMany();
res.status(200).json({ data });
} catch (error) {
// FIXME: Add zod for validation/error handling
res.status(400).json({ error: error });
}
} else res.status(400).json({ error: 'Your api key is not valid' });
const data = await prisma.user.findMany();
if (data) res.status(200).json({ data });
else res.status(400).json({ error: "No data found" });
}

View File

@ -17,7 +17,7 @@ async function createUser(req: NextApiRequest, res: NextApiResponse<ResponseData
if (method === "POST" && safe.success) {
await prisma.user
.create({ data: safe.data })
.then((user) => res.status(201).json({ data: user }))
.then((data) => res.status(201).json({ data }))
.catch((error) => res.status(400).json({ message: "Could not create user type", error: error }));
// Reject any other HTTP method than POST
} else res.status(405).json({ error: "Only POST Method allowed" });