diff --git a/src/prices/__tests__/is-variable-price.test.ts b/src/prices/__tests__/is-variable-price.test.ts index 5e122c1..dacae83 100644 --- a/src/prices/__tests__/is-variable-price.test.ts +++ b/src/prices/__tests__/is-variable-price.test.ts @@ -54,7 +54,28 @@ describe('isVariablePrice', () => { expect(isVariablePrice(makePrice({ pricing_model: 'external_getag' }))).toBe(false); }); - it('returns false for CompositePrice', () => { + it('returns true for undefined pricing_model with variable_price true', () => { + expect(isVariablePrice({ variable_price: true } as Price)).toBe(true); + }); + + it('returns false for undefined pricing_model with variable_price false', () => { + expect(isVariablePrice({ variable_price: false } as Price)).toBe(false); + }); + + it('returns true for null pricing_model with variable_price true', () => { + expect(isVariablePrice({ pricing_model: null, variable_price: true } as unknown as Price)).toBe(true); + }); + it('returns false for null pricing_model with variable_price false', () => { + expect(isVariablePrice({ pricing_model: null, variable_price: false } as unknown as Price)).toBe(false); + }); + + it('returns false for CompositePrice with is_composite_price', () => { + const composite = { is_composite_price: true, price_components: [] } as unknown as CompositePrice; + + expect(isVariablePrice(composite)).toBe(false); + }); + + it('returns false for CompositePrice with price_components array', () => { const composite = { price_components: [makePrice()] } as unknown as CompositePrice; expect(isVariablePrice(composite)).toBe(false); diff --git a/src/prices/is-variable-price.ts b/src/prices/is-variable-price.ts index a28ec33..e076b5c 100644 --- a/src/prices/is-variable-price.ts +++ b/src/prices/is-variable-price.ts @@ -10,21 +10,24 @@ const isTieredPrice = (price: Price): boolean => { }; export const isVariablePrice = (price: Price | CompositePrice): boolean => { - if (price.is_composite_price) { - return false; - } + if (price.is_composite_price || Array.isArray(price.price_components)) return false; const p = price as Price; + const { pricing_model, get_ag, variable_price } = p; + + if (isTieredPrice(p) || pricing_model === PricingModel.dynamicTariff) return true; + + if (pricing_model === PricingModel.externalGetAG) { + return ( + get_ag?.type === TypeGetAg.workPrice || + (get_ag?.type === TypeGetAg.basePrice && get_ag?.markup_pricing_model === MarkupPricingModel.tieredFlatFee) + ); + } - if (isTieredPrice(p)) return true; - if (p.pricing_model === PricingModel.dynamicTariff) return true; - if (p.pricing_model === PricingModel.externalGetAG) { - if (p.get_ag?.type === TypeGetAg.workPrice) return true; - if (p.get_ag?.type === TypeGetAg.basePrice && p.get_ag?.markup_pricing_model === MarkupPricingModel.tieredFlatFee) { - return true; - } + // We treat undefined or null pricing_model as perUnit + if (pricing_model === PricingModel.perUnit || !pricing_model) { + return Boolean(variable_price); } - if (p.pricing_model === PricingModel.perUnit && p.variable_price) return true; return false; };