Skip to content

feat(realunit): add cron-cached admin KPI stats endpoint#3821

Open
TaprootFreak wants to merge 2 commits into
developfrom
feat/realunit-admin-stats
Open

feat(realunit): add cron-cached admin KPI stats endpoint#3821
TaprootFreak wants to merge 2 commits into
developfrom
feat/realunit-admin-stats

Conversation

@TaprootFreak
Copy link
Copy Markdown
Collaborator

What

Adds a new cron-cached aggregate admin endpoint GET /v1/realunit/admin/stats (role REALUNIT/ADMIN) returning RealUnit KPIs. Mirrors the existing StatisticService cron/cache pattern: an hourly @DfxCron refreshes an in-memory cache; the route just returns it. Process.UPDATE_REALUNIT_STATS is added so the job can be disabled like other cron processes.

A companion frontend PR lives in DFXswiss/services; the DTO field names below are the shared contract.

Response shape

RealUnitStatsDto {
  updated: Date;
  growth:       { accounts: Period; wallets: Period };
  kycFunnel:    { step: KycStepName; reached: Period; completed: Period }[];
  registration: { started: Period; inReview: Period; completed: Period };
  trading:      { buyVolumeChf: Period; buyCount: Period; sellVolumeChf: Period; sellCount: Period };
}
Period { total: number; last30Days: number; last7Days: number }

Each KPI is reported over three windows: all-time total, last30Days, last7Days. New reusable aggregate query helpers were added (all SQL-side, grouped counts/sums): KycAdminService.getKycStepCounts, UserDataService.getNewUserDataCount, UserService.getNewUserCount, TransactionService.getAssetTradingStats.

Scoping / data-source notes

  • growth: global new UserData (accounts) / User (wallets) by created. The API has no RealUnit-app client tag, so this is platform-wide growth, not RealUnit-only.
  • kycFunnel: counts over KycSteps whose name is in contextRequiredSteps(REALUNIT_BUY) (canonical helper, not hardcoded). KycStep has no context column, so this is the step set used for RealUnit buy, not a per-user RealUnit filter; drop-off is derivable across steps. completed = steps with ReviewStatus.COMPLETED.
  • registration: the RealUnit-specific REALUNIT_REGISTRATION KycStep — started (all), inReview (in-progress/review statuses), completed.
  • trading: completed transactions on the REALU asset — CHF volume (SUM(amountInChf), rounded to Config.defaultVolumeDecimal) and counts, split by buy/sell.
  • Only flat aggregate counts/sums, no per-user attributes, so not re-identifying. A suppression threshold can be added later if Legal requests.

Tests

100% coverage on new code (new service, new DTO, new helper methods). Cron is not auto-fired in tests; doUpdate() is called directly then getStats() asserted. format / lint / type-check / full jest suite all green locally.

Add GET /v1/realunit/admin/stats (REALUNIT/ADMIN role) returning aggregated
RealUnit KPIs from an hourly cron-refreshed cache, mirroring the existing
StatisticService pattern.

- New RealUnitStatsService computes growth, KYC funnel, registration and
  trading KPIs over total/30d/7d windows via Promise.all
- New RealUnitStatsDto contract with Period (total/last30Days/last7Days)
- Add generic aggregate query helpers: KycAdminService.getKycStepCounts,
  UserDataService.getNewUserDataCount, UserService.getNewUserCount,
  TransactionService.getAssetTradingStats
- Add Process.UPDATE_REALUNIT_STATS for cron disable support
- Full unit coverage on new service, DTO and helper methods
@TaprootFreak TaprootFreak marked this pull request as ready for review June 4, 2026 15:14
@TaprootFreak TaprootFreak requested a review from davidleomay as a code owner June 4, 2026 15:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant