Skip to content

refactor: split src/db/queries/ by responsibility — close #406 cluster#411

Merged
elfensky merged 4 commits into
developfrom
feature/db-queries-actions-split
May 24, 2026
Merged

refactor: split src/db/queries/ by responsibility — close #406 cluster#411
elfensky merged 4 commits into
developfrom
feature/db-queries-actions-split

Conversation

@elfensky
Copy link
Copy Markdown
Owner

Summary

Closes the db-queries-actions-split cluster from /desloppify (issue #406). The src/db/queries/ directory had drifted into a misleading mix of three different concerns behind one label:

  1. Pure data-access (getCampaign, getCascadeLeaderboard, getCrossSeasonStats, upsertEvent, upsertSeason, ...) — stays.
  2. Auth-gated server actions (admin user management, API key management, account export/delete) — moves to feature directories.
  3. HTTP-boundary helpers (validateApiKey, the auth guards) — moves to src/shared/utils/api/.

After this PR, each layer lives next to its consumer.

What changed

Boundary helpers → src/shared/utils/api/

From To
src/db/queries/_authGuards.mjs src/shared/utils/api/authGuards.mjs
src/db/queries/validateApiKey.mjs src/shared/utils/api/validateApiKey.mjs

Admin server actions → src/features/admin/actions.mjs (merged)

The 7 actions previously in src/db/queries/admin.mjs (getAllUsers, updateUserRole, toggleUserBan, adminGetUserApiKeys, adminRevokeApiKey, getSystemStats, getAllApiKeys) merge into the existing file alongside sendTestNotification. The file now has clear section headers (User management / Admin API key management / System overview / Push notifications).

Account server actions → src/features/account/actions.mjs (new, consolidated)

Two separate files merge into one new module:

  • src/db/queries/api.mjs → API key management section (3 actions)
  • src/db/queries/account.mjs → User data lifecycle section (2 actions)

Updated importers

  • 9 source files (4 admin components, 3 account components, reseedSeason.mjs, rebroadcast/route.js)
  • 7 test files (5 unit tests + 2 component tests with vi.mock paths)
  • 2 docs MDX pages (/docs/authentication, /docs/database)

What's left in src/db/queries/

Pure data-access only:
```
getCampaign.mjs
getCascadeLeaderboard.mjs
getCrossSeasonStats.mjs
getKillsTrend.mjs ← has 'use server' but is a query, called from RSC pages
getPlayersAvg24h.mjs ← ditto
rebroadcast.mjs ← wire-format reconstruction extracted in #409
upsertEvent.mjs
upsertEventProgress.mjs
upsertSeason.mjs
upsertStatistic.mjs
upsertStatus.mjs
```

Behavior changes

None. Pure relocation — every action's logic is byte-for-byte identical. Only file paths and import paths changed.

Verification

  • npm run lint ✅ (0 errors, 2 pre-existing React Compiler warnings)
  • npm run typecheck
  • npm run test:unit ✅ — 1416 tests pass (133 files)
  • npm run build

Commits

Branched into 4 logical commits for readability:

  1. boundary helpers — relocate authGuards + validateApiKey to shared/utils/api/; remove the three soon-to-be-relocated server-action files (their replacements land in the next two commits)
  2. admin consolidation — merge db/queries/admin.mjs content into features/admin/actions.mjs; update 4 importers + test
  3. account consolidation — create features/account/actions.mjs from db/queries/api.mjs + account.mjs; update 3 importers + 4 tests
  4. docs — changelog entry

(Reviewing as a series rather than per-commit is recommended — commits 2-3 fix the orphaned imports temporarily introduced by commit 1, in the same branch.)

Test plan

  • npm run lint
  • npm run typecheck
  • npm run test:unit
  • npm run build
  • Manual smoke: sign in, generate/delete API key, export account, hit admin /profile dashboard

Closes

🤖 Generated with Claude Code

elfensky and others added 4 commits May 23, 2026 21:44
…server-action files

Auth-guard helpers (requireSession / requireUser / requireAdmin) and
the API-key request validator (validateApiKey) are HTTP-boundary
concerns, not database queries. They were sitting in src/db/queries/
behind a misleading directory name.

Relocated next to responses.mjs and methodNotAllowed.mjs:
  - src/db/queries/_authGuards.mjs    → src/shared/utils/api/authGuards.mjs
  - src/db/queries/validateApiKey.mjs → src/shared/utils/api/validateApiKey.mjs

Also removes (without replacement in this commit) the three
server-action files that move to feature directories in follow-up
commits. Their consumers still reference the old paths through HEAD~1;
the next two commits in this branch land the merged feature files and
update every consumer in lockstep. Reviewing as a series is cleaner
than reviewing each commit standalone.

Removed (relocated in commits 2 + 3):
  - src/db/queries/admin.mjs    (7 admin actions → features/admin/actions.mjs)
  - src/db/queries/api.mjs      (3 API key actions → features/account/actions.mjs)
  - src/db/queries/account.mjs  (2 user lifecycle actions → features/account/actions.mjs)

Updated importers in this commit (only the two outside the relocation set):
  - src/features/archives/reseedSeason.mjs    (requireAdmin)
  - src/app/api/h1/rebroadcast/route.js       (validateApiKey)

Plus two test files and the two docs MDX pages.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…/actions.mjs

The 7 admin server actions previously in src/db/queries/admin.mjs
(getAllUsers, updateUserRole, toggleUserBan, adminGetUserApiKeys,
adminRevokeApiKey, getSystemStats, getAllApiKeys) merge into the
existing src/features/admin/actions.mjs alongside sendTestNotification.
Each action's logic is byte-for-byte identical — only the file
location changed; auth-guard imports now point to the new
src/shared/utils/api/authGuards.mjs path.

Updates 4 admin component importers + the unit test file.

Closes the admin_ac design_coherence finding (admin actions previously
split between db/queries/admin.mjs and features/admin/actions.mjs with
no clear rule for which goes where).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
…features/account/actions.mjs

Merges what was previously two separate server-action files into one
co-located actions module next to the components that call them:

  - src/db/queries/api.mjs    (3 API-key management actions)
  - src/db/queries/account.mjs (2 user lifecycle actions)
                                 ↓
       src/features/account/actions.mjs

The new file has two clearly-headed sections (API key management /
User data lifecycle). Each action's logic is byte-for-byte identical
— only location changed; auth-guard imports now point to
src/shared/utils/api/authGuards.mjs.

Updates 3 component importers (ApiForm, ApiDashboard, AccountActions)
and 4 test files (2 unit tests still in /unit/queries/ for now, 2
jsx component tests with vi.mock paths updated).

Closes the db_queri high_level_elegance finding (src/db/queries/ now
contains only pure data-access: get*/upsert*/the rebroadcast wire-
format reconstruction).

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
@elfensky elfensky merged commit d0db345 into develop May 24, 2026
4 checks passed
@elfensky elfensky deleted the feature/db-queries-actions-split branch May 24, 2026 12:21
elfensky added a commit that referenced this pull request May 24, 2026
…e review (0.51.7)

PR #411 (db/queries split) + PR #412 (validator unification) went through a 5-angle code review that surfaced 23 candidates; verification confirmed 23, ranked the 15 most severe, and this branch fixes all 15.

Highlights:
- Critical: admin Revoke API key button was completely broken (form-action arity mismatch)
- 3x TOCTOU races wrapped in db.\$transaction with Serializable isolation
- validateApiKey DB outages no longer surface as 401 Unauthorized (now 503)
- 4 UI consumers now handle result.errors envelopes explicitly
- deleteUserAccount: Zod-validates input, reorders revoke/delete, fires revalidatePath
- authGuards.mjs: dropped unnecessary 'use server' RPC surface
- docs/data-flow: code samples now use correct .safeParse() form

Verification: lint clean, typecheck clean, 1419/1419 unit tests pass, build compiles.
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