feat: add fallback action options to routing form fallback section (#27449)

* feat: add no users found fallback action for routing forms

- Add NoUsersFoundFallbackActionType enum with CustomPageMessage and ExternalRedirectUrl options
- Add noUsersFoundFallbackAction field to zodNonRouterRoute schema
- Add NoUsersFoundFallback UI component in RouteBuilder for configuring fallback action
- Update handleResponse to return noUsersFoundFallbackAction when no team members match
- Add i18n translation keys for new UI strings

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

* refactor: change fallback to use same action options as main route

- Replace noUsersFoundFallbackAction with fallbackAction that has same structure as main action
- Add action dropdown and input fields to fallback attributes query builder section
- Support Event redirect, External URL, and Custom page options in fallback
- Remove separate NoUsersFoundFallback toggle component
- Update handleResponse to return fallbackAction

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

* fix: ensure fallbackAction type is always defined in onChange handlers

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

* fix: add guards for route.fallbackAction in onChange handlers

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

* fix: only return fallbackAction when no users are found

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

* refactor: restructure fallback section to mirror main route structure

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

* fix: ensure backwards compatibility for existing routes with fallbackAttributesQueryValue

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

* fix: initialize fallbackAction with main event type for backwards compatibility

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

* fix: include fallbackAction in getRoutesToSave to persist changes

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

* fix: use fallbackAction in getRoutedUrl when no team members found

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

* fix: use i18n for fallback label and add tests for fallback action

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

* fix: do not trigger fallback action when CRM contact owner is found

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

* fix: prioritize fallbackAction over fallbackAttributesQueryValue

When a route has a fallbackAction configured, skip the
fallbackAttributesQueryValue to ensure the fallbackAction is triggered
when no team members are found. This maintains backwards compatibility
by only using fallbackAttributesQueryValue when fallbackAction is not set.

Also treats teamMemberIdsMatchingAttributeLogic being null (routing couldn't
run, e.g., missing orgId) the same as an empty array for the purpose of
triggering the fallbackAction.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* refactor: extract RouteActionSelector shared component for action selection UI

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

* fix: add i18n string for default custom page message

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

* feat: add webhook trigger when routing form fallback route is hit

- Add ROUTING_FORM_FALLBACK_HIT to WebhookTriggerEvents enum in Prisma schema
- Add new trigger to routing-forms webhook triggers in constants
- Add translation string for the new webhook trigger
- Implement triggerFallbackWebhook function in formSubmissionUtils.ts
- Call webhook trigger from handleResponse.ts when fallback action is used

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

* fix: remove webhook object from error log to avoid exposing secrets

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

* revert: remove webhook trigger changes (to be moved to separate PR)

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

* fix: only show fallback query builder when fallbackAction is explicitly set

The fallback attributes query builder was being shown for new routes
when fallbackAction was not set, causing an extra 'Add rule' button
to appear. This broke e2e tests that relied on the button index.

Now the fallback query builder is only shown when fallbackAction is
explicitly set to EventTypeRedirectUrl, which happens after the route
is saved and reloaded (via auto-initialization in useRoutes).

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

* fix: use data-testid for attribute query builder selectors in e2e tests

- Add data-testid='attributes-query-builder' to the main attributes query builder div
- Update addAttributeRoutingRule and addAttributeRoutingRuleWithOperator to use the data-testid selector instead of nth() indices
- This makes the tests more robust and independent of DOM structure changes

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

* fix: initialize fallbackAction for new routes in useCreateRoute hook

- New routes created via useCreateRoute now initialize fallbackAction
- This ensures new routes have a valid fallbackAction when saved
- Matches the initialization logic for existing routes in useRoutes

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

* fix: initialize fallbackAction at save time when action is EventTypeRedirectUrl

- When saving routes, if fallbackAction is undefined but action is EventTypeRedirectUrl,
  initialize fallbackAction from the main action
- This handles the case where user creates a new route and changes action to an event type
- Fixes e2e test failures where form save was failing due to undefined fallbackAction

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

* fix: initialize fallbackAction when main action changes to EventTypeRedirectUrl

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

* fix: only show fallback section when main action has valid event type selected

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

* fix: check for valid fallbackAction event type before preserving existing value

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

* Give more structure to main and fallback routes

* Update comment

* Rename fallbackAttributesQueryBuilder to matchingMembersFallbackRoute

* Display custom event redirect input

* fix: address Cubic AI review feedback for routing form fallback

- Simplify getFallbackAction logic to use checkedFallback flag instead of
  manually parsing attributesQueryValue (confidence 9/10)
- Add i18n translation for 'And connect with specific team members' string
  using t('connect_with_specific_team_members') (confidence 9/10)

Co-Authored-By: unknown <>

* fix: update getFallbackAction to use checkedFallback and handle missing orgId case

- Use checkedFallback flag as primary indicator for triggering fallback action
- Also handle edge case where attribute routing was configured but couldn't run
  due to missing orgId (teamMemberIdsMatchingAttributeLogic is null)
- Update test to mock checkedFallback: true when expecting fallback action

Co-Authored-By: unknown <>

* Address UI comment

* fix: Use fallbackAttributesQueryValue when fallback action is event redirect

Only use fallbackAttributesQueryValue when:
1. fallbackAction is EventTypeRedirectUrl (attribute routing applies)
2. fallbackAction is not set (backwards compatibility)

Skip it for CustomPageMessage and ExternalRedirectUrl fallback actions
since attribute routing doesn't apply to those action types.

Co-Authored-By: Claude Opus 4.5 <[email protected]>

* fix: correct JSX syntax error in matchingMembersFallbackRoute

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

* refactor: extract getCustomSlug helper to deduplicate custom event type slug logic

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

* fix: only show fallback attribute filtering for teams under orgs

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

* fix: show fallback action for all team forms, gate attribute filtering on isOrganization

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

---------

Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
Co-authored-by: Claude Opus 4.5 <[email protected]>
Co-authored-by: Hariom Balhara <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-02-11 09:15:16 -05:00
1 parent f6fa24de2b
commit 6089f47d24
8 files changed
+909 -319

No files matched your search

@@ -67,9 +67,9 @@ async function selectOperatorOption({ fromLocator, option }: { fromLocator: Loca
}
async function addAttributeRoutingRule(page: Page) {
// TODO: Use a better selector maybe?
await page.locator('text="Add rule"').nth(1).click();
const attributeQueryBuilder = page.locator(".group-container").nth(1);
// Use data-testid to target the main attributes query builder specifically
const attributeQueryBuilder = page.locator('[data-testid="attributes-query-builder"]');
await attributeQueryBuilder.locator('text="Add rule"').click();
const attributeSelectorFirstRule = attributeQueryBuilder.locator(".rule--field").nth(0);
await selectFirstAttributeOption({
fromLocator: attributeSelectorFirstRule,
@@ -84,8 +84,9 @@ async function addAttributeRoutingRule(page: Page) {
}
async function addAttributeRoutingRuleWithOperator(page: Page, valueFrom: string, valueTo: string) {
await page.locator('text="Add rule"').nth(1).click();
const attributeQueryBuilder = page.locator(".group-container").nth(1);
// Use data-testid to target the main attributes query builder specifically
const attributeQueryBuilder = page.locator('[data-testid="attributes-query-builder"]');
await attributeQueryBuilder.locator('text="Add rule"').click();
const attributeSelectorFirstRule = attributeQueryBuilder.locator(".rule--field").nth(0);
await selectFirstAttributeOption({
fromLocator: attributeSelectorFirstRule,
+11
View File
@@ -92,6 +92,17 @@ export const zodNonRouterRoute = z.object({
eventTypeId: z.number().optional(),
value: z.string(),
}),
/**
* Fallback action used when the main attributes query finds no matching team members
* and no CRM contact owner (if configured to check) exists.
*/
fallbackAction: z
.object({
type: routeActionTypeSchema,
eventTypeId: z.number().optional(),
value: z.string(),
})
.optional(),
});
export const zodNonRouterRouteView = zodNonRouterRoute;
@@ -354,4 +354,152 @@ describe("getRoutedUrl", () => {
);
});
});
describe("Fallback action", () => {
it("should use fallbackAction for external redirect when no team members found", async () => {
vi.mocked(PrismaRoutingFormRepository.findFormByIdIncludeUserTeamAndOrg).mockResolvedValue(
mockForm as never
);
const mainRedirectUrl = "test-user/30min";
const fallbackUrl = "https://example.com/fallback";
const mockRoute = {
id: "route1",
action: { type: "eventTypeRedirectUrl", value: mainRedirectUrl },
};
vi.mocked(findMatchingRoute).mockReturnValue(mockRoute as never);
vi.mocked(handleResponse).mockResolvedValue({
teamMembersMatchingAttributeLogic: [],
formResponse: { id: 1 },
queuedFormResponse: null,
attributeRoutingConfig: null,
timeTaken: {},
crmContactOwnerEmail: null,
crmContactOwnerRecordType: null,
crmAppSlug: null,
isPreview: false,
fallbackAction: { type: "externalRedirectUrl", value: fallbackUrl },
});
const context = mockContext({ email: "[email protected]" });
const result = await getRoutedUrl(context);
expect(result).toEqual({
redirect: {
destination: expect.stringContaining(fallbackUrl),
permanent: false,
},
});
});
it("should use fallbackAction for custom page message when no team members found", async () => {
vi.mocked(PrismaRoutingFormRepository.findFormByIdIncludeUserTeamAndOrg).mockResolvedValue(
mockForm as never
);
const mainRedirectUrl = "test-user/30min";
const customMessage = "No team members available";
const mockRoute = {
id: "route1",
action: { type: "eventTypeRedirectUrl", value: mainRedirectUrl },
};
vi.mocked(findMatchingRoute).mockReturnValue(mockRoute as never);
vi.mocked(handleResponse).mockResolvedValue({
teamMembersMatchingAttributeLogic: [],
formResponse: { id: 1 },
queuedFormResponse: null,
attributeRoutingConfig: null,
timeTaken: {},
crmContactOwnerEmail: null,
crmContactOwnerRecordType: null,
crmAppSlug: null,
isPreview: false,
fallbackAction: { type: "customPageMessage", value: customMessage },
});
const context = mockContext({ email: "[email protected]" });
const result = await getRoutedUrl(context);
expect(result).toEqual({
props: {
isEmbed: false,
form: mockSerializableForm,
message: customMessage,
errorMessage: null,
},
});
});
it("should use main action when fallbackAction is null", async () => {
vi.mocked(PrismaRoutingFormRepository.findFormByIdIncludeUserTeamAndOrg).mockResolvedValue(
mockForm as never
);
const redirectUrl = "test-user/30min";
const mockRoute = { id: "route1", action: { type: "eventTypeRedirectUrl", value: redirectUrl } };
vi.mocked(findMatchingRoute).mockReturnValue(mockRoute as never);
vi.mocked(getAbsoluteEventTypeRedirectUrlWithEmbedSupport).mockReturnValue(`/${redirectUrl}`);
vi.mocked(handleResponse).mockResolvedValue({
teamMembersMatchingAttributeLogic: [123],
formResponse: { id: 1 },
queuedFormResponse: null,
attributeRoutingConfig: null,
timeTaken: {},
crmContactOwnerEmail: null,
crmContactOwnerRecordType: null,
crmAppSlug: null,
isPreview: false,
fallbackAction: null,
});
const context = mockContext({ email: "[email protected]" });
const result = await getRoutedUrl(context);
expect(result).toEqual({
redirect: {
destination: `/${redirectUrl}`,
permanent: false,
},
});
});
it("should use fallbackAction for event type redirect when no team members found", async () => {
vi.mocked(PrismaRoutingFormRepository.findFormByIdIncludeUserTeamAndOrg).mockResolvedValue(
mockForm as never
);
const mainRedirectUrl = "test-user/30min";
const fallbackRedirectUrl = "test-user/60min";
const mockRoute = {
id: "route1",
action: { type: "eventTypeRedirectUrl", value: mainRedirectUrl },
};
vi.mocked(findMatchingRoute).mockReturnValue(mockRoute as never);
vi.mocked(substituteVariables).mockReturnValue(fallbackRedirectUrl);
vi.mocked(getAbsoluteEventTypeRedirectUrlWithEmbedSupport).mockReturnValue(`/${fallbackRedirectUrl}`);
vi.mocked(handleResponse).mockResolvedValue({
teamMembersMatchingAttributeLogic: [],
formResponse: { id: 1 },
queuedFormResponse: null,
attributeRoutingConfig: null,
timeTaken: {},
crmContactOwnerEmail: null,
crmContactOwnerRecordType: null,
crmAppSlug: null,
isPreview: false,
fallbackAction: { type: "eventTypeRedirectUrl", value: fallbackRedirectUrl },
});
const context = mockContext({ email: "[email protected]" });
const result = await getRoutedUrl(context);
expect(substituteVariables).toHaveBeenCalledWith(
fallbackRedirectUrl,
{ email: "[email protected]" },
mockSerializableForm.fields
);
expect(result).toEqual({
redirect: {
destination: `/${fallbackRedirectUrl}`,
permanent: false,
},
});
});
});
});
@@ -158,6 +158,7 @@ const _getRoutedUrl = async (context: Pick<GetServerSidePropsContext, "query" |
let crmContactOwnerEmail: string | null = null;
let crmContactOwnerRecordType: string | null = null;
let crmAppSlug: string | null = null;
let fallbackAction: typeof decidedAction | null = null;
try {
const result = await handleResponse({
form: serializableForm,
@@ -178,6 +179,7 @@ const _getRoutedUrl = async (context: Pick<GetServerSidePropsContext, "query" |
crmContactOwnerEmail = result.crmContactOwnerEmail;
crmContactOwnerRecordType = result.crmContactOwnerRecordType;
crmAppSlug = result.crmAppSlug;
fallbackAction = result.fallbackAction ?? null;
timeTaken = {
...timeTaken,
...result.timeTaken,
@@ -212,19 +214,22 @@ const _getRoutedUrl = async (context: Pick<GetServerSidePropsContext, "query" |
// TODO: To be done using sentry tracing
console.log("Server-Timing", getServerTimingHeader(timeTaken));
// Use fallbackAction if set (when no team members found), otherwise use the main decidedAction
const actionToUse = fallbackAction ?? decidedAction;
//TODO: Maybe take action after successful mutation
if (decidedAction.type === "customPageMessage") {
if (actionToUse.type === "customPageMessage") {
return {
props: {
...pageProps,
form: serializableForm,
message: decidedAction.value,
message: actionToUse.value,
errorMessage: null,
},
};
} else if (decidedAction.type === "eventTypeRedirectUrl") {
} else if (actionToUse.type === "eventTypeRedirectUrl") {
const eventTypeUrlWithResolvedVariables = substituteVariables(
decidedAction.value,
actionToUse.value,
response,
serializableForm.fields
);
@@ -258,10 +263,10 @@ const _getRoutedUrl = async (context: Pick<GetServerSidePropsContext, "query" |
permanent: false,
},
};
} else if (decidedAction.type === "externalRedirectUrl") {
} else if (actionToUse.type === "externalRedirectUrl") {
return {
redirect: {
destination: `${decidedAction.value}?${stringify(context.query)}&cal.action=externalRedirectUrl`,
destination: `${actionToUse.value}?${stringify(context.query)}&cal.action=externalRedirectUrl`,
permanent: false,
},
};
@@ -342,4 +342,334 @@ describe("handleResponse", () => {
})
).rejects.toThrow(/Chosen route is a router/);
});
describe("Fallback action", () => {
// Mock attributesQueryValue with actual rules
const mockAttributesQueryValueWithRules = {
type: "group",
children1: {
"rule-1": {
type: "rule",
properties: {
field: "attr-1",
operator: "select_equals",
value: ["option-1"],
},
},
},
};
it("should return fallbackAction when no team members match attribute logic", async () => {
const fallbackAction = {
type: "externalRedirectUrl" as const,
value: "https://example.com/fallback",
};
const chosenRoute = {
id: "route1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryValue: { type: "group", children1: {} } as any,
action: {
type: "eventTypeRedirectUrl" as const,
value: "team/30min",
},
attributeRoutingConfig: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributesQueryValue: mockAttributesQueryValueWithRules as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fallbackAttributesQueryValue: { type: "group", children1: {} } as any,
fallbackAction,
isFallback: false,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const formWithRoute: TargetRoutingFormForResponse = { ...mockForm, routes: [chosenRoute as any] };
vi.mocked(findTeamMembersMatchingAttributeLogic).mockResolvedValue({
teamMembersMatchingAttributeLogic: [], // Empty array = no users found
checkedFallback: true, // Fallback was checked and triggered
fallbackAttributeLogicBuildingWarnings: [],
mainAttributeLogicBuildingWarnings: [],
timeTaken: {},
});
const result = await handleResponse({
response: mockResponse,
form: formWithRoute,
formFillerId: "user1",
chosenRouteId: "route1",
isPreview: true,
identifierKeyedResponse: {
name: "John Doe",
email: "[email protected]",
},
});
expect(result.teamMembersMatchingAttributeLogic).toEqual([]);
expect(result.fallbackAction).toEqual(fallbackAction);
// Verify that fallbackAttributesQueryValue is NOT passed when fallbackAction is configured
expect(findTeamMembersMatchingAttributeLogic).toHaveBeenCalledWith(
expect.objectContaining({
fallbackAttributesQueryValue: undefined,
}),
expect.anything()
);
});
it("should return null fallbackAction when team members are found", async () => {
const fallbackAction = {
type: "externalRedirectUrl" as const,
value: "https://example.com/fallback",
};
const chosenRoute = {
id: "route1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryValue: { type: "group", children1: {} } as any,
action: {
type: "eventTypeRedirectUrl" as const,
value: "team/30min",
},
attributeRoutingConfig: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributesQueryValue: mockAttributesQueryValueWithRules as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fallbackAttributesQueryValue: { type: "group", children1: {} } as any,
fallbackAction,
isFallback: false,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const formWithRoute: TargetRoutingFormForResponse = { ...mockForm, routes: [chosenRoute as any] };
vi.mocked(findTeamMembersMatchingAttributeLogic).mockResolvedValue({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
teamMembersMatchingAttributeLogic: [{ userId: 123, result: "MATCH" as any }],
checkedFallback: false,
fallbackAttributeLogicBuildingWarnings: [],
mainAttributeLogicBuildingWarnings: [],
timeTaken: {},
});
const result = await handleResponse({
response: mockResponse,
form: formWithRoute,
formFillerId: "user1",
chosenRouteId: "route1",
isPreview: true,
identifierKeyedResponse: {
name: "John Doe",
email: "[email protected]",
},
});
expect(result.teamMembersMatchingAttributeLogic).toEqual([123]);
expect(result.fallbackAction).toBeNull();
});
it("should return null fallbackAction when route has no fallbackAction configured", async () => {
const chosenRoute = {
id: "route1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryValue: { type: "group", children1: {} } as any,
action: {
type: "eventTypeRedirectUrl" as const,
value: "team/30min",
},
attributeRoutingConfig: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributesQueryValue: { type: "group", children1: {} } as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fallbackAttributesQueryValue: { type: "group", children1: {} } as any,
// No fallbackAction configured
isFallback: false,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const formWithRoute: TargetRoutingFormForResponse = { ...mockForm, routes: [chosenRoute as any] };
vi.mocked(findTeamMembersMatchingAttributeLogic).mockResolvedValue({
teamMembersMatchingAttributeLogic: [], // Empty array = no users found
checkedFallback: false,
fallbackAttributeLogicBuildingWarnings: [],
mainAttributeLogicBuildingWarnings: [],
timeTaken: {},
});
const result = await handleResponse({
response: mockResponse,
form: formWithRoute,
formFillerId: "user1",
chosenRouteId: "route1",
isPreview: true,
identifierKeyedResponse: {
name: "John Doe",
email: "[email protected]",
},
});
expect(result.teamMembersMatchingAttributeLogic).toEqual([]);
expect(result.fallbackAction).toBeNull();
// Verify that fallbackAttributesQueryValue IS passed when fallbackAction is NOT configured (backwards compatibility)
expect(findTeamMembersMatchingAttributeLogic).toHaveBeenCalledWith(
expect.objectContaining({
fallbackAttributesQueryValue: { type: "group", children1: {} },
}),
expect.anything()
);
});
it("should return null fallbackAction when teamMembersMatchingAttributeLogic is null (non-team form)", async () => {
const fallbackAction = {
type: "externalRedirectUrl" as const,
value: "https://example.com/fallback",
};
const chosenRoute = {
id: "route1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryValue: { type: "group", children1: {} } as any,
action: {
type: "eventTypeRedirectUrl" as const,
value: "team/30min",
},
attributeRoutingConfig: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributesQueryValue: { type: "group", children1: {} } as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fallbackAttributesQueryValue: { type: "group", children1: {} } as any,
fallbackAction,
isFallback: false,
};
// Non-team form (no teamId or orgId)
const nonTeamForm: TargetRoutingFormForResponse = {
...mockForm,
teamId: null,
team: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
routes: [chosenRoute as any],
};
const result = await handleResponse({
response: mockResponse,
form: nonTeamForm,
formFillerId: "user1",
chosenRouteId: "route1",
isPreview: true,
identifierKeyedResponse: {
name: "John Doe",
email: "[email protected]",
},
});
// teamMembersMatchingAttributeLogic is null for non-team forms
expect(result.teamMembersMatchingAttributeLogic).toBeNull();
// fallbackAction should be null because attribute routing has no rules configured (empty children1)
expect(result.fallbackAction).toBeNull();
});
it("should return fallbackAction when team has no orgId but attribute routing is configured", async () => {
const fallbackAction = {
type: "externalRedirectUrl" as const,
value: "https://example.com/fallback",
};
const chosenRoute = {
id: "route1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryValue: { type: "group", children1: {} } as any,
action: {
type: "eventTypeRedirectUrl" as const,
value: "team/30min",
},
attributeRoutingConfig: null,
// Route has attribute routing rules configured
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributesQueryValue: mockAttributesQueryValueWithRules as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fallbackAttributesQueryValue: { type: "group", children1: {} } as any,
fallbackAction,
isFallback: false,
};
// Team form but team has no parentId (not part of an org)
const teamWithoutOrgForm: TargetRoutingFormForResponse = {
...mockForm,
teamId: 123,
team: { parentId: null }, // No org
// eslint-disable-next-line @typescript-eslint/no-explicit-any
routes: [chosenRoute as any],
};
const result = await handleResponse({
response: mockResponse,
form: teamWithoutOrgForm,
formFillerId: "user1",
chosenRouteId: "route1",
isPreview: true,
identifierKeyedResponse: {
name: "John Doe",
email: "[email protected]",
},
});
// teamMembersMatchingAttributeLogic is null because orgId is missing (attribute routing couldn't run)
expect(result.teamMembersMatchingAttributeLogic).toBeNull();
// fallbackAction SHOULD be returned because attribute routing was configured but couldn't run
expect(result.fallbackAction).toEqual(fallbackAction);
});
it("should return null fallbackAction when no team members found but CRM contact owner exists", async () => {
const fallbackAction = {
type: "externalRedirectUrl" as const,
value: "https://example.com/fallback",
};
const chosenRoute = {
id: "route1",
// eslint-disable-next-line @typescript-eslint/no-explicit-any
queryValue: { type: "group", children1: {} } as any,
action: {
type: "eventTypeRedirectUrl" as const,
value: "team/30min",
},
attributeRoutingConfig: null,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
attributesQueryValue: mockAttributesQueryValueWithRules as any,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
fallbackAttributesQueryValue: { type: "group", children1: {} } as any,
fallbackAction,
isFallback: false,
};
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const formWithRoute: TargetRoutingFormForResponse = { ...mockForm, routes: [chosenRoute as any] };
vi.mocked(findTeamMembersMatchingAttributeLogic).mockResolvedValue({
teamMembersMatchingAttributeLogic: [], // Empty array = no users found
checkedFallback: false,
fallbackAttributeLogicBuildingWarnings: [],
mainAttributeLogicBuildingWarnings: [],
timeTaken: {},
});
// Mock CRM contact owner found
vi.mocked(routerGetCrmContactOwnerEmail).mockResolvedValue({
email: "[email protected]",
recordType: "contact",
crmAppSlug: "hubspot",
recordId: "123",
});
const result = await handleResponse({
response: mockResponse,
form: formWithRoute,
formFillerId: "user1",
chosenRouteId: "route1",
isPreview: true,
fetchCrm: true,
identifierKeyedResponse: {
name: "John Doe",
email: "[email protected]",
},
});
expect(result.teamMembersMatchingAttributeLogic).toEqual([]);
expect(result.crmContactOwnerEmail).toEqual("[email protected]");
// fallbackAction should be null because CRM contact owner was found
expect(result.fallbackAction).toBeNull();
});
});
});
@@ -4,6 +4,7 @@ import {
type TargetRoutingFormForResponse,
} from "@calcom/app-store/routing-forms/lib/formSubmissionUtils";
import isRouter from "@calcom/app-store/routing-forms/lib/isRouter";
import { RouteActionType } from "@calcom/app-store/routing-forms/zod";
import type { RoutingFormTraceService } from "@calcom/features/routing-trace/domains/RoutingFormTraceService";
import type { RoutingTraceService } from "@calcom/features/routing-trace/services/RoutingTraceService";
import { emailSchema } from "@calcom/lib/emailSchema";
@@ -135,6 +136,14 @@ const _handleResponse = async ({
crmRecordId = contactOwnerQuery?.recordId ?? null;
})(),
(async () => {
// Determine when to use fallbackAttributesQueryValue:
// 1. If fallbackAction is EventTypeRedirectUrl → use it (attribute routing applies to event redirect)
// 2. If fallbackAction is not set → use it (backwards compatibility)
// 3. If fallbackAction is CustomPageMessage or ExternalRedirectUrl → don't use it (attribute routing doesn't apply)
const hasFallbackAction = "fallbackAction" in chosenRoute && chosenRoute.fallbackAction;
const shouldUseFallbackAttributesQuery =
!hasFallbackAction ||
chosenRoute.fallbackAction?.type === RouteActionType.EventTypeRedirectUrl;
const teamMembersMatchingAttributeLogicWithResult =
formTeamId && formOrgId
? await findTeamMembersMatchingAttributeLogic(
@@ -144,7 +153,9 @@ const _handleResponse = async ({
fields: form.fields || [],
},
attributesQueryValue: chosenRoute.attributesQueryValue ?? null,
fallbackAttributesQueryValue: chosenRoute.fallbackAttributesQueryValue,
fallbackAttributesQueryValue: shouldUseFallbackAttributesQuery
? chosenRoute.fallbackAttributesQueryValue
: undefined,
teamId: formTeamId,
orgId: formOrgId,
routeName: chosenRoute.name,
@@ -222,6 +233,37 @@ const _handleResponse = async ({
};
}
}
const getFallbackAction = () => {
if (!chosenRoute || !("fallbackAction" in chosenRoute)) {
return null;
}
const hasCrmContactOwner = crmContactOwnerEmail !== null;
if (hasCrmContactOwner) {
return null;
}
if (checkedFallback) {
return chosenRoute.fallbackAction;
}
// Handle case where attribute routing was configured but couldn't run (e.g., missing orgId)
const attributesQueryValue =
"attributesQueryValue" in chosenRoute ? chosenRoute.attributesQueryValue : null;
const hasAttributeRoutingConfigured =
attributesQueryValue &&
typeof attributesQueryValue === "object" &&
"children1" in attributesQueryValue &&
attributesQueryValue.children1 &&
Object.keys(attributesQueryValue.children1 as Record<string, unknown>).length > 0;
if (hasAttributeRoutingConfigured && teamMemberIdsMatchingAttributeLogic === null) {
return chosenRoute.fallbackAction;
}
return null;
};
return {
isPreview: !!isPreview,
formResponse: dbFormResponse,
@@ -238,6 +280,7 @@ const _handleResponse = async ({
: null,
checkedFallback,
timeTaken,
fallbackAction: getFallbackAction(),
};
} catch (e) {
if (e instanceof Prisma.PrismaClientKnownRequestError) {