feat: Routing trace presenter (#27372)
* feat: Add routing trace presenters Add domain-specific presenters (SalesforceRoutingTracePresenter, RoutingFormTracePresenter) that format trace steps into human-readable strings, and a core RoutingTracePresenter that delegates to them based on step domain. Includes unit tests for all presenters. Co-Authored-By: Claude Opus 4.5 <[email protected]> * feat: Add findByBookingUid to RoutingTraceRepository Add method to look up a routing trace by booking UID, needed by the routing trace presenter tRPC endpoint. Co-Authored-By: Claude Opus 4.5 <[email protected]> * feat: Add getRoutingTrace tRPC endpoint Expose routing trace data for a booking via viewer.bookings.getRoutingTrace. Tries the permanent RoutingTrace first (round robin bookings), then falls back to PendingRoutingTrace via the booking's form response relation. Co-Authored-By: Claude Opus 4.5 <[email protected]> * feat: Add routing trace side sheet to booking list item Add a route icon button on booking list items that came from routing forms. Clicking it opens a side sheet displaying the full routing trace as human-readable steps. Adds RoutingTraceSheet component, store state, and translation key. Co-Authored-By: Claude Opus 4.5 <[email protected]> * refactor: Move routing trace action to dropdown menu Move the routing trace button from a standalone icon on the booking list item into the actions dropdown menu alongside "Report wrong assignment". The RoutingTraceSheet is now rendered from BookingActionsDropdown. Co-Authored-By: Claude Opus 4.5 <[email protected]> * fix: Improve routing trace UI and handle unnamed routes - Redesign RoutingTraceSheet with vertical timeline layout, domain badges, skeleton loading state, and millisecond timestamps - Use Salesforce app-store icon for Salesforce steps - Fall back to "Unnamed route" when route name is missing or matches route ID - Fix dropdown menu icon to git-merge (valid icon, unique in menu) Co-Authored-By: Claude Opus 4.5 <[email protected]> * test: Add tests for getRoutingTrace handler and findByBookingUid repository method Co-Authored-By: [email protected] <[email protected]> * Abstract functions * Fix build error * Add permission check * fix: Update getRoutingTrace tests to include ctx and mock BookingAccessService Co-Authored-By: [email protected] <[email protected]> --------- Co-authored-by: Claude Opus 4.5 <[email protected]> Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
24 files changed
+1086
-24
No files matched your search
@@ -0,0 +1,27 @@
|
||||
import { Icon } from "@calcom/ui/components/icon";
|
||||
|
||||
import { ROUTING_TRACE_DOMAINS } from "../constants";
|
||||
|
||||
import type { IconName } from "@calcom/ui/components/icon";
|
||||
|
||||
const DOMAIN_ICONS: Record<string, { type: "icon"; name: IconName } | { type: "img"; src: string; alt: string }> = {
|
||||
[ROUTING_TRACE_DOMAINS.SALESFORCE]: { type: "img", src: "/app-store/salesforce/icon.png", alt: "Salesforce" },
|
||||
[ROUTING_TRACE_DOMAINS.ROUTING_FORM]: { type: "icon", name: "file-text" },
|
||||
};
|
||||
|
||||
const DEFAULT_ICON: IconName = "shuffle";
|
||||
|
||||
export function DomainIcon({ domain }: { domain: string }) {
|
||||
const config = DOMAIN_ICONS[domain];
|
||||
|
||||
if (config?.type === "img") {
|
||||
return <img src={config.src} alt={config.alt} className="h-4 w-4" />;
|
||||
}
|
||||
|
||||
return (
|
||||
<Icon
|
||||
name={config?.type === "icon" ? config.name : DEFAULT_ICON}
|
||||
className="text-subtle h-4 w-4"
|
||||
/>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
export const ROUTING_TRACE_DOMAINS = {
|
||||
SALESFORCE: "salesforce",
|
||||
ROUTING_FORM: "routing_form",
|
||||
} as const;
|
||||
|
||||
export const ROUTING_TRACE_STEPS = {
|
||||
SALESFORCE_ASSIGNMENT: "salesforce_assignment",
|
||||
ATTRIBUTE_LOGIC_EVALUATED: "attribute-logic-evaluated",
|
||||
} as const;
|
||||
@@ -1,6 +1,6 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import { ROUTING_TRACE_DOMAINS } from "../constants";
|
||||
import type { RoutingTraceService } from "../services/RoutingTraceService";
|
||||
import { ROUTING_TRACE_DOMAINS } from "../services/RoutingTraceService";
|
||||
import { ROUTING_FORM_STEPS, RoutingFormTraceService } from "./RoutingFormTraceService";
|
||||
|
||||
describe("RoutingFormTraceService", () => {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { ROUTING_TRACE_DOMAINS } from "../constants";
|
||||
import type { RoutingTraceService } from "../services/RoutingTraceService";
|
||||
import { ROUTING_TRACE_DOMAINS } from "../services/RoutingTraceService";
|
||||
|
||||
export const ROUTING_FORM_STEPS = {
|
||||
ROUTE_MATCHED: "route_matched",
|
||||
|
||||
@@ -0,0 +1,93 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { RoutingStep } from "../repositories/RoutingTraceRepository.interface";
|
||||
import { RoutingFormTracePresenter } from "./RoutingFormTracePresenter";
|
||||
|
||||
function makeStep(step: string, data: Record<string, unknown> = {}): RoutingStep {
|
||||
return { domain: "routing_form", step, timestamp: Date.now(), data };
|
||||
}
|
||||
|
||||
describe("RoutingFormTracePresenter", () => {
|
||||
it("presents route_matched", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("route_matched", { routeId: "route-1", routeName: "Enterprise" })
|
||||
);
|
||||
expect(result).toBe('Route matched: "Enterprise" (ID: route-1)');
|
||||
});
|
||||
|
||||
it("presents fallback_route_used", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("fallback_route_used", { routeId: "route-2", routeName: "Default" })
|
||||
);
|
||||
expect(result).toBe('Fallback route used: "Default" (ID: route-2)');
|
||||
});
|
||||
|
||||
it("presents attribute-logic-evaluated with all fields", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("attribute-logic-evaluated", {
|
||||
routeName: "APAC",
|
||||
routeIsFallback: false,
|
||||
attributeRoutingDetails: [
|
||||
{ attributeName: "Company Size", attributeValue: "Enterprise" },
|
||||
{ attributeName: "Region", attributeValue: "APAC" },
|
||||
],
|
||||
})
|
||||
);
|
||||
expect(result).toBe(
|
||||
'Attribute logic evaluated: Route: "APAC" Attributes: [Company Size=Enterprise, Region=APAC]'
|
||||
);
|
||||
});
|
||||
|
||||
it("presents attribute-logic-evaluated with fallback", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("attribute-logic-evaluated", {
|
||||
routeName: "Fallback Route",
|
||||
routeIsFallback: true,
|
||||
})
|
||||
);
|
||||
expect(result).toBe('Attribute logic evaluated: Route: "Fallback Route" (fallback)');
|
||||
});
|
||||
|
||||
it("presents attribute-logic-evaluated with no optional fields", () => {
|
||||
const result = RoutingFormTracePresenter.present(makeStep("attribute-logic-evaluated", {}));
|
||||
expect(result).toBe('Attribute logic evaluated: Route: "Unnamed route"');
|
||||
});
|
||||
|
||||
it("presents attribute_fallback_used with routeName", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("attribute_fallback_used", { routeName: "Default Route" })
|
||||
);
|
||||
expect(result).toBe('Attribute fallback used: "Default Route"');
|
||||
});
|
||||
|
||||
it("presents attribute_fallback_used without routeName", () => {
|
||||
const result = RoutingFormTracePresenter.present(makeStep("attribute_fallback_used", {}));
|
||||
expect(result).toBe('Attribute fallback used: "Unnamed route"');
|
||||
});
|
||||
|
||||
it("presents route_matched without routeName", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("route_matched", { routeId: "route-1" })
|
||||
);
|
||||
expect(result).toBe('Route matched: "Unnamed route" (ID: route-1)');
|
||||
});
|
||||
|
||||
it("presents fallback_route_used without routeName", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("fallback_route_used", { routeId: "route-2" })
|
||||
);
|
||||
expect(result).toBe('Fallback route used: "Unnamed route" (ID: route-2)');
|
||||
});
|
||||
|
||||
it("treats routeName matching routeId as unnamed", () => {
|
||||
const result = RoutingFormTracePresenter.present(
|
||||
makeStep("route_matched", { routeId: "abc-123", routeName: "abc-123" })
|
||||
);
|
||||
expect(result).toBe('Route matched: "Unnamed route" (ID: abc-123)');
|
||||
});
|
||||
|
||||
it("returns fallback for unknown step", () => {
|
||||
const result = RoutingFormTracePresenter.present(makeStep("unknown_step", {}));
|
||||
expect(result).toBe("Routing Form: unknown_step");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { RoutingStep } from "../repositories/RoutingTraceRepository.interface";
|
||||
|
||||
export class RoutingFormTracePresenter {
|
||||
static present(step: RoutingStep): string {
|
||||
const d = step.data;
|
||||
const routeName = d.routeName && d.routeName !== d.routeId ? d.routeName : "Unnamed route";
|
||||
switch (step.step) {
|
||||
case "route_matched":
|
||||
return `Route matched: "${routeName}" (ID: ${d.routeId})`;
|
||||
case "fallback_route_used":
|
||||
return `Fallback route used: "${routeName}" (ID: ${d.routeId})`;
|
||||
case "attribute-logic-evaluated": {
|
||||
const parts: string[] = [];
|
||||
parts.push(`Route: "${routeName}"`);
|
||||
if (d.routeIsFallback) parts.push("(fallback)");
|
||||
if (d.attributeRoutingDetails && Array.isArray(d.attributeRoutingDetails)) {
|
||||
const details = (d.attributeRoutingDetails as Array<{ attributeName: string; attributeValue: string }>)
|
||||
.map((a) => `${a.attributeName}=${a.attributeValue}`)
|
||||
.join(", ");
|
||||
parts.push(`Attributes: [${details}]`);
|
||||
}
|
||||
return `Attribute logic evaluated: ${parts.join(" ")}`;
|
||||
}
|
||||
case "attribute_fallback_used":
|
||||
return `Attribute fallback used: "${routeName}"`;
|
||||
default:
|
||||
return `Routing Form: ${step.step}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { RoutingTrace } from "../repositories/RoutingTraceRepository.interface";
|
||||
import { RoutingTracePresenter } from "./RoutingTracePresenter";
|
||||
|
||||
describe("RoutingTracePresenter", () => {
|
||||
it("delegates salesforce steps to SalesforceRoutingTracePresenter", () => {
|
||||
const trace: RoutingTrace = [
|
||||
{
|
||||
domain: "salesforce",
|
||||
step: "searching_by_website_value",
|
||||
timestamp: 1000,
|
||||
data: { emailDomain: "acme.com" },
|
||||
},
|
||||
];
|
||||
|
||||
const result = RoutingTracePresenter.present(trace);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toEqual({
|
||||
message: 'Searching for Salesforce account by website matching domain "acme.com"',
|
||||
domain: "salesforce",
|
||||
step: "searching_by_website_value",
|
||||
timestamp: 1000,
|
||||
});
|
||||
});
|
||||
|
||||
it("delegates routing form steps to RoutingFormTracePresenter", () => {
|
||||
const trace: RoutingTrace = [
|
||||
{
|
||||
domain: "routing_form",
|
||||
step: "route_matched",
|
||||
timestamp: 2000,
|
||||
data: { routeId: "route-1", routeName: "Enterprise" },
|
||||
},
|
||||
];
|
||||
|
||||
const result = RoutingTracePresenter.present(trace);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0]).toEqual({
|
||||
message: 'Route matched: "Enterprise" (ID: route-1)',
|
||||
domain: "routing_form",
|
||||
step: "route_matched",
|
||||
timestamp: 2000,
|
||||
});
|
||||
});
|
||||
|
||||
it("handles unknown domains with fallback", () => {
|
||||
const trace: RoutingTrace = [
|
||||
{
|
||||
domain: "unknown_domain",
|
||||
step: "some_step",
|
||||
timestamp: 3000,
|
||||
data: {},
|
||||
},
|
||||
];
|
||||
|
||||
const result = RoutingTracePresenter.present(trace);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].message).toBe("unknown_domain: some_step");
|
||||
});
|
||||
|
||||
it("presents a multi-step trace in order", () => {
|
||||
const trace: RoutingTrace = [
|
||||
{
|
||||
domain: "routing_form",
|
||||
step: "route_matched",
|
||||
timestamp: 1000,
|
||||
data: { routeId: "route-1", routeName: "Sales" },
|
||||
},
|
||||
{
|
||||
domain: "salesforce",
|
||||
step: "graphql_query_initiated",
|
||||
timestamp: 2000,
|
||||
data: { email: "[email protected]", emailDomain: "acme.com" },
|
||||
},
|
||||
{
|
||||
domain: "salesforce",
|
||||
step: "salesforce_assignment",
|
||||
timestamp: 3000,
|
||||
data: { email: "[email protected]", recordType: "Contact", recordId: "003ABC" },
|
||||
},
|
||||
];
|
||||
|
||||
const result = RoutingTracePresenter.present(trace);
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
expect(result[0].message).toBe('Route matched: "Sales" (ID: route-1)');
|
||||
expect(result[1].message).toBe("GraphQL account resolution initiated for [email protected] (domain: acme.com)");
|
||||
expect(result[2].message).toBe("Salesforce assignment: [email protected] via Contact (ID: 003ABC)");
|
||||
});
|
||||
|
||||
it("returns empty array for empty trace", () => {
|
||||
const result = RoutingTracePresenter.present([]);
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,33 @@
|
||||
import type { RoutingStep, RoutingTrace } from "../repositories/RoutingTraceRepository.interface";
|
||||
import { ROUTING_TRACE_DOMAINS } from "../constants";
|
||||
import { RoutingFormTracePresenter } from "./RoutingFormTracePresenter";
|
||||
import { SalesforceRoutingTracePresenter } from "./SalesforceRoutingTracePresenter";
|
||||
|
||||
export interface PresentedStep {
|
||||
message: string;
|
||||
domain: string;
|
||||
step: string;
|
||||
timestamp: number;
|
||||
}
|
||||
|
||||
export class RoutingTracePresenter {
|
||||
static present(trace: RoutingTrace): PresentedStep[] {
|
||||
return trace.map((step) => ({
|
||||
message: RoutingTracePresenter.presentStep(step),
|
||||
domain: step.domain,
|
||||
step: step.step,
|
||||
timestamp: step.timestamp,
|
||||
}));
|
||||
}
|
||||
|
||||
private static presentStep(step: RoutingStep): string {
|
||||
switch (step.domain) {
|
||||
case ROUTING_TRACE_DOMAINS.SALESFORCE:
|
||||
return SalesforceRoutingTracePresenter.present(step);
|
||||
case ROUTING_TRACE_DOMAINS.ROUTING_FORM:
|
||||
return RoutingFormTracePresenter.present(step);
|
||||
default:
|
||||
return `${step.domain}: ${step.step}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import type { RoutingStep } from "../repositories/RoutingTraceRepository.interface";
|
||||
import { SalesforceRoutingTracePresenter } from "./SalesforceRoutingTracePresenter";
|
||||
|
||||
function makeStep(step: string, data: Record<string, unknown> = {}): RoutingStep {
|
||||
return { domain: "salesforce", step, timestamp: Date.now(), data };
|
||||
}
|
||||
|
||||
describe("SalesforceRoutingTracePresenter", () => {
|
||||
it("presents searching_by_website_value", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("searching_by_website_value", { emailDomain: "acme.com" })
|
||||
);
|
||||
expect(result).toBe('Searching for Salesforce account by website matching domain "acme.com"');
|
||||
});
|
||||
|
||||
it("presents account_found_by_website", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("account_found_by_website", { accountId: "001ABC", website: "acme.com" })
|
||||
);
|
||||
expect(result).toBe('Account found by website "acme.com" (Account ID: 001ABC)');
|
||||
});
|
||||
|
||||
it("presents searching_by_contact_email_domain", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("searching_by_contact_email_domain", { emailDomain: "acme.com", contactCount: 5 })
|
||||
);
|
||||
expect(result).toBe('Searching contacts with email domain "acme.com" (5 contacts found)');
|
||||
});
|
||||
|
||||
it("presents account_selected_by_most_contacts", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("account_selected_by_most_contacts", { accountId: "001ABC", contactCount: 3 })
|
||||
);
|
||||
expect(result).toBe("Account selected with most contacts: 001ABC (3 contacts)");
|
||||
});
|
||||
|
||||
it("presents no_account_found", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("no_account_found", { email: "[email protected]", reason: "free email domain" })
|
||||
);
|
||||
expect(result).toBe("No account found for [email protected]: free email domain");
|
||||
});
|
||||
|
||||
it("presents lookup_field_query with accountId", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("lookup_field_query", { fieldName: "Owner__c", salesforceObject: "Account", accountId: "001ABC" })
|
||||
);
|
||||
expect(result).toBe('Querying lookup field "Owner__c" on Account (Account: 001ABC)');
|
||||
});
|
||||
|
||||
it("presents lookup_field_query without accountId", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("lookup_field_query", { fieldName: "Owner__c", salesforceObject: "Account", accountId: null })
|
||||
);
|
||||
expect(result).toBe('Querying lookup field "Owner__c" on Account');
|
||||
});
|
||||
|
||||
it("presents user_query_from_lookup_field", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("user_query_from_lookup_field", { lookupFieldUserId: "005ABC", userEmail: "[email protected]" })
|
||||
);
|
||||
expect(result).toBe("Lookup field resolved to user [email protected] (User ID: 005ABC)");
|
||||
});
|
||||
|
||||
it("presents contact_owner_lookup", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("contact_owner_lookup", { contactId: "003ABC", ownerEmail: "[email protected]", ownerId: "005ABC" })
|
||||
);
|
||||
expect(result).toBe("Contact owner lookup: [email protected] (Contact: 003ABC)");
|
||||
});
|
||||
|
||||
it("presents lead_owner_lookup", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("lead_owner_lookup", { leadId: "00Q123", ownerEmail: "[email protected]", ownerId: "005ABC" })
|
||||
);
|
||||
expect(result).toBe("Lead owner lookup: [email protected] (Lead: 00Q123)");
|
||||
});
|
||||
|
||||
it("presents account_owner_lookup", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("account_owner_lookup", { accountId: "001ABC", ownerEmail: "[email protected]", ownerId: "005ABC" })
|
||||
);
|
||||
expect(result).toBe("Account owner lookup: [email protected] (Account: 001ABC)");
|
||||
});
|
||||
|
||||
it("presents owner_validated", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("owner_validated", { ownerEmail: "[email protected]", isTeamMember: true })
|
||||
);
|
||||
expect(result).toBe("Owner [email protected] validated as team member: true");
|
||||
});
|
||||
|
||||
it("presents owner_lookup_skipped with email", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("owner_lookup_skipped", { reason: "free email domain", email: "[email protected]" })
|
||||
);
|
||||
expect(result).toBe("Owner lookup skipped: free email domain ([email protected])");
|
||||
});
|
||||
|
||||
it("presents owner_lookup_skipped without email", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("owner_lookup_skipped", { reason: "feature disabled" })
|
||||
);
|
||||
expect(result).toBe("Owner lookup skipped: feature disabled");
|
||||
});
|
||||
|
||||
it("presents contact_owner_check_skipped", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("contact_owner_check_skipped", { reason: "route config" })
|
||||
);
|
||||
expect(result).toBe("Contact owner check skipped: route config");
|
||||
});
|
||||
|
||||
it("presents graphql_query_initiated", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("graphql_query_initiated", { email: "[email protected]", emailDomain: "acme.com" })
|
||||
);
|
||||
expect(result).toBe("GraphQL account resolution initiated for [email protected] (domain: acme.com)");
|
||||
});
|
||||
|
||||
it("presents graphql_existing_contact_found", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("graphql_existing_contact_found", {
|
||||
contactId: "003ABC",
|
||||
accountId: "001ABC",
|
||||
ownerEmail: "[email protected]",
|
||||
})
|
||||
);
|
||||
expect(result).toBe("Existing contact found via GraphQL: 003ABC (Account: 001ABC, Owner: [email protected])");
|
||||
});
|
||||
|
||||
it("presents graphql_account_found_by_website", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("graphql_account_found_by_website", { accountId: "001ABC", ownerEmail: "[email protected]" })
|
||||
);
|
||||
expect(result).toBe("Account found by website via GraphQL: 001ABC (Owner: [email protected])");
|
||||
});
|
||||
|
||||
it("presents graphql_searching_by_contact_domain", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("graphql_searching_by_contact_domain", { emailDomain: "acme.com", contactCount: 3 })
|
||||
);
|
||||
expect(result).toBe('Searching contacts by domain via GraphQL: "acme.com" (3 contacts)');
|
||||
});
|
||||
|
||||
it("presents graphql_dominant_account_selected", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("graphql_dominant_account_selected", {
|
||||
accountId: "001ABC",
|
||||
contactCount: 5,
|
||||
ownerEmail: "[email protected]",
|
||||
})
|
||||
);
|
||||
expect(result).toBe("Dominant account selected via GraphQL: 001ABC (5 contacts, Owner: [email protected])");
|
||||
});
|
||||
|
||||
it("presents graphql_no_account_found", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("graphql_no_account_found", { email: "[email protected]", reason: "no matching records" })
|
||||
);
|
||||
expect(result).toBe("No account found via GraphQL for [email protected]: no matching records");
|
||||
});
|
||||
|
||||
it("presents salesforce_assignment", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(
|
||||
makeStep("salesforce_assignment", { email: "[email protected]", recordType: "Contact", recordId: "003ABC" })
|
||||
);
|
||||
expect(result).toBe("Salesforce assignment: [email protected] via Contact (ID: 003ABC)");
|
||||
});
|
||||
|
||||
it("returns fallback for unknown step", () => {
|
||||
const result = SalesforceRoutingTracePresenter.present(makeStep("unknown_step", {}));
|
||||
expect(result).toBe("Salesforce: unknown_step");
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
import type { RoutingStep } from "../repositories/RoutingTraceRepository.interface";
|
||||
|
||||
export class SalesforceRoutingTracePresenter {
|
||||
static present(step: RoutingStep): string {
|
||||
const d = step.data;
|
||||
switch (step.step) {
|
||||
case "searching_by_website_value":
|
||||
return `Searching for Salesforce account by website matching domain "${d.emailDomain}"`;
|
||||
case "account_found_by_website":
|
||||
return `Account found by website "${d.website}" (Account ID: ${d.accountId})`;
|
||||
case "searching_by_contact_email_domain":
|
||||
return `Searching contacts with email domain "${d.emailDomain}" (${d.contactCount} contacts found)`;
|
||||
case "account_selected_by_most_contacts":
|
||||
return `Account selected with most contacts: ${d.accountId} (${d.contactCount} contacts)`;
|
||||
case "no_account_found":
|
||||
return `No account found for ${d.email}: ${d.reason}`;
|
||||
case "lookup_field_query":
|
||||
return `Querying lookup field "${d.fieldName}" on ${d.salesforceObject}${d.accountId ? ` (Account: ${d.accountId})` : ""}`;
|
||||
case "user_query_from_lookup_field":
|
||||
return `Lookup field resolved to user ${d.userEmail} (User ID: ${d.lookupFieldUserId})`;
|
||||
case "contact_owner_lookup":
|
||||
return `Contact owner lookup: ${d.ownerEmail} (Contact: ${d.contactId})`;
|
||||
case "lead_owner_lookup":
|
||||
return `Lead owner lookup: ${d.ownerEmail} (Lead: ${d.leadId})`;
|
||||
case "account_owner_lookup":
|
||||
return `Account owner lookup: ${d.ownerEmail} (Account: ${d.accountId})`;
|
||||
case "owner_validated":
|
||||
return `Owner ${d.ownerEmail} validated as team member: ${d.isTeamMember}`;
|
||||
case "owner_lookup_skipped":
|
||||
return `Owner lookup skipped: ${d.reason}${d.email ? ` (${d.email})` : ""}`;
|
||||
case "contact_owner_check_skipped":
|
||||
return `Contact owner check skipped: ${d.reason}`;
|
||||
case "graphql_query_initiated":
|
||||
return `GraphQL account resolution initiated for ${d.email} (domain: ${d.emailDomain})`;
|
||||
case "graphql_existing_contact_found":
|
||||
return `Existing contact found via GraphQL: ${d.contactId} (Account: ${d.accountId}, Owner: ${d.ownerEmail})`;
|
||||
case "graphql_account_found_by_website":
|
||||
return `Account found by website via GraphQL: ${d.accountId} (Owner: ${d.ownerEmail})`;
|
||||
case "graphql_searching_by_contact_domain":
|
||||
return `Searching contacts by domain via GraphQL: "${d.emailDomain}" (${d.contactCount} contacts)`;
|
||||
case "graphql_dominant_account_selected":
|
||||
return `Dominant account selected via GraphQL: ${d.accountId} (${d.contactCount} contacts, Owner: ${d.ownerEmail})`;
|
||||
case "graphql_no_account_found":
|
||||
return `No account found via GraphQL for ${d.email}: ${d.reason}`;
|
||||
case "salesforce_assignment":
|
||||
return `Salesforce assignment: ${d.email} via ${d.recordType} (ID: ${d.recordId})`;
|
||||
default:
|
||||
return `Salesforce: ${step.step}`;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
import { ROUTING_TRACE_DOMAINS } from "../constants";
|
||||
|
||||
const DOMAIN_LABELS: Record<string, string> = {
|
||||
[ROUTING_TRACE_DOMAINS.SALESFORCE]: "Salesforce",
|
||||
[ROUTING_TRACE_DOMAINS.ROUTING_FORM]: "Routing Form",
|
||||
};
|
||||
|
||||
export function getDomainLabel(domain: string): string {
|
||||
return DOMAIN_LABELS[domain] ?? domain;
|
||||
}
|
||||
@@ -1,6 +1,5 @@
|
||||
import type { PrismaClient } from "@calcom/prisma";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import { PrismaRoutingTraceRepository } from "./PrismaRoutingTraceRepository";
|
||||
|
||||
describe("PrismaRoutingTraceRepository", () => {
|
||||
@@ -9,6 +8,7 @@ describe("PrismaRoutingTraceRepository", () => {
|
||||
const mockPrisma = {
|
||||
routingTrace: {
|
||||
create: vi.fn(),
|
||||
findFirst: vi.fn(),
|
||||
},
|
||||
} as unknown as PrismaClient;
|
||||
|
||||
@@ -249,4 +249,98 @@ describe("PrismaRoutingTraceRepository", () => {
|
||||
expect(result.trace).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("findByBookingUid", () => {
|
||||
it("should return routing trace when found", async () => {
|
||||
const trace = [
|
||||
{
|
||||
domain: "routing_form",
|
||||
step: "route_matched",
|
||||
timestamp: Date.now(),
|
||||
data: { routeId: "route-1", routeName: "Enterprise" },
|
||||
},
|
||||
];
|
||||
|
||||
const mockResult = {
|
||||
id: "trace-1",
|
||||
createdAt: new Date("2025-01-01T10:00:00Z"),
|
||||
trace,
|
||||
formResponseId: 123,
|
||||
queuedFormResponseId: null,
|
||||
bookingUid: "booking-uid-123",
|
||||
assignmentReasonId: 42,
|
||||
};
|
||||
|
||||
vi.mocked(mockPrisma.routingTrace.findFirst).mockResolvedValue(mockResult);
|
||||
|
||||
const result = await repository.findByBookingUid("booking-uid-123");
|
||||
|
||||
expect(mockPrisma.routingTrace.findFirst).toHaveBeenCalledWith({
|
||||
where: { bookingUid: "booking-uid-123" },
|
||||
});
|
||||
expect(result).toEqual({
|
||||
id: "trace-1",
|
||||
createdAt: new Date("2025-01-01T10:00:00Z"),
|
||||
trace,
|
||||
formResponseId: 123,
|
||||
queuedFormResponseId: null,
|
||||
bookingUid: "booking-uid-123",
|
||||
assignmentReasonId: 42,
|
||||
});
|
||||
});
|
||||
|
||||
it("should return null when routing trace not found", async () => {
|
||||
vi.mocked(mockPrisma.routingTrace.findFirst).mockResolvedValue(null);
|
||||
|
||||
const result = await repository.findByBookingUid("non-existent-uid");
|
||||
|
||||
expect(mockPrisma.routingTrace.findFirst).toHaveBeenCalledWith({
|
||||
where: { bookingUid: "non-existent-uid" },
|
||||
});
|
||||
expect(result).toBeNull();
|
||||
});
|
||||
|
||||
it("should return routing trace with multiple steps", async () => {
|
||||
const trace = [
|
||||
{
|
||||
domain: "routing_form",
|
||||
step: "route_matched",
|
||||
timestamp: 1000,
|
||||
data: { routeId: "route-1", routeName: "Sales" },
|
||||
},
|
||||
{
|
||||
domain: "salesforce",
|
||||
step: "graphql_query_initiated",
|
||||
timestamp: 2000,
|
||||
data: { email: "[email protected]", emailDomain: "acme.com" },
|
||||
},
|
||||
{
|
||||
domain: "salesforce",
|
||||
step: "salesforce_assignment",
|
||||
timestamp: 3000,
|
||||
data: { email: "[email protected]", recordType: "Contact", recordId: "003ABC" },
|
||||
},
|
||||
];
|
||||
|
||||
const mockResult = {
|
||||
id: "trace-2",
|
||||
createdAt: new Date("2025-01-02T10:00:00Z"),
|
||||
trace,
|
||||
formResponseId: 456,
|
||||
queuedFormResponseId: null,
|
||||
bookingUid: "booking-uid-456",
|
||||
assignmentReasonId: 100,
|
||||
};
|
||||
|
||||
vi.mocked(mockPrisma.routingTrace.findFirst).mockResolvedValue(mockResult);
|
||||
|
||||
const result = await repository.findByBookingUid("booking-uid-456");
|
||||
|
||||
expect(result).not.toBeNull();
|
||||
expect(result?.trace).toHaveLength(3);
|
||||
expect(result?.trace[0].domain).toBe("routing_form");
|
||||
expect(result?.trace[1].domain).toBe("salesforce");
|
||||
expect(result?.trace[2].domain).toBe("salesforce");
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -27,4 +27,15 @@ export class PrismaRoutingTraceRepository implements IRoutingTraceRepository {
|
||||
trace: result.trace as RoutingTrace,
|
||||
};
|
||||
}
|
||||
|
||||
async findByBookingUid(bookingUid: string): Promise<RoutingTraceRecord | null> {
|
||||
const result = await this.prisma.routingTrace.findFirst({
|
||||
where: { bookingUid },
|
||||
});
|
||||
if (!result) return null;
|
||||
return {
|
||||
...result,
|
||||
trace: result.trace as RoutingTrace,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -35,4 +35,5 @@ export interface IRoutingTraceRepositoryCreateArgs {
|
||||
|
||||
export interface IRoutingTraceRepository {
|
||||
create(args: IRoutingTraceRepositoryCreateArgs): Promise<RoutingTraceRecord>;
|
||||
findByBookingUid(bookingUid: string): Promise<RoutingTraceRecord | null>;
|
||||
}
|
||||
@@ -4,6 +4,7 @@ import type { AssignmentReasonRepository } from "@calcom/features/assignment-rea
|
||||
import logger from "@calcom/lib/logger";
|
||||
import { AssignmentReasonEnum } from "@calcom/prisma/enums";
|
||||
|
||||
import { ROUTING_TRACE_DOMAINS, ROUTING_TRACE_STEPS } from "../constants";
|
||||
import type { IPendingRoutingTraceRepository } from "../repositories/PendingRoutingTraceRepository.interface";
|
||||
import type {
|
||||
IRoutingTraceRepository,
|
||||
@@ -11,16 +12,6 @@ import type {
|
||||
RoutingTrace,
|
||||
} from "../repositories/RoutingTraceRepository.interface";
|
||||
|
||||
export const ROUTING_TRACE_DOMAINS = {
|
||||
SALESFORCE: "salesforce",
|
||||
ROUTING_FORM: "routing_form",
|
||||
} as const;
|
||||
|
||||
export const ROUTING_TRACE_STEPS = {
|
||||
SALESFORCE_ASSIGNMENT: "salesforce_assignment",
|
||||
ATTRIBUTE_LOGIC_EVALUATED: "attribute-logic-evaluated",
|
||||
} as const;
|
||||
|
||||
interface IRoutingTraceServiceDeps {
|
||||
pendingRoutingTraceRepository: IPendingRoutingTraceRepository;
|
||||
routingTraceRepository: IRoutingTraceRepository;
|
||||
|
||||
Reference in new issue
Block a user