-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* refactor: handleNewBooking #3 * refactor: create booking factor * refactor: handleNewBooking * refactor: seats and rescheduleUId * chore: remove comment * fix: type err * chore: add missing statement * chore: use less params and other improvements * chore: name * chore: improvement * fix: type err * Typo fix * chore: fix type err * refactor: more readable * refactor: improve code * fix: conflicts --------- Co-authored-by: Joe Au-Yeung <j.auyeung419@gmail.com> Co-authored-by: Joe Au-Yeung <65426560+joeauyeung@users.noreply.github.com> Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com>
- Loading branch information
1 parent
a99e285
commit b4f1b5a
Showing
22 changed files
with
620 additions
and
481 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
24 changes: 24 additions & 0 deletions
24
packages/features/bookings/lib/handleNewBooking/addVideoCallDataToEvent.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { BookingReference } from "@calcom/prisma/client"; | ||
import type { CalendarEvent } from "@calcom/types/Calendar"; | ||
|
||
/** Updates the evt object with video call data found from booking references | ||
* | ||
* @param bookingReferences | ||
* @param evt | ||
* | ||
* @returns updated evt with video call data | ||
*/ | ||
export const addVideoCallDataToEvent = (bookingReferences: BookingReference[], evt: CalendarEvent) => { | ||
const videoCallReference = bookingReferences.find((reference) => reference.type.includes("_video")); | ||
|
||
if (videoCallReference) { | ||
evt.videoCallData = { | ||
type: videoCallReference.type, | ||
id: videoCallReference.meetingId, | ||
password: videoCallReference?.meetingPassword, | ||
url: videoCallReference.meetingUrl, | ||
}; | ||
} | ||
|
||
return evt; | ||
}; |
43 changes: 43 additions & 0 deletions
43
packages/features/bookings/lib/handleNewBooking/checkBookingAndDurationLimits.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import dayjs from "@calcom/dayjs"; | ||
import { checkBookingLimits, checkDurationLimits } from "@calcom/lib/server"; | ||
import type { IntervalLimit } from "@calcom/types/Calendar"; | ||
|
||
import type { NewBookingEventType } from "./types"; | ||
|
||
type EventType = Pick<NewBookingEventType, "bookingLimits" | "durationLimits" | "id" | "schedule">; | ||
|
||
type InputProps = { | ||
eventType: EventType; | ||
reqBodyStart: string; | ||
reqBodyRescheduleUid?: string; | ||
}; | ||
|
||
export const checkBookingAndDurationLimits = async ({ | ||
eventType, | ||
reqBodyStart, | ||
reqBodyRescheduleUid, | ||
}: InputProps) => { | ||
if ( | ||
Object.prototype.hasOwnProperty.call(eventType, "bookingLimits") || | ||
Object.prototype.hasOwnProperty.call(eventType, "durationLimits") | ||
) { | ||
const startAsDate = dayjs(reqBodyStart).toDate(); | ||
if (eventType.bookingLimits && Object.keys(eventType.bookingLimits).length > 0) { | ||
await checkBookingLimits( | ||
eventType.bookingLimits as IntervalLimit, | ||
startAsDate, | ||
eventType.id, | ||
reqBodyRescheduleUid, | ||
eventType.schedule?.timeZone | ||
); | ||
} | ||
if (eventType.durationLimits) { | ||
await checkDurationLimits( | ||
eventType.durationLimits as IntervalLimit, | ||
startAsDate, | ||
eventType.id, | ||
reqBodyRescheduleUid | ||
); | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
packages/features/bookings/lib/handleNewBooking/getCustomInputsResponses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import type z from "zod"; | ||
|
||
import { slugify } from "@calcom/lib/slugify"; | ||
import type { bookingCreateSchemaLegacyPropsForApi } from "@calcom/prisma/zod-utils"; | ||
import type { CalendarEvent } from "@calcom/types/Calendar"; | ||
|
||
import type { getEventTypeResponse } from "./types"; | ||
|
||
type CustomInputs = z.infer<typeof bookingCreateSchemaLegacyPropsForApi>["customInputs"]; | ||
|
||
type RequestBody = { | ||
responses?: Record<string, object>; | ||
customInputs?: CustomInputs; | ||
}; | ||
|
||
function mapCustomInputs( | ||
customInputs: { label: string; value: CustomInputs[number]["value"] }[] | ||
): Record<string, CustomInputs[number]["value"]> { | ||
return customInputs.reduce((acc, { label, value }) => { | ||
acc[label] = value; | ||
return acc; | ||
}, {} as Record<string, CustomInputs[number]["value"]>); | ||
} | ||
|
||
function mapResponsesToCustomInputs( | ||
responses: Record<string, object>, | ||
eventTypeCustomInputs: getEventTypeResponse["customInputs"] | ||
): NonNullable<CalendarEvent["customInputs"]> { | ||
// Backward Compatibility: Map new `responses` to old `customInputs` format so that webhooks can still receive same values. | ||
return Object.entries(responses).reduce((acc, [fieldName, fieldValue]) => { | ||
const foundInput = eventTypeCustomInputs.find((input) => slugify(input.label) === fieldName); | ||
if (foundInput) { | ||
acc[foundInput.label] = fieldValue; | ||
} | ||
return acc; | ||
}, {} as NonNullable<CalendarEvent["customInputs"]>); | ||
} | ||
|
||
export function getCustomInputsResponses( | ||
reqBody: RequestBody, | ||
eventTypeCustomInputs: getEventTypeResponse["customInputs"] | ||
): NonNullable<CalendarEvent["customInputs"]> { | ||
if (reqBody.customInputs && reqBody.customInputs.length > 0) { | ||
return mapCustomInputs(reqBody.customInputs); | ||
} | ||
|
||
const responses = reqBody.responses || {}; | ||
return mapResponsesToCustomInputs(responses, eventTypeCustomInputs); | ||
} |
Oops, something went wrong.