Files
cal.diy/packages/embeds/embed-core/src/sdk-action-manager.ts
T
0af62da16b feat: add dry run events for SDK (dryRunBookingSuccessfulV2 and dryRunRescheduleBookingSuccessfulV2) (#23072)
* feat: add dry run events for SDK (dryRunBookingSuccessfulV2 and dryRunRescheduleBookingSuccessfulV2)

- Add new event types dryRunBookingSuccessfulV2 and dryRunRescheduleBookingSuccessfulV2 to EventDataMap
- Create payload functions that exclude uid field for dry run events
- Fire dry run events when isDryRun is true in both regular and recurring booking flows
- Add unit tests to verify dry run event payloads are correct and exclude uid field
- Ensures dry run mode no longer skips event firing but uses appropriate dry run variants

Co-Authored-By: [email protected] <[email protected]>

* refactor: make dry run events more DRY by reusing types and payload logic

- Create BaseBookingEventPayload type to eliminate duplication between regular and dry run events
- Use intersection types to compose final event types in sdk-action-manager.ts
- Create getBaseBookingEventPayload function for common payload generation logic
- Export dry run payload functions and import them in tests instead of redefining
- Fix missing videoCallUrl field in rescheduleBookingSuccessfulV2 type

Co-Authored-By: [email protected] <[email protected]>

* Remove test file

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Udit Takkar <[email protected]>
2025-08-15 05:21:08 +00:00

179 lines
4.7 KiB
TypeScript

type Namespace = string;
type CustomEventDetail = Record<string, unknown>;
function _fireEvent(fullName: string, detail: CustomEventDetail) {
const event = new window.CustomEvent(fullName, {
detail: detail,
});
window.dispatchEvent(event);
}
type BaseBookingEventPayload = {
title: string | undefined;
startTime: string | undefined;
endTime: string | undefined;
eventTypeId: number | null | undefined;
status: string | undefined;
paymentRequired: boolean;
isRecurring: boolean;
/**
* This is only used for recurring bookings
*/
allBookings?: { startTime: string; endTime: string }[];
videoCallUrl?: string;
};
export type EventDataMap = {
eventTypeSelected: {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
eventType: any;
};
linkFailed: {
code: string;
msg: string;
data: {
url: string;
};
};
linkReady: Record<string, never>;
__connectInitiated: Record<string, never>;
__connectCompleted: Record<string, never>;
bookingSuccessfulV2: BaseBookingEventPayload & {
uid: string | undefined;
};
/**
* @deprecated Use `bookingSuccessfulV2` instead. We restrict the data heavily there, only sending what is absolutely needed and keeping it light as well. Plus, more importantly that can be documented well.
*/
bookingSuccessful: {
// TODO: Shouldn't send the entire booking and eventType objects, we should send specific fields from them.
booking: unknown;
eventType: unknown;
date: string;
duration: number | undefined;
organizer: {
name: string;
email: string;
timeZone: string;
};
confirmed: boolean;
};
rescheduleBookingSuccessfulV2: BaseBookingEventPayload & {
uid: string | undefined;
};
dryRunBookingSuccessfulV2: BaseBookingEventPayload;
dryRunRescheduleBookingSuccessfulV2: BaseBookingEventPayload;
/**
* @deprecated Use `rescheduleBookingSuccessfulV2` instead. We restrict the data heavily there, only sending what is absolutely needed and keeping it light as well. Plus, more importantly that can be documented well.
*/
rescheduleBookingSuccessful: {
booking: unknown;
eventType: unknown;
date: string;
duration: number | undefined;
organizer: {
name: string;
email: string;
timeZone: string;
};
confirmed: boolean;
};
bookingCancelled: {
booking: unknown;
organizer: {
name: string;
email: string;
timeZone?: string | undefined;
};
eventType: unknown;
};
routed: {
actionType: "customPageMessage" | "externalRedirectUrl" | "eventTypeRedirectUrl";
actionValue: string;
};
navigatedToBooker: Record<string, never>;
"*": Record<string, unknown>;
__routeChanged: Record<string, never>;
__windowLoadComplete: Record<string, never>;
__closeIframe: Record<string, never>;
__iframeReady: {
isPrerendering: boolean;
};
__dimensionChanged: {
iframeHeight: number;
iframeWidth: number;
isFirstTime: boolean;
};
__scrollByDistance: {
/**
* Distance in pixels to scroll by.
*/
distance: number;
};
};
export type EventData<T extends keyof EventDataMap> = {
[K in T]: {
type: string;
namespace: string;
fullType: string;
data: EventDataMap[K];
};
}[T];
export type EmbedEvent<T extends keyof EventDataMap> = CustomEvent<EventData<T>>;
export class SdkActionManager {
namespace: Namespace;
static parseAction(fullType: string) {
if (!fullType) {
return null;
}
//FIXME: Ensure that any action if it has :, it is properly encoded.
const [cal, calNamespace, type] = fullType.split(":");
if (cal !== "CAL") {
return null;
}
return {
ns: calNamespace,
type,
};
}
getFullActionName(name: string) {
return this.namespace ? `CAL:${this.namespace}:${name}` : `CAL::${name}`;
}
fire<T extends keyof EventDataMap>(name: T, data: EventDataMap[T]) {
const fullName = this.getFullActionName(name);
const detail = {
type: name,
namespace: this.namespace,
fullType: fullName,
data,
};
_fireEvent(fullName, detail);
// Wildcard Event
_fireEvent(this.getFullActionName("*"), detail);
}
on<T extends keyof EventDataMap>(name: T, callback: (arg0: CustomEvent<EventData<T>>) => void) {
const fullName = this.getFullActionName(name);
window.addEventListener(fullName, callback as EventListener);
}
off<T extends keyof EventDataMap>(name: T, callback: (arg0: CustomEvent<EventData<T>>) => void) {
const fullName = this.getFullActionName(name);
window.removeEventListener(fullName, callback as EventListener);
}
constructor(ns: string | null) {
ns = ns || "";
this.namespace = ns;
}
}