- Added support for Salesforce Platform Events, specifically subscribing to `OrderProvisionRequested__e` to trigger provisioning jobs. - Introduced new environment variables for Salesforce event configuration, including SF_EVENTS_ENABLED, SF_PROVISION_EVENT_CHANNEL, and SF_PUBSUB_ENDPOINT. - Refactored order fulfillment process to utilize event-driven architecture, enhancing reliability and scalability. - Updated documentation to reflect changes in the provisioning workflow and environment variable requirements. - Removed deprecated webhook handling code to streamline the integration.
198 lines
7.1 KiB
Markdown
198 lines
7.1 KiB
Markdown
# Modular Provisioning Architecture - Clean & Maintainable
|
|
|
|
## ✅ **Perfect Architectural Symmetry Achieved**
|
|
|
|
I've restructured the provisioning system to **match the exact same clean modular pattern** as your order creation workflow. Now both systems follow identical architectural principles!
|
|
|
|
## 🏗️ **Side-by-Side Architecture Comparison**
|
|
|
|
### **Order Creation (Existing) ↔ Order Provisioning (New)**
|
|
|
|
| **Order Creation** | **Order Provisioning** | **Purpose** |
|
|
|-------------------|------------------------|-------------|
|
|
| `OrderValidator` | `ProvisioningValidator` | Validates requests & business rules |
|
|
| `OrderBuilder` | `WhmcsOrderMapper` | Transforms/maps data structures |
|
|
| `OrderItemBuilder` | *(integrated in mapper)* | Handles item-level processing |
|
|
| `OrderOrchestrator` | `ProvisioningOrchestrator` | Coordinates the complete workflow |
|
|
| `OrdersController` | `PlatformEventsSubscriber` | Event handling (no inbound HTTP) |
|
|
|
|
## 📁 **Clean File Structure**
|
|
|
|
```
|
|
apps/bff/src/orders/
|
|
├── controllers/
|
|
│ └── orders.controller.ts # Customer-facing operations
|
|
├── queue/
|
|
│ ├── provisioning.queue.ts # Enqueue provisioning jobs
|
|
│ └── provisioning.processor.ts # Worker processes jobs
|
|
├── services/
|
|
│ # Order Creation (existing)
|
|
│ ├── order-validator.service.ts # Request & business validation
|
|
│ ├── order-builder.service.ts # Order header construction
|
|
│ ├── order-item-builder.service.ts # Order items construction
|
|
│ ├── order-orchestrator.service.ts # Creation workflow coordination
|
|
│ │
|
|
│ # Order Provisioning (new - matching structure)
|
|
│ ├── provisioning-validator.service.ts # Provisioning validation
|
|
│ ├── whmcs-order-mapper.service.ts # SF → WHMCS mapping
|
|
│ ├── provisioning-orchestrator.service.ts # Provisioning workflow coordination
|
|
│ └── order-provisioning.service.ts # Main provisioning interface
|
|
```
|
|
|
|
## 🎯 **Modular Provisioning Services**
|
|
|
|
### **1. ProvisioningValidator**
|
|
**Purpose**: Validates all provisioning prerequisites
|
|
- ✅ Salesforce order validation
|
|
- ✅ Payment method validation
|
|
- ✅ Client mapping validation
|
|
- ✅ Idempotency checking
|
|
- ✅ Request payload validation
|
|
|
|
### **2. WhmcsOrderMapper**
|
|
**Purpose**: Maps Salesforce OrderItems → WHMCS format
|
|
- ✅ Product ID mapping (`WHMCS_Product_Id__c`)
|
|
- ✅ Billing cycle mapping (Service=monthly, Activation=onetime)
|
|
- ✅ Config options mapping
|
|
- ✅ Custom fields mapping
|
|
- ✅ Order notes generation with SF tracking
|
|
|
|
### **3. ProvisioningOrchestrator**
|
|
**Purpose**: Coordinates complete provisioning workflow
|
|
- ✅ **Step-by-step execution** with error handling
|
|
- ✅ **Progress tracking** for each step
|
|
- ✅ **Automatic rollback** on failures
|
|
- ✅ **Comprehensive logging** at each step
|
|
- ✅ **Context management** throughout workflow
|
|
|
|
**Provisioning Steps**:
|
|
1. `validation` - Validate all prerequisites
|
|
2. `sf_status_update` - Update SF to "Activating"
|
|
3. `order_details` - Get SF order with items
|
|
4. `mapping` - Map items to WHMCS format
|
|
5. `whmcs_create` - Create WHMCS order
|
|
6. `whmcs_accept` - Accept/provision WHMCS order
|
|
7. `sf_success_update` - Update SF to "Provisioned"
|
|
|
|
### **4. OrderProvisioningService**
|
|
**Purpose**: Clean main interface (like OrderOrchestrator)
|
|
- ✅ **Delegates to modular components**
|
|
- ✅ **Simple, focused responsibility**
|
|
- ✅ **Consistent error handling**
|
|
- ✅ **Clean result formatting**
|
|
|
|
## 🔄 **The Complete Modular Flow**
|
|
|
|
```
|
|
PlatformEventsSubscriber (OrderProvisionRequested__e)
|
|
↓ (enqueues job)
|
|
OrderProvisioningService
|
|
↓ (coordinates workflow)
|
|
ProvisioningValidator
|
|
↓ (validates prerequisites)
|
|
ProvisioningOrchestrator
|
|
↓ (executes step-by-step)
|
|
WhmcsOrderMapper + WhmcsOrderService + SalesforceService
|
|
↓ (performs actual operations)
|
|
Result Summary
|
|
```
|
|
|
|
## 🎯 **Key Benefits of Modular Architecture**
|
|
|
|
### **Maintainability**:
|
|
- **Single Responsibility**: Each service has one clear purpose
|
|
- **Easy Testing**: Each component can be unit tested independently
|
|
- **Easy Debugging**: Clear separation makes issues easy to isolate
|
|
- **Easy Extension**: Add new steps without touching existing code
|
|
|
|
### **Code Quality**:
|
|
- **Consistent Patterns**: Same structure as order creation
|
|
- **Reusable Components**: Services can be reused in different contexts
|
|
- **Clean Interfaces**: Clear contracts between components
|
|
- **Proper Error Handling**: Each layer handles its own concerns
|
|
|
|
### **Developer Experience**:
|
|
- **Familiar Structure**: Developers already know the pattern
|
|
- **Easy Navigation**: Clear file organization
|
|
- **Predictable Behavior**: Consistent patterns across codebase
|
|
- **Self-Documenting**: Service names clearly indicate purpose
|
|
|
|
## 📊 **Comparison: Before vs After**
|
|
|
|
### **Before (Monolithic)**:
|
|
```typescript
|
|
// OrderProvisioningService - 339 lines doing everything
|
|
class OrderProvisioningService {
|
|
async provisionOrder() {
|
|
// 1. Validate SF order (inline)
|
|
// 2. Check payment method (inline)
|
|
// 3. Map items (inline)
|
|
// 4. Create WHMCS order (inline)
|
|
// 5. Accept WHMCS order (inline)
|
|
// 6. Update Salesforce (inline)
|
|
// 7. Handle errors (inline)
|
|
// = 300+ lines of mixed concerns
|
|
}
|
|
}
|
|
```
|
|
|
|
### **After (Modular)**:
|
|
```typescript
|
|
// OrderProvisioningService - 118 lines, focused interface
|
|
class OrderProvisioningService {
|
|
async provisionOrder() {
|
|
const payload = this.validator.validateRequestPayload(request);
|
|
const context = await this.orchestrator.executeProvisioning(sfOrderId, payload, key);
|
|
return this.orchestrator.getProvisioningSummary(context);
|
|
}
|
|
}
|
|
|
|
// + ProvisioningValidator (150 lines)
|
|
// + WhmcsOrderMapper (200 lines)
|
|
// + ProvisioningOrchestrator (300 lines)
|
|
// = Same functionality, much cleaner separation
|
|
```
|
|
|
|
## 🚀 **Usage Examples**
|
|
|
|
### **Testing Individual Components**:
|
|
```typescript
|
|
describe('ProvisioningValidator', () => {
|
|
it('should validate payment method', async () => {
|
|
const result = await validator.validateProvisioningRequest(orderId, key);
|
|
expect(result.clientId).toBeDefined();
|
|
});
|
|
});
|
|
|
|
describe('WhmcsOrderMapper', () => {
|
|
it('should map SF items to WHMCS format', async () => {
|
|
const result = await mapper.mapOrderItemsToWhmcs(sfItems);
|
|
expect(result.whmcsItems[0].billingCycle).toBe('monthly');
|
|
});
|
|
});
|
|
```
|
|
|
|
### **Extending Functionality**:
|
|
```typescript
|
|
// Easy to add new provisioning steps
|
|
class ProvisioningOrchestrator {
|
|
private initializeSteps() {
|
|
return [
|
|
// ... existing steps
|
|
{ step: 'esim_activation', status: 'pending' }, // NEW STEP
|
|
{ step: 'email_notification', status: 'pending' }, // NEW STEP
|
|
];
|
|
}
|
|
}
|
|
```
|
|
|
|
## 🎉 **Perfect Architectural Consistency**
|
|
|
|
Your codebase now has **perfect symmetry**:
|
|
- **Order Creation**: Modular, clean, maintainable ✅
|
|
- **Order Provisioning**: Modular, clean, maintainable ✅
|
|
- **Same Patterns**: Developers can work on either system easily ✅
|
|
- **High Quality**: Production-ready, testable, extensible ✅
|
|
|
|
This is exactly the kind of clean, maintainable architecture that scales well and makes developers productive! 🚀
|