# raouf-b-dev/ecommerce-store-api architecture

> Architecture diagram of the GitHub repository raouf-b-dev/ecommerce-store-api, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-20). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/raouf-b-dev/ecommerce-store-api
- Repository: https://github.com/raouf-b-dev/ecommerce-store-api

## Overview

#### 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: [`src/main.ts`](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/main.ts) (Nest bootstrap), [`src/guards/auth.guard.ts`](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/guards/auth.guard.ts) (Request security pipeline), [`src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts`](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts) (Checkout saga), [`src/infrastructure/redis/redis.module.ts`](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/redis/redis.module.ts) (Redis)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_runtime["Runtime &amp; platform"]
  node_bootstrap["Nest bootstrap<br/>application composition<br/>[main.ts]"]
  node_infrastructure["Infrastructure module<br/>platform composition"]
  node_postgres[("PostgreSQL<br/>primary datastore<br/>[database.module.ts]")]
  node_redis[("Redis<br/>cache and queue backing<br/>[redis.module.ts]")]
end

subgraph group_api["API &amp; security"]
  node_config["Global configuration<br/>configuration module<br/>[config.module.ts]"]
  node_request_pipeline["Request security pipeline<br/>global guards and interceptors<br/>[auth.guard.ts]"]
  node_idempotency["Redis idempotency<br/>HTTP interceptor"]
  node_jwt_auth["JWT authentication<br/>authentication module"]
  node_permissions["Permission enforcement<br/>authorization guard"]
  node_feature_api["Feature controllers<br/>versioned REST adapters<br/>[app.module.ts]"]
end

subgraph group_commerce["Commerce modules"]
  node_commerce_modules["Commerce bounded contexts<br/>hexagonal modules"]
  node_cart_repository["Cached cart repository<br/>cart persistence adapter"]
  node_inventory["Inventory reservations<br/>inventory application"]
  node_checkout["Checkout saga<br/>order use case"]
  node_payments["Payment gateway factory<br/>payment integration adapter"]
  node_payment_webhooks["Payment webhook handling<br/>payment application service"]
end

subgraph group_async["Async &amp; realtime"]
  node_bullmq["BullMQ workflows<br/>job workers"]
  node_domain_events["Domain event publisher<br/>in-process event bus"]
  node_notifications["Notification processor<br/>notification worker"]
  node_websocket["Redis Socket.IO adapter<br/>authenticated WebSocket delivery"]
end

subgraph group_operations["Operations"]
  node_observability["Metrics and tracing<br/>observability platform<br/>[metrics.module.ts]"]
  node_shutdown["Graceful shutdown<br/>lifecycle service"]
end

node_bootstrap -->|"loads"| node_infrastructure
node_bootstrap -->|"loads"| node_config
node_bootstrap -->|"serves"| node_feature_api
node_feature_api -->|"passes through"| node_request_pipeline
node_request_pipeline -->|"uses"| node_idempotency
node_request_pipeline -->|"authenticates with"| node_jwt_auth
node_request_pipeline -->|"authorizes with"| node_permissions
node_feature_api -->|"invokes use cases"| node_commerce_modules
node_commerce_modules -->|"persists carts via"| node_cart_repository
node_commerce_modules -->|"starts checkout"| node_checkout
node_checkout -->|"reserves / compensates"| node_inventory
node_checkout -->|"processes payment through"| node_payments
node_payment_webhooks -->|"updates payment and orders"| node_commerce_modules
node_checkout -->|"stages asynchronously"| node_bullmq
node_bullmq -->|"uses"| node_redis
node_commerce_modules -->|"repository persistence"| node_postgres
node_cart_repository -->|"caches in"| node_redis
node_idempotency -->|"stores keys"| node_redis
node_commerce_modules -->|"publishes"| node_domain_events
node_domain_events -->|"triggers"| node_notifications
node_notifications -->|"queues delivery flows"| node_bullmq
node_notifications -->|"delivers realtime"| node_websocket
node_websocket -->|"scales through"| node_redis
node_bootstrap -->|"emits telemetry"| node_observability
node_bootstrap -->|"coordinates lifecycle"| node_shutdown

click node_bootstrap "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/main.ts"
click node_infrastructure "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/infrastructure.module.ts"
click node_config "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/config/config.module.ts"
click node_request_pipeline "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/guards/auth.guard.ts"
click node_jwt_auth "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/authentication/authentication.module.ts"
click node_permissions "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/authorization/primary-adapter/guards/permissions.guard.ts"
click node_feature_api "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/app.module.ts"
click node_cart_repository "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/carts/secondary-adapters/repositories/cached-cart-repository/cached.cart-repository.ts"
click node_inventory "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/inventory/core/application/reserve-stock/reserve-stock.usecase.ts"
click node_checkout "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts"
click node_payments "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/payments/secondary-adapters/gateways/payment-gateway.factory.ts"
click node_payment_webhooks "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/payments/core/application/services/handle-payment-webhook/handle-payment-webhook.service.ts"
click node_postgres "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/database/database.module.ts"
click node_redis "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/redis/redis.module.ts"
click node_domain_events "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/events/event-emitter2.domain-event-publisher.ts"
click node_notifications "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/notifications/notifications.processor.ts"
click node_websocket "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/websocket/adapters/redis-io.adapter.ts"
click node_observability "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/metrics/metrics.module.ts"
click node_shutdown "https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/shutdown/shutdown.service.ts"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_bootstrap,node_infrastructure,node_postgres,node_redis toneBlue
class node_config,node_request_pipeline,node_idempotency,node_jwt_auth,node_permissions,node_feature_api toneAmber
class node_commerce_modules,node_cart_repository,node_inventory,node_checkout,node_payments,node_payment_webhooks toneMint
class node_bullmq,node_domain_events,node_notifications,node_websocket toneRose
class node_observability,node_shutdown toneIndigo
```

## Components

### Runtime & platform

- [Nest bootstrap](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/main.ts): application composition · Composes modules, HTTP API, and in-process workers. · `src/main.ts`
- [Infrastructure module](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/infrastructure.module.ts): platform composition · `src/infrastructure/infrastructure.module.ts`
- [PostgreSQL](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/database/database.module.ts): primary datastore · Backs repository schemas for commerce and authorization. · `src/infrastructure/database/database.module.ts`
- [Redis](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/redis/redis.module.ts): cache and queue backing · Supports cache, RedisJSON, search, throttling, idempotency, and BullMQ. · `src/infrastructure/redis/redis.module.ts`

### API & security

- [Global configuration](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/config/config.module.ts): configuration module · `src/config/config.module.ts`
- [Request security pipeline](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/guards/auth.guard.ts): global guards and interceptors · Applies authentication, permissions, throttling, errors, logging, and idempotency. · `src/guards/auth.guard.ts`
- **Redis idempotency**: HTTP interceptor
- [JWT authentication](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/authentication/authentication.module.ts): authentication module · Verifies RSA JWT/JWKS and manages refresh sessions. · `src/modules/authentication/authentication.module.ts`
- [Permission enforcement](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/authorization/primary-adapter/guards/permissions.guard.ts): authorization guard · `src/modules/authorization/primary-adapter/guards/permissions.guard.ts`
- [Feature controllers](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/app.module.ts): versioned REST adapters · Controllers expose bounded-context use cases and Swagger API. · `src/app.module.ts`

### Commerce modules

- **Commerce bounded contexts**: hexagonal modules · Identity, catalog, inventory, carts, orders, payments, and authorization use ports between contexts.
- [Cached cart repository](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/carts/secondary-adapters/repositories/cached-cart-repository/cached.cart-repository.ts): cart persistence adapter · Combines Postgres with RedisJSON-oriented cache storage. · `src/modules/carts/secondary-adapters/repositories/cached-cart-repository/cached.cart-repository.ts`
- [Inventory reservations](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/inventory/core/application/reserve-stock/reserve-stock.usecase.ts): inventory application · Checks stock and reserves, confirms, or releases inventory. · `src/modules/inventory/core/application/reserve-stock/reserve-stock.usecase.ts`
- [Checkout saga](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts): order use case · Coordinates validation, order creation, reservation, payment, confirmation, and compensation. · `src/modules/orders/core/application/usecases/checkout/checkout.usecase.ts`
- [Payment gateway factory](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/payments/secondary-adapters/gateways/payment-gateway.factory.ts): payment integration adapter · Selects COD, Stripe, or PayPal gateway implementations. · `src/modules/payments/secondary-adapters/gateways/payment-gateway.factory.ts`
- [Payment webhook handling](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/payments/core/application/services/handle-payment-webhook/handle-payment-webhook.service.ts): payment application service · 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**: job workers · Schedules checkout stages and notification delivery flows.
- [Domain event publisher](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/events/event-emitter2.domain-event-publisher.ts): in-process event bus · `src/infrastructure/events/event-emitter2.domain-event-publisher.ts`
- [Notification processor](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/modules/notifications/notifications.processor.ts): notification worker · Persists, delivers, tracks, and cleans up notifications. · `src/modules/notifications/notifications.processor.ts`
- [Redis Socket.IO adapter](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/websocket/adapters/redis-io.adapter.ts): authenticated WebSocket delivery · `src/infrastructure/websocket/adapters/redis-io.adapter.ts`

### Operations

- [Metrics and tracing](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/metrics/metrics.module.ts): observability platform · Exports health, metrics, structured logs, and OpenTelemetry traces. · `src/infrastructure/metrics/metrics.module.ts`
- [Graceful shutdown](https://github.com/raouf-b-dev/ecommerce-store-api/blob/master/src/infrastructure/shutdown/shutdown.service.ts): lifecycle service · Coordinates worker and connection cleanup. · `src/infrastructure/shutdown/shutdown.service.ts`

## Connections

- 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
