How raouf-b-dev/ecommerce-store-api works
Purpose and entry points
A TypeScript/NestJS modular-monolith e-commerce REST API with WebSocket notifications. Bootstrap and module composition are in src/main.ts, src/app.module.ts, and src/infrastructure/infrastructure.module.ts. HTTP endpoints are versioned and exposed through feature controllers; Swagger is served at /api. The process also runs BullMQ workers/processors within the application.
Architectural style and boundaries
Each business bounded context follows hexagonal layering: pure domain entities/value objects and repository/gateway ports; application use cases; primary HTTP/job adapters; and secondary persistence/integration adapters. The shared kernel provides cross-context domain contracts and Result-style error handling.
• src/shared-kernel/domain/result.ts
• src/modules/orders/core/
• src/modules/orders/secondary-adapters/
Cross-context calls must go through application ports and module gateway adapters, rather than directly accessing another module’s repositories. The primary domains are identity, authentication, authorization, catalog/products, inventory, carts, orders, payments, and notifications.
Request, security, and API pipeline
Incoming REST requests pass through global configuration, exception/result/sanitization handling, correlation logging, authentication, permission checks, throttling, and optional Redis-backed idempotency before reaching controllers and use cases.
• src/config/config.module.ts
• src/guards/auth.guard.ts
• src/infrastructure/idempotency/idempotency.interceptor.ts
Authentication supports RSA JWT verification/JWKS and refresh-token sessions; authorization persists roles and permissions and enforces them at primary adapters.
• src/infrastructure/jwt/jwt.module.ts
• src/modules/authentication/authentication.module.ts
• src/modules/authorization/primary-adapter/guards/permissions.guard.ts
Commerce state and persistence
Products, identity, inventory, orders, payments, and authorization use domain repositories backed by PostgreSQL schemas/repositories, commonly wrapped by Redis cache-aside adapters. Carts use both Postgres and RedisJSON-oriented cached repositories; Redis also supports search, caching, throttling, and idempotency.
• src/infrastructure/database/database.module.ts
• src/infrastructure/redis/redis.module.ts
• src/modules/carts/secondary-adapters/repositories/cached-cart-repository/cached.cart-repository.ts
Inventory manages stock and reservations as separate domain concepts, supplying stock checks, reserve/confirm/release operations to carts and orders.
• src/modules/inventory/inventory.module.ts
• src/modules/inventory/core/application/reserve-stock/reserve-stock.usecase.ts
• src/modules/inventory/secondary-adapters/repositories/postgres-reservation-repository/postgres.reservation-repository.ts
Checkout and asynchronous orchestration
Orders coordinate checkout as a Saga: validate cart/user/address, create an order, reserve stock, create/process payment, then confirm reservation/order and clear the cart. Failures trigger compensating stock release, payment refund, and cancellation paths. BullMQ schedules and executes the staged workflow, decoupling HTTP initiation from completion.
• src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts
• src/modules/orders/primary-adapters/jobs/
• src/modules/orders/primary-adapters/listeners/checkout-failure.listener.ts
Payments abstract COD, Stripe, and PayPal behind gateway interfaces/factory adapters. Provider webhooks are signature-verified and translated into payment/order event handling.
• src/modules/payments/core/domain/gateways/payment-gateway.interface.ts
• src/modules/payments/secondary-adapters/gateways/payment-gateway.factory.ts
• src/modules/payments/core/application/services/handle-payment-webhook/handle-payment-webhook.service.ts
Events, notifications, and realtime delivery
Domain events use an EventEmitter publisher for in-process reactions and business metrics. Notifications persist history, use BullMQ flows for delivery/status/cleanup, and deliver over an authenticated Redis Socket.IO WebSocket adapter.
• src/infrastructure/events/event-emitter2.domain-event-publisher.ts
• src/modules/notifications/notifications.processor.ts
• src/infrastructure/websocket/adapters/redis-io.adapter.ts
Runtime, operations, and observability
Node.js runs in Docker with PostgreSQL and Redis; Compose can additionally provision Prometheus, Loki, Tempo, Promtail, and Grafana. The API exposes health and metrics endpoints, emits structured/correlated logs, and exports traces through OpenTelemetry.
• Dockerfile
• docker-compose.prod.yml
• src/infrastructure/metrics/metrics.module.ts
• src/infrastructure/tracing/tracing.ts
Graceful shutdown coordinates worker and connection cleanup; operational bootstrap/migrations/seeding are supplied through the data source and scripts.
• src/infrastructure/shutdown/shutdown.service.ts
• data-source.ts
• scripts/docker-migrate.js
Main entry points: main.ts Nest bootstrap · auth.guard.ts Request security pipeline · checkout.usecase.ts Checkout saga · redis.module.ts Redis
Components 22
Runtime & platform
- Nest bootstrap · Composes modules, HTTP API, and in-process workers.
src/main.ts - Infrastructure module
src/infrastructure/infrastructure.module.ts - PostgreSQL · Backs repository schemas for commerce and authorization.
src/infrastructure/database/database.module.ts - Redis · Supports cache, RedisJSON, search, throttling, idempotency, and BullMQ.
src/infrastructure/redis/redis.module.ts
API & security
- Global configuration
src/config/config.module.ts - Request security pipeline · Applies authentication, permissions, throttling, errors, logging, and idempotency.
src/guards/auth.guard.ts - Redis idempotency
- JWT authentication · Verifies RSA JWT/JWKS and manages refresh sessions.
src/modules/authentication/authentication.module.ts - Permission enforcement
src/modules/authorization/primary-adapter/guards/permissions.guard.ts - Feature controllers · Controllers expose bounded-context use cases and Swagger API.
src/app.module.ts
Commerce modules
- Commerce bounded contexts · Identity, catalog, inventory, carts, orders, payments, and authorization use ports between contexts.
- Cached cart repository · Combines Postgres with RedisJSON-oriented cache storage.
src/modules/carts/secondary-adapters/repositories/cached-cart-repository/cached.cart-repository.ts - Inventory reservations · Checks stock and reserves, confirms, or releases inventory.
src/modules/inventory/core/application/reserve-stock/reserve-stock.usecase.ts - Checkout saga · Coordinates validation, order creation, reservation, payment, confirmation, and compensation.
src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts - Payment gateway factory · Selects COD, Stripe, or PayPal gateway implementations.
src/modules/payments/secondary-adapters/gateways/payment-gateway.factory.ts - Payment webhook handling · Verifies provider events and updates payment/order handling.
src/modules/payments/core/application/services/handle-payment-webhook/handle-payment-webhook.service.ts
Async & realtime
- BullMQ workflows · Schedules checkout stages and notification delivery flows.
- Domain event publisher
src/infrastructure/events/event-emitter2.domain-event-publisher.ts - Notification processor · Persists, delivers, tracks, and cleans up notifications.
src/modules/notifications/notifications.processor.ts - Redis Socket.IO adapter
src/infrastructure/websocket/adapters/redis-io.adapter.ts
Operations
- Metrics and tracing · Exports health, metrics, structured logs, and OpenTelemetry traces.
src/infrastructure/metrics/metrics.module.ts - Graceful shutdown · Coordinates worker and connection cleanup.
src/infrastructure/shutdown/shutdown.service.ts
Connections 25
- Nest bootstrap → Infrastructure module: loads
- Nest bootstrap → Global configuration: loads
- Nest bootstrap → Feature controllers: serves
- Feature controllers → Request security pipeline: passes through
- Request security pipeline → Redis idempotency: uses
- Request security pipeline → JWT authentication: authenticates with
- Request security pipeline → Permission enforcement: authorizes with
- Feature controllers → Commerce bounded contexts: invokes use cases
- Commerce bounded contexts → Cached cart repository: persists carts via
- Commerce bounded contexts → Checkout saga: starts checkout
- Checkout saga → Inventory reservations: reserves / compensates
- Checkout saga → Payment gateway factory: processes payment through
- Payment webhook handling → Commerce bounded contexts: updates payment and orders
- Checkout saga → BullMQ workflows: stages asynchronously
- BullMQ workflows → Redis: uses
- Commerce bounded contexts → PostgreSQL: repository persistence
- Cached cart repository → Redis: caches in
- Redis idempotency → Redis: stores keys
- Commerce bounded contexts → Domain event publisher: publishes
- Domain event publisher → Notification processor: triggers
- Notification processor → BullMQ workflows: queues delivery flows
- Notification processor → Redis Socket.IO adapter: delivers realtime
- Redis Socket.IO adapter → Redis: scales through
- Nest bootstrap → Metrics and tracing: emits telemetry
- Nest bootstrap → Graceful shutdown: coordinates lifecycle