Skip to content

Commit 48cbb94

Browse files
joeauyeungdevin-ai-integration[bot]claude
authored
feat: add webhook trigger when routing form fallback route is hit (#27588)
* 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: joe@cal.com <j.auyeung419@gmail.com> * 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: joe@cal.com <j.auyeung419@gmail.com> * fix: ensure fallbackAction type is always defined in onChange handlers Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: add guards for route.fallbackAction in onChange handlers Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: only return fallbackAction when no users are found Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: restructure fallback section to mirror main route structure Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: ensure backwards compatibility for existing routes with fallbackAttributesQueryValue Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: initialize fallbackAction with main event type for backwards compatibility Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: include fallbackAction in getRoutesToSave to persist changes Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: use fallbackAction in getRoutedUrl when no team members found Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: use i18n for fallback label and add tests for fallback action Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: do not trigger fallback action when CRM contact owner is found Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * 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 <noreply@anthropic.com> * refactor: extract RouteActionSelector shared component for action selection UI Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: add i18n string for default custom page message Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * 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: joe@cal.com <j.auyeung419@gmail.com> * fix: remove webhook object from error log to avoid exposing secrets Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * revert: remove webhook trigger changes (to be moved to separate PR) Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * 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: joe@cal.com <j.auyeung419@gmail.com> * fix: remove webhook object from error log to avoid exposing secrets Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: add ROUTING_FORM_FALLBACK_HIT to PayloadBuilderFactory trigger mapping Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * style: apply biome formatting fixes Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: restore removed comments in RouteBuilder Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * chore: add prisma migration for ROUTING_FORM_FALLBACK_HIT enum value Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * fix: add ROUTING_FORM_FALLBACK_HIT to FormTriggerEvents type union Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * test: add test coverage for triggerFallbackWebhook function Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * refactor: consolidate fallback webhook firing with form submission webhooks Move ROUTING_FORM_FALLBACK_HIT webhooks into the same Promise.all() batch as FORM_SUBMITTED / FORM_SUBMITTED_NO_EVENT, eliminating redundant getWebhooks/getWebhookTargetEntity/getOrgIdFromMemberOrTeamId calls. - Add fallbackAction parameter to _onFormSubmission and onSubmissionOfFormResponse - Remove triggerFallbackWebhook function and all its usages/imports - Move getFallbackAction() earlier in handleResponse.ts and pass result through - Update all tests to verify batched webhook behavior Co-Authored-By: joe@cal.com <j.auyeung419@gmail.com> * Add UI option for trigger * feat: persist fallbackAction on queued form responses When a routing form response is queued, the fallbackAction determined during routing is now stored on the QueuedFormResponse record. When the queued response is later converted to a real FormResponse, the stored fallbackAction is passed to onSubmissionOfFormResponse so the ROUTING_FORM_FALLBACK_HIT webhook fires correctly. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * test: add coverage for fallbackAction in queued form response flow Verify that fallbackAction is persisted when queuing and passed through to onSubmissionOfFormResponse when the queued response is converted. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8238d4f commit 48cbb94

15 files changed

Lines changed: 510 additions & 67 deletions

File tree

‎apps/web/app/api/routing-forms/queued-response/__tests__/queued-response.test.ts‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ const mockQueuedFormResponse = {
4242
disabled: false,
4343
},
4444
chosenRouteId: "mock-chosen-route-id",
45+
fallbackAction: null,
4546
createdAt: new Date(),
4647
updatedAt: new Date(),
4748
response: {},
@@ -126,6 +127,57 @@ describe("queuedResponseHandler", () => {
126127
});
127128
});
128129

130+
it("should pass fallbackAction to onSubmissionOfFormResponse when present", async () => {
131+
const fallbackAction = {
132+
type: "eventTypeRedirectUrl" as const,
133+
value: "team/fallback-event",
134+
eventTypeId: 42,
135+
};
136+
137+
vi.mocked(mockRoutingFormResponseRepository.getQueuedFormResponseFromId).mockResolvedValue({
138+
...mockQueuedFormResponse,
139+
fallbackAction,
140+
});
141+
142+
vi.mocked(getSerializableForm).mockResolvedValue({
143+
id: "mock-form-id",
144+
name: "Test Form",
145+
description: "Test Form Description",
146+
fields: [],
147+
routes: [],
148+
userId: 1,
149+
teamId: null,
150+
position: 1,
151+
updatedById: null,
152+
disabled: false,
153+
createdAt: new Date().toISOString(),
154+
updatedAt: new Date().toISOString(),
155+
settings: {},
156+
} as unknown as Awaited<ReturnType<typeof getSerializableForm>>);
157+
158+
vi.mocked(getResponseToStore).mockReturnValue({} as ReturnType<typeof getResponseToStore>);
159+
160+
vi.mocked(onSubmissionOfFormResponse).mockResolvedValue(undefined as unknown as Awaited<
161+
ReturnType<typeof onSubmissionOfFormResponse>
162+
>);
163+
164+
vi.mocked(mockRoutingFormResponseRepository.recordFormResponse).mockResolvedValue({
165+
id: 1,
166+
response: {},
167+
} as Awaited<ReturnType<typeof mockRoutingFormResponseRepository.recordFormResponse>>);
168+
169+
await queuedResponseHandler({
170+
queuedFormResponseId: "1",
171+
params: {},
172+
});
173+
174+
expect(onSubmissionOfFormResponse).toHaveBeenCalledWith(
175+
expect.objectContaining({
176+
fallbackAction,
177+
})
178+
);
179+
});
180+
129181
it("if no queued form response is found, should return early", async () => {
130182
vi.mocked(mockRoutingFormResponseRepository.getQueuedFormResponseFromId).mockResolvedValue(null);
131183
const response = await queuedResponseHandler({

‎apps/web/app/api/routing-forms/queued-response/route.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,20 @@ export const queuedResponseHandler = async ({
7272
}
7373

7474
const chosenRoute = serializableForm.routes?.find((r) => r.id === queuedFormResponse.chosenRouteId);
75+
const fallbackAction = queuedFormResponse.fallbackAction as {
76+
type: "customPageMessage" | "externalRedirectUrl" | "eventTypeRedirectUrl";
77+
value: string;
78+
eventTypeId?: number;
79+
} | null;
80+
7581
await onSubmissionOfFormResponse({
7682
form: {
7783
...queuedFormResponse.form,
7884
...serializableForm,
7985
},
8086
formResponseInDb: formResponse,
8187
chosenRouteAction: chosenRoute ? ("action" in chosenRoute ? chosenRoute.action : null) : null,
88+
fallbackAction,
8289
});
8390

8491
return {

‎apps/web/modules/webhooks/components/WebhookForm.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const WEBHOOK_TRIGGER_EVENTS_GROUPED_BY_APP_V2: Record<string, WebhookTriggerEve
7373
"routing-forms": [
7474
{ value: WebhookTriggerEvents.FORM_SUBMITTED, label: "form_submitted" },
7575
{ value: WebhookTriggerEvents.FORM_SUBMITTED_NO_EVENT, label: "form_submitted_no_event" },
76+
{ value: WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT, label: "routing_form_fallback_hit_trigger" },
7677
],
7778
} as const;
7879

‎packages/app-store/routing-forms/lib/formSubmissionUtils.test.ts‎

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,3 +397,215 @@ describe("_onFormSubmission", () => {
397397
});
398398
});
399399
});
400+
401+
describe("_onFormSubmission with fallbackAction", () => {
402+
const mockForm = {
403+
id: "form-1",
404+
name: "Test Form",
405+
disabled: false,
406+
userId: 1,
407+
position: 0,
408+
description: null,
409+
updatedById: null,
410+
fields: [
411+
{ id: "field-1", identifier: "email", label: "Email", type: "email", required: false },
412+
] as Field[],
413+
user: { id: 1, email: "test@example.com", timeFormat: 12, locale: "en" },
414+
teamId: 1,
415+
settings: {},
416+
routes: [],
417+
createdAt: new Date().toISOString(),
418+
updatedAt: new Date().toISOString(),
419+
connectedForms: [],
420+
routers: [],
421+
teamMembers: [],
422+
};
423+
424+
const mockResponse: FormResponse = {
425+
"field-1": { label: "Email", value: "test@response.com" },
426+
};
427+
428+
const mockFallbackAction = {
429+
type: "externalRedirectUrl" as const,
430+
value: "https://example.com/fallback",
431+
};
432+
433+
beforeEach(() => {
434+
vi.clearAllMocks();
435+
});
436+
437+
afterEach(() => {
438+
vi.restoreAllMocks();
439+
});
440+
441+
it("should fetch ROUTING_FORM_FALLBACK_HIT webhooks when fallbackAction is provided", async () => {
442+
vi.mocked(getWebhooks).mockResolvedValue([]);
443+
444+
await _onFormSubmission(mockForm, mockResponse, 123, undefined, mockFallbackAction);
445+
446+
expect(getWebhooks).toHaveBeenCalledWith({
447+
userId: null,
448+
teamId: 1,
449+
orgId: 1,
450+
triggerEvent: WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT,
451+
});
452+
});
453+
454+
it("should not fetch ROUTING_FORM_FALLBACK_HIT webhooks when fallbackAction is not provided", async () => {
455+
vi.mocked(getWebhooks).mockResolvedValue([]);
456+
457+
await _onFormSubmission(mockForm, mockResponse, 123);
458+
459+
const calls = vi.mocked(getWebhooks).mock.calls;
460+
const fallbackCalls = calls.filter(
461+
(call) => call[0].triggerEvent === WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT
462+
);
463+
expect(fallbackCalls).toHaveLength(0);
464+
});
465+
466+
it("should send ROUTING_FORM_FALLBACK_HIT webhook payload with correct data", async () => {
467+
const mockWebhook: WebhookSubscriber = {
468+
id: "wh-1",
469+
secret: "secret",
470+
subscriberUrl: "https://example.com/webhook",
471+
payloadTemplate: null,
472+
appId: null,
473+
eventTriggers: [WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT],
474+
time: null,
475+
timeUnit: null,
476+
version: WebhookVersionEnum.V_2021_10_20,
477+
};
478+
vi.mocked(getWebhooks).mockImplementation(async (opts) => {
479+
if (opts.triggerEvent === WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT) {
480+
return [mockWebhook];
481+
}
482+
return [];
483+
});
484+
485+
await _onFormSubmission(mockForm, mockResponse, 123, undefined, mockFallbackAction);
486+
487+
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
488+
expect.objectContaining({
489+
secretKey: "secret",
490+
triggerEvent: "ROUTING_FORM_FALLBACK_HIT",
491+
webhook: mockWebhook,
492+
data: {
493+
formId: "form-1",
494+
formName: "Test Form",
495+
teamId: 1,
496+
responseId: 123,
497+
fallbackAction: {
498+
type: "externalRedirectUrl",
499+
value: "https://example.com/fallback",
500+
},
501+
responses: mockResponse,
502+
},
503+
})
504+
);
505+
});
506+
507+
it("should include eventTypeId in fallbackAction payload when present", async () => {
508+
const mockWebhook: WebhookSubscriber = {
509+
id: "wh-1",
510+
secret: "secret",
511+
subscriberUrl: "https://example.com/webhook",
512+
payloadTemplate: null,
513+
appId: null,
514+
eventTriggers: [WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT],
515+
time: null,
516+
timeUnit: null,
517+
version: WebhookVersionEnum.V_2021_10_20,
518+
};
519+
vi.mocked(getWebhooks).mockImplementation(async (opts) => {
520+
if (opts.triggerEvent === WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT) {
521+
return [mockWebhook];
522+
}
523+
return [];
524+
});
525+
526+
const fallbackWithEventType = {
527+
type: "eventTypeRedirectUrl" as const,
528+
value: "team/30min",
529+
eventTypeId: 42,
530+
};
531+
532+
await _onFormSubmission(mockForm, mockResponse, 123, undefined, fallbackWithEventType);
533+
534+
expect(sendGenericWebhookPayload).toHaveBeenCalledWith(
535+
expect.objectContaining({
536+
data: expect.objectContaining({
537+
fallbackAction: {
538+
type: "eventTypeRedirectUrl",
539+
value: "team/30min",
540+
eventTypeId: 42,
541+
},
542+
}),
543+
})
544+
);
545+
});
546+
547+
it("should send to multiple ROUTING_FORM_FALLBACK_HIT webhooks", async () => {
548+
const mockWebhooks: WebhookSubscriber[] = [
549+
{
550+
id: "wh-1",
551+
secret: "secret1",
552+
subscriberUrl: "https://example.com/webhook1",
553+
payloadTemplate: null,
554+
appId: null,
555+
eventTriggers: [WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT],
556+
time: null,
557+
timeUnit: null,
558+
version: WebhookVersionEnum.V_2021_10_20,
559+
},
560+
{
561+
id: "wh-2",
562+
secret: "secret2",
563+
subscriberUrl: "https://example.com/webhook2",
564+
payloadTemplate: null,
565+
appId: null,
566+
eventTriggers: [WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT],
567+
time: null,
568+
timeUnit: null,
569+
version: WebhookVersionEnum.V_2021_10_20,
570+
},
571+
];
572+
vi.mocked(getWebhooks).mockImplementation(async (opts) => {
573+
if (opts.triggerEvent === WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT) {
574+
return mockWebhooks;
575+
}
576+
return [];
577+
});
578+
579+
await _onFormSubmission(mockForm, mockResponse, 123, undefined, mockFallbackAction);
580+
581+
const fallbackCalls = vi.mocked(sendGenericWebhookPayload).mock.calls.filter(
582+
(call) => call[0].triggerEvent === "ROUTING_FORM_FALLBACK_HIT"
583+
);
584+
expect(fallbackCalls).toHaveLength(2);
585+
});
586+
587+
it("should not throw when ROUTING_FORM_FALLBACK_HIT webhook payload fails", async () => {
588+
const mockWebhook: WebhookSubscriber = {
589+
id: "wh-1",
590+
secret: "secret",
591+
subscriberUrl: "https://example.com/webhook",
592+
payloadTemplate: null,
593+
appId: null,
594+
eventTriggers: [WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT],
595+
time: null,
596+
timeUnit: null,
597+
version: WebhookVersionEnum.V_2021_10_20,
598+
};
599+
vi.mocked(getWebhooks).mockImplementation(async (opts) => {
600+
if (opts.triggerEvent === WebhookTriggerEvents.ROUTING_FORM_FALLBACK_HIT) {
601+
return [mockWebhook];
602+
}
603+
return [];
604+
});
605+
vi.mocked(sendGenericWebhookPayload).mockRejectedValueOnce(new Error("Network error"));
606+
607+
await expect(
608+
_onFormSubmission(mockForm, mockResponse, 123, undefined, mockFallbackAction)
609+
).resolves.not.toThrow();
610+
});
611+
});

0 commit comments

Comments
 (0)