From 26f5d4577fe424f704a8691c1ca9f5c7b62ef76c Mon Sep 17 00:00:00 2001 From: Mahesh Date: Wed, 5 Jun 2024 21:45:37 +0530 Subject: [PATCH 1/4] fix currency breaking issue with currency conversion --- src/components/EReceipt.tsx | 4 ++-- .../MoneyRequestPreviewContent.tsx | 2 +- src/libs/CurrencyUtils.ts | 14 ++++++++++++-- src/libs/ModifiedExpenseMessage.ts | 4 ++-- src/libs/ReportUtils.ts | 10 +++++----- 5 files changed, 22 insertions(+), 12 deletions(-) diff --git a/src/components/EReceipt.tsx b/src/components/EReceipt.tsx index 40f5d242d005..bfb59dc748ab 100644 --- a/src/components/EReceipt.tsx +++ b/src/components/EReceipt.tsx @@ -36,13 +36,13 @@ function EReceipt({transaction, transactionID}: EReceiptProps) { const { amount: transactionAmount, - currency: transactionCurrency = '', + currency: transactionCurrency, merchant: transactionMerchant, created: transactionDate, cardID: transactionCardID, } = ReportUtils.getTransactionDetails(transaction, CONST.DATE.MONTH_DAY_YEAR_FORMAT) ?? {}; const formattedAmount = CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency); - const currency = CurrencyUtils.getCurrencySymbol(transactionCurrency); + const currency = CurrencyUtils.getCurrencySymbol(transactionCurrency ?? ''); const amount = currency ? formattedAmount.replace(currency, '') : formattedAmount; const cardDescription = transactionCardID ? CardUtils.getCardDescription(transactionCardID) : ''; diff --git a/src/components/ReportActionItem/MoneyRequestPreview/MoneyRequestPreviewContent.tsx b/src/components/ReportActionItem/MoneyRequestPreview/MoneyRequestPreviewContent.tsx index 3331572ab625..807743074d2c 100644 --- a/src/components/ReportActionItem/MoneyRequestPreview/MoneyRequestPreviewContent.tsx +++ b/src/components/ReportActionItem/MoneyRequestPreview/MoneyRequestPreviewContent.tsx @@ -334,7 +334,7 @@ function MoneyRequestPreviewContent({ {splitShare && ( - {translate('iou.yourSplit', {amount: CurrencyUtils.convertToDisplayString(splitShare ?? 0, requestCurrency ?? '')})} + {translate('iou.yourSplit', {amount: CurrencyUtils.convertToDisplayString(splitShare ?? 0, requestCurrency)})} )} diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index d3660c5f16f4..78556a3d77f9 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -2,6 +2,7 @@ import Onyx from 'react-native-onyx'; import CONST from '@src/CONST'; import type {OnyxValues} from '@src/ONYXKEYS'; import ONYXKEYS from '@src/ONYXKEYS'; +import type {CurrencyList} from '@src/types/onyx'; import BaseLocaleListener from './Localize/LocaleListener/BaseLocaleListener'; import * as NumberFormatUtils from './NumberFormatUtils'; @@ -110,11 +111,20 @@ function convertToFrontendAmountAsString(amountAsInt: number | null | undefined) * @param amountInCents – should be an integer. Anything after a decimal place will be dropped. * @param currency - IOU currency */ -function convertToDisplayString(amountInCents = 0, currency: string = CONST.CURRENCY.USD): string { +function convertToDisplayString(amountInCents = 0, currency: keyof CurrencyList | undefined = CONST.CURRENCY.USD): string { const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); + /** + * Handle currency empty which can break the application. + * We're doing validation here so that we never send the value which will break the application. + * More: https://github.com/Expensify/App/issues/43004 + */ + let validatedCurrency = currency; + if (!currency || currency.length === 0 || currency.length > 3) { + validatedCurrency = CONST.CURRENCY.USD; + } return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { style: 'currency', - currency, + currency: validatedCurrency, // We are forcing the number of decimals because we override the default number of decimals in the backend for RSD // See: https://github.com/Expensify/PHP-Libs/pull/834 diff --git a/src/libs/ModifiedExpenseMessage.ts b/src/libs/ModifiedExpenseMessage.ts index 2df75030ac19..003074aa55f9 100644 --- a/src/libs/ModifiedExpenseMessage.ts +++ b/src/libs/ModifiedExpenseMessage.ts @@ -126,11 +126,11 @@ function getForReportAction(reportID: string | undefined, reportAction: OnyxEntr const hasModifiedMerchant = reportActionOriginalMessage && 'oldMerchant' in reportActionOriginalMessage && 'merchant' in reportActionOriginalMessage; if (hasModifiedAmount) { - const oldCurrency = reportActionOriginalMessage?.oldCurrency ?? ''; + const oldCurrency = reportActionOriginalMessage?.oldCurrency; const oldAmountValue = reportActionOriginalMessage?.oldAmount ?? 0; const oldAmount = oldAmountValue > 0 ? CurrencyUtils.convertToDisplayString(reportActionOriginalMessage?.oldAmount ?? 0, oldCurrency) : ''; - const currency = reportActionOriginalMessage?.currency ?? ''; + const currency = reportActionOriginalMessage?.currency; const amount = CurrencyUtils.convertToDisplayString(reportActionOriginalMessage?.amount ?? 0, currency); // Only Distance edits should modify amount and merchant (which stores distance) in a single transaction. diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index d82fb224784b..4b12634f9630 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -2895,7 +2895,7 @@ function getReportPreviewMessage( } const transactionDetails = getTransactionDetails(linkedTransaction); - const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency ?? ''); + const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency); return Localize.translateLocal('iou.didSplitAmount', {formattedAmount, comment: transactionDetails?.comment ?? ''}); } } @@ -2917,7 +2917,7 @@ function getReportPreviewMessage( } const transactionDetails = getTransactionDetails(linkedTransaction); - const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency ?? ''); + const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency); return Localize.translateLocal('iou.trackedAmount', {formattedAmount, comment: transactionDetails?.comment ?? ''}); } } @@ -6412,12 +6412,12 @@ function getNonHeldAndFullAmount(iouReport: OnyxEntry, policy: OnyxEntry if (hasUpdatedTotal(iouReport, policy) && hasPendingTransaction) { const unheldTotal = transactions.reduce((currentVal, transaction) => currentVal - (!TransactionUtils.isOnHold(transaction) ? transaction.amount : 0), 0); - return [CurrencyUtils.convertToDisplayString(unheldTotal, iouReport?.currency ?? ''), CurrencyUtils.convertToDisplayString((iouReport?.total ?? 0) * -1, iouReport?.currency ?? '')]; + return [CurrencyUtils.convertToDisplayString(unheldTotal, iouReport?.currency), CurrencyUtils.convertToDisplayString((iouReport?.total ?? 0) * -1, iouReport?.currency)]; } return [ - CurrencyUtils.convertToDisplayString((iouReport?.unheldTotal ?? 0) * -1, iouReport?.currency ?? ''), - CurrencyUtils.convertToDisplayString((iouReport?.total ?? 0) * -1, iouReport?.currency ?? ''), + CurrencyUtils.convertToDisplayString((iouReport?.unheldTotal ?? 0) * -1, iouReport?.currency), + CurrencyUtils.convertToDisplayString((iouReport?.total ?? 0) * -1, iouReport?.currency), ]; } From 6ce5244891771f36699a3df363746b387be8bfe9 Mon Sep 17 00:00:00 2001 From: Mahesh Vagicherla Date: Thu, 6 Jun 2024 03:02:53 +0530 Subject: [PATCH 2/4] update comment --- src/libs/CurrencyUtils.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index 78556a3d77f9..ffbeb9263a18 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -114,12 +114,10 @@ function convertToFrontendAmountAsString(amountAsInt: number | null | undefined) function convertToDisplayString(amountInCents = 0, currency: keyof CurrencyList | undefined = CONST.CURRENCY.USD): string { const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); /** - * Handle currency empty which can break the application. - * We're doing validation here so that we never send the value which will break the application. - * More: https://github.com/Expensify/App/issues/43004 + * Fallback currency to USD if it empty string or undefined */ let validatedCurrency = currency; - if (!currency || currency.length === 0 || currency.length > 3) { + if (!currency || currency.length === 0) { validatedCurrency = CONST.CURRENCY.USD; } return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { From 850f3fa67fda2c67e7955f868df8b7d60e126ffd Mon Sep 17 00:00:00 2001 From: Mahesh Vagicherla Date: Thu, 6 Jun 2024 10:34:27 +0530 Subject: [PATCH 3/4] update changes --- src/libs/CurrencyUtils.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index ffbeb9263a18..270f600c5fa4 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -111,18 +111,18 @@ function convertToFrontendAmountAsString(amountAsInt: number | null | undefined) * @param amountInCents – should be an integer. Anything after a decimal place will be dropped. * @param currency - IOU currency */ -function convertToDisplayString(amountInCents = 0, currency: keyof CurrencyList | undefined = CONST.CURRENCY.USD): string { +function convertToDisplayString(amountInCents = 0, currency: string = CONST.CURRENCY.USD): string { const convertedAmount = convertToFrontendAmountAsInteger(amountInCents); /** * Fallback currency to USD if it empty string or undefined */ - let validatedCurrency = currency; - if (!currency || currency.length === 0) { - validatedCurrency = CONST.CURRENCY.USD; + let currencyWithFallback = currency; + if (!currency) { + currencyWithFallback = CONST.CURRENCY.USD; } return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { style: 'currency', - currency: validatedCurrency, + currency: currencyWithFallback, // We are forcing the number of decimals because we override the default number of decimals in the backend for RSD // See: https://github.com/Expensify/PHP-Libs/pull/834 From 54ddb2dc8979684b838cfebeaf8590775aa03d02 Mon Sep 17 00:00:00 2001 From: Mahesh Vagicherla Date: Thu, 6 Jun 2024 10:42:53 +0530 Subject: [PATCH 4/4] remove import --- src/libs/CurrencyUtils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index 270f600c5fa4..90aa6bd82cd0 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -2,7 +2,6 @@ import Onyx from 'react-native-onyx'; import CONST from '@src/CONST'; import type {OnyxValues} from '@src/ONYXKEYS'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {CurrencyList} from '@src/types/onyx'; import BaseLocaleListener from './Localize/LocaleListener/BaseLocaleListener'; import * as NumberFormatUtils from './NumberFormatUtils';