Onboarding Guide: treetracker-wallet-api

Generated from the Understand-Anything knowledge graph. Source commit e95c15a.

Project Overview

treetracker-wallet-api is a Greenstand Node.js/Express REST API backed by PostgreSQL (via Knex). It manages wallets that accumulate tree-planting tokens, trust relationships that gate which wallets may move tokens between each other, and the transfer lifecycle (pending, requested, completed, declined, cancelled) for both explicit tokens and unclaimed bundles. Every protected endpoint sits behind JWT (RS256) and API-key authentication, with per-wallet audit event logging.

The code follows a layered flow: router → handler → service → model → repository → database, with JWT/API-key authentication and event/audit logging as cross-cutting concerns.

Architecture Layers

Key Concepts

Guided Tour

Follow these steps in order to learn the codebase end to end:

  1. Project Overview: Read the README for setup (database, environment variables, JWT keygen) and the layered service/model/repository architecture.
  2. Bootstrap and App Configuration: server/server.js boots the process; server/app.js wires CORS, body parsing, all routes, and the global error handler.
  3. Routing Layer: server/routes/index.js aggregates sub-routers (auth, token, transfer, trust, wallet, event); each router maps a URL prefix to handler functions.
  4. Handlers and Request Validation: Handlers validate input via schemas.js, then delegate to the matching service (authHandler → AuthService, transferHandler → TransferService, etc.), keeping the service layer free of Express-specific code.
  5. Authentication: JWT and API Keys: AuthService verifies a hashed password and calls JWTService to sign an RS256 token; ApiKeyService validates the API-key header on protected endpoints.
  6. Service Layer: Business Logic: WalletService (CRUD, control-over checks), TrustService (trust lifecycle), and TransferService (the largest: initiate, accept, decline, cancel, fulfill, each wrapped in a transaction with an audit event).
  7. Domain Models: Wallet, Trust, Transfer: Wallet.js implements hierarchical control checks; Trust.js manages the trust lifecycle; Transfer.js orchestrates trust/control checks and state transitions per trust-enums.js / transfer-enum.js.
  8. Repositories and Database Session: BaseRepository provides shared CRUD and query building; WalletRepository unions self/child/managed wallets; Session.js wraps the Knex connection/transaction that TransferService depends on for atomicity.
  9. Database Schema and Migrations: db-migrate migrations create the wallet, entity_trust, and transfer tables; database/README.md explains the migration command reference.
  10. Testing Strategy: Unit, Integration, E2E: Unit tests (TransferService.spec.js) mock dependencies; integration tests (bundle-transfer-decline.spec.js) hit a seeded real database; E2E tests (__tests__/e2e/) run Mocha root hooks against a live deployed environment.
  11. Containerization: The Dockerfile (node:16-alpine, single-stage) packages the app; docker-compose.yml spins up local Postgres 13 for development.
  12. Kubernetes Deployment: Kustomize base manifests (deployment.yaml, kustomization.yaml) define production rollout (2 replicas, JWT/DB/S3 secrets), with dev/test/prod overlays.
  13. CI/CD Pipelines: The pull-request workflow lints, migrates, and tests on every PR; the build-deploy-dev workflow builds/pushes a Docker image, deploys to Kubernetes, then runs E2E tests against the freshly deployed environment.

File Map (by layer)

Complexity Hotspots

Approach these areas carefully: they carry the most logic or the largest surface (97 files rated moderate+, 33 rated complex):


Regenerate with /understand then /understand-domain then /understand-onboard.