Skip to content

0-sayed/wallet

Repository files navigation

Wallet and Royalty API

CI Node 22 TypeScript License: MIT

A production-minded backend for wallet balances, royalty splits, and async financial reporting, designed around correctness under concurrency.

About

This project is a mini financial system API built to prioritise transactional integrity over raw HTTP throughput.

  • Uses PostgreSQL transactions and SELECT FOR UPDATE locking to prevent double-spending.
  • Enforces idempotency with a client-owned Idempotency-Key and database uniqueness constraints.
  • Stores all money as integer cents to avoid floating point errors.
  • Uses deterministic royalty math: author gets floor(70%), platform gets the remainder.
  • Uses BullMQ + Redis for non-blocking report generation.

Tech Stack

  • NestJS + TypeScript (strict)
  • PostgreSQL + Drizzle ORM
  • BullMQ + Redis
  • Jest + Supertest
  • Docker Compose + pnpm

API Surface

All endpoints except /health require an X-User-Id header (UUID).

Method Path Description Extra Headers
GET /health Health check
POST /wallets/:walletId/deposit Deposit funds into a wallet
POST /purchases Purchase an item (3-wallet royalty split) Idempotency-Key (UUID)
POST /reports/financial Request an async financial report
GET /reports/financial/:jobId Poll report status and result

Swagger UI is available at /api when SWAGGER_ENABLED=true.

Run Locally

Prerequisites: Node.js 22+, pnpm 9+, Docker + Docker Compose.

  1. Install dependencies.
pnpm install
  1. Create local environment file.
cp .env.example .env
  1. Start PostgreSQL and Redis.
docker compose up -d
  1. Apply database migrations.
pnpm db:migrate
  1. Seed the platform account.
pnpm db:seed
  1. Start the API.
pnpm start:dev
  1. Verify health endpoint.
curl -s http://localhost:3000/health
# → { "status": "ok" }

Commands

Command Description
pnpm lint ESLint with auto-fix
pnpm format Prettier write
pnpm typecheck TypeScript type check
pnpm test Unit tests
pnpm test:e2e E2e tests
pnpm test:cov Unit tests with coverage
pnpm knip Dead code / unused deps check
pnpm validate Full local quality gate (lint + typecheck + test + knip + build)
pnpm db:generate Generate Drizzle migration from schema changes
pnpm db:migrate Apply pending migrations
pnpm db:seed Seed platform user and wallet
pnpm db:studio Drizzle Studio GUI

Architecture

For system context diagrams, module structure, data model, concurrency details, and the full request lifecycle, see docs/architecture.md.

Design Decisions

Key decisions are captured in ADRs:

Project Docs

License

MIT

About

Wallet and royalty API focused on idempotency, pessimistic locking, and atomic transactions.

Topics

Resources

License

Security policy

Stars

Watchers

Forks

Contributors