From 8355bbff11691b4b2599bba6e0d59dab912819f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carneiro?= Date: Wed, 8 Jan 2025 13:59:47 +0000 Subject: [PATCH 1/2] Clean up isValidPrice --- src/pricing.ts | 32 ++++++++++---------------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/src/pricing.ts b/src/pricing.ts index 8765f8d..2bcea64 100644 --- a/src/pricing.ts +++ b/src/pricing.ts @@ -116,30 +116,18 @@ export const computePriceComponent = ( }; const isValidPrice = (priceComponent: Price): boolean => { - if ( - (!priceComponent.pricing_model || priceComponent.pricing_model === PricingModel.perUnit) && - typeof priceComponent.unit_amount !== 'number' - ) { - return false; - } - - if ( - (!priceComponent.pricing_model || priceComponent.pricing_model === PricingModel.perUnit) && - !priceComponent.unit_amount_decimal - ) { - return false; - } + const pricingModel = priceComponent.pricing_model || PricingModel.perUnit; - if ( - (priceComponent.pricing_model === PricingModel.tieredFlatFee || - priceComponent.pricing_model === PricingModel.tieredVolume || - priceComponent.pricing_model === PricingModel.tieredGraduated) && - !priceComponent.tiers - ) { - return false; + switch (pricingModel) { + case PricingModel.perUnit: + return Boolean(typeof priceComponent.unit_amount === 'number' && priceComponent.unit_amount_decimal); + case PricingModel.tieredFlatFee: + case PricingModel.tieredVolume: + case PricingModel.tieredGraduated: + return Boolean(priceComponent.tiers); + default: + return true; } - - return true; }; const ensureComponentWithValidPrice = (itemComponent: PriceItemDto): PriceItemDto => ({ From 6ca2d6a215582942d90db6b6d6fdc8f6b209b1af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Carneiro?= Date: Wed, 8 Jan 2025 14:02:45 +0000 Subject: [PATCH 2/2] Remove pricingModel variable --- src/pricing.ts | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/pricing.ts b/src/pricing.ts index 2bcea64..50a29fb 100644 --- a/src/pricing.ts +++ b/src/pricing.ts @@ -116,9 +116,7 @@ export const computePriceComponent = ( }; const isValidPrice = (priceComponent: Price): boolean => { - const pricingModel = priceComponent.pricing_model || PricingModel.perUnit; - - switch (pricingModel) { + switch (priceComponent.pricing_model || PricingModel.perUnit) { case PricingModel.perUnit: return Boolean(typeof priceComponent.unit_amount === 'number' && priceComponent.unit_amount_decimal); case PricingModel.tieredFlatFee: