Skip to content

Commit 9c95e74

Browse files
fix(routing-forms): use direct eventTypeId lookup instead of brittle slug extraction
1 parent 321b07a commit 9c95e74

File tree

3 files changed

+34
-14
lines changed

3 files changed

+34
-14
lines changed

apps/api/v2/src/modules/organizations/routing-forms/services/shared-routing-form-response.service.ts

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class SharedRoutingFormResponseService {
2121
private readonly slotsService: SlotsService_2024_09_04,
2222
private readonly teamsEventTypesRepository: TeamsEventTypesRepository,
2323
private readonly eventTypesRepository: EventTypesRepository_2024_06_14
24-
) {}
24+
) { }
2525

2626
async createRoutingFormResponseWithSlots(
2727
routingFormId: string,
@@ -153,8 +153,24 @@ export class SharedRoutingFormResponseService {
153153
}
154154

155155
private async extractEventTypeAndCrmParams(userId: number, routingUrl: URL) {
156-
// Extract team and event type information
157-
// 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
156+
const urlParams = routingUrl.searchParams;
157+
158+
// Prefer direct eventTypeId lookup if available (more reliable than slug-based)
159+
const eventTypeIdParam = urlParams.get("cal.eventTypeId");
160+
if (eventTypeIdParam) {
161+
const eventTypeId = parseInt(eventTypeIdParam, 10);
162+
if (!isNaN(eventTypeId)) {
163+
const eventType = await this.eventTypesRepository.getEventTypeById(eventTypeId);
164+
if (eventType?.id) {
165+
return {
166+
eventTypeId: eventType.id,
167+
crmParams: this.extractCrmParamsFromUrl(urlParams),
168+
};
169+
}
170+
}
171+
}
172+
173+
// Fall back to slug-based lookup for backward compatibility
158174
const { teamId, eventTypeSlug } = this.extractTeamIdAndEventTypeSlugFromRedirectUrl(routingUrl);
159175
const eventType = teamId
160176
? await this.teamsEventTypesRepository.getEventTypeByTeamIdAndSlug(teamId, eventTypeSlug)
@@ -167,15 +183,20 @@ export class SharedRoutingFormResponseService {
167183
);
168184
}
169185

170-
// Extract CRM parameters from URL
171-
const urlParams = routingUrl.searchParams;
172-
const crmParams = {
186+
return {
187+
eventTypeId: eventType.id,
188+
crmParams: this.extractCrmParamsFromUrl(routingUrl.searchParams),
189+
};
190+
}
191+
192+
private extractCrmParamsFromUrl(urlParams: URLSearchParams) {
193+
return {
173194
teamMemberEmail: urlParams.get("cal.crmContactOwnerEmail") || undefined,
174195
routedTeamMemberIds: urlParams.get("cal.routedTeamMemberIds")
175196
? urlParams
176-
.get("cal.routedTeamMemberIds")!
177-
.split(",")
178-
.map((id) => parseInt(id))
197+
.get("cal.routedTeamMemberIds")!
198+
.split(",")
199+
.map((id) => parseInt(id))
179200
: undefined,
180201
routingFormResponseId: urlParams.get("cal.routingFormResponseId")
181202
? parseInt(urlParams.get("cal.routingFormResponseId")!)
@@ -187,11 +208,6 @@ export class SharedRoutingFormResponseService {
187208
crmAppSlug: urlParams.get("cal.crmAppSlug") || undefined,
188209
crmOwnerRecordType: urlParams.get("cal.crmContactOwnerRecordType") || undefined,
189210
};
190-
191-
return {
192-
eventTypeId: eventType.id,
193-
crmParams,
194-
};
195211
}
196212

197213
private isEventTypeRedirectUrl(routingUrl: URL) {

packages/features/routing-forms/lib/getRoutedUrl.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ const _getRoutedUrl = async (context: Pick<GetServerSidePropsContext, "query" |
252252
crmLookupDone: fetchCrm,
253253
teamId: form?.teamId,
254254
orgId: form.team?.parentId,
255+
eventTypeId: decidedAction.eventTypeId ?? null,
255256
}),
256257
isEmbed: pageProps.isEmbed,
257258
}),

packages/features/routing-forms/lib/getUrlSearchParamsToForward.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type GetUrlSearchParamsToForwardOptions = {
3434
reroutingFormResponses?: FormResponseValueOnly;
3535
teamId?: number | null;
3636
orgId?: number | null;
37+
eventTypeId?: number | null;
3738
};
3839

3940
export function getUrlSearchParamsToForward({
@@ -51,6 +52,7 @@ export function getUrlSearchParamsToForward({
5152
reroutingFormResponses,
5253
teamId,
5354
orgId,
55+
eventTypeId,
5456
}: GetUrlSearchParamsToForwardOptions) {
5557
type Params = Record<string, string | string[]>;
5658
const paramsFromResponse: Params = {};
@@ -138,6 +140,7 @@ export function getUrlSearchParamsToForward({
138140
...(reroutingFormResponses
139141
? { ["cal.reroutingFormResponses"]: JSON.stringify(reroutingFormResponses) }
140142
: null),
143+
...(eventTypeId ? { ["cal.eventTypeId"]: String(eventTypeId) } : null),
141144
};
142145

143146
const allQueryURLSearchParams = new URLSearchParams();

0 commit comments

Comments
 (0)