A production-ready TypeScript microservices architecture for e-commerce order and invoice management, built with Domain-Driven Design (DDD), Command Query Responsibility Segregation (CQRS), and Event-Driven Architecture.
- Complete Order Lifecycle Management with status transitions:
CREATED
βACCEPTED
βREJECTED
βSHIPPING_IN_PROGRESS
βSHIPPED
- RESTful API with full CRUD operations
- Role-based Access Control (SELLER/BUYER roles)
- Event-driven architecture with domain events
- Comprehensive validation and error handling
- PDF Invoice Upload and management
- Automatic invoice sending when orders are shipped
- File storage with MIME type validation
- Cross-service communication via message queues
- Order projection for invoice generation
- Asynchronous messaging via RabbitMQ
- Domain events for loose coupling
- Event sourcing patterns
- Message handlers for cross-service operations
- Node.js 22 - Runtime environment
- TypeScript 5.8 - Type-safe development
- NestJS 11 - Progressive Node.js framework
- MikroORM - TypeScript ORM for MongoDB
- RabbitMQ - Message broker for async communication
- Vitest - Fast unit testing framework
- Biome - Fast formatter and linter
- Docker & Docker Compose - Containerization
- MongoDB 8.0 - NoSQL database
- RabbitMQ 3.12 - Message broker
- pnpm - Fast package manager
- Domain-Driven Design (DDD) - Business logic organization
- Command Query Responsibility Segregation (CQRS) - Command/Query separation
- Event-Driven Architecture - Loose coupling via events
- Hexagonal Architecture - Clean architecture principles
- Docker & Docker Compose - For containerized services
- Node.js 22+ - For local development
- pnpm - Package manager (install with
npm install -g pnpm
)
git clone https://github.com/sergioalmela/typescript-ecommerce-microservices.git
cd typescript-ecommerce-microservices
# Root environment
cp .env.example .env
# Order service environment
cp ./order-service/.env.example ./order-service/.env
# Invoice service environment
cp ./invoice-service/.env.example ./invoice-service/.env
# Start with Docker Compose
pnpm dev
# Or start with build
pnpm dev:build
- Order Service API: http://localhost:4000/api
- Invoice Service API: http://localhost:4001/api
- RabbitMQ Management: http://localhost:15672 (admin/securepassword)
Both services include Swagger UI for interactive API documentation:
Method | Endpoint | Description | Role Required |
---|---|---|---|
POST |
/orders |
Create a new order | BUYER |
GET |
/orders |
List all orders | SELLER |
GET |
/orders/:id |
Get order details | SELLER |
PATCH |
/orders/:id |
Update order status | SELLER |
Method | Endpoint | Description | Role Required |
---|---|---|---|
POST |
/invoices/:orderId/invoices |
Upload invoice PDF | SELLER |
All endpoints require authentication headers:
x-user-id: your-user-id
x-user-role: SELLER|BUYER
- OrderCreatedEvent - Published when a new order is created
- OrderStatusUpdatedEvent - Published when order status changes
- OrderShippedEvent - Published when order is marked as shipped
- CreateOrderProjectionMessageHandler - Listens to
OrderCreatedEvent
- Creates order projection for invoice service
- Queue:
invoice-service-order-created-queue
- SendPdfToCustomerMessageHandler - Listens to
OrderShippedEvent
- Automatically sends invoice when order is shipped
- Queue:
invoice-service-order-shipped-queue
curl -X POST http://localhost:4000/orders \
-H "Content-Type: application/json" \
-H "x-user-id: buyer-123" \
-H "x-user-role: BUYER" \
-d '{
"id": "123e4567-e89b-12d3-a456-426614174000",
"productId": "456e7890-e89b-12d3-a456-426614174000",
"quantity": 2,
"price": 29.99,
"customerId": "789e0123-e89b-12d3-a456-426614174000",
"sellerId": "012e3456-e89b-12d3-a456-426614174000"
}'
curl -X PATCH http://localhost:4000/orders/123e4567-e89b-12d3-a456-426614174000 \
-H "Content-Type: application/json" \
-H "x-user-id: seller-456" \
-H "x-user-role: SELLER" \
-d '{
"status": "SHIPPED"
}'
curl -X POST http://localhost:4001/invoices/123e4567-e89b-12d3-a456-426614174000/invoices \
-H "x-user-id: seller-456" \
-H "x-user-role: SELLER" \
-F "[email protected]"
pnpm test
pnpm test:coverage
pnpm test:watch
# Order service tests
cd order-service && pnpm test
# Invoice service tests
cd invoice-service && pnpm test
typescript-ecommerce-microservices/
βββ order-service/ # Order management microservice
β βββ src/
β β βββ app/ # Application layer
β β β βββ controllers/ # REST API controllers
β β β βββ auth/ # Authentication & authorization
β β βββ application/ # Application layer (CQRS)
β β β βββ command/ # Command handlers
β β β βββ query/ # Query handlers
β β βββ domain/ # Domain layer (DDD)
β β β βββ order/ # Order domain logic
β β β βββ common/ # Shared domain concepts
β β βββ infrastructure/ # Infrastructure layer
β β βββ persistence/ # Database adapters
β β βββ event/ # Event bus implementations
β β βββ testing/ # Test doubles
β βββ package.json
βββ invoice-service/ # Invoice management microservice
β βββ src/
β β βββ app/ # Application layer
β β β βββ controllers/ # REST API controllers
β β β βββ listeners/ # Message handlers
β β β βββ auth/ # Authentication & authorization
β β βββ application/ # Application layer (CQRS)
β β βββ domain/ # Domain layer (DDD)
β β βββ infrastructure/ # Infrastructure layer
β βββ package.json
βββ docker-compose.yml # Multi-service orchestration
βββ pnpm-workspace.yaml # pnpm workspace configuration
βββ README.md
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- Follow TypeScript best practices
- Maintain test coverage above 90%
- Use conventional commits
- Follow DDD principles
- Write clear documentation
β Star this repository if you find it helpful!