Skip to content

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:

  1. Receive Input Parameters - The workflow accepts phone number, action type, outcome data, and new microaction details
  2. 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
  3. Execute Database Operations - The generated SQL statements are executed against the PostgreSQL database
  4. 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
{
  "phoneNumber": "string",
  "type": "credit|profit",
  "outcome_step": "string (optional)",
  "outcome_data": "JSON string (optional)",
  "new_micro_action": "string (optional)",
  "new_micro_action_type": "string (optional)"
}

Output:

1
2
3
4
{
  "success": true,
  "history_id": "number|null"
}

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

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

  1. Import the Workflow

    • Import the JSON into your n8n instance
    • Ensure it's placed in the "Sifa V4" project
  2. Configure Database Credentials

    • Create PostgreSQL credential named sifaV4Dev
    • Configure connection to database containing v4_microaction_history table
    • Test connection and verify table access
  3. Database Schema Requirements

    • Ensure v4_microaction_history table exists with required columns:
      • phonenumber, micro_action, micro_action_type, source, status
      • outcome_step, outcome_data (JSONB), points_awarded, resolved_at
  4. 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
  5. Integration

    • Update calling workflows to use the correct workflow ID
    • Ensure input parameter names match exactly
    • Handle the returned success/failure responses appropriately