feat: Add CrmRoutingTraceService and SalesforceRoutingTraceService (#27318)

* Add DB schema

* Init zod schema

* Init RoutingTrace and PendingRoutingTrace repository interfaces

* Create PrismaPendingRoutingTraceRepository

* Init RoutingTraceService

* Create RoutingTraceService container

* User routing trace service in routing

* Create RoutingTraceRepository and PrismaRoutingTraceRepoistory

* Add findByFormResponseId and findByQueuedFormResponseId to PendingRoutingTraceRepository

* Update DI containers

* RoutingTraceService create process booking method

* Use pending routing trace rather than URL params

* Fix schema

* Fix writing assignment reason for routed booking

* Remove  from  service

* Refactor RoutingTraceService to not rely on async local storage

* Pass RoutingTraceService through routing call

* Add attribute-logic-evaluated to routing trace step

* Add routing trace to trpc endpoint

* Add CRM routing trace step

* Fix extracting routing trace to assignment reason

* Add back CRM params to prevent refetching

* test: Add unit tests for RoutingTraceService and repositories

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

* test: Add missing mock for RoutingTraceService in getRoutedUrl tests

Also fix pre-existing lint issues in the test file:
- Add explicit types to mockForm and mockSerializableForm variables
- Add explicit type to url parameter in mockContext
- Replace 'as any' with 'as unknown as InstanceType<typeof UserRepository>'

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

* Link pending form submission to routing trace

* Clean up

* Add lookup field assignment reasons

* Rename to PendingRoutingTrace

* Add migration file

* fix: Update RoutingTraceService tests to use assignmentReasonRepository mock

- Add getStepsCount() method back to RoutingTraceService
- Add queuedFormResponseId support to processForBooking method
- Update tests to use mockAssignmentReasonRepository instead of prisma mock
- Remove test for missing routingTraceRepository (all deps now required)

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

* fix: Remove PII (organizer email) from log payload in RoutingTraceService

Addresses Cubic AI review feedback with confidence 9/10.
Logging PII violates sensitive information logging rules.

Co-Authored-By: unknown <>

* Write field values at the time of routing

* Write attributes used to route

* feat: add CRM routing trace service

Add CrmRoutingTraceService as a reusable wrapper around RoutingTraceService
for CRM-specific tracing. Also adds CrmRoutingTraceServiceInterface type
to support passing trace services through the CRM call chain.

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

* feat: add Salesforce routing trace infrastructure

Add SalesforceRoutingTrace static class with 19 trace methods covering:
- Account resolution (SOQL path): searching by website, contact domain
- Lookup field queries
- Owner lookups (contact, lead, account)
- Validation and skip scenarios
- GraphQL three-tier resolution

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

* feat: wire CRM trace service through call chain

Pass crmTrace parameter through the CRM call chain:
- routerGetCrmContactOwnerEmail creates CrmRoutingTraceService
- Passes to app booking form handlers and CRM round robin skip
- CrmManager.getContacts accepts and forwards crmTrace
- All handlers accept optional crmTrace parameter

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

* feat: add trace instrumentation to Salesforce CRM service

Instrument Salesforce CRM methods with routing trace steps:
- getContacts: trace owner lookups for contact/lead/account
- getAccountIdBasedOnEmailDomainOfContacts: trace website and domain searches
- findUserEmailFromLookupField: trace lookup field queries
- GetAccountRecordsForRRSkip (GraphQL): trace three-tier resolution

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

* refactor: rename SalesforceRoutingTrace to SalesforceRoutingTraceService

Consistent naming with CrmRoutingTraceService.

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

* test: add unit tests for CrmRoutingTraceService and SalesforceRoutingTraceService

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

* refactor: use AsyncLocalStorage for CRM routing trace

Replace explicit crmTrace parameter passing with AsyncLocalStorage:

- Add AsyncLocalStorage to RoutingTraceService with getCurrent() and runAsync()
- Update SalesforceRoutingTraceService to auto-resolve trace from AsyncLocalStorage
- Remove CrmRoutingTraceService wrapper (no longer needed)
- Remove crmTrace parameter from all CRM method signatures
- Wrap CRM operations in routingTraceService.runAsync() context

This is cleaner than threading crmTrace through 5+ function layers.
The trace context is available within the withReporting wrapper.

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

* Use async local storage for `CrmRoutingTraceService`

* fix: correct template literal syntax in RoutingTraceService

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

* fix: use narrowed eventTypeId variable in nested async function

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

* fix: update SalesforceRoutingTraceService tests to use AsyncLocalStorage API

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

* refactor: rename crmTrace to crmRoutingTraceService for consistency

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: Volnei Munhoz <[email protected]>
This commit is contained in:
authored and GitHub committed 2026-01-28 17:28:35 -03:00
1 parent f536100224
commit 034fbd63f1
9 files changed
+1052 -41

No files matched your search

@@ -0,0 +1,95 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { CrmRoutingTraceService } from "./CrmRoutingTraceService";
import type { RoutingTraceService } from "./RoutingTraceService";
describe("CrmRoutingTraceService", () => {
let mockParentTraceService: Pick<RoutingTraceService, "addStep">;
function createMockRoutingTraceService(): Pick<RoutingTraceService, "addStep"> {
return {
addStep: vi.fn(),
};
}
beforeEach(() => {
vi.clearAllMocks();
mockParentTraceService = createMockRoutingTraceService();
});
describe("create", () => {
it("should return undefined when parent is undefined", () => {
const result = CrmRoutingTraceService.create(undefined);
expect(result).toBeUndefined();
});
it("should return a CrmRoutingTraceService instance when parent is provided", () => {
const result = CrmRoutingTraceService.create(mockParentTraceService as RoutingTraceService);
expect(result).toBeInstanceOf(CrmRoutingTraceService);
});
});
describe("addStep", () => {
it("should delegate to parent trace service with correct parameters", () => {
const crmTrace = new CrmRoutingTraceService(mockParentTraceService as RoutingTraceService);
crmTrace.addStep("salesforce", "account_lookup", { accountId: "123" });
expect(mockParentTraceService.addStep).toHaveBeenCalledWith({
domain: "salesforce",
step: "account_lookup",
data: { accountId: "123" },
});
});
it("should use empty object as default data when not provided", () => {
const crmTrace = new CrmRoutingTraceService(mockParentTraceService as RoutingTraceService);
crmTrace.addStep("hubspot", "contact_search");
expect(mockParentTraceService.addStep).toHaveBeenCalledWith({
domain: "hubspot",
step: "contact_search",
data: {},
});
});
it("should pass complex data objects correctly", () => {
const crmTrace = new CrmRoutingTraceService(mockParentTraceService as RoutingTraceService);
const complexData = {
email: "[email protected]",
recordType: "Contact",
recordId: "003ABC123",
nested: { key: "value" },
};
crmTrace.addStep("salesforce", "contact_owner_lookup", complexData);
expect(mockParentTraceService.addStep).toHaveBeenCalledWith({
domain: "salesforce",
step: "contact_owner_lookup",
data: complexData,
});
});
it("should allow multiple steps to be added", () => {
const crmTrace = new CrmRoutingTraceService(mockParentTraceService as RoutingTraceService);
crmTrace.addStep("salesforce", "step1", { data: 1 });
crmTrace.addStep("salesforce", "step2", { data: 2 });
crmTrace.addStep("salesforce", "step3", { data: 3 });
expect(mockParentTraceService.addStep).toHaveBeenCalledTimes(3);
});
});
describe("constructor", () => {
it("should create instance with parent trace service", () => {
const crmTrace = new CrmRoutingTraceService(mockParentTraceService as RoutingTraceService);
expect(crmTrace).toBeInstanceOf(CrmRoutingTraceService);
});
});
});
@@ -0,0 +1,46 @@
import { AsyncLocalStorage } from "node:async_hooks";
import type { RoutingTraceService } from "./RoutingTraceService";
/**
* CRM-specific wrapper around RoutingTraceService.
* Uses AsyncLocalStorage to make the trace service available to CRM-specific
* trace services (e.g., SalesforceRoutingTraceService) without explicit parameter passing.
*/
export class CrmRoutingTraceService {
private static als = new AsyncLocalStorage<CrmRoutingTraceService>();
constructor(private parentTraceService: RoutingTraceService) {}
/**
* Get the current CrmRoutingTraceService from AsyncLocalStorage.
* Returns undefined if not within a CRM trace context.
*/
static getCurrent(): CrmRoutingTraceService | undefined {
return CrmRoutingTraceService.als.getStore();
}
/**
* Factory method to create a CrmRoutingTraceService if parent exists.
* Returns undefined if no parent trace service is provided.
*/
static create(parent: RoutingTraceService | undefined): CrmRoutingTraceService | undefined {
if (!parent) return undefined;
return new CrmRoutingTraceService(parent);
}
/**
* Run an async function within this CRM trace service's context.
* Any code within the callback can access this trace service via getCurrent().
*/
runAsync<T>(fn: () => Promise<T>): Promise<T> {
return CrmRoutingTraceService.als.run(this, fn);
}
/**
* Add a trace step to the parent service.
*/
addStep(domain: string, step: string, data: Record<string, unknown> = {}): void {
this.parentTraceService.addStep({ domain, step, data });
}
}
@@ -1,3 +1,5 @@
import { AsyncLocalStorage } from "node:async_hooks";
import type { AssignmentReasonRepository } from "@calcom/features/assignment-reason/repositories/AssignmentReasonRepository";
import logger from "@calcom/lib/logger";
import { AssignmentReasonEnum } from "@calcom/prisma/enums";
@@ -33,6 +35,32 @@ export interface ProcessRoutingTraceResult {
}
export class RoutingTraceService {
private static als = new AsyncLocalStorage<RoutingTraceService>();
/**
* Get the current RoutingTraceService from AsyncLocalStorage.
* Returns undefined if not within a trace context.
*/
static getCurrent(): RoutingTraceService | undefined {
return this.als.getStore();
}
/**
* Run a function within this trace service's context.
* Any code within the callback can access this trace service via getCurrent().
*/
run<T>(fn: () => T): T {
return RoutingTraceService.als.run(this, fn);
}
/**
* Run an async function within this trace service's context.
* Any code within the callback can access this trace service via getCurrent().
*/
runAsync<T>(fn: () => Promise<T>): Promise<T> {
return RoutingTraceService.als.run(this, fn);
}
private routingTraceSteps: RoutingStep[] = [];
constructor(private readonly deps: IRoutingTraceServiceDeps) {}
@@ -80,7 +108,6 @@ export class RoutingTraceService {
}): Promise<ProcessRoutingTraceResult | null> {
const { formResponseId, queuedFormResponseId, bookingId, bookingUid, organizerEmail, isRerouting, reroutedByEmail } = args;
// 1. Look up pending trace by formResponseId or queuedFormResponseId
let pendingTrace = null;
if (formResponseId) {
pendingTrace = await this.deps.pendingRoutingTraceRepository.findByFormResponseId(formResponseId);
@@ -93,7 +120,6 @@ export class RoutingTraceService {
return null;
}
// 2. Extract assignment reason from trace
const assignmentReasonData = this.extractAssignmentReasonFromTrace(
pendingTrace.trace,
{
@@ -105,7 +131,6 @@ export class RoutingTraceService {
let assignmentReasonId: number | undefined;
// 3. Create assignment reason record if we have trace-based data
if (assignmentReasonData) {
const createdReason = await this.deps.assignmentReasonRepository.createAssignmentReason({
bookingId,
@@ -115,7 +140,6 @@ export class RoutingTraceService {
assignmentReasonId = createdReason.id;
}
// 4. Create permanent routing trace
await this.deps.routingTraceRepository.create({
trace: pendingTrace.trace,
formResponseId: pendingTrace.formResponseId ?? undefined,
@@ -168,7 +192,6 @@ export class RoutingTraceService {
rrSKipToAccountLookupFieldName?: string | null;
};
// Only use CRM assignment if the booking organizer matches the contact owner
if (
email &&
email.toLowerCase() === context.organizerEmail.toLowerCase()
@@ -177,7 +200,7 @@ export class RoutingTraceService {
reasonEnum: AssignmentReasonEnum.SALESFORCE_ASSIGNMENT,
reasonString: this.buildSalesforceReasonString({
email,
recordType: recordType ?? "Contact", // Default to Contact if not specified
recordType: recordType ?? "Contact",
recordId,
rrSkipToAccountLookupField,
rrSKipToAccountLookupFieldName,
@@ -236,7 +259,6 @@ export class RoutingTraceService {
.join(", ")
: "";
// Combine both parts, matching main branch behavior
return `${reroutingPart} ${attributesPart}`.trim();
}