The system runs on two separate n8n workflows with clear single responsibilities.
Fires every time a customer places an order or requests a refund. Processes end-to-end in under 3 seconds.
Node 1 — Event Webhook
Method: POST. Path: /webhook/retainiq-event. Receives customer_email, event type (order_placed or refunded), product_name, and order_value. Configured via Cloudflare Zero Trust tunnel on Oracle Cloud VM.
Node 2 — Validate Inputs
Code node. Confirms customer_email and event are present. Validates event type. Throws a clean error on corrupt payloads.
Node 3 — Fetch Customer
HTTP GET to Supabase customers table filtered by email. Returns full customer record including lifecycle_stage, churn_score, avg_repurchase_days, total_orders, and last_order_date.
Node 4 — Extract Customer Data
Code node. Handles both array and single-object Supabase responses. Merges customer record with event data into a single clean object.
Node 5 — Fetch Order History
HTTP GET to Supabase orders table filtered by customer_id, ordered by created_at descending. Returns all historical orders for metric recalculation.
Node 6 — Calculate Metrics and Stage
Code node. The intelligence core of Workflow 1. Recalculates avg_repurchase_days from full order history, calculates days_since_last_order, determines new lifecycle_stage using multiplier thresholds, calculates new churn_score using stage-specific formula, detects stage transitions, and determines intervention_type.
Node 7 — Update Customer Record
HTTP PATCH to Supabase customers table. Updates lifecycle_stage, churn_score, total_orders, total_spent, avg_repurchase_days, days_since_last_order, and last_order_date in a single write.
Node 8 — Insert Order Record
HTTP POST to Supabase orders table. Logs the new order or refund event.