A production-grade order management system with service-oriented architecture, event sourcing, and comprehensive testing.
- Service Layer: Clean separation with
OrderService,PaymentService,EventService - Event Sourcing: Every action creates audit events
- Authentication: Real password hashing with bcrypt
- Rate Limiting: Redis-backed with memory fallback
- Testing: Vitest with unit and integration tests
# Install dependencies
npm install
# Setup database
npm run db:seed
# Start development server
npm run dev
# Run tests
npm run test:unit
npm run test:integrationnpm run dev- Start development servernpm run build- Build for productionnpm run start- Start production server
npm run test- Run all testsnpm run test:unit- Run unit tests onlynpm run test:integration- Run integration tests onlynpm run test:watch- Run tests in watch modenpm run test:coverage- Generate coverage reportnpm run test:ui- Open test UI
npm run db:generate- Generate Prisma clientnpm run db:migrate- Run migrationsnpm run db:studio- Open Prisma Studionpm run db:seed- Reset and seed databasenpm run seed- Seed database only
npm run lint- Run ESLintnpm run lint:fix- Fix ESLint issuesnpm run typecheck- Run TypeScript type checking
import { orderService } from './server/modules/orders/order.service'
// Create order with full validation and events
const order = await orderService.createOrder({
sellerId,
customerId,
items: [...],
deliveryFeeMinor: 500,
}, actorUserId)
// Apply status transitions with validation
await orderService.applyTransition({
orderId,
newStatus: 'PACKED',
actorUserId
})import { paymentService } from "./server/modules/orders/payment.service";
// Simulate payment with automatic order updates
await paymentService.simulatePayment(orderId, true, actorUserId);
// Real payment processing
await paymentService.initiatePayment({ orderId, provider });
await paymentService.confirmPayment({ orderId, provider, providerReference });import { eventService } from "./server/modules/orders/event.service";
// Query audit trail
const events = await eventService.getOrderEvents(orderId);Real authentication with password hashing:
import { authenticateUser } from "./server/lib/auth";
const { user, token } = await authenticateUser(email, password);Demo credentials:
- Email:
demo@seller.com - Password:
demo123
Every action creates immutable events:
order_created- New order creationpayment_initiated- Payment startedpayment_completed- Payment successfulstatus_changed- Order status updatespayment_failed- Payment failures
Comprehensive test suite with Vitest:
# Unit tests
npm run test:unit
# Integration tests
npm run test:integration
# Full coverage
npm run test:coverage- Users: Authentication and roles
- Sellers: Store management
- Products: Inventory with stock tracking
- Customers: Customer information
- Orders: Order management with status
- OrderItems: Line items with snapshots
- OrderEvents: Complete audit trail
- PaymentAttempts: Payment processing
Redis-backed rate limiting with memory fallback:
- Public API: 10 requests/minute
- Auth endpoints: 5 requests/minute
- Seller API: 100 requests/minute
Structured JSON logging with request IDs:
logger.info("Order created", { orderId, sellerId });
logger.error("Payment failed", { orderId, reason });PENDING β CONFIRMED β PACKED β OUT_FOR_DELIVERY β DELIVERED
β β β β
CANCELLED CANCELLED CANCELLED CANCELLED
PENDING β PROCESSING β COMPLETED β REFUNDED
β β
FAILED FAILED
# Database
DATABASE_URL="file:./dev.db"
# Authentication
JWT_SECRET="your-jwt-secret"
# Rate Limiting (Optional)
REDIS_URL="redis://localhost:6379"- Build the application:
npm run build-
Set production environment variables
-
Run database migrations:
npm run db:deploy- Start the server:
npm run startReset development environment:
npm run db:seedGET /api/public/{sellerSlug}/products- List active productsPOST /api/public/{sellerSlug}/orders- Create new order
POST /api/auth/login- Login and get JWT tokenDELETE /api/auth/login- LogoutGET /api/me- Get current user info
GET /api/seller/orders- List orders with paginationGET /api/seller/orders/{id}- Get order detailsPATCH /api/seller/orders/{id}/status- Update order statusGET /api/seller/products- List productsPOST /api/seller/products- Create productPATCH /api/seller/products/{id}- Update productDELETE /api/seller/products/{id}- Delete product
- Fork the repository
- Create a feature branch
- Make your changes
- Run tests:
npm run test - Submit a pull request
MIT License - see LICENSE file for details.