Stage 1: Problem
The Problem
You are building an order management system. When an order is placed, several things must happen:
typescript
| 1 | class OrderService { |
| 2 | async placeOrder(order: Order): Promise<void> { |
| 3 | await this.orderRepo.save(order); |
| 4 | |
| 5 | // Send confirmation email |
| 6 | await this.emailService.sendConfirmation(order.customerEmail, order); |
| 7 | |
| 8 | // Update inventory |
| 9 | await this.inventoryService.reduceStock(order.items); |
| 10 | |
| 11 | // Notify analytics |
| 12 | await this.analyticsService.trackPurchase(order); |
| 13 | |
| 14 | // Send to warehouse |
| 15 | await this.warehouseService.queueFulfillment(order); |
| 16 | } |
| 17 | } |
This works. Then:
- SMS notifications ā marketing wants to send an SMS to customers who opted in.
- Loyalty points ā the rewards team needs to credit points on purchase.
- Fraud detection ā security team needs to score every order.
- Real-time dashboard ā ops wants live order counts pushed to a dashboard via WebSocket.
- Partner API ā a third-party marketplace needs webhook notifications.
Each new requirement means editing placeOrder. The OrderService now imports 8 different services. It's become the God method ā the single point of failure for the entire order flow.
How do you decouple order placement from all the things that react to it?