-
Notifications
You must be signed in to change notification settings - Fork 7
feat(evo-flow): execute Move to Pipeline Stage Journey node (EVO-1272) #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
src/modules/temporal/activities/nodes/evoai/pipeline/move-to-pipeline-stage.node.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import { | ||
| MoveToPipelineStageNode, | ||
| MoveToPipelineStageNodeInput, | ||
| } from './move-to-pipeline-stage.node'; | ||
|
|
||
| describe('MoveToPipelineStageNode', () => { | ||
| let node: MoveToPipelineStageNode; | ||
| let moveToPipelineStage: jest.Mock; | ||
|
|
||
| const baseInput: MoveToPipelineStageNodeInput = { | ||
| nodeId: 'n1', | ||
| conversationId: 'conv-1', | ||
| sessionId: 's1', | ||
| nodeData: { pipeline_id: 'p1', pipeline_stage_id: 'st1' }, | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| node = new MoveToPipelineStageNode(); | ||
| moveToPipelineStage = jest.fn(); | ||
| (node as any).crmService = { moveToPipelineStage }; | ||
| jest | ||
| .spyOn(node as any, 'interpolateNodeData') | ||
| .mockImplementation(async (_input, nodeData) => nodeData); | ||
| }); | ||
|
|
||
| // The mocks below mirror the real CRM `success_response` envelope: | ||
| // executeRequest stores the whole body under `data`, so the move result is | ||
| // nested at `data.data` — the node must unwrap that level (regression guard). | ||
| it('moves the conversation to the target pipeline stage (happy path)', async () => { | ||
| moveToPipelineStage.mockResolvedValue({ | ||
| success: true, | ||
| data: { success: true, data: { moved: true, movement_type: 'cross_pipeline' } }, | ||
| }); | ||
|
|
||
| const result = await node.execute(baseInput); | ||
|
|
||
| expect(moveToPipelineStage).toHaveBeenCalledWith( | ||
| 'p1', | ||
| 'conv-1', | ||
| 'st1', | ||
| 'move-to-pipeline-stage', | ||
| ); | ||
| expect(result.success).toBe(true); | ||
| expect(result.variables).toMatchObject({ | ||
| node_n1_pipeline_moved: true, | ||
| node_n1_pipeline_id: 'p1', | ||
| node_n1_stage_id: 'st1', | ||
| }); | ||
| }); | ||
|
|
||
| it('skips when stage_id is missing', async () => { | ||
| const result = await node.execute({ | ||
| ...baseInput, | ||
| nodeData: { pipeline_id: 'p1' }, | ||
| }); | ||
|
|
||
| expect(moveToPipelineStage).not.toHaveBeenCalled(); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| it('skips when pipeline_id is missing', async () => { | ||
| const result = await node.execute({ | ||
| ...baseInput, | ||
| nodeData: { pipeline_stage_id: 'st1' }, | ||
| }); | ||
|
|
||
| expect(moveToPipelineStage).not.toHaveBeenCalled(); | ||
| expect(result.success).toBe(true); | ||
| }); | ||
|
|
||
| it('surfaces a CRM skip for a deleted target stage as skipped (AC3)', async () => { | ||
| moveToPipelineStage.mockResolvedValue({ | ||
| success: true, | ||
| data: { success: true, data: { moved: false, skipped: true, reason: 'stage_not_found' } }, | ||
| }); | ||
|
|
||
| const result = await node.execute(baseInput); | ||
|
|
||
| expect(moveToPipelineStage).toHaveBeenCalledTimes(1); | ||
| expect(result.success).toBe(true); | ||
| expect(result.variables).toMatchObject({ | ||
| node_n1_pipeline_moved: false, | ||
| }); | ||
| }); | ||
|
|
||
| it('returns an error result when the CRM call fails', async () => { | ||
| moveToPipelineStage.mockResolvedValue({ success: false, error: 'boom' }); | ||
|
|
||
| const result = await node.execute(baseInput); | ||
|
|
||
| expect(result.success).toBe(false); | ||
| }); | ||
| }); |
138 changes: 138 additions & 0 deletions
138
src/modules/temporal/activities/nodes/evoai/pipeline/move-to-pipeline-stage.node.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| import { BaseNode, NodeExecutionResult } from '../../base.node'; | ||
| import { CrmClientService } from '../../../../../../shared/crm-client/crm-client.service'; | ||
|
|
||
| interface MoveResponseData { | ||
| moved?: boolean; | ||
| skipped?: boolean; | ||
| reason?: string; | ||
| movement_type?: string; | ||
| } | ||
|
|
||
| export interface MoveToPipelineStageNodeInput { | ||
| nodeId: string; | ||
| conversationId?: string; | ||
| sessionId: string; | ||
| nodeData: { | ||
| pipeline_id?: string; | ||
| pipelineId?: string; | ||
| stage_id?: string; | ||
| pipeline_stage_id?: string; | ||
| nextNodeId?: string; | ||
| }; | ||
| } | ||
|
|
||
| export class MoveToPipelineStageNode extends BaseNode { | ||
| private crmService: CrmClientService | null = null; | ||
|
|
||
| constructor() { | ||
| super('move-to-pipeline-stage'); | ||
| } | ||
|
|
||
| private getCrmService(): CrmClientService { | ||
| if (!this.crmService) this.crmService = new CrmClientService(); | ||
| return this.crmService; | ||
| } | ||
|
|
||
| async execute( | ||
| input: MoveToPipelineStageNodeInput, | ||
| ): Promise<NodeExecutionResult> { | ||
| return await this.executeWithTiming(input.nodeId, input, async () => { | ||
| const data = await this.interpolateNodeData(input, input.nodeData); | ||
| const pipelineId = data.pipeline_id || data.pipelineId; | ||
| const stageId = data.pipeline_stage_id || data.stage_id; | ||
|
|
||
| if (!stageId) { | ||
| this.logger.warn('No stage_id configured; skipping', { | ||
| nodeId: input.nodeId, | ||
| }); | ||
| return this.skipped('no_stage_id', pipelineId, stageId); | ||
| } | ||
| if (!pipelineId) { | ||
| this.logger.warn('No pipeline_id configured; skipping', { | ||
| nodeId: input.nodeId, | ||
| }); | ||
| return this.skipped('no_pipeline_id', pipelineId, stageId); | ||
| } | ||
| if (!input.conversationId) { | ||
| this.logger.warn('No conversationId available from trigger event', { | ||
| nodeId: input.nodeId, | ||
| }); | ||
| return this.skipped('no_conversation_id', pipelineId, stageId); | ||
| } | ||
|
|
||
| const response = await this.getCrmService().moveToPipelineStage( | ||
| String(pipelineId), | ||
| input.conversationId, | ||
| String(stageId), | ||
| 'move-to-pipeline-stage', | ||
| ); | ||
|
|
||
| if (!response.success) { | ||
| throw new Error( | ||
| `Failed to move conversation to pipeline stage: ${response.error}`, | ||
| ); | ||
| } | ||
|
|
||
| // The CRM wraps payloads in a `success_response` envelope | ||
| // (`{ success, data, meta }`), and executeRequest stores that whole body | ||
| // under `response.data` — so the move result lives at `response.data.data`. | ||
| const envelope = (response.data ?? {}) as { data?: MoveResponseData }; | ||
| const crmData = (envelope.data ?? {}) as MoveResponseData; | ||
|
|
||
| // A deleted/invalid target stage degrades to a logged skip on the CRM | ||
| // side (AC3) — surface it as skipped rather than a successful move. | ||
| if (crmData.skipped) { | ||
| this.logger.warn('CRM skipped the move', { | ||
| nodeId: input.nodeId, | ||
| reason: crmData.reason, | ||
| }); | ||
| return { | ||
| moved: false, | ||
| skipped: true, | ||
| reason: crmData.reason || 'stage_not_found', | ||
| pipelineId, | ||
| stageId, | ||
| conversationId: input.conversationId, | ||
| timestamp: new Date().toISOString(), | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| moved: true, | ||
| movementType: crmData.movement_type, | ||
| pipelineId, | ||
| stageId, | ||
| conversationId: input.conversationId, | ||
| timestamp: new Date().toISOString(), | ||
| crmResponse: crmData, | ||
| }; | ||
| }) | ||
| .then(({ result, executionTime }) => { | ||
| return this.createSuccessResult(input, executionTime, { | ||
| [`node_${input.nodeId}_pipeline_moved`]: result.moved, | ||
| [`node_${input.nodeId}_pipeline_id`]: result.pipelineId, | ||
| [`node_${input.nodeId}_stage_id`]: result.stageId, | ||
| }); | ||
| }) | ||
| .catch((error) => { | ||
| const executionTime = Date.now(); | ||
| this.logger.error('Failed to move conversation to pipeline stage', { | ||
| conversationId: input.conversationId, | ||
| nodeId: input.nodeId, | ||
| error: error.message, | ||
| }); | ||
| return this.createErrorResult(error, executionTime); | ||
| }); | ||
| } | ||
|
|
||
| private skipped(reason: string, pipelineId?: string, stageId?: string) { | ||
| return { | ||
| moved: false, | ||
| skipped: true, | ||
| reason, | ||
| pipelineId, | ||
| stageId, | ||
| timestamp: new Date().toISOString(), | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (bug_risk): Error path uses
Date.now()forexecutionTime, which likely diverges from the timing semantics used in the success path.The success path gets
executionTimeas a duration fromexecuteWithTiming, but the error path usesDate.now()and passes that intocreateErrorResult. IfexecutionTimeis meant to be a duration, this will distort error metrics and make them inconsistent with success timings. Please either route errors throughexecuteWithTiming(if possible) or measure a duration around the call (e.g.const start = Date.now(); ... catch { const executionTime = Date.now() - start; }) so both paths use the same timing semantics.