Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
7.3 KiB
Context
Approved product design: docs/superpowers/specs/2026-12-01-lead-dashboard-design.md (definitions, figures, access). This file fixes the technical contract so the services-counter, mobile-api and portal tasks can be built in parallel.
Facts the design relies on:
event_lead.statusalready holds OPEN / CONTACTED / QUALIFIED / WON / LOST (event-lead-capture). Only the current value is stored.event_lead.captured_atandupdated_atareLocalDateTime.now()in the services-counter JVM, i.e. wall time in the JVM's default zone (UTC in the containers). There is no zone column.- services-counter has no database tests; every existing test is a Mockito unit test.
- Salesperson names live in services-system; mobile-api already resolves them per user id with a per-request map (
AdminLeadService.salesperson).
Decisions
1. Status history table
service-counter.event_lead_status_change:
- columns
idbigint PK auto,company_idbigint NOT NULL,event_lead_idbigint NOT NULL,from_statusvarchar(10) NULL,to_statusvarchar(10) NOT NULL,changed_atdatetime NOT NULL,changed_byvarchar(255) NULL; - key
idx_elsc_event_lead (event_lead_id, changed_at)and keyidx_elsc_company_to_changed (company_id, to_status, changed_at).
EventLeadService.capture saves one row (null → initial status, changed_at = the event lead's captured_at, changed_by = doneBy) in the capture transaction. update saves one row when the requested status differs from the stored one (changed_at = the new updated_at). Entity EventLeadStatusChange, repository EventLeadStatusChangeRepository. Nothing deletes or updates these rows.
Backfill (sql/02-status-change-backfill.sql, idempotent: only event leads with no history rows):
- one row null → OPEN at
captured_at,changed_by=captured_by; - where
status <> 'OPEN', one row OPEN → status atCOALESCE(updated_at, captured_at),changed_by=COALESCE(updated_by, captured_by).
2. Aggregate in Java over lean rows, not in SQL
services-counter runs three plain native queries with bind parameters and aggregates the rows in a pure Java class (LeadDashboardCalculator), which is unit-tested without a database:
- Captures: event leads with
captured_atin [fromUtc, toUtc) and the company filter, joined toeventfor name and company:event_lead.id, event_uuid, event name, company_id, salesperson_user_id, lead_type, status, captured_at. - Wins: event leads with
status = 'WON'whose latest history row withto_status = 'WON'haschanged_atin [fromUtc, toUtc):event_lead.id, captured_at, won_at. - Overdue:
salesperson_user_id, COUNT(*)for status not in (WON, LOST) andnext_follow_up_date < today, grouped by salesperson.
Interests for the captures come from the existing EventLeadInterestRepository.findByIdEventLeadIdIn, in chunks of 1000 ids, and interest names from InterestRepository.
Rejected: one grouped SQL query per figure. Its correctness could only be checked against a live database (no DB tests here), and adding Testcontainers is a new build dependency for one screen.
ponytail: row volume is one row per capture in the period. Fine for thousands per period; move the sums into SQL when one company captures hundreds of thousands of leads a year.
3. Time zone
mobile-api turns the period into dates; services-counter turns [from 00:00, to+1 00:00) in Asia/Kuala_Lumpur into the JVM default zone's wall time for the queries, and turns each stored time back to a Malaysian date for bucketing. "Today" for overdue is today in Asia/Kuala_Lumpur.
4. services-counter contract
POST /counter/dashboard/leads (route already under /counter/**), body:
{ "companyId": 2, "from": "2026-11-01", "to": "2026-11-30", "bucket": "DAY" }
companyId null = all companies. bucket is DAY or MONTH. from after to → 400 INVALID_PERIOD. Response:
{
"captures": 10, "newLeads": 6, "wonInPeriod": 3, "medianDaysToWin": 5, "overdueFollowUps": 4,
"funnel": { "OPEN": 3, "CONTACTED": 1, "QUALIFIED": 1, "WON": 3, "LOST": 2 },
"trend": [ { "bucket": "2026-11-01", "captures": 2, "wins": 0 } ],
"events": [ { "eventUuid": "123", "name": "KL Expo", "companyId": 2, "captures": 10, "newLeads": 6, "won": 3 } ],
"salespeople": [ { "userId": 7, "assigned": 4, "worked": 3, "won": 1, "lost": 1, "overdue": 2 } ],
"interests": [ { "interestId": 5, "name": "Cookware", "captures": 4, "won": 1 } ]
}
medianDaysToWinis null when there are no wins; with an even count it is the lower middle value (whole days).trendlists every bucket in the period, zero-filled; a month bucket is the month's first day.eventsis ordered by captures descending, then name;salespeopleby assigned descending, withuserIdnull for Unassigned;salespeoplealso includes a salesperson with overdue follow-ups but no captures in the period (assigned 0).interestsby captures descending, then name.eventUuidis a string (JavaScript loses precision on snowflake ids).
Rates are not sent; the portal divides.
5. mobile-api contract
GET /api/admin/dashboard/leads?from=2026-11-01&to=2026-11-30&companyId=2 under @AdminModule("BO_DASHBOARD"):
from/tooptional; when absent, from = first day of the current Malaysian month, to = today (Malaysia).- Bad date format or from after to → 400
{code: "INVALID_PERIOD", message}. - bucket =
DAYwhento - from + 1 <= 62days, elseMONTH. - company from
AdminCompanyScope.scope(caller, companyId). - Response: the counter response plus
from,to,bucket, and each salesperson row gainsname(the user's name, or null for Unassigned and for a user that no longer exists). Salesperson names are resolved with the existing per-request map pattern.
6. Module
service-system.module row (40050000, 'BO_DASHBOARD', 'Dashboard', 'BO', 'M', 40000000, '/', 'LayoutDashboard'). No role grant: super admins grant it under Roles. Id 40050000 sorts it first in the sidebar, which orders by module id. The portal's route gate (ROUTE_MODULES) does not list /: the home page itself decides between the dashboard and the welcome, using hasModule("BO_DASHBOARD").
7. Portal
rechartsadded topackage.json;app/components/ui/chart.tsxcopied from babyboo's portal.app/lib/services/adminDashboard.ts: types for decision 5 andgetLeadDashboard({from, to, companyId}).app/page.tsx: welcome for users without the module; otherwise the dashboard with a period control (This month / Last month / Custom with the existing date picker), the company picker for super admins (viauseCompanyScope), tiles, funnel bars, a recharts line or bar chart of captures and wins, and three tables (cards belowmd). Event rows link to/event-leads/{eventUuid}. The Company column on the event table shows only whenshowCompanyColumn.- Rates show as whole percent; a null median or zero captures shows "–".
- All labels are message keys with English defaults, as on other screens.
Risks
- Backfilled history times are approximate (deployment notes say so).
- A lead whose status was set by a direct DB edit has no history row for that change; the dashboard then misses that win. Nothing in the app does this.
captured_atstored in the JVM zone: if production pods ever run in a different zone from when rows were written, older rows shift. Same risk already exists for every listing.