5.4 KiB
5.4 KiB
API Refactor Lightweight Plan
Goal
Reduce api/src/server.ts size and duplication with low-risk, incremental moves.
Current state (2026-03-17):
server.tsstill holds most business routes, but Phases 1-5 are complete.- Completed move logs:
docs/api-phase1-move-log.mddocs/api-phase2-move-log.mddocs/api-phase3-move-log.mddocs/api-phase4-move-log.mddocs/api-phase5-move-log.md
Refactor Guardrails
- Keep route behavior identical while moving code.
- Move one domain at a time; do not mix domains in one PR.
- Do not redesign architecture (no repositories/DI/container work).
- Extract only duplicated logic into shared helpers/services.
- Preserve global hooks and plugin registration in
server.tsuntil final cleanup. - Keep response shapes stable (
ok,code,message, etc.) during moves. - Require tests to pass after each move phase.
Canonical Source Rule
server.ts is the canonical route logic right now.
When moving a domain:
- Copy current logic from
server.tsinto the domain route module. - Register the module.
- Remove the original block from
server.ts. - Confirm no duplicate route registrations remain.
Shared Helpers (Phase 0)
Create and/or finish these helpers to keep endpoint moves lightweight:
api/src/services/user-context.ts
getUserTimezone(...)- Removes repeated user timezone lookup logic.
api/src/services/category-shares.ts
computePercentShares(...)computeWithdrawShares(...)computeOverdraftShares(...)computeDepositShares(...)- Centralizes repeated category-share math.
api/src/services/budget-session.ts
getLatestBudgetSession(...)ensureBudgetSession(...)ensureBudgetSessionAvailableSynced(...)- Prevents session/value drift bugs.
api/src/services/api-errors.ts
- Standard typed error object and response body builder.
- Keeps endpoint error handling consistent.
Progress Snapshot
Completed:
- Phase 1: low-risk endpoints (
health,session,me,user/config). - Phase 2:
auth+accountendpoints. - Phase 3:
variable-categoriesendpoints. - Phase 4:
transactionsendpoints. - Phase 5:
fixed-plansendpoints.
Remaining:
- Phase 6:
income,budget,paydayendpoints. - Phase 7:
dashboard+crisis-status. - Phase 8:
admin+ site access endpoints. - Phase 9: final cleanup and helper consolidation.
Remaining Plan (Detailed)
Phase 5: Fixed Plans Domain
Move these routes out of server.ts into api/src/routes/fixed-plans.ts (or split module if needed):
PATCH /fixed-plans/:id/early-fundingPOST /fixed-plans/:id/attempt-final-fundingPATCH /fixed-plans/:id/mark-unpaidPOST /fixed-plans/:id/fund-from-availablePOST /fixed-plans/:id/catch-up-fundingPOST /fixed-plansPATCH /fixed-plans/:idDELETE /fixed-plans/:idPOST /fixed-plans/:id/true-up-actualGET /fixed-plans/duePOST /fixed-plans/:id/pay-now
Primary risk:
- Payment/funding workflows are tightly coupled with available budget math and rollover rules.
Test focus:
api/tests/fixed-plans*.test.tsapi/tests/payment-rollover.test.ts
Phase 6: Income, Budget, Payday Domain
Move these routes into a dedicated module (e.g., api/src/routes/budget-income.ts):
POST /incomeGET /income/historyPOST /income/previewPOST /budget/allocatePOST /budget/fundPOST /budget/reconcileGET /payday/statusPOST /payday/dismiss
Primary risk:
- Budget-session synchronization and allocator side effects.
Test focus:
- Income/budget allocation tests
- Any tests asserting payday status/dismiss behavior
Phase 7: Dashboard Read Domain
Move read endpoints into api/src/routes/dashboard.ts:
GET /dashboardGET /crisis-status
Primary risk:
- Derived numbers diverging between dashboard and rebalance/session APIs.
Test focus:
- Dashboard API contract checks and UI smoke verification.
Phase 8: Admin and Site Access Domain
Move operational endpoints into api/src/routes/admin.ts and/or api/src/routes/site-access.ts:
POST /admin/rolloverGET /site-access/statusPOST /site-access/unlockPOST /site-access/lock
Primary risk:
- Lockout/maintenance flow regressions and accidental open access.
Test focus:
- Site access flow tests
- Admin rollover auth/permission checks
Phase 9: Final Cleanup
- Remove dead helper duplicates from
server.tsand route modules. - Consolidate common helpers into
api/src/services/*. - Normalize error envelopes where safe (no contract change unless explicitly planned).
- Re-run full API/security suites and perform deployment smoke checks.
Reference Audit Requirement (Per Move)
For every endpoint moved:
- Record original location in
server.ts. - Record new route module location.
- Record frontend references (
web/src/api/*, hooks, pages). - Record test references.
- Add a phase move log under
docs/api-phaseX-move-log.md.
Verification Steps (Per Phase)
- Build:
cd api && npm run build
- Run focused tests for moved domain.
- Run security suites relevant to moved endpoints.
- Deploy.
- Run production endpoint smoke checks for the moved routes.
- Confirm logs show expected status codes (no unexplained 401/403/500 shifts).
Definition of Done per Phase
- Endpoints compile and register once.
- Existing tests pass.
- No route path/method changes.
- No response contract changes.
server.tsnet line count decreases.