Files
calendar/packages/features/bookings/lib/handleNewBooking/getBookingData.ts
T
417a612aa3 refactor: extract NextRequest from handle new booking (#20082)
* extract NextRequest

* update api and tests

* booking limits tests

* fix more tests to use new approach

* update more tests with new format

* extract getOrgDomainConfig to not use req

* extract req from loadNewUsers and pass in hostname/forcedSlug

* fix instant meeting types and hostname fixes

* fix handleNewBookingReq

* fix type errors in tests

* make hostName and forcedSlug optional

* fix type err

* Revert "fix type err"

This reverts commit 9d5de9019d9dafe348c97b876baaa1d0675967e5.

* wip fix e2e

* fix: add missing headers

* migrate handle recurring event and also create tests specific to fn

* platform recurringbooking

* fix type

* hard code types on request object

* bump libraries

* fixup! bump libraries

* fix: accessing host if headers not passed

* fix: v2 recurring booking

* fix: accessing host if headers not passed

* chore: bump platform libraries

* fix tests

* push

* chore: bump libraries

* push lock changes

* bump libraries

---------

Co-authored-by: amrit <iamamrit27@gmail.com>
Co-authored-by: Morgan <33722304+ThyMinimalDev@users.noreply.github.com>
Co-authored-by: Morgan Vernay <morgan@cal.com>
Co-authored-by: supalarry <laurisskraucis@gmail.com>
Co-authored-by: Lauris Skraucis <lauris.skraucis@gmail.com>
2025-04-02 17:57:58 -04:00

91 lines
3.5 KiB
TypeScript

import type { EventTypeCustomInput } from "@prisma/client";
import type z from "zod";
import dayjs from "@calcom/dayjs";
import { getCalEventResponses } from "@calcom/features/bookings/lib/getCalEventResponses";
import { ErrorCode } from "@calcom/lib/errorCodes";
import { bookingCreateSchemaLegacyPropsForApi } from "@calcom/prisma/zod/custom/booking";
import type { TgetBookingDataSchema } from "../getBookingDataSchema";
import type { getEventTypeResponse } from "./getEventTypesFromDB";
import { handleCustomInputs } from "./handleCustomInputs";
type ReqBodyWithEnd = TgetBookingDataSchema & { end: string };
export async function getBookingData<T extends z.ZodType>({
reqBody,
eventType,
schema,
}: {
reqBody: Record<string, any>;
eventType: getEventTypeResponse;
schema: T;
}) {
const parsedBody = await schema.parseAsync(reqBody);
const parsedBodyWithEnd = (body: TgetBookingDataSchema): body is ReqBodyWithEnd => {
// Use the event length to auto-set the event end time.
if (!Object.prototype.hasOwnProperty.call(body, "end")) {
body.end = dayjs.utc(body.start).add(eventType.length, "minutes").format();
}
return true;
};
if (!parsedBodyWithEnd(parsedBody)) {
throw new Error(ErrorCode.RequestBodyWithouEnd);
}
// parsedBody.end is no longer an optional property.
if (parsedBody.customInputs) {
// Check if required custom inputs exist
handleCustomInputs(eventType.customInputs as EventTypeCustomInput[], parsedBody.customInputs);
const reqBodyWithLegacyProps = bookingCreateSchemaLegacyPropsForApi.parse(parsedBody);
return {
...parsedBody,
name: reqBodyWithLegacyProps.name,
email: reqBodyWithLegacyProps.email,
guests: reqBodyWithLegacyProps.guests,
location: reqBodyWithLegacyProps.location || "",
smsReminderNumber: reqBodyWithLegacyProps.smsReminderNumber,
notes: reqBodyWithLegacyProps.notes,
rescheduleReason: reqBodyWithLegacyProps.rescheduleReason,
// So TS doesn't complain about unknown properties
calEventUserFieldsResponses: undefined,
calEventResponses: undefined,
customInputs: undefined,
attendeePhoneNumber: undefined,
};
}
if (!parsedBody.responses) {
throw new Error("`responses` must not be nullish");
}
const responses = parsedBody.responses;
const { userFieldsResponses: calEventUserFieldsResponses, responses: calEventResponses } =
getCalEventResponses({
bookingFields: eventType.bookingFields,
responses,
});
return {
...parsedBody,
name: responses.name,
email: responses.email,
attendeePhoneNumber: responses.attendeePhoneNumber,
guests: responses.guests ? responses.guests : [],
location: responses.location?.optionValue || responses.location?.value || "",
smsReminderNumber: responses.smsReminderNumber,
notes: responses.notes || "",
calEventUserFieldsResponses,
rescheduleReason: responses.rescheduleReason,
calEventResponses,
// So TS doesn't complain about unknown properties
customInputs: undefined,
};
}
export type AwaitedBookingData = Awaited<ReturnType<typeof getBookingData>>;
export type RescheduleReason = AwaitedBookingData["rescheduleReason"];
export type NoEmail = AwaitedBookingData["noEmail"];
export type AdditionalNotes = AwaitedBookingData["notes"];
export type ReqAppsStatus = AwaitedBookingData["appsStatus"];
export type SmsReminderNumber = AwaitedBookingData["smsReminderNumber"];
export type EventTypeId = AwaitedBookingData["eventTypeId"];
export type ReqBodyMetadata = ReqBodyWithEnd["metadata"];