feat(admin): add credits rollup + events endpoints
Merged
SW
5mo ago+808+808+012345678901234567890123456789-28-28-01234567890123456789
Summary
Adds two admin-only endpoints powering the admin credits dashboard. Contract was defined first in recoupable/docs#213 (merged).
GET /api/admins/credits/rollup— per-account credit usage totals over the selected period. Sorted by total credits DESC. Joined with account names + emails for human-readable rows. Page-based pagination withtotal_count.GET /api/admins/credits/events— rawusage_eventsrows for one account in the selected period. Powers the drilldown subtable lazy-loaded when an admin expands a rollup row.
Design notes
- Period selector: follows existing admin convention —
all | daily | weekly | monthly(defaultmonthly). - Aggregation: rollup aggregates events by
account_idin JS, not SQL. Acceptable at admin scale; documented in code as a future Postgres-function migration target. - Auth: both endpoints require
validateAdminAuth(x-api-key OR Bearer token +checkIsAdmin). - Pagination: page-based with separate COUNT query for
total_count; nohas_more(per docs PR review).
Files
app/api/admins/credits/{rollup,events}/route.ts— thin route delegatorslib/admins/credits/getAdminCredits{Rollup,Events}Handler.ts— business logiclib/admins/credits/validateGetAdminCredits{Rollup,Events}Query.ts— Zod validatorslib/admins/credits/getCutoffMs.ts— period → UTC midnight cutoff helperlib/supabase/usage_events/selectAdminCredits{Rollup,Events}.ts— DB querieslib/admins/credits/__tests__/— 15 vitest cases covering auth, validation, aggregation, sort, pagination, period cutoff
Test plan
-
pnpm test lib/admins/credits/__tests__/— 15/15 pass -
pnpm test(full suite) — 491 files / 2872 tests pass -
pnpm formatclean -
pnpm lintclean - Test against preview deployment with admin API key (rollup + events for sweetman Pro account)
- Verify period filter behavior (monthly vs all)
- Verify pagination (page=2, limit=2)
🤖 Generated with Claude Code
Summary by cubic
Adds admin credits rollup and events endpoints for the dashboard with period filters and pagination. Centralizes cutoff utils in lib/admins/ and makes reads deterministic; rollup fetches all matching rows to avoid Supabase caps.
-
New Features
GET /api/admins/credits/rollup— per-account totals for the selected period, sorted by total credits desc; joins account name and email; page-based pagination withtotal_count.GET /api/admins/credits/events— paginatedusage_eventsfor one account over the selected period, sorted bycreated_atdesc.
-
Refactors
- Moved
getCutoffMsandPERIOD_DAYStolib/admins/;getCutoffMsnow returnsnumber | null(nullforall); updatedprivyandslack. - Data access: range-based
selectUsageEventswith deterministic ordering (created_at+iddesc) andcountUsageEvents; rollup useslib/admins/credits/selectAllUsageEventsto fetch all batches, breaks ties byaccount_idasc, and picks the most-recentaccount_emails.email.
- Moved
Summary by CodeRabbit
- New Features
- Added admin API endpoints to retrieve detailed credit usage events with pagination support.
- Added admin API endpoint to view aggregated credit usage rollup by account with sorting and pagination.