From ee5690ae42278bea0750eb7f166239ef882f48d2 Mon Sep 17 00:00:00 2001 From: adrien2p Date: Thu, 1 Sep 2022 09:37:20 +0200 Subject: [PATCH] feat(medusa): cleanup calculate tax amount utils and its usage --- packages/medusa/src/services/pricing.ts | 15 +++++++++++++-- packages/medusa/src/services/totals.ts | 6 +++++- .../src/strategies/__tests__/tax-calculation.js | 11 +++-------- packages/medusa/src/strategies/tax-calculation.ts | 13 ++++++------- .../src/utils/calculate-price-tax-amount.ts | 5 +---- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/packages/medusa/src/services/pricing.ts b/packages/medusa/src/services/pricing.ts index 5fee6f5ce6e71..92b61a10c121a 100644 --- a/packages/medusa/src/services/pricing.ts +++ b/packages/medusa/src/services/pricing.ts @@ -18,6 +18,7 @@ import { } from "../interfaces/price-selection-strategy" import { FlagRouter } from "../utils/flag-router" import { calculatePriceTaxAmount } from "../utils" +import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing" type InjectedDependencies = { manager: EntityManager @@ -121,11 +122,16 @@ class PricingService extends TransactionBaseService { } if (variantPricing.calculated_price !== null) { + const includesTax = !!( + this.featureFlagRouter.isFeatureEnabled( + TaxInclusivePricingFeatureFlag.key + ) && variantPricing.calculated_price_includes_tax + ) taxedPricing.calculated_tax = Math.round( calculatePriceTaxAmount({ price: variantPricing.calculated_price, taxRate: rate, - includesTax: variantPricing.calculated_price_includes_tax ?? false, + includesTax, }) ) @@ -136,11 +142,16 @@ class PricingService extends TransactionBaseService { } if (variantPricing.original_price !== null) { + const includesTax = !!( + this.featureFlagRouter.isFeatureEnabled( + TaxInclusivePricingFeatureFlag.key + ) && variantPricing.original_price_includes_tax + ) taxedPricing.original_tax = Math.round( calculatePriceTaxAmount({ price: variantPricing.original_price, taxRate: rate, - includesTax: variantPricing.original_price_includes_tax ?? false, + includesTax, }) ) diff --git a/packages/medusa/src/services/totals.ts b/packages/medusa/src/services/totals.ts index aa2c29b7ae5ce..cfd5d0ca8045e 100644 --- a/packages/medusa/src/services/totals.ts +++ b/packages/medusa/src/services/totals.ts @@ -776,13 +776,17 @@ class TotalsService extends TransactionBaseService { if (isOrder(cartOrOrder) && cartOrOrder.tax_rate !== null) { const taxRate = cartOrOrder.tax_rate / 100 + const includesTax = + this.featureFlagRouter_.isFeatureEnabled( + TaxInclusivePricingFeatureFlag.key + ) && lineItem.includes_tax const taxIncludedInPrice = !lineItem.includes_tax ? 0 : Math.round( calculatePriceTaxAmount({ price: lineItem.unit_price, taxRate: taxRate, - includesTax: lineItem.includes_tax, + includesTax, }) ) lineItemTotals.subtotal = diff --git a/packages/medusa/src/strategies/__tests__/tax-calculation.js b/packages/medusa/src/strategies/__tests__/tax-calculation.js index 63f264af00b50..1dcbbddd088ba 100644 --- a/packages/medusa/src/strategies/__tests__/tax-calculation.js +++ b/packages/medusa/src/strategies/__tests__/tax-calculation.js @@ -1,6 +1,7 @@ import TaxCalculationStrategy from "../tax-calculation" import TaxInclusivePricingFeatureFlag from "../../loaders/feature-flags/tax-inclusive-pricing"; import { featureFlagRouter } from "../../loaders/feature-flags"; +import { FlagRouter } from "../../utils/flag-router"; const toTest = [ [ @@ -169,19 +170,13 @@ describe("TaxCalculationStrategy", () => { test.each(toTest)( "%s", async (title, { items, taxLines, context, expected, flags }) => { - if (flags) { - Object.entries(flags).forEach(([key, value]) => featureFlagRouter.setFlag(key, value)) - } - + const featureFlagRouter = new FlagRouter(flags ?? {}) const calcStrat = new TaxCalculationStrategy({ featureFlagRouter }) + const val = await calcStrat.calculate(items, taxLines, context) expect(val).toEqual(expected) - - if (flags) { - Object.entries(flags).forEach(([key]) => featureFlagRouter.setFlag(key, false)) - } } ) }) diff --git a/packages/medusa/src/strategies/tax-calculation.ts b/packages/medusa/src/strategies/tax-calculation.ts index 4dd9363ee4680..589a51a4679a2 100644 --- a/packages/medusa/src/strategies/tax-calculation.ts +++ b/packages/medusa/src/strategies/tax-calculation.ts @@ -48,14 +48,13 @@ class TaxCalculationStrategy implements ITaxCalculationStrategy { const allocations = context.allocation_map[item.id] || {} const filteredTaxLines = taxLines.filter((tl) => tl.item_id === item.id) - - let taxableAmount - if ( + const includesTax = this.featureFlagRouter_.isFeatureEnabled( TaxInclusivePricingFeatureFlag.key - ) && - item.includes_tax - ) { + ) && item.includes_tax + + let taxableAmount + if (includesTax) { const taxRate = filteredTaxLines.reduce( (accRate: number, nextLineItemTaxLine: LineItemTaxLine) => { return accRate + (nextLineItemTaxLine.rate || 0) / 100 @@ -66,7 +65,7 @@ class TaxCalculationStrategy implements ITaxCalculationStrategy { calculatePriceTaxAmount({ price: item.unit_price, taxRate, - includesTax: item.includes_tax, + includesTax, }) ) taxableAmount = (item.unit_price - taxIncludedInPrice) * item.quantity diff --git a/packages/medusa/src/utils/calculate-price-tax-amount.ts b/packages/medusa/src/utils/calculate-price-tax-amount.ts index dcb826eeb2046..72e1e1673a1c1 100644 --- a/packages/medusa/src/utils/calculate-price-tax-amount.ts +++ b/packages/medusa/src/utils/calculate-price-tax-amount.ts @@ -1,6 +1,3 @@ -import { featureFlagRouter } from "../loaders/feature-flags"; -import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing"; - /** * Return the tax amount that * - is includes in the price if it is tax inclusive @@ -18,7 +15,7 @@ export function calculatePriceTaxAmount({ includesTax?: boolean taxRate: number }): number { - if (featureFlagRouter.isFeatureEnabled(TaxInclusivePricingFeatureFlag.key) && includesTax) { + if (includesTax) { return (taxRate * price) / (1 + taxRate) }