V4 - saveMicroactionTool - Warenga¶
A unified microaction management workflow that handles the complete lifecycle of user microactions in the Warenga coaching system, including outcome recording, status transitions, and new microaction creation through a single database interface.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
How It Works¶
This workflow processes microaction data through a three-branch logic system:
- Receive Input Parameters - The workflow accepts phone number, action type, outcome data, and new microaction details
- Build Dynamic SQL - JavaScript code analyzes the input to determine which database operations are needed based on three scenarios:
- Complete cycle: Record outcome AND create new microaction
- Outcome only: Update existing microaction with results
- New microaction only: Replace current microaction with a new one
- Execute Database Operations - The generated SQL statements are executed against the PostgreSQL database
- Return Results - Success status and any generated history ID are returned to the calling workflow
The workflow intelligently handles SQL injection prevention by escaping single quotes and manages JSON data serialization for the outcome_data field.
Workflow Diagram¶
graph TD
A[When Executed by Another Workflow] --> B[Build SQL]
B --> C[Execute SQL]
C --> D[Return Result]
Trigger¶
Execute Workflow Trigger - This workflow is designed to be called by other workflows in the system, not triggered directly by external events.
Input Parameters:
- phoneNumber - User's phone number identifier
- type - Action type (credit/profit)
- outcome_step - Step identifier for completed actions
- outcome_data - JSON string containing outcome details and points
- new_micro_action - Text of new microaction to create
- new_micro_action_type - Category of the new microaction
Nodes Used¶
| Node Type | Node Name | Purpose |
|---|---|---|
| Execute Workflow Trigger | When Executed by Another Workflow | Receives input parameters from calling workflows |
| Code (JavaScript) | Build SQL | Constructs dynamic SQL statements based on input logic |
| PostgreSQL | Execute SQL | Executes the generated SQL against the database |
| Code (JavaScript) | Return Result | Formats the response with success status and history ID |
External Services & Credentials Required¶
PostgreSQL Database:
- Credential: sifaV4Dev
- Database: Contains v4_microaction_history table
- Required permissions: SELECT, UPDATE, INSERT on microaction history table
Environment Variables¶
No environment variables are used in this workflow. All configuration is handled through n8n credentials and input parameters.
Data Flow¶
Input:
1 2 3 4 5 6 7 8 | |
Output:
1 2 3 4 | |
Database Operations: - Updates existing microactions with outcomes and point awards - Creates new microaction records with 'active' status - Manages status transitions ('active' → 'done' or 'superseded') - Tracks resolution timestamps and source attribution
Error Handling¶
The workflow includes basic error handling: - JSON parsing errors in outcome_data default to empty object - SQL injection prevention through quote escaping - Database errors will cause workflow failure and should be handled by calling workflows
Known Limitations¶
- No input validation on phone number format
- Limited error reporting - database errors are not caught or transformed
- No rollback mechanism for multi-statement transactions
- Assumes single active microaction per phone number
Related Workflows¶
This workflow is designed to be called by other coaching workflows in the system, particularly those handling: - Evening coaching sessions - Credit/profit outcome processing (steps C5, C7, P4, P5) - Microaction lifecycle management
Setup Instructions¶
-
Import the Workflow
- Import the JSON into your n8n instance
- Ensure it's placed in the "Sifa V4" project
-
Configure Database Credentials
- Create PostgreSQL credential named
sifaV4Dev - Configure connection to database containing
v4_microaction_historytable - Test connection and verify table access
- Create PostgreSQL credential named
-
Database Schema Requirements
- Ensure
v4_microaction_historytable exists with required columns:phonenumber,micro_action,micro_action_type,source,statusoutcome_step,outcome_data(JSONB),points_awarded,resolved_at
- Ensure
-
Test the Workflow
- Create a test workflow that calls this one with sample data
- Verify database updates occur correctly for all three logic branches
- Confirm proper status transitions and data integrity
-
Integration
- Update calling workflows to use the correct workflow ID
- Ensure input parameter names match exactly
- Handle the returned success/failure responses appropriately