Skip to content

Commit

Permalink
refactor: removed isNotAnApiCall (calcom#13568)
Browse files Browse the repository at this point in the history
  • Loading branch information
zomars authored Feb 7, 2024
1 parent 8469c91 commit d2b4760
Show file tree
Hide file tree
Showing 14 changed files with 596 additions and 643 deletions.
3 changes: 2 additions & 1 deletion apps/api/pages/api/bookings/_post.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { NextApiRequest } from "next";

import getBookingDataSchemaForApi from "@calcom/features/bookings/lib/getBookingDataSchemaForApi";
import handleNewBooking from "@calcom/features/bookings/lib/handleNewBooking";
import { defaultResponder } from "@calcom/lib/server";

Expand Down Expand Up @@ -206,7 +207,7 @@ async function handler(req: NextApiRequest) {
const { userId, isAdmin } = req;
if (isAdmin) req.userId = req.body.userId || userId;

return await handleNewBooking(req);
return await handleNewBooking(req, getBookingDataSchemaForApi);
}

export default defaultResponder(handler);
4 changes: 1 addition & 3 deletions apps/web/pages/api/book/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ async function handler(req: NextApiRequest & { userId?: number }, res: NextApiRe
const session = await getServerSession({ req, res });
/* To mimic API behavior and comply with types */
req.userId = session?.user?.id || -1;
const booking = await handleNewBooking(req, {
isNotAnApiCall: true,
});
const booking = await handleNewBooking(req);
return booking;
}

Expand Down
12 changes: 3 additions & 9 deletions apps/web/pages/api/book/recurring-event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,7 @@ async function handler(req: NextApiRequest & { userId?: number }, res: NextApiRe
noEmail: false,
};

const firstBookingResult = await handleNewBooking(recurringEventReq, {
isNotAnApiCall: true,
});
const firstBookingResult = await handleNewBooking(recurringEventReq);
luckyUsers = firstBookingResult.luckyUsers?.map((user) => user.id);
}

Expand Down Expand Up @@ -92,12 +90,8 @@ async function handler(req: NextApiRequest & { userId?: number }, res: NextApiRe
luckyUsers,
};

const promiseEachRecurringBooking: ReturnType<typeof handleNewBooking> = handleNewBooking(
recurringEventReq,
{
isNotAnApiCall: true,
}
);
const promiseEachRecurringBooking: ReturnType<typeof handleNewBooking> =
handleNewBooking(recurringEventReq);

const eachRecurringBooking = await promiseEachRecurringBooking;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export const useBookingForm = ({ event }: IUseBookingForm) => {
.object({
responses: event?.data
? getBookingResponsesSchema({
eventType: event?.data,
bookingFields: event.data.bookingFields,
view: rescheduleUid ? "reschedule" : "booking",
})
: // Fallback until event is loaded.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export function useInitialFormValues({
return {};
}
const querySchema = getBookingResponsesPartialSchema({
eventType: {
bookingFields: eventType.bookingFields,
},
bookingFields: eventType.bookingFields,
view: rescheduleUid ? "reschedule" : "booking",
});

Expand Down
84 changes: 12 additions & 72 deletions packages/features/bookings/lib/getBookingDataSchema.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,20 @@
import { z } from "zod";

import {
bookingCreateSchemaLegacyPropsForApi,
bookingCreateBodySchemaForApi,
extendedBookingCreateBody,
} from "@calcom/prisma/zod-utils";
import { extendedBookingCreateBody } from "@calcom/prisma/zod-utils";

import type { getBookingFieldsWithSystemFields } from "./getBookingFields";
import getBookingResponsesSchema from "./getBookingResponsesSchema";
import type { getEventTypesFromDB } from "./handleNewBooking";

const getBookingDataSchema = (
rescheduleUid: string | undefined,
isNotAnApiCall: boolean,
eventType: Awaited<ReturnType<typeof getEventTypesFromDB>>
) => {
const responsesSchema = getBookingResponsesSchema({
eventType: {
bookingFields: eventType.bookingFields,
},
view: rescheduleUid ? "reschedule" : "booking",
});
const bookingDataSchema = isNotAnApiCall
? extendedBookingCreateBody.merge(
z.object({
responses: responsesSchema,
})
)
: bookingCreateBodySchemaForApi
.merge(
z.object({
responses: responsesSchema.optional(),
})
)
.superRefine((val, ctx) => {
if (val.responses && val.customInputs) {
ctx.addIssue({
code: "custom",
message:
"Don't use both customInputs and responses. `customInputs` is only there for legacy support.",
});
return;
}
const legacyProps = Object.keys(bookingCreateSchemaLegacyPropsForApi.shape);

if (val.responses) {
const unwantedProps: string[] = [];
legacyProps.forEach((legacyProp) => {
if (typeof val[legacyProp as keyof typeof val] !== "undefined") {
console.error(
`Deprecated: Unexpected falsy value for: ${unwantedProps.join(
","
)}. They can't be used with \`responses\`. This will become a 400 error in the future.`
);
}
if (val[legacyProp as keyof typeof val]) {
unwantedProps.push(legacyProp);
}
});
if (unwantedProps.length) {
ctx.addIssue({
code: "custom",
message: `Legacy Props: ${unwantedProps.join(",")}. They can't be used with \`responses\``,
});
return;
}
} else if (val.customInputs) {
const { success } = bookingCreateSchemaLegacyPropsForApi.safeParse(val);
if (!success) {
ctx.addIssue({
code: "custom",
message: `With \`customInputs\` you must specify legacy props ${legacyProps.join(",")}`,
});
}
}
});
return bookingDataSchema;
const getBookingDataSchema = ({
view = "booking",
bookingFields,
}: {
view: "booking" | "reschedule";
bookingFields: Awaited<ReturnType<typeof getBookingFieldsWithSystemFields>>;
}) => {
return extendedBookingCreateBody.merge(
z.object({ responses: getBookingResponsesSchema({ bookingFields, view }) })
);
};

export default getBookingDataSchema;
69 changes: 69 additions & 0 deletions packages/features/bookings/lib/getBookingDataSchemaForApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { z } from "zod";

import {
bookingCreateBodySchemaForApi,
bookingCreateSchemaLegacyPropsForApi,
} from "@calcom/prisma/zod-utils";

import type { getBookingFieldsWithSystemFields } from "./getBookingFields";
import getBookingResponsesSchema from "./getBookingResponsesSchema";

const getBookingDataSchemaForApi = ({
view = "booking",
bookingFields,
}: {
view: "booking" | "reschedule";
bookingFields: Awaited<ReturnType<typeof getBookingFieldsWithSystemFields>>;
}) => {
const responsesSchema = getBookingResponsesSchema({ bookingFields, view });
return bookingCreateBodySchemaForApi
.merge(
z.object({
responses: responsesSchema.optional(),
})
)
.superRefine((val, ctx) => {
if (val.responses && val.customInputs) {
ctx.addIssue({
code: "custom",
message:
"Don't use both customInputs and responses. `customInputs` is only there for legacy support.",
});
return;
}
const legacyProps = Object.keys(bookingCreateSchemaLegacyPropsForApi.shape);

if (val.responses) {
const unwantedProps: string[] = [];
legacyProps.forEach((legacyProp) => {
if (typeof val[legacyProp as keyof typeof val] !== "undefined") {
console.error(
`Deprecated: Unexpected falsy value for: ${unwantedProps.join(
","
)}. They can't be used with \`responses\`. This will become a 400 error in the future.`
);
}
if (val[legacyProp as keyof typeof val]) {
unwantedProps.push(legacyProp);
}
});
if (unwantedProps.length) {
ctx.addIssue({
code: "custom",
message: `Legacy Props: ${unwantedProps.join(",")}. They can't be used with \`responses\``,
});
return;
}
} else if (val.customInputs) {
const { success } = bookingCreateSchemaLegacyPropsForApi.safeParse(val);
if (!success) {
ctx.addIssue({
code: "custom",
message: `With \`customInputs\` you must specify legacy props ${legacyProps.join(",")}`,
});
}
}
});
};

export default getBookingDataSchemaForApi;
Loading

0 comments on commit d2b4760

Please sign in to comment.