Last active
March 4, 2019 23:24
-
-
Save austin-sa-wang/0936d377bc7da0b0a6a7fb60ccd7e5d0 to your computer and use it in GitHub Desktop.
Dependency Inversion Example
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
type IApplicableFeeMap = Map<string, IApplicableFee> | |
class ApplicableFeesCooridinator { | |
protected applicableFees: IApplicableFee[] | |
constructor(protected Models: IModels, prices: IOfferingPriceDerivedBase[]) { | |
... | |
this.applicableFees = this.generateApplicableFees(prices) | |
} | |
public isApplicable(fee: IFeeDerived, price: IOfferingPriceDerivedBase): boolean { | |
const result = _.filter( | |
this.applicableFees, | |
(applicableFee) => applicableFee.feeId === fee.feeId && applicableFee.sourceOfferingId === price.offeringId | |
) | |
return result.length > 0 | |
} | |
private generateApplicableFees(prices: IOfferingPriceDerivedBase[]): IApplicableFee[] { | |
... | |
return Array.from(applicableFeesMap.values()) | |
} | |
private isFeeAlwaysApplied(fee: IFeeDerived): boolean { | |
return ( | |
fee.feeTypeId === this.Models.FeeType.ID.OTHER && !fee.isAppliedOnce | |
) || ( | |
fee.feeTypeId !== this.Models.FeeType.ID.OTHER && | |
fee.feeType.feeTypeApplicableRuleTypeId === this.Models.FeeTypeApplicableRuleType.ID.APPLY_ALL | |
) | |
} | |
private isFeeApplyLowest(fee: IFeeDerived): boolean { | |
return fee.feeType.feeTypeApplicableRuleTypeId === this.Models.FeeTypeApplicableRuleType.ID.APPLY_LOWEST | |
} | |
private generateApplicableFeeHash (fee: IFeeDerived, offeringId: number) { | |
if (fee.feeTypeId === this.Models.FeeType.ID.OTHER) | |
return `applyOncePerFee?feeId=${fee.feeId}` // only one fee should be included | |
else | |
... | |
} | |
} | |
export default class FeePriceCalculator { | |
protected className: string | |
constructor(protected Connectors: IConnectors, protected Models: IModels, protected useMaster: boolean) { | |
this.className = this.constructor.name | |
} | |
public mutatingReapplyFeesAndFeeDiscounts(prices: IOfferingPriceDerivedBase[]) { | |
const applicableFeesCoordinator = new ApplicableFeesCooridinator(this.Models, prices) | |
... | |
_.forEach(prices, (price) => { | |
// mutate fees | |
price.fees = _.filter(price.fees, (fee) => applicableFeesCoordinator.isApplicable(fee, price)) | |
... | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment