Skip to content

Advertising

Internals of the Advertising domain. For the public API, see Advertising API.

The domain has two unrelated halves that share a Go package (domain/advertising): campaigns (Tellyboard, digital signage, /api/v1/campaigns) and advertising tickets (ticketing storefront cards, part of the ticketing lane). Facts were checked against CoreAPI staging on 2026-11-16.

How it fits together

graph LR
    TB["Tellyboard console"] -->|/api/v1/campaigns + X-Organization-ID| C["CoreAPI campaigns (flat handlers)"]
    TA["SpatioViewAdmin (TicketAdmin)"] -->|/api/advertisingTickets/:ws| A["CoreAPI advertising tickets (ticketing lane)"]
    C --> PUB[("public.campaigns")]
    A --> JZ[("johorzoo.advertising_ticket\njohorzoo.rail_menu_advertising_ticket")]
    S["Campaign scheduler (5 s)"] --> PUB
    S -->|MQTT PLAY / STOP| D["Screens"]
    D -->|POST /campaigns/verify-active-public| C
    R["Catalogue rail menu (storefront)"] --> JZ

Campaigns

  • Flat, old-style handlers in domain/advertising/handler.go, registered by RegisterCampaignRoutes from routes_platform.go on the /api/v1 group. Raw SQL through db.DB; no repository or service layer.
  • Scope comes from the X-Organization-ID, X-Workspace-ID and X-User-ID headers. No named permission is checked (not even workspace:campaigns:*, which exist in pkg/permissions).
  • The legacy scope guard (pkg/middleware/legacy_scope.go) covers /campaigns. Mode from COREAPI_LEGACY_SCOPE: off, log (default: count and log only, nothing refused) or enforce (no valid token → 401, headers that disagree with the token → 403, good requests get their headers overwritten with the verified values). Counters: GET /api/v1/internal/legacy-scope (PSK). The partner page documents the enforce contract.
  • Reach (screenCount, readyScreens, …) is computed per request from spatial.ResolveTargetGroupEndpointIDs and the endpoints/devices tables. readyScreens = devices with last_seen_at in the last 5 minutes.
  • PUT writes every editable column from the body, so omitted fields are cleared and an empty status, source, objective or delivery_mode fails the table's check constraint (500).

Campaign scheduler

advertising.StartCampaignScheduler() is started from infra.go. Every 5 seconds (was 15 s; it is the worst-case delay for a Pause/Resume to reach screens) runCampaignTick:

  1. Sets status = 'completed' on live campaigns whose end_date (as a calendar date in Asia/Bangkok) is before today. Nothing else moves a campaign out of live.
  2. Selects live campaigns with a target group, inside their date range (calendar dates, Asia/Bangkok).
  3. Skips those outside schedule_rule (catalogue.ScheduleRuleActiveNow, Asia/Bangkok; a missing or unparseable rule means Daily, All day) and those without playlist items.
  4. Publishes an MQTT PLAY to each target device, respecting the content priority in domain/catalogue/livecontrol_priority.go (BuildOverrideClaims): Emergency (1) beats Campaign (2), which beats Direct Playlist (3) and Feed (4–5).
  5. Sets devices.current_campaign_id on claimed devices (Now Playing), and for devices that lost the claim this tick, resumes the Direct Playlist / Feed or sends STOP.

MQTT PLAY is retained, so a stale PLAY can arrive after a pause or delete; screens call POST /api/v1/campaigns/verify-active-public before playing. end_date is stored as midnight of the chosen day and means "through this day", which is why every comparison uses calendar dates.

Advertising tickets

  • Layered module (handlers/, services/, repositories/, dto/), wired in routes/setup.go from setupTicketingService. The rail menu repository is passed in from the catalogue module.
  • Routes: ProtectedRS256 + RequireWorkspacePermission(workspace:advertising:manage) on :workspaceId; write routes add PopulateLegacyLocals so the handler can read fullName/role for audit strings and admin notifications.
  • organization_id and workspace_id columns are plain (no FK), set from the validated token scope, never from a header. Rail menu assignment checks both the rail menu and every advertising ticket against the workspace, so one workspace can't attach another's ticket to its own rail menu.
  • Every service error, including "not found", is answered as 500 { error }.
  • The storefront rail menu response (catalogue) merges advertising tickets among ticket groups by placement, with type: "advertisement" (AdvertisingTicketDTO).

Data model

erDiagram
    Campaign {
        uuid id
        string organization_id
        string workspace_id
        string name
        uuid target_group_id
        uuid playlist_id
        string status
        timestamptz start_date
        timestamptz end_date
        string source
        string objective
        string delivery_mode
        jsonb schedule_rule
        jsonb action_experience
        string created_by
    }
    AdvertisingTicket {
        bigint advertising_ticket_id
        string organization_id
        string workspace_id
        string advertisement_type
        string advertiser
        string color
        int placement
        string title_bm_en_cn
        string header_bm_en_cn
        text desc_bm_en_cn
        string action_button
        string redirect_url
        string image_url
        string active_start_date
        string active_end_date
        bool is_active
    }
    RailMenuAdvertisingTicket {
        bigint rail_menu_id
        bigint advertising_ticket_id
    }
    AdvertisingTicket ||--o{ RailMenuAdvertisingTicket : "placed in"
  • public.campaigns (GORM model Campaign in pkg/db/migrate.go): check constraints on status (draft, scheduled, live, paused, completed, archived), source, objective and delivery_mode. schedule_rule and action_experience are jsonb, sent and returned as JSON strings; the Tellyboard ScheduleRuleSheet.jsx defines the rule shape.
  • johorzoo.advertising_ticket and johorzoo.rail_menu_advertising_ticket (join table, composite key, no FK constraints). Titles, headers and descriptions have _bm, _en and _cn columns.

Implementation status

Campaigns (/api/v1/campaigns)

  • GET /campaigns, GET /campaigns/:campaignId
  • POST /campaigns, PUT /campaigns/:campaignId, DELETE /campaigns/:campaignId
  • POST /campaigns/verify-active-public
  • Scheduler: auto-complete and MQTT playback
  • Permission check (workspace:campaigns:*)
  • Link to advertising tickets
  • Impression and click tracking

Advertising tickets (/api/advertisingTickets/:workspaceId)

  • GET /, POST /, PUT /, DELETE /
  • POST /railMenus, DELETE /railMenus

Known issues

  • POST /campaigns/verify-active-public is under the /campaigns prefix of the legacy scope guard but is not in legacyOpenRoutes. In enforce mode, screens calling it without a user token would get 401. Add POST /campaigns/verify-active-public to legacyOpenRoutes before switching to enforce.
  • Campaign routes check no permission; any signed-in member of the organization can create, change or delete them.
  • Advertising ticket "not found" returns 500, not 404.

Change history

  • 2026-09-22: inWindowNow on campaigns.
  • 2026-09-15: verify-active-public; scheduler tick 15 s → 5 s.
  • 2026-09-14: advertising tickets scoped to :workspaceId with workspace:advertising:manage.
  • 2026-09-08 to 2026-09-09: campaign fields for source, objective, delivery mode, schedule rule and action experience; GET /campaigns/:campaignId.
  • 2026-06-23: first advertising documentation.