persistStateTool - Francis¶
A utility workflow that updates user state information in the youth entrepreneurs database. This workflow accepts various user parameters and persists them to a PostgreSQL database, providing a centralized way to maintain user progress and configuration data.
Purpose¶
No business context provided yet — add a context.md to enrich this documentation.
This workflow serves as a state persistence service for youth entrepreneur users, allowing other workflows to update user information such as current stage, progress tracking, business metrics, and engagement status in a consistent manner.
How It Works¶
- Receives Input Parameters: The workflow accepts 15 different parameters including phone number, current stage, progress metrics, and business data
- Updates Database: Executes a PostgreSQL UPDATE query that only modifies fields where new values are provided (using COALESCE to preserve existing values)
- Calculates Derived Fields: Automatically computes daily fixed costs from monthly costs and business days
- Returns Confirmation: Provides a formatted output message confirming the state update with key user information
Workflow Diagram¶
graph TD
A[When Executed by Another Workflow] --> B[updateUserState]
B --> C[setOutput]
B -.-> D[(PostgreSQL Database<br/>youthEntrepreneursReal)]
Trigger¶
Execute Workflow Trigger: This workflow is designed to be called by other workflows, not triggered directly by external events. It accepts the following input parameters:
phoneNumber(string) - User identifiercurrentStage(string) - Current program stagecurrentDay(number) - Progress day counterroutingDecision(string) - Routing logic decisionconsecutiveMissedDays(number) - Missed days counteronboardingStatus(string) - Onboarding progressinteractionFrequency(string) - Communication frequencypreferredTime(string) - Preferred contact timeselfInitiated(boolean) - Self-initiated flagmonthlyFixedCosts(number) - Monthly business costsbusinessDaysPerMonth(number) - Working days per monthstockType(string) - Type of stock/inventorydurableStockItems(string) - Durable stock items datapracticeBaselineAvg(number) - Practice baseline averageceaEngagementStatus(string) - CEA engagement status
Nodes Used¶
| Node Type | Node Name | Purpose |
|---|---|---|
| Execute Workflow Trigger | When Executed by Another Workflow | Receives input parameters from calling workflows |
| PostgreSQL | updateUserState | Updates user state in the database using parameterized query |
| Set | setOutput | Formats confirmation message with updated user information |
External Services & Credentials Required¶
PostgreSQL Database¶
- Credential Name:
PostgresOnSupabase - Purpose: Connects to the youth entrepreneurs database to update user state
- Required Permissions: UPDATE access on
youthEntrepreneursRealtable
Environment Variables¶
No environment variables are used in this workflow. All configuration is handled through the PostgreSQL credentials.
Data Flow¶
Input¶
The workflow accepts up to 15 optional parameters, with phoneNumber being the key identifier for locating the user record.
Processing¶
- Uses COALESCE in SQL to only update fields where new values are provided
- Automatically calculates
dailyFixedCostwhen bothmonthlyFixedCostsandbusinessDaysPerMonthare provided - Handles JSON data type conversion for
durableStockItems
Output¶
Returns a formatted string containing: - Phone number - Updated current stage - Updated current day - Updated onboarding status
Example output: "State updated: +1234567890 | stage: practice | day: 15 | onboarding: completed"
Error Handling¶
The workflow uses basic PostgreSQL error handling: - Database connection failures will stop execution - Invalid data types will cause SQL errors - Missing phone numbers will result in no rows updated
No custom error handling or retry logic is implemented.
Known Limitations¶
- No validation of input parameters before database update
- No rollback mechanism if partial updates fail
- Limited error reporting - only basic SQL errors are surfaced
- Assumes phone number exists in database (no upsert functionality)
Related Workflows¶
This workflow is designed to be called by other workflows in the youth entrepreneur program system. No specific related workflows are identified in the current documentation.
Setup Instructions¶
-
Import Workflow
- Import the workflow JSON into your n8n instance
- The workflow will be inactive by default
-
Configure Database Credentials
- Create a PostgreSQL credential named
PostgresOnSupabase - Configure connection to your youth entrepreneurs database
- Ensure the credential has UPDATE permissions on
youthEntrepreneursRealtable
- Create a PostgreSQL credential named
-
Verify Database Schema
- Ensure the
youthEntrepreneursRealtable exists with all required columns - Verify column data types match the workflow expectations
- Ensure the
-
Test the Workflow
- Create a test workflow that calls this one with sample data
- Verify database updates are working correctly
- Check output formatting
-
Activate and Use
- Activate the workflow once testing is complete
- Reference this workflow ID in other workflows that need to persist user state