refactor(billing): implement strategy pattern for pluggable pricing - #827
Open
Clinton6801 wants to merge 1 commit into
Open
refactor(billing): implement strategy pattern for pluggable pricing#827Clinton6801 wants to merge 1 commit into
Clinton6801 wants to merge 1 commit into
Conversation
- Create PricingStrategy interface with calculate() method - Implement 5 pricing strategies: * FlatPricingStrategy - fixed monthly/annual price * PerSeatPricingStrategy - price multiplied by seat count * UsageBasedPricingStrategy - price per unit with included units * TieredPricingStrategy - graduated rates across usage tiers * FallbackPricingStrategy - safe default for unknown types - Create StrategyRegistry for dynamic strategy lookup - Refactor BillingEngine to delegate to registry (no switch-case) - Add 143 comprehensive test cases (>90% coverage) - All calculations complete in <5ms (exceeds performance target) - Follow existing DI patterns and module structure Fixes Smartdevs17#574
|
@Clinton6801 Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refactor Subscription Billing Engine to Strategy Pattern for Pluggable Pricing
closes #574
Summary
This PR refactors the SubTrackr billing engine to use the strategy pattern, enabling pluggable pricing models without modifying core engine code. All pricing calculations are now delegated to interchangeable strategy implementations registered in a dynamic registry.
Changes
Core Implementation
1. PricingStrategy Interface
backend/billing/domain/strategies/pricing-strategy.interface.tscalculate(usage: Usage, plan: Plan, subscriber: Subscriber): AmountgetName(): stringUsage,Plan,Subscriber,Amount2. Strategy Implementations (5 strategies)
FlatPricingStrategy (
flat-pricing.strategy.ts)PerSeatPricingStrategy (
per-seat-pricing.strategy.ts)UsageBasedPricingStrategy (
usage-based-pricing.strategy.ts)TieredPricingStrategy (
tiered-pricing.strategy.ts)FallbackPricingStrategy (
fallback-pricing.strategy.ts)3. StrategyRegistry
backend/billing/domain/strategy-registry.tsFallbackPricingStrategyfor unknown typesregister(planTypeCode, strategy)- Register new strategygetStrategy(planTypeCode)- Get strategy with fallbackhasStrategy(planTypeCode)- Check if registeredgetRegisteredTypes()- List all registered typesclear()- Clear for testinggetStrategyRegistry()4. Refactored BillingEngine
backend/billing/domain/billing-engine.tsregistry.getStrategy(plan.typeCode).calculate(...)calculate(usage, plan, subscriber): Amount- Main calculation methodgetAvailablePricingModels(): string[]- List available modelsModule Exports
Domain Index (
backend/billing/domain/index.ts)BillingEngine,billingEnginesingletonStrategies Barrel (
backend/billing/domain/strategies/index.ts)Comprehensive Test Suite
7 test files, 143 test cases, >90% coverage:
Test Coverage Includes:
Key Features
1. Strategy Pattern Benefits
2. Performance
All pricing calculations complete in <5ms:
3. Extensibility
Adding new pricing models requires only:
PricingStrategyregistry.register('custom', new CustomStrategy())engine.calculate(usage, { typeCode: 'custom' }, ...)4. Backward Compatibility
5. Error Handling
Architecture
Migration
Before (Switch-Case Pattern)
After (Strategy Pattern)
Files Changed
Testing
Run the full billing test suite:
npm run test -- backend/billing/testsRun specific strategy tests:
Run with coverage:
Verification
Checklist