Skip to main content

Automated Trigger Rules

Overview

A trigger rule is a "when this event happens, do this" rule that runs in the background without anyone pressing a button. The supported trigger sources map directly to the RabbitMQ queues the platform already publishes to:

EventQueue
A member joins or leaves an audienceaudience_membership_trigger
Someone submits a formform_submitted_trigger
Someone clicks a campaign linkcampaign_click_trigger
An inbound message arrivesmessage_received_trigger
A booking is created or changes statusbooking_event_trigger

This module covers only the authoring side — defining rules and reviewing their statistics and execution history. The actual execution happens in the line-management-worker-go service. Trigger rules are also the foundation that Workflow & Chatbot builds on, since every workflow must be bound to a trigger rule to have a starting point.

Business Flow

  1. Load available eventsGET /api/trigger-rules/dropdown returns the selectable trigger sources, cached in Redis under the key TRIGGER_SOURCE_TYPES.
  2. Create a rulePOST /api/trigger-rules where the user picks a source event, its conditions, and the action to perform.
    • The service calls validateReferences (raw SQL) to confirm the referenced audience, form, or campaign actually exists. If any reference is missing, the request fails with a cause payload identifying the bad reference.
  3. List rulesGET /api/trigger-rules returns a paginated list. Each row carries execution stats (run count, successes, failures) computed with a per-row subquery.
  4. Check dependenciesGET /api/trigger-rules/by-audience/:audienceId lists every rule bound to a given audience. This powers the warning shown before an audience is deleted.
  5. Manage a single ruleGET /api/trigger-rules/:id for details, PUT /api/trigger-rules/:id to edit, and DELETE /api/trigger-rules/:id to remove.
  6. Monitor resultsGET /api/trigger-rules/:id/stats returns a summary (raw SQL), and GET /api/trigger-rules/:id/logs returns the paginated per-execution history from the trigger_log table.
  7. Runtime side — When an event fires, the worker reads the rules from Redis (TRIGGER_RULES), takes TRIGGER_LOCK to prevent overlapping runs, executes the configured action, and writes the outcome to trigger_log.

Key Files & Functions

The code lives in internal/modules/triggerrule/, made up of controller.go, service.go, and dto.go.

MethodRouteHandlerPolicy
GET/api/trigger-rulesct.findAllread trigger-rule
GET/api/trigger-rules/dropdownct.getDropdownread trigger-rule
GET/api/trigger-rules/by-audience/:audienceIdct.findByAudienceIDread trigger-rule
GET/api/trigger-rules/:idct.findOneread trigger-rule
GET/api/trigger-rules/:id/statsct.getStatsread trigger-rule
GET/api/trigger-rules/:id/logsct.getLogsread trigger-rule
POST/api/trigger-rulesct.createcreate trigger-rule
PUT/api/trigger-rules/:idct.updateupdate trigger-rule
DELETE/api/trigger-rules/:idct.deletedelete trigger-rule

Every route is wrapped with modulegate.ModuleGate(d, "trigger-rule").

Connections to Other Services

  • Access control — All routes pass through ModuleGate("trigger-rule"), with PolicyModuleTriggerRule supplied as policy metadata.
  • Tablestrigger_rule, trigger_log, audience, form_builder, campaign, workflow
  • RedisTRIGGER_RULES (rule set consumed by the worker), TRIGGER_SOURCE_TYPES (dropdown cache), TRIGGER_LOCK (overlap guard)
  • RabbitMQ — Consumes audience_membership_trigger, form_submitted_trigger, campaign_click_trigger, message_received_trigger, and booking_event_trigger; dispatches outbound work through web_request_execute.
  • Executorline-management-worker-go performs the actual rule execution.
  • Related modulesWorkflow, Audience Management, Form Builder, Campaign Management, and the Appointment Booking App.