How colin-stark/personal_budget works
Purpose and Entry Points
Personal Budget API is a Node.js/Express REST backend for authenticated budget tracking: accounts, categories, monthly budgets, transactions, and monthly summaries. Local startup enters through server.js; Vercel invokes the serverless Express wrapper at api/index.js, with routing configured by vercel.json. All public API endpoints are versioned under /api/v1.
Runtime and Infrastructure
The service targets Node.js 20+ with Express 5 and Mongoose-backed MongoDB, typically MongoDB Atlas. Vercel is the production serverless runtime; api/index.js lazily establishes Mongo connectivity to accommodate cold starts. Required external configuration is MONGO_URI and JWT_SECRET. Logging is centralized in src/lib/logger.js.
HTTP Composition and Request Boundary
Express route modules define resource-facing HTTP contracts and delegate business behavior to services:
• src/routes/auth.js
• src/routes/accounts.js
• src/routes/categories.js
• src/routes/budgets.js
• src/routes/transactions.js
• src/routes/summaries.js
Requests pass through rate limiting, JWT authentication for protected routes, Joi write validation, and centralized error handling:
• src/middleware/rateLimit.js
• src/middleware/auth.js
• src/middleware/validation.js
• src/middleware/errorHandler.js
This separates untrusted HTTP input and response formatting from domain and persistence operations.
Identity and Session Lifecycle
Authentication supports registration, password login, refresh-token rotation, and logout. User credentials and refresh-token hashes are persisted in MongoDB; bcrypt protects passwords and refresh tokens, while short-lived JWTs authorize subsequent resource requests. The route/service/model boundary is:
• src/routes/auth.js
• src/services/authService.js
• src/models/user.js
The auth middleware validates bearer JWTs and establishes the user context that scopes every protected resource operation.
User-Owned Financial Resources
Accounts and categories are user-scoped master data. Budget records bind a category to a month and amount, with upsert behavior for the same user/category/month. Services enforce ownership and deletion constraints: accounts cannot be removed when referenced by transactions, and categories cannot be removed when referenced by transactions or budgets.
• src/services/accountService.js, src/models/account.js
• src/services/categoryService.js, src/models/category.js
• src/services/budgetService.js, src/models/budget.js
Transaction Recording and Consistency
Transactions link users to accounts and optionally categories. Creation supports idempotency keys, backed by a sparse unique persistence constraint, so retried writes return the existing record rather than duplicate spending. Deletes are soft deletes; normal listings exclude deleted records. Core implementation is:
• src/routes/transactions.js
• src/services/transactionService.js
• src/models/transaction.js
Read Aggregation
Monthly summaries derive totals, category breakdowns, and budget progress from persisted transactions, categories, and budgets rather than storing a separate reporting projection. The summary endpoint is protected by the same user scope and accepts year/month query input:
• src/routes/summaries.js
• src/services/summaryService.js
• src/models/budget.js
Contract and Delivery Boundaries
The intended public API contract is documented in specs/001-budget-api/contracts/openapi.yaml. CI runs linting, Mongo-dependent tests, secret scanning, and CodeQL through .github/workflows/ci.yml and .github/workflows/codeql-analysis.yml. Deployment rewrites incoming Vercel traffic to the serverless API via vercel.json.
Main entry points: transactions.js Resource routes · auth.js Request guards · summaryService.js Summary service · accountService.js Account & category services
Components 21
Runtime
- Local server
server.js - Serverless entry · Lazily connects to MongoDB for cold starts.
api/index.js - Central logger
src/lib/logger.js
HTTP boundary
- Express API v1 · Public endpoints are mounted under /api/v1.
- Request guards · Rate limiting, JWT context, Joi validation, and error formatting.
src/middleware/auth.js - Auth routes
src/routes/auth.js - Resource routes · Accounts, categories, budgets, transactions, and summaries.
src/routes/transactions.js
Domain services
- Auth service · Registration, login, refresh rotation, and logout.
src/services/authService.js - Account & category services · Enforce ownership and referenced-record deletion constraints.
src/services/accountService.js - Budget service · Upserts one budget per user, category, and month.
src/services/budgetService.js - Transaction service · Idempotent writes and soft-delete listings.
src/services/transactionService.js - Summary service · Derives monthly totals, category breakdowns, and budget progress.
src/services/summaryService.js
MongoDB persistence
- Users & refresh tokens · Stores credentials and refresh-token hashes.
src/models/user.js - Accounts
src/models/account.js - Categories
src/models/category.js - Budgets
src/models/budget.js - Transactions · Sparse unique idempotency key; deleted records are retained.
src/models/transaction.js - MongoDB Atlas · Configured by MONGO_URI.
Contract & delivery
- OpenAPI contract
specs/001-budget-api/contracts/openapi.yaml - Vercel routing · Rewrites traffic to the serverless API.
vercel.json - CI & security · Runs lint, Mongo tests, secret scanning, and CodeQL.
.github/workflows/ci.yml
Connections 27
- Local server → Express API v1: starts
- Serverless entry → Express API v1: invokes
- Vercel routing → Serverless entry: rewrites to
- Express API v1 → Request guards: applies
- Request guards → Auth routes: protects
- Request guards → Resource routes: protects and validates
- Auth routes → Auth service: delegates
- Resource routes → Account & category services: delegates
- Resource routes → Budget service: delegates
- Resource routes → Transaction service: delegates
- Resource routes → Summary service: delegates
- Auth service → Users & refresh tokens: reads and writes
- Request guards → Users & refresh tokens: validates user context
- Account & category services → Accounts: manages
- Account & category services → Categories: manages
- Budget service → Budgets: upserts
- Transaction service → Transactions: creates and soft-deletes
- Summary service → Transactions: aggregates
- Summary service → Categories: breaks down by
- Summary service → Budgets: calculates progress from
- Users & refresh tokens → MongoDB Atlas: persists in
- Accounts → MongoDB Atlas: persists in
- Categories → MongoDB Atlas: persists in
- Budgets → MongoDB Atlas: persists in
- Transactions → MongoDB Atlas: persists in
- OpenAPI contract → Express API v1: documents
- CI & security → OpenAPI contract: checks repository delivery