Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/modules/temporal/activities/nodes/conditional.node.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,55 @@ describe('ConditionalNode — {{contact.customAttributes.*}}', () => {
expect((result as any).nextNodeHandle).toBe('p1');
});

it('skips the CRM contact round-trip when conditions only read session variables', async () => {
const loadSpy = jest.spyOn(node as any, 'loadContactData');
jest
.spyOn(node as any, 'loadSessionVariables')
.mockResolvedValue({ tier: 'gold' });

const variableOnlyInput: ConditionalNodeInput = {
nodeId: 'n1',
contactId: 'c1',
sessionId: 's1',
nodeData: {
paths: [
{
id: 'p1',
name: 'Variable path',
conditions: [
{
id: 'cond-1',
type: 'custom',
field: '{{tier}}',
operator: 'equals',
value: 'gold',
},
],
logicalOperator: 'AND',
},
],
},
};

const result = await node.execute(variableOnlyInput);

expect(loadSpy).not.toHaveBeenCalled();
expect((result as any).nextNodeHandle).toBe('p1');
});

it('still loads the contact for a type:contact condition selected by name only', async () => {
const loadSpy = jest.spyOn(node as any, 'loadContactData');

// `field` is a bare contact attribute name (no {{contact.*}} wrapper), so
// gating must fall back to `type: 'contact'` to know it needs the contact.
const result = await node.execute(
inputWith('email', 'equals', 'a@b.com', 'contact'),
);

expect(loadSpy).toHaveBeenCalled();
expect((result as any).nextNodeHandle).toBe('p1');
});

it('routing: contact-attribute field on a non-contact-typed condition still matches', async () => {
// Proves the pre-switch {{contact.*}} handling — not the `type` switch —
// drives contact resolution. The picker is shared across condition types,
Expand Down
19 changes: 17 additions & 2 deletions src/modules/temporal/activities/nodes/conditional.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,23 @@ export class ConditionalNode extends BaseNode {
throw new Error('No conditional paths configured');
}

// Load contact data for evaluation
const contactData = await this.loadContactData(input.contactId);
// Load contact data only when a condition actually needs the contact —
// either a {{contact.*}} field (resolved by field pattern regardless of
// type, mirroring evaluateCondition) or a `type: 'contact'` condition.
// Avoids a CRM round-trip on Conditional nodes that only read session
// variables. Empty object when out of scope: resolveContactField then
// returns undefined for every field, the correct "no match" signal.
const needsContactData = paths.some((path) =>
(path.conditions || []).some(
(condition) =>
condition?.type === 'contact' ||
(typeof condition?.field === 'string' &&
CONTACT_FIELD_PATTERN.test(condition.field)),
),
);
const contactData = needsContactData
? await this.loadContactData(input.contactId)
: {};

// Load conversation data only when a path actually references a
// {{conversation.*}} field — avoids a CRM round-trip on every Conditional
Expand Down
Loading