Compare commits

...

4 Commits

Author SHA1 Message Date
Keith Williams 27028f0464 Trying to slim down app store for experiment 2023-10-30 13:10:30 -03:00
Keith Williams 4064901fcf Remove import 2023-10-30 11:42:13 -03:00
Keith Williams 8582ec8327 Edge runtime issues with getServerErrorFromUnknown 2023-10-30 10:38:09 -03:00
Keith Williams d078909bff chore: Change API slots route to edge 2023-10-30 10:12:07 -03:00
6 changed files with 128 additions and 42 deletions

View File

@ -9,6 +9,10 @@ import { getAvailableSlots } from "@calcom/trpc/server/routers/viewer/slots/util
import { TRPCError } from "@trpc/server";
import { getHTTPStatusCodeFromError } from "@trpc/server/http";
export const config = {
runtime: "edge",
};
async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const input = getScheduleSchema.parse(req.query);

View File

@ -1,42 +1,48 @@
// const appStore = {
// // example: () => import("./example"),
// alby: () => import("./alby"),
// applecalendar: () => import("./applecalendar"),
// aroundvideo: () => import("./around"),
// //caldavcalendar: () => import("./caldavcalendar"),
// closecom: () => import("./closecom"),
// dailyvideo: () => import("./dailyvideo"),
// googlecalendar: () => import("./googlecalendar"),
// googlevideo: () => import("./googlevideo"),
// hubspot: () => import("./hubspot"),
// huddle01video: () => import("./huddle01video"),
// jitsivideo: () => import("./jitsivideo"),
// larkcalendar: () => import("./larkcalendar"),
// office365calendar: () => import("./office365calendar"),
// office365video: () => import("./office365video"),
// plausible: () => import("./plausible"),
// paypal: () => import("./paypal"),
// //salesforce: () => import("./salesforce"),
// //zohocrm: () => import("./zohocrm"),
// sendgrid: () => import("./sendgrid"),
// stripepayment: () => import("./stripepayment"),
// tandemvideo: () => import("./tandemvideo"),
// //vital: () => import("./vital"),
// zoomvideo: () => import("./zoomvideo"),
// wipemycalother: () => import("./wipemycalother"),
// webexvideo: () => import("./webex"),
// giphy: () => import("./giphy"),
// zapier: () => import("./zapier"),
// make: () => import("./make"),
// //exchange2013calendar: () => import("./exchange2013calendar"),
// //exchange2016calendar: () => import("./exchange2016calendar"),
// //exchangecalendar: () => import("./exchangecalendar"),
// facetime: () => import("./facetime"),
// sylapsvideo: () => import("./sylapsvideo"),
// zohocalendar: () => import("./zohocalendar"),
// //"zoho-bigin": () => import("./zoho-bigin"),
// basecamp3: () => import("./basecamp3"),
// telegramvideo: () => import("./telegram"),
// };
const appStore = {
// example: () => import("./example"),
alby: () => import("./alby"),
applecalendar: () => import("./applecalendar"),
aroundvideo: () => import("./around"),
caldavcalendar: () => import("./caldavcalendar"),
closecom: () => import("./closecom"),
dailyvideo: () => import("./dailyvideo"),
googlecalendar: () => import("./googlecalendar"),
googlevideo: () => import("./googlevideo"),
hubspot: () => import("./hubspot"),
huddle01video: () => import("./huddle01video"),
jitsivideo: () => import("./jitsivideo"),
larkcalendar: () => import("./larkcalendar"),
office365calendar: () => import("./office365calendar"),
office365video: () => import("./office365video"),
plausible: () => import("./plausible"),
paypal: () => import("./paypal"),
salesforce: () => import("./salesforce"),
zohocrm: () => import("./zohocrm"),
sendgrid: () => import("./sendgrid"),
stripepayment: () => import("./stripepayment"),
tandemvideo: () => import("./tandemvideo"),
vital: () => import("./vital"),
zoomvideo: () => import("./zoomvideo"),
wipemycalother: () => import("./wipemycalother"),
webexvideo: () => import("./webex"),
giphy: () => import("./giphy"),
zapier: () => import("./zapier"),
make: () => import("./make"),
exchange2013calendar: () => import("./exchange2013calendar"),
exchange2016calendar: () => import("./exchange2016calendar"),
exchangecalendar: () => import("./exchangecalendar"),
facetime: () => import("./facetime"),
sylapsvideo: () => import("./sylapsvideo"),
zohocalendar: () => import("./zohocalendar"),
"zoho-bigin": () => import("./zoho-bigin"),
basecamp3: () => import("./basecamp3"),
telegramvideo: () => import("./telegram"),
};
export default appStore;

View File

@ -1,6 +1,5 @@
import type { NextApiRequest, NextApiResponse } from "next";
import { getServerErrorFromUnknown } from "./getServerErrorFromUnknown";
import { performance } from "./perfObserver";
type Handle<T> = (req: NextApiRequest, res: NextApiResponse) => Promise<T>;
@ -16,9 +15,10 @@ export function defaultResponder<T>(f: Handle<T>) {
if (result) res.json(result);
} catch (err) {
console.error(err);
const error = getServerErrorFromUnknown(err);
res.statusCode = error.statusCode;
res.json({ message: error.message });
// const error = getServerErrorFromUnknown(err);
// res.statusCode = error.statusCode;
res.statusCode = 500;
res.json({ message: "Something went wrong" });
} finally {
performance.mark("End");
performance.measure(`[${ok ? "OK" : "ERROR"}][$1] ${req.method} '${req.url}'`, "Start", "End");

View File

@ -4,6 +4,5 @@ export { checkDurationLimits, checkDurationLimit } from "./checkDurationLimits";
export { defaultHandler } from "./defaultHandler";
export { defaultResponder } from "./defaultResponder";
export { getLuckyUser } from "./getLuckyUser";
export { getServerErrorFromUnknown } from "./getServerErrorFromUnknown";
export { getTranslation } from "./i18n";
export { default as perfObserver } from "./perfObserver";

View File

@ -0,0 +1,71 @@
import type { Prisma } from "@prisma/client/edge";
import { PrismaClient as PrismaClientWithoutExtension } from "@prisma/client/edge";
import { withAccelerate } from "@prisma/extension-accelerate";
import { bookingReferenceMiddleware } from "./middleware";
const prismaOptions: Prisma.PrismaClientOptions = {};
const globalForPrisma = global as unknown as {
prismaWithoutClientExtensions: PrismaClientWithoutExtension;
prismaWithClientExtensions: PrismaClientWithExtensions;
};
if (!!process.env.NEXT_PUBLIC_DEBUG) prismaOptions.log = ["query", "error", "warn"];
// Prevents flooding with idle connections
const prismaWithoutClientExtensions =
globalForPrisma.prismaWithoutClientExtensions || new PrismaClientWithoutExtension(prismaOptions);
export const customPrisma = (options?: Prisma.PrismaClientOptions) =>
new PrismaClientWithoutExtension({ ...prismaOptions, ...options }).$extends(withAccelerate());
// If any changed on middleware server restart is required
// TODO: Migrate it to $extends
bookingReferenceMiddleware(prismaWithoutClientExtensions);
// FIXME: Due to some reason, there are types failing in certain places due to the $extends. Fix it and then enable it
// Specifically we get errors like `Type 'string | Date | null | undefined' is not assignable to type 'Exact<string | Date | null | undefined, string | Date | null | undefined>'`
const prismaWithClientExtensions = prismaWithoutClientExtensions
//
.$extends(withAccelerate());
// .$extends({
// query: {
// $allModels: {
// async $allOperations({ model, operation, args, query }) {
// const start = performance.now();
// /* your custom logic here */
// const res = await query(args);
// const end = performance.now();
// logger.debug("Query Perf: ", `${model}.${operation} took ${(end - start).toFixed(2)}ms\n`);
// return res;
// },
// },
// },
// });
// .$extends({
// name: "teamUpdateWithMetadata",
// query: {
// team: {
// async update({ model, operation, args, query }) {
// if (args.data.metadata) {
// // Prepare args.data with merged metadata
// }
// return query(args);
// },
// },
// },
// })
export const prisma = globalForPrisma.prismaWithClientExtensions || prismaWithClientExtensions;
if (process.env.NODE_ENV !== "production") {
globalForPrisma.prismaWithoutClientExtensions = prismaWithoutClientExtensions;
globalForPrisma.prismaWithClientExtensions = prisma;
}
type PrismaClientWithExtensions = typeof prismaWithClientExtensions;
export type PrismaClient = PrismaClientWithExtensions;
export default prisma;
export * from "./selects";

View File

@ -1,5 +1,4 @@
// eslint-disable-next-line no-restricted-imports
import { countBy } from "lodash";
import { v4 as uuid } from "uuid";
import { getAggregatedAvailability } from "@calcom/core/getAggregatedAvailability";
@ -14,9 +13,9 @@ import isTimeOutOfBounds from "@calcom/lib/isOutOfBounds";
import logger from "@calcom/lib/logger";
import { performance } from "@calcom/lib/server/perfObserver";
import getSlots from "@calcom/lib/slots";
import prisma, { availabilityUserSelect } from "@calcom/prisma";
import { SchedulingType } from "@calcom/prisma/enums";
import { BookingStatus } from "@calcom/prisma/enums";
import prisma, { availabilityUserSelect } from "@calcom/prisma/index-edge";
import { credentialForCalendarServiceSelect } from "@calcom/prisma/selects/credential";
import { EventTypeMetaDataSchema } from "@calcom/prisma/zod-utils";
import type { EventBusyDate } from "@calcom/types/Calendar";
@ -609,6 +608,13 @@ export async function getAvailableSlots({ input, ctx }: GetScheduleOptions) {
};
}
function countBy(array, selector) {
return array.map(selector).reduce((acc, cur) => {
acc[cur] = (acc[cur] || 0) + 1;
return acc;
}, {});
}
async function getUserIdFromUsername(
username: string,
organizationDetails: { currentOrgDomain: string | null; isValidOrgDomain: boolean }