Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.
TypeScript ai-agentsbill-of-materialserpinventory-managementleanmanufacturingmanufacturing-execution-systemmcp-servermesmrpopen-source-erppostgresqlproduction-planningqmsquality-management-systemreact-routerself-hostedsupabasetraceabilitytypescript
Add Event System documentation and workflows
Sidwebworks53ed0da040fd
diff --git a/llm/cache/event-system.md b/llm/cache/event-system.mdnew file mode 100644index 0000000000..063ef4077f--- /dev/null+++ b/llm/cache/event-system.md@@ -0,0 +1,278 @@+# Event System++## Overview++The Event System is Carbon's async event processing infrastructure built on PostgreSQL triggers, PGMQ (PostgreSQL Message Queue), and Trigger.dev. It enables reactive workflows triggered by database changes without blocking write operations.++## Architecture++```+Database Change → Statement-Level Trigger → dispatch_event_batch() → PGMQ Queue+ ↓+ event-queue (cron, every 1 min) → reads batch from PGMQ+ ↓+ Routes to handler tasks:+ - event-handler-webhook+ - event-handler-workflow+ - event-handler-sync+ - event-handler-search+```++## Key Components++### 1. Database Tables++#### `eventSystemSubscription`++Stores subscription configurations that determine which events trigger which handlers.++```sql+CREATE TABLE "eventSystemSubscription" (+ "id" TEXT NOT NULL DEFAULT id(),+ "name" TEXT NOT NULL, -- Unique name per company+ "table" TEXT NOT NULL, -- Table to watch (e.g., 'customer')+ "companyId" TEXT NOT NULL, -- Multi-tenant isolation+ "operations" TEXT[] NOT NULL, -- ['INSERT', 'UPDATE', 'DELETE']+ "handlerType" TEXT NOT NULL, -- 'WEBHOOK' | 'WORKFLOW' | 'SYNC' | 'SEARCH'+ "config" JSONB DEFAULT '{}', -- Handler-specific config (URL, workflowId, etc.)+ "filter" JSONB DEFAULT '{}', -- Row-level filtering (e.g., { "status": "paid" })+ "active" BOOLEAN DEFAULT TRUE,+ "createdAt" TIMESTAMP WITH TIME ZONE DEFAULT NOW(),+ "updatedAt" TIMESTAMP WITH TIME ZONE,++ CONSTRAINT "unique_subscription_name_per_company" UNIQUE ("name", "companyId")+);+```++### 2. Trigger Functions++#### `dispatch_event_batch()` (Statement-Level, AFTER)++- Fires once per statement (not per row)+- Checks if any active subscriptions exist for the table/company/operation+- If subscriptions exist, batches all affected rows and sends to PGMQ+- Uses transition tables (`batched_new`, `batched_old`) for efficient batch access++#### `dispatch_event_interceptors()` (Row-Level, BEFORE)++- For synchronous interceptors that must run before the operation completes+- Executes a list of functions passed as trigger arguments+- Used for data integrity requirements (not for async processing)++#### `attach_event_trigger(table_name, sync_functions[])`++Helper function to attach event triggers to a table:++```sql+SELECT attach_event_trigger('customer', ARRAY[]::TEXT[]); -- Async only+SELECT attach_event_trigger('job', ARRAY['validate_job_status']::TEXT[]); -- With sync interceptor+```++### 3. PGMQ Queue++Queue name: `event_system`++Message format:++```typescript+{+ subscriptionId: string;+ triggerType: "ROW" | "STATEMENT";+ handlerType: "WEBHOOK" | "WORKFLOW" | "SYNC" | "SEARCH";+ handlerConfig: Record<string, any>;+ companyId: string;+ event: {+ table: string;+ operation: "INSERT" | "UPDATE" | "DELETE";+ recordId: string;+ new: Record<string, any> | null;+ old: Record<string, any> | null;+ timestamp: string;+ }+}+```++### 4. Trigger.dev Tasks++#### `event-queue` (Cron Task)++- **Location**: `packages/jobs/trigger/event/queue.ts`+- **Schedule**: Every 1 minute (`* * * * *`)+- **Purpose**: Polls PGMQ, routes events to appropriate handler tasks++#### `event-handler-webhook`++- **Location**: `packages/jobs/trigger/event/webhook.ts`+- **Purpose**: Sends HTTP POST to configured URL+- **Config**: `{ url: string, headers?: Record<string, string> }`++#### `event-handler-workflow`++- **Location**: `packages/jobs/trigger/event/workflow.ts`+- **Purpose**: Dispatches to internal workflow engine+- **Config**: `{ workflowId: string }`++#### `event-handler-sync`++- **Location**: `packages/jobs/trigger/event/sync.ts`+- **Purpose**: Syncs data to external accounting systems (Xero, etc.)+- **Config**: `{ provider: "xero" | "quickbooks" }`++#### `event-handler-search`++- **Location**: `packages/jobs/trigger/event/search.ts`+- **Purpose**: Updates company-specific search indexes asynchronously+- **Config**: `{}` (no config needed)++## Handler Types++| Handler | Use Case | Config |+| ---------- | ------------------------------------ | ------------------ |+| `WEBHOOK` | External integrations, notifications | `{ url, headers }` |+| `WORKFLOW` | Internal automation workflows | `{ workflowId }` |+| `SYNC` | Accounting system sync (Xero, QB) | `{ provider }` |+| `SEARCH` | Search index updates | `{}` |++## TypeScript API++### Location++`packages/database/src/event.ts`++### Creating Subscriptions++```typescript+import { getCarbonServiceRole } from "@carbon/auth";+import { createEventSystemSubscription } from "@carbon/database/event";++const client = getCarbonServiceRole();++// Create a webhook subscription+const subscription = await createEventSystemSubscription(client, {+ name: "order-notifications",+ table: "salesOrder",+ companyId: "company-123",+ operations: ["INSERT", "UPDATE"],+ type: "WEBHOOK",+ config: {+ url: "https://example.com/webhook",+ headers: { "X-API-Key": "secret" },+ },+ filter: { status: "confirmed" }, // Only trigger for confirmed orders+ active: true,+});+```++### Deleting Subscriptions++```typescript+import {+ deleteEventSystemSubscription,+ deleteEventSystemSubscriptionsByName,+} from "@carbon/database/event";++// Delete by ID+await deleteEventSystemSubscription(client, "subscription-id");++// Delete by name (for a specific company)+await deleteEventSystemSubscriptionsByName(+ client,+ "company-123",+ "order-notifications"+);+```++## RPC Functions++The event system provides these PostgreSQL RPC functions:++```sql+-- Create/update subscription+create_event_system_subscription(+ p_name, p_table, p_company_id, p_operations,+ p_handler_type, p_config, p_filter, p_active+) RETURNS TABLE (id, name, handlerType, table)++-- Delete by ID+delete_event_system_subscription(p_subscription_id) RETURNS VOID++-- Delete by name + company+delete_event_system_subscriptions_by_name(p_company_id, p_name) RETURNS VOID++-- Search index helpers+delete_from_search_index(p_company_id, p_entity_type, p_entity_id) RETURNS VOID+upsert_to_search_index(p_company_id, p_entity_type, p_entity_id, ...) RETURNS VOID+```++## Tables with Event Triggers Attached++The following tables have async event triggers attached (as of the search migration):++- `employee`+- `customer`+- `supplier`+- `item`+- `job`+- `purchaseOrder`+- `salesInvoice`+- `purchaseInvoice`+- `nonConformance`+- `gauge`+- `quote`+- `salesRfq`+- `salesOrder`+- `supplierQuote`++## Performance Considerations++### Benefits++- **Non-blocking writes**: Database operations complete immediately+- **Batch processing**: Statement-level triggers handle bulk operations efficiently+- **Automatic retries**: Trigger.dev handles failures with exponential backoff+- **Eventual consistency**: Acceptable for search, notifications, external syncs++### Costs++- **Cron cost**: ~$1.41/month for idle 1-minute cron+- **Processing cost**: Varies based on event volume and handler complexity+- **Latency**: Up to 1 minute delay for async processing++### When NOT to Use++- Data integrity requirements (use sync interceptors instead)+- Real-time requirements (< 1 second latency)+- Transactional consistency requirements++## Debugging++### Check Subscriptions++```sql+SELECT * FROM "eventSystemSubscription"+WHERE "companyId" = 'your-company-id' AND "active" = TRUE;+```++### Check Queue Status++```sql+SELECT * FROM pgmq.metrics('event_system');+SELECT * FROM pgmq.read('event_system', 30, 10); -- Peek at messages+```++### View Trigger Status++```sql+SELECT * FROM "eventSystemTrigger"; -- View showing attached triggers+```++## Migration History++- `20260116215036_event_system_impl.sql` - Initial event system implementation+- `20260204070000_remove-event-system-batch-size.sql` - Removed unused batchSize column+- `20260204080000_async-search-triggers.sql` - Migrated search to async, added SEARCH handler++## Related Documentation++- [Accounting Sync Handlers](./accounting-sync-handlers.md) - SYNC handler details+- [Database Migration Patterns](./database-migration-patterns.md) - Migration conventionsdiff --git a/llm/workflows/event-system.md b/llm/workflows/event-system.mdnew file mode 100644index 0000000000..485ee78456--- /dev/null+++ b/llm/workflows/event-system.md@@ -0,0 +1,356 @@+# Event System Workflow++This workflow describes how to use Carbon's Event System to create reactive workflows triggered by database changes.++## Prerequisites++- Understand the event system architecture (see `llm/cache/event-system.md`)+- Have access to the Supabase client (`getCarbonServiceRole()`)+- Know which table you want to watch and what handler type to use++## Use Cases++| Use Case | Handler Type | Example |+| ---------------------- | ------------ | ----------------------------------------- |+| Notify external system | `WEBHOOK` | Send Slack message when order created |+| Sync to accounting | `SYNC` | Push new customer to Xero |+| Internal automation | `WORKFLOW` | Auto-assign tasks when job status changes |+| Update search index | `SEARCH` | Re-index customer after update |++---++## Workflow A: Create a Webhook Subscription++### Step 1: Ensure Event Trigger is Attached to Table++Check if the table already has an event trigger:++```sql+SELECT * FROM "eventSystemTrigger" WHERE "table" = 'yourTable';+```++If not attached, create a migration to attach it:++```sql+SELECT attach_event_trigger('yourTable', ARRAY[]::TEXT[]);+```++### Step 2: Create the Subscription++```typescript+import { getCarbonServiceRole } from "@carbon/auth";+import { createEventSystemSubscription } from "@carbon/database/event";++const client = getCarbonServiceRole();++const subscription = await createEventSystemSubscription(client, {+ name: "my-webhook-subscription", // Unique per company+ table: "salesOrder", // Table to watch+ companyId: companyId, // Your company ID+ operations: ["INSERT", "UPDATE"], // Which operations to trigger on+ type: "WEBHOOK",+ config: {+ url: "https://your-webhook-endpoint.com/webhook",+ headers: {+ Authorization: "Bearer your-token",+ "Content-Type": "application/json",+ },+ },+ filter: { status: "confirmed" }, // Optional: only trigger for specific rows+ active: true,+});++console.log("Created subscription:", subscription.id);+```++### Step 3: Handle the Webhook++Your endpoint will receive POST requests with this payload:++```json+{+ "table": "salesOrder",+ "operation": "INSERT",+ "recordId": "so_abc123",+ "new": {+ /* full row data */+ },+ "old": null,+ "timestamp": "2024-01-15T10:30:00Z"+}+```++---++## Workflow B: Create a SYNC Subscription (Accounting)++### Step 1: Ensure Accounting Integration is Configured++The company must have an active accounting integration (Xero, QuickBooks).++### Step 2: Create the Subscription++```typescript+const subscription = await createEventSystemSubscription(client, {+ name: "sync-customers-to-xero",+ table: "customer",+ companyId: companyId,+ operations: ["INSERT", "UPDATE"],+ type: "SYNC",+ config: {+ provider: "xero", // or "quickbooks"+ },+ active: true,+});+```++### Step 3: The sync handler will automatically:++1. Fetch the full customer record+2. Transform it to accounting provider format+3. Push to the external system+4. Track sync status in `externalIntegrationMapping`++---++## Workflow C: Add a New Handler Type++### Step 1: Update the Handler Type Enum++In `packages/database/src/event.ts`:++```typescript+const HandlerTypeSchema = z.enum([+ "WEBHOOK",+ "WORKFLOW",+ "SYNC",+ "SEARCH",+ "YOUR_NEW_TYPE",+]);+```++### Step 2: Update the Database Constraint++Create a migration:++```sql+ALTER TABLE "eventSystemSubscription"+DROP CONSTRAINT IF EXISTS "eventSystemSubscription_handlerType_check";++ALTER TABLE "eventSystemSubscription"+ADD CONSTRAINT "eventSystemSubscription_handlerType_check"+CHECK ("handlerType" IN ('WEBHOOK', 'WORKFLOW', 'SYNC', 'SEARCH', 'YOUR_NEW_TYPE'));+```++### Step 3: Create the Handler Task++Create `packages/jobs/trigger/event/your-handler.ts`:++```typescript+import { logger, task } from "@trigger.dev/sdk/v3";+import { z } from "zod";++const YourPayloadSchema = z.object({+ records: z.array(+ z.object({+ event: z.object({+ table: z.string(),+ operation: z.enum(["INSERT", "UPDATE", "DELETE"]),+ recordId: z.string(),+ new: z.record(z.any()).nullable(),+ old: z.record(z.any()).nullable(),+ timestamp: z.string(),+ }),+ companyId: z.string(),+ handlerConfig: z.record(z.any()),+ })+ ),+});++export const yourHandlerTask = task({+ id: "event-handler-your-type",+ retry: {+ maxAttempts: 3,+ factor: 2,+ randomize: true,+ },+ run: async (input: unknown) => {+ const payload = YourPayloadSchema.parse(input);++ logger.info(`Processing ${payload.records.length} events`);++ for (const record of payload.records) {+ // Your handler logic here+ logger.info(`Processing ${record.event.table} ${record.event.operation}`);+ }++ return { processed: payload.records.length };+ },+});+```++### Step 4: Update the Queue Router++In `packages/jobs/trigger/event/queue.ts`:++```typescript+import { yourHandlerTask } from "./your-handler.ts";++// Add to grouped types+const grouped: Record<HandlerType, QueueJob[]> = {+ WEBHOOK: [],+ WORKFLOW: [],+ SYNC: [],+ SEARCH: [],+ YOUR_NEW_TYPE: [], // Add this+};++// Add handler in the all() block+async yourNewType() {+ let queue: number[] = [];++ if (grouped.YOUR_NEW_TYPE.length === 0) return queue;++ const records = grouped.YOUR_NEW_TYPE.map((job) => {+ queue.push(job.msg_id);+ return {+ event: job.message.event,+ companyId: job.message.companyId,+ handlerConfig: job.message.handlerConfig,+ };+ });++ await yourHandlerTask.trigger({ records });++ return queue;+},++// Include in total+total = total.concat(webhooks, workflows, syncs, searches, yourNewType);+```++---++## Workflow D: Attach Event Triggers to a New Table++### Step 1: Create Migration++```sql+-- Attach async event trigger (no sync interceptors)+SELECT attach_event_trigger('yourNewTable', ARRAY[]::TEXT[]);++-- OR with sync interceptors (functions run synchronously before the operation)+SELECT attach_event_trigger('yourNewTable', ARRAY['validate_before_insert']::TEXT[]);+```++### Step 2: Create Subscriptions for Existing Companies (if needed)++```sql+-- Example: Create search subscriptions for all companies+DO $$+DECLARE+ company_record RECORD;+BEGIN+ FOR company_record IN SELECT id FROM "company" LOOP+ INSERT INTO "eventSystemSubscription" (+ "name", "table", "companyId", "operations",+ "handlerType", "config", "filter", "active"+ )+ VALUES (+ 'search-index-yourNewTable',+ 'yourNewTable',+ company_record.id,+ ARRAY['INSERT', 'UPDATE', 'DELETE'],+ 'SEARCH',+ '{}'::jsonb,+ '{}'::jsonb,+ TRUE+ )+ ON CONFLICT ON CONSTRAINT "unique_subscription_name_per_company" DO NOTHING;+ END LOOP;+END $$;+```++---++## Workflow E: Delete a Subscription++### By ID++```typescript+import { deleteEventSystemSubscription } from "@carbon/database/event";++await deleteEventSystemSubscription(client, subscriptionId);+```++### By Name (for a company)++```typescript+import { deleteEventSystemSubscriptionsByName } from "@carbon/database/event";++await deleteEventSystemSubscriptionsByName(+ client,+ companyId,+ "my-webhook-subscription"+);+```++---++## Debugging++### Check if Subscription Exists++```sql+SELECT * FROM "eventSystemSubscription"+WHERE "companyId" = 'your-company-id'+ AND "table" = 'yourTable'+ AND "active" = TRUE;+```++### Check PGMQ Queue++```sql+-- Queue metrics+SELECT * FROM pgmq.metrics('event_system');++-- Peek at pending messages+SELECT * FROM pgmq.read('event_system', 30, 10);+```++### Check Trigger.dev Dashboard++1. Go to Trigger.dev dashboard+2. Look for `event-queue` task runs (cron)+3. Check downstream handler tasks (`event-handler-webhook`, etc.)+4. Review logs and error messages++### Test a Subscription Manually++Insert/update a row in the watched table and verify:++1. Message appears in PGMQ queue (within same transaction)+2. `event-queue` picks it up (within 1 minute)+3. Handler task executes successfully++---++## Common Pitfalls++1. **Forgetting to attach trigger**: Table must have event trigger attached via `attach_event_trigger()`+2. **Missing companyId**: Subscriptions are company-scoped; events without companyId are skipped+3. **Wrong operation array**: Use `["INSERT"]` not `["insert"]` (uppercase)+4. **Filter not matching**: JSONB filter uses `@>` containment operator+5. **Handler not deployed**: Ensure Trigger.dev tasks are deployed after code changes++---++## Checklist++- [ ] Event trigger attached to table (check `eventSystemTrigger` view)+- [ ] Subscription created with correct `companyId`+- [ ] Handler type matches your use case+- [ ] Config contains required fields for handler type+- [ ] Filter (if used) matches expected row structure+- [ ] Handler task is deployed to Trigger.dev+- [ ] Tested with a real database operation