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 byRegisterCampaignRoutesfromroutes_platform.goon the/api/v1group. Raw SQL throughdb.DB; no repository or service layer. - Scope comes from the
X-Organization-ID,X-Workspace-IDandX-User-IDheaders. No named permission is checked (not evenworkspace:campaigns:*, which exist inpkg/permissions). - The legacy scope guard (
pkg/middleware/legacy_scope.go) covers/campaigns. Mode fromCOREAPI_LEGACY_SCOPE:off,log(default: count and log only, nothing refused) orenforce(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 fromspatial.ResolveTargetGroupEndpointIDsand theendpoints/devicestables.readyScreens= devices withlast_seen_atin the last 5 minutes. PUTwrites every editable column from the body, so omitted fields are cleared and an emptystatus,source,objectiveordelivery_modefails 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:
- Sets
status = 'completed'onlivecampaigns whoseend_date(as a calendar date in Asia/Bangkok) is before today. Nothing else moves a campaign out oflive. - Selects
livecampaigns with a target group, inside their date range (calendar dates, Asia/Bangkok). - Skips those outside
schedule_rule(catalogue.ScheduleRuleActiveNow, Asia/Bangkok; a missing or unparseable rule means Daily, All day) and those without playlist items. - Publishes an MQTT
PLAYto each target device, respecting the content priority indomain/catalogue/livecontrol_priority.go(BuildOverrideClaims): Emergency (1) beats Campaign (2), which beats Direct Playlist (3) and Feed (4–5). - Sets
devices.current_campaign_idon claimed devices (Now Playing), and for devices that lost the claim this tick, resumes the Direct Playlist / Feed or sendsSTOP.
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 inroutes/setup.gofromsetupTicketingService. The rail menu repository is passed in from the catalogue module. - Routes:
ProtectedRS256+RequireWorkspacePermission(workspace:advertising:manage)on:workspaceId; write routes addPopulateLegacyLocalsso the handler can readfullName/rolefor audit strings and admin notifications. organization_idandworkspace_idcolumns 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, withtype: "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 modelCampaigninpkg/db/migrate.go): check constraints onstatus(draft,scheduled,live,paused,completed,archived),source,objectiveanddelivery_mode.schedule_ruleandaction_experiencearejsonb, sent and returned as JSON strings; the TellyboardScheduleRuleSheet.jsxdefines the rule shape.johorzoo.advertising_ticketandjohorzoo.rail_menu_advertising_ticket(join table, composite key, no FK constraints). Titles, headers and descriptions have_bm,_enand_cncolumns.
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-publicis under the/campaignsprefix of the legacy scope guard but is not inlegacyOpenRoutes. Inenforcemode, screens calling it without a user token would get 401. AddPOST /campaigns/verify-active-publictolegacyOpenRoutesbefore 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:
inWindowNowon campaigns. - 2026-09-15:
verify-active-public; scheduler tick 15 s → 5 s. - 2026-09-14: advertising tickets scoped to
:workspaceIdwithworkspace: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.