diff --git a/apps/api/v2/src/modules/organizations/routing-forms/services/shared-routing-form-response.service.ts b/apps/api/v2/src/modules/organizations/routing-forms/services/shared-routing-form-response.service.ts index 4daab24e29c401..677b76420722dc 100644 --- a/apps/api/v2/src/modules/organizations/routing-forms/services/shared-routing-form-response.service.ts +++ b/apps/api/v2/src/modules/organizations/routing-forms/services/shared-routing-form-response.service.ts @@ -21,7 +21,7 @@ export class SharedRoutingFormResponseService { private readonly slotsService: SlotsService_2024_09_04, private readonly teamsEventTypesRepository: TeamsEventTypesRepository, private readonly eventTypesRepository: EventTypesRepository_2024_06_14 - ) {} + ) { } async createRoutingFormResponseWithSlots( routingFormId: string, @@ -153,8 +153,24 @@ export class SharedRoutingFormResponseService { } private async extractEventTypeAndCrmParams(userId: number, routingUrl: URL) { - // Extract team and event type information - // TODO: Route action also has eventTypeId directly now and instead of using this brittle approach for getting event type by slug, we should get by eventTypeId + const urlParams = routingUrl.searchParams; + + // Prefer direct eventTypeId lookup if available (more reliable than slug-based) + const eventTypeIdParam = urlParams.get("cal.eventTypeId"); + if (eventTypeIdParam) { + const eventTypeId = parseInt(eventTypeIdParam, 10); + if (!isNaN(eventTypeId)) { + const eventType = await this.eventTypesRepository.getEventTypeById(eventTypeId); + if (eventType?.id) { + return { + eventTypeId: eventType.id, + crmParams: this.extractCrmParamsFromUrl(urlParams), + }; + } + } + } + + // Fall back to slug-based lookup for backward compatibility const { teamId, eventTypeSlug } = this.extractTeamIdAndEventTypeSlugFromRedirectUrl(routingUrl); const eventType = teamId ? await this.teamsEventTypesRepository.getEventTypeByTeamIdAndSlug(teamId, eventTypeSlug) @@ -167,15 +183,20 @@ export class SharedRoutingFormResponseService { ); } - // Extract CRM parameters from URL - const urlParams = routingUrl.searchParams; - const crmParams = { + return { + eventTypeId: eventType.id, + crmParams: this.extractCrmParamsFromUrl(routingUrl.searchParams), + }; + } + + private extractCrmParamsFromUrl(urlParams: URLSearchParams) { + return { teamMemberEmail: urlParams.get("cal.crmContactOwnerEmail") || undefined, routedTeamMemberIds: urlParams.get("cal.routedTeamMemberIds") ? urlParams - .get("cal.routedTeamMemberIds")! - .split(",") - .map((id) => parseInt(id)) + .get("cal.routedTeamMemberIds")! + .split(",") + .map((id) => parseInt(id)) : undefined, routingFormResponseId: urlParams.get("cal.routingFormResponseId") ? parseInt(urlParams.get("cal.routingFormResponseId")!) @@ -187,11 +208,6 @@ export class SharedRoutingFormResponseService { crmAppSlug: urlParams.get("cal.crmAppSlug") || undefined, crmOwnerRecordType: urlParams.get("cal.crmContactOwnerRecordType") || undefined, }; - - return { - eventTypeId: eventType.id, - crmParams, - }; } private isEventTypeRedirectUrl(routingUrl: URL) { diff --git a/packages/features/routing-forms/lib/getRoutedUrl.ts b/packages/features/routing-forms/lib/getRoutedUrl.ts index 26ae5703dc1302..4b40f299e18040 100644 --- a/packages/features/routing-forms/lib/getRoutedUrl.ts +++ b/packages/features/routing-forms/lib/getRoutedUrl.ts @@ -252,6 +252,7 @@ const _getRoutedUrl = async (context: Pick; const paramsFromResponse: Params = {}; @@ -138,6 +140,7 @@ export function getUrlSearchParamsToForward({ ...(reroutingFormResponses ? { ["cal.reroutingFormResponses"]: JSON.stringify(reroutingFormResponses) } : null), + ...(eventTypeId ? { ["cal.eventTypeId"]: String(eventTypeId) } : null), }; const allQueryURLSearchParams = new URLSearchParams();