From 008752c4cc5e9cc43ba173141c061923308b56f9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20Hern=C3=A1ndez?= <122884379+andreshernandez-e2x@users.noreply.github.com> Date: Fri, 14 Jun 2024 09:43:18 -0400 Subject: [PATCH] fix: use $0.00 for price if price is not defined, log info (#198) Originally submitted by Kyle (KthProg): https://github.com/klaviyo/commercetools-klaviyo/pull/14 --- plugin/src/utils/get-typed-money-as-number.ts | 8 +++++++- plugin/src/utils/locale-currency-utils.spec.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/plugin/src/utils/get-typed-money-as-number.ts b/plugin/src/utils/get-typed-money-as-number.ts index 21e8f56..7e4f707 100644 --- a/plugin/src/utils/get-typed-money-as-number.ts +++ b/plugin/src/utils/get-typed-money-as-number.ts @@ -1,5 +1,11 @@ import { TypedMoney } from '@commercetools/platform-sdk'; +import logger from './log'; + +export const getTypedMoneyAsNumber = (money?: TypedMoney): number => { + if (!money) { + logger.info('No price found for product.'); + return 0; + } -export const getTypedMoneyAsNumber = (money: TypedMoney): number => { return money.centAmount / Math.pow(10, money.fractionDigits); }; diff --git a/plugin/src/utils/locale-currency-utils.spec.ts b/plugin/src/utils/locale-currency-utils.spec.ts index 4818c0f..42866d9 100644 --- a/plugin/src/utils/locale-currency-utils.spec.ts +++ b/plugin/src/utils/locale-currency-utils.spec.ts @@ -315,4 +315,14 @@ describe('getProductPriceByPriority', () => { currency: 'EUR', }); }); + + it('should return a single price with an amount of 0 (zero) when prices are not defined', async () => { + const price = getProductPriceByPriority([]); + + expect(price).toEqual({ + amount: 0, + country: undefined, + currency: undefined, + }); + }); });