Availabilty consolitadion (#3010)
This commit is contained in:
1 parent
1f1e364a30
commit
22d2bae46b
28 files changed
+553
-519
No files matched your search
@@ -1,5 +1,10 @@
|
||||
import type { EventTypeCustomInput } from "@prisma/client";
|
||||
import { PeriodType, SchedulingType, UserPlan } from "@prisma/client";
|
||||
import { PeriodType, Prisma, SchedulingType, UserPlan } from "@prisma/client";
|
||||
|
||||
import { baseUserSelect } from "@calcom/prisma/selects";
|
||||
|
||||
const userSelectData = Prisma.validator<Prisma.UserArgs>()({ select: baseUserSelect });
|
||||
type User = Prisma.UserGetPayload<typeof userSelectData>;
|
||||
|
||||
const availability = [
|
||||
{
|
||||
@@ -81,7 +86,13 @@ const commons = {
|
||||
theme: null,
|
||||
brandColor: "#292929",
|
||||
darkBrandColor: "#fafafa",
|
||||
},
|
||||
availability: [],
|
||||
selectedCalendars: [],
|
||||
startTime: 0,
|
||||
endTime: 0,
|
||||
schedules: [],
|
||||
defaultScheduleId: null,
|
||||
} as User,
|
||||
],
|
||||
};
|
||||
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
|
||||
import { userSelect } from "@calcom/prisma";
|
||||
|
||||
type User = Prisma.UserGetPayload<typeof userSelect>;
|
||||
|
||||
export function getLuckyUsers(
|
||||
users: User[],
|
||||
bookingCounts: {
|
||||
username: string | null;
|
||||
bookingCount: number;
|
||||
}[]
|
||||
) {
|
||||
if (!bookingCounts.length) users.slice(0, 1);
|
||||
|
||||
const [firstMostAvailableUser] = bookingCounts.sort((a, b) => (a.bookingCount > b.bookingCount ? 1 : -1));
|
||||
const luckyUser = users.find((user) => user.username === firstMostAvailableUser?.username);
|
||||
return luckyUser ? [luckyUser] : users;
|
||||
}
|
||||
@@ -1,2 +1,3 @@
|
||||
export { getLuckyUsers } from "./getLuckyUsers";
|
||||
export { default as isPrismaObj, isPrismaObjOrUndefined } from "./isPrismaObj";
|
||||
export * from "./isRecurringEvent";
|
||||
@@ -0,0 +1,22 @@
|
||||
import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
|
||||
|
||||
type Handlers = {
|
||||
[method in "GET" | "POST" | "PATCH" | "PUT" | "DELETE"]?: Promise<{ default: NextApiHandler }>;
|
||||
};
|
||||
|
||||
/** Allows us to split big API handlers by method and auto catch unsupported methods */
|
||||
const defaultHandler = (handlers: Handlers) => async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
const handler = (await handlers[req.method as keyof typeof handlers])?.default;
|
||||
|
||||
if (!handler) return res.status(405).json({ message: "Method not allowed" });
|
||||
|
||||
try {
|
||||
await handler(req, res);
|
||||
return;
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
return res.status(500).json({ message: "Something went wrong" });
|
||||
}
|
||||
};
|
||||
|
||||
export default defaultHandler;
|
||||
@@ -0,0 +1,29 @@
|
||||
import type { NextApiRequest, NextApiResponse } from "next";
|
||||
import { performance } from "perf_hooks";
|
||||
|
||||
import { perfObserver } from ".";
|
||||
import { getServerErrorFromUnkown } from "./getServerErrorFromUnkown";
|
||||
|
||||
type Handle<T> = (req: NextApiRequest, res: NextApiResponse) => Promise<T>;
|
||||
|
||||
perfObserver.observe({ entryTypes: ["measure"], buffered: true });
|
||||
|
||||
/** Allows us to get type inference from API handler responses */
|
||||
function defaultResponder<T>(f: Handle<T>) {
|
||||
return async (req: NextApiRequest, res: NextApiResponse) => {
|
||||
try {
|
||||
performance.mark("Start");
|
||||
const result = await f(req, res);
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
const error = getServerErrorFromUnkown(err);
|
||||
res.statusCode = error.statusCode;
|
||||
res.json({ message: error.message });
|
||||
} finally {
|
||||
performance.mark("End");
|
||||
performance.measure("Measuring endpoint: " + req.url, "Start", "End");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
export default defaultResponder;
|
||||
@@ -0,0 +1,29 @@
|
||||
import { Prisma } from "@prisma/client";
|
||||
import Stripe from "stripe";
|
||||
import { ZodError } from "zod";
|
||||
|
||||
import { HttpError } from "../http-error";
|
||||
|
||||
export function getServerErrorFromUnkown(cause: unknown): HttpError {
|
||||
if (cause instanceof Prisma.PrismaClientKnownRequestError) {
|
||||
return new HttpError({ statusCode: 400, message: cause.message, cause });
|
||||
}
|
||||
if (cause instanceof Error) {
|
||||
return new HttpError({ statusCode: 500, message: cause.message, cause });
|
||||
}
|
||||
if (cause instanceof HttpError) {
|
||||
return cause;
|
||||
}
|
||||
if (cause instanceof Stripe.errors.StripeInvalidRequestError) {
|
||||
return new HttpError({ statusCode: 400, message: cause.message, cause });
|
||||
}
|
||||
if (cause instanceof ZodError) {
|
||||
return new HttpError({ statusCode: 400, message: cause.message, cause });
|
||||
}
|
||||
if (typeof cause === "string") {
|
||||
// @ts-expect-error https://github.com/tc39/proposal-error-cause
|
||||
return new Error(cause, { cause });
|
||||
}
|
||||
|
||||
return new HttpError({ statusCode: 500, message: `Unhandled error of type '${typeof cause}'` });
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
export { default as defaultHandler } from "./defaultHandler";
|
||||
export { default as defaultResponder } from "./defaultResponder";
|
||||
export { getServerErrorFromUnkown } from "./getServerErrorFromUnkown";
|
||||
export { getTranslation } from "./i18n";
|
||||
export { default as perfObserver } from "./perfObserver";
|
||||
@@ -0,0 +1,20 @@
|
||||
import { PerformanceObserver } from "perf_hooks";
|
||||
|
||||
declare global {
|
||||
// eslint-disable-next-line no-var
|
||||
var perfObserver: PerformanceObserver | undefined;
|
||||
}
|
||||
|
||||
export const perfObserver =
|
||||
globalThis.perfObserver ||
|
||||
new PerformanceObserver((items) => {
|
||||
items.getEntries().forEach((entry) => {
|
||||
console.log(entry); // fake call to our custom logging solution
|
||||
});
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== "production") {
|
||||
globalThis.perfObserver = perfObserver;
|
||||
}
|
||||
|
||||
export default perfObserver;
|
||||
Reference in new issue
Block a user