- Deleted migration file that removed cached profile fields from the users table, centralizing profile data retrieval from WHMCS. - Updated CsrfMiddleware to include new public authentication endpoints for password reset, setting password, and WHMCS account linking. - Enhanced error handling in password and WHMCS linking workflows to provide clearer feedback on missing mappings and improve user experience. - Adjusted user creation and update methods in UsersFacade to handle cases where WHMCS mappings are not yet available, ensuring smoother account setup.
8.8 KiB
SIM Manager Migration Package
This folder contains all the code, logic, and rules for the SIM Manager feature and Freebit API integration. Use this package when migrating SIM Manager functionality to a different branch.
📁 Folder Structure
sim-manager-migration/
├── backend/
│ ├── freebit-integration/ # Freebit API integration layer
│ │ ├── services/ # Freebit API service implementations
│ │ ├── interfaces/ # TypeScript interfaces and types
│ │ └── freebit.module.ts # NestJS module
│ ├── sim-management/ # SIM management business logic
│ │ ├── services/ # SIM management services
│ │ ├── types/ # Request/response types
│ │ ├── interfaces/ # Business logic interfaces
│ │ ├── sim-management.module.ts # NestJS module
│ │ └── sim-management.service.ts # Main service facade
│ └── controllers/ # API endpoint definitions
│ └── sim-endpoints.controller.ts
├── frontend/ # React/Next.js components
│ ├── components/ # SIM management UI components
│ ├── utils/ # Frontend utilities
│ └── index.ts # Exports
└── docs/ # Documentation
├── FREEBIT-SIM-MANAGEMENT.md # Complete implementation guide
└── SIM-MANAGEMENT-API-DATA-FLOW.md # API data flow documentation
🚀 Migration Steps
1. Backend Setup
Step 1.1: Copy Freebit Integration
Copy the entire backend/freebit-integration/ folder to:
apps/bff/src/integrations/freebit/
Step 1.2: Copy SIM Management Module
Copy the entire backend/sim-management/ folder to:
apps/bff/src/modules/subscriptions/sim-management/
Also copy:
backend/sim-management/sim-management.service.ts
to:
apps/bff/src/modules/subscriptions/sim-management.service.ts
Step 1.3: Add Controller Endpoints
The file backend/controllers/sim-endpoints.controller.ts contains all SIM-related endpoints. You need to:
- Option A: Add these methods to your existing
SubscriptionsController - Option B: Create a separate
SimManagementControllerand register it
The endpoints are:
GET /api/subscriptions/:id/sim- Get comprehensive SIM infoGET /api/subscriptions/:id/sim/details- Get SIM detailsGET /api/subscriptions/:id/sim/usage- Get usage dataGET /api/subscriptions/:id/sim/top-up-history- Get top-up historyPOST /api/subscriptions/:id/sim/top-up- Top up data quotaPOST /api/subscriptions/:id/sim/change-plan- Change planPOST /api/subscriptions/:id/sim/cancel- Cancel SIMPOST /api/subscriptions/:id/sim/reissue-esim- Reissue eSIMPOST /api/subscriptions/:id/sim/features- Update featuresGET /api/subscriptions/:id/sim/debug- Debug endpoint
Step 1.4: Register Modules
Ensure your main app module imports both modules:
// apps/bff/src/app.module.ts or subscriptions.module.ts
import { FreebitModule } from "@bff/integrations/freebit/freebit.module";
import { SimManagementModule } from "@bff/modules/subscriptions/sim-management/sim-management.module";
@Module({
imports: [
// ... other modules
FreebitModule,
SimManagementModule,
],
// ...
})
2. Frontend Setup
Step 2.1: Copy Frontend Components
Copy the entire frontend/ folder to:
apps/portal/src/features/sim-management/
Step 2.2: Integrate into Subscription Page
The main component is SimManagementSection.tsx. Import and use it in your subscription detail page:
import { SimManagementSection } from "@/features/sim-management";
// In your subscription detail component:
<SimManagementSection subscriptionId={subscriptionId} />
3. Environment Configuration
Add these environment variables to your .env file:
# Freebit API Configuration
FREEBIT_BASE_URL=https://i1-q.mvno.net/emptool/api/
# Production: FREEBIT_BASE_URL=https://i1.mvno.net/emptool/api
FREEBIT_OEM_ID=PASI
FREEBIT_OEM_KEY=your_oem_key_here
FREEBIT_TIMEOUT=30000
FREEBIT_RETRY_ATTEMPTS=3
4. Dependencies
Required NestJS Modules
@nestjs/common@nestjs/confignestjs-pino(for logging)
Required Services
The SIM Manager depends on these services (ensure they exist in your branch):
-
WHMCS Integration (for top-up payments):
WhmcsServicewithcreateInvoice()andcapturePayment()methods- Required for the top-up payment flow
-
Subscriptions Service:
SubscriptionsService- for subscription data access
-
Mappings Service:
MappingsModule- for ID mapping between systems
-
Email Service:
EmailModule- for notifications
-
Sim Usage Store:
SimUsageStoreService- for caching usage data
5. Type Definitions
The code uses types from @customer-portal/domain. Ensure these types are available:
SimTopupRequestSimChangePlanRequestSimCancelRequestSimFeaturesRequestSimDetailsSimUsageSimTopUpHistory
If these don't exist in your domain package, they are defined in:
backend/sim-management/types/sim-requests.types.tsbackend/freebit-integration/interfaces/freebit.types.ts
📋 Freebit API Endpoints Used
The implementation uses the following Freebit APIs:
- PA01-01: OEM Authentication (
/authOem/) - PA03-02: Get Account Details (
/mvno/getDetail/) - PA04-04: Add Specs & Quota (
/master/addSpec/) - PA05-01: MVNO Communication Information (
/mvno/getTrafficInfo/) - PA05-02: MVNO Quota Addition History (
/mvno/getQuotaHistory/) - PA05-04: MVNO Plan Cancellation (
/mvno/releasePlan/) - PA05-21: MVNO Plan Change (
/mvno/changePlan/) - PA05-22: MVNO Quota Settings (
/mvno/eachQuota/) - PA05-42: eSIM Profile Reissue (
/esim/reissueProfile/) - Enhanced: eSIM Add Account/Reissue (
/mvno/esim/addAcnt/)
🔧 Key Features
SIM Management Operations
- ✅ View SIM details (ICCID, MSISDN, plan, status)
- ✅ Real-time data usage monitoring
- ✅ Data quota top-up with payment processing (1GB = ¥500)
- ✅ eSIM profile reissue
- ✅ SIM service cancellation
- ✅ Plan change functionality
- ✅ Usage history tracking
- ✅ Service options management (Voice Mail, Call Waiting, Roaming)
Payment Flow (Top-Up)
The top-up feature includes a complete payment flow:
- User requests top-up amount
- WHMCS invoice created
- Payment captured automatically
- Freebit API called to add quota
- Success/error handling
📚 Documentation
- FREEBIT-SIM-MANAGEMENT.md: Complete implementation guide with all features, API endpoints, and configuration
- SIM-MANAGEMENT-API-DATA-FLOW.md: Detailed API data flow and system architecture documentation
⚠️ Important Notes
-
WHMCS Integration Required: The top-up feature requires WHMCS integration with
createInvoice()andcapturePayment()methods. If these don't exist in your branch, you'll need to implement them or modify the top-up service. -
Module Dependencies: The
FreebitModuleandSimManagementModulehave circular dependencies resolved withforwardRef(). Ensure this pattern is maintained. -
Authentication: All endpoints require authentication. The controller uses
@ApiBearerAuth()and expects a JWT token. -
Validation: Request validation uses Zod schemas. Ensure
ZodValidationPipeis available in your validation utilities. -
Error Handling: The implementation includes comprehensive error handling and user-friendly error messages.
🧪 Testing
After migration, test the following:
- SIM Details: Verify SIM information displays correctly
- Usage Data: Check that usage charts and data are accurate
- Top-Up: Test the complete payment flow
- Plan Change: Verify plan changes work correctly
- eSIM Reissue: Test eSIM profile reissue (if applicable)
- Error Handling: Test error scenarios (invalid subscription, API failures, etc.)
🔍 Troubleshooting
Common Issues
- "Module not found" errors: Ensure all modules are properly imported in your app module
- "FREEBIT_OEM_KEY is not configured": Add the environment variable
- Payment failures: Verify WHMCS integration is working
- API timeouts: Check Freebit API connectivity and increase timeout if needed
Debug Endpoints
Use the debug endpoint to troubleshoot:
GET /api/subscriptions/:id/sim/debug
GET /api/debug/sim-details/:account
📞 Support
For detailed implementation information, refer to:
docs/FREEBIT-SIM-MANAGEMENT.md- Complete feature documentationdocs/SIM-MANAGEMENT-API-DATA-FLOW.md- API integration details
Last Updated: January 2025
Migration Package Version: 1.0