* feat: Add rescheduledBy & canceledBy fields in the DB * fix: type check * fix: type check * fix: use session user email for reschedule * fix: unit test * feat: db field email validation * feat: rescheduledBy and cancelledBy in webhooks * revert unrelated changes. * make session user secondary, default to Anonymous * if condition not required * Make cancelledBy optional Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> * update cancel booking type * fix: update cancelledBy in db when requesting reschedule * remove default value for fields * fix: type check * feat: manage fields via api v1 * fix: add fields in booking read api v1 * test: expand to cover new fields * fix: use cancelledBy param on booking page --------- Co-authored-by: Anik Dhabal Babu <81948346+anikdhabal@users.noreply.github.com> Co-authored-by: Syed Ali Shahbaz <52925846+alishaz-polymath@users.noreply.github.com> Co-authored-by: Peer Richelsen <peeroke@gmail.com>
110 lines
4.1 KiB
TypeScript
110 lines
4.1 KiB
TypeScript
import type { UseBookingFormReturnType } from "@calcom/features/bookings/Booker/components/hooks/useBookingForm";
|
|
import { useBookerStore } from "@calcom/features/bookings/Booker/store";
|
|
import {
|
|
useTimePreferences,
|
|
mapBookingToMutationInput,
|
|
mapRecurringBookingToMutationInput,
|
|
} from "@calcom/features/bookings/lib";
|
|
import type { BookerEvent } from "@calcom/features/bookings/types";
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
|
import type { BookingCreateBody } from "@calcom/prisma/zod-utils";
|
|
|
|
import type { UseCreateBookingInput } from "./useCreateBooking";
|
|
|
|
type UseHandleBookingProps = {
|
|
bookingForm: UseBookingFormReturnType["bookingForm"];
|
|
event?: {
|
|
data?: Pick<
|
|
BookerEvent,
|
|
"id" | "isDynamic" | "metadata" | "recurringEvent" | "length" | "slug" | "schedulingType"
|
|
> | null;
|
|
};
|
|
metadata: Record<string, string>;
|
|
hashedLink?: string | null;
|
|
teamMemberEmail?: string;
|
|
handleBooking: (input: UseCreateBookingInput) => void;
|
|
handleInstantBooking: (input: BookingCreateBody) => void;
|
|
handleRecBooking: (input: BookingCreateBody[]) => void;
|
|
locationUrl?: string;
|
|
};
|
|
|
|
export const useHandleBookEvent = ({
|
|
bookingForm,
|
|
event,
|
|
metadata,
|
|
hashedLink,
|
|
teamMemberEmail,
|
|
handleBooking,
|
|
handleInstantBooking,
|
|
handleRecBooking,
|
|
locationUrl,
|
|
}: UseHandleBookingProps) => {
|
|
const setFormValues = useBookerStore((state) => state.setFormValues);
|
|
const timeslot = useBookerStore((state) => state.selectedTimeslot);
|
|
const duration = useBookerStore((state) => state.selectedDuration);
|
|
const { timezone } = useTimePreferences();
|
|
const rescheduleUid = useBookerStore((state) => state.rescheduleUid);
|
|
const rescheduledBy = useBookerStore((state) => state.rescheduledBy);
|
|
const { t, i18n } = useLocale();
|
|
const username = useBookerStore((state) => state.username);
|
|
const recurringEventCount = useBookerStore((state) => state.recurringEventCount);
|
|
const bookingData = useBookerStore((state) => state.bookingData);
|
|
const seatedEventData = useBookerStore((state) => state.seatedEventData);
|
|
const isInstantMeeting = useBookerStore((state) => state.isInstantMeeting);
|
|
const orgSlug = useBookerStore((state) => state.org);
|
|
|
|
const handleBookEvent = () => {
|
|
const values = bookingForm.getValues();
|
|
if (timeslot) {
|
|
// Clears form values stored in store, so old values won't stick around.
|
|
setFormValues({});
|
|
bookingForm.clearErrors();
|
|
|
|
// It shouldn't be possible that this method is fired without having event data,
|
|
// but since in theory (looking at the types) it is possible, we still handle that case.
|
|
if (!event?.data) {
|
|
bookingForm.setError("globalError", { message: t("error_booking_event") });
|
|
return;
|
|
}
|
|
|
|
// Ensures that duration is an allowed value, if not it defaults to the
|
|
// default event duration.
|
|
const validDuration = event.data.isDynamic
|
|
? duration || event.data.length
|
|
: duration && event.data.metadata?.multipleDuration?.includes(duration)
|
|
? duration
|
|
: event.data.length;
|
|
|
|
const bookingInput = {
|
|
values,
|
|
duration: validDuration,
|
|
event: event.data,
|
|
date: timeslot,
|
|
timeZone: timezone,
|
|
language: i18n.language,
|
|
rescheduleUid: rescheduleUid || undefined,
|
|
rescheduledBy: rescheduledBy || undefined,
|
|
bookingUid: (bookingData && bookingData.uid) || seatedEventData?.bookingUid || undefined,
|
|
username: username || "",
|
|
metadata: metadata,
|
|
hashedLink,
|
|
teamMemberEmail,
|
|
orgSlug: orgSlug ? orgSlug : undefined,
|
|
};
|
|
|
|
if (isInstantMeeting) {
|
|
handleInstantBooking(mapBookingToMutationInput(bookingInput));
|
|
} else if (event.data?.recurringEvent?.freq && recurringEventCount && !rescheduleUid) {
|
|
handleRecBooking(mapRecurringBookingToMutationInput(bookingInput, recurringEventCount));
|
|
} else {
|
|
handleBooking({ ...mapBookingToMutationInput(bookingInput), locationUrl });
|
|
}
|
|
// Clears form values stored in store, so old values won't stick around.
|
|
setFormValues({});
|
|
bookingForm.clearErrors();
|
|
}
|
|
};
|
|
|
|
return handleBookEvent;
|
|
};
|