From dc229aed780d51b24c74b276f4000606886b180b Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 16 Apr 2024 10:47:30 -0700 Subject: [PATCH 1/4] Define the TransactionCustomUnit type --- src/types/onyx/Transaction.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/types/onyx/Transaction.ts b/src/types/onyx/Transaction.ts index 281b6b4228ce..12820ec43c06 100644 --- a/src/types/onyx/Transaction.ts +++ b/src/types/onyx/Transaction.ts @@ -47,12 +47,20 @@ type Comment = { waypoints?: WaypointCollection; isLoading?: boolean; type?: string; - customUnit?: Record; + customUnit?: TransactionCustomUnit; source?: string; originalTransactionID?: string; splits?: Split[]; }; +type TransactionCustomUnit = { + customUnitID?: string; + customUnitRateID?: string; + quantity?: number; + name?: string; + defaultP2PRate?: number; +}; + type GeometryType = 'LineString'; type Geometry = { From 792cec802b0eba3af7cfa84b2baf0931ddccc39f Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 16 Apr 2024 10:49:19 -0700 Subject: [PATCH 2/4] Update how distance is accessed --- src/components/MoneyRequestConfirmationList.tsx | 2 +- .../MoneyTemporaryForRefactorRequestConfirmationList.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index aa6c75edbf5d..906a593f254a 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -237,7 +237,7 @@ function MoneyRequestConfirmationList({ rate: 0, currency: CONST.CURRENCY.USD, }; - const distance = transaction?.routes?.route0.distance ?? 0; + const distance = transaction?.comment?.customUnit?.quantity ?? 0; const shouldCalculateDistanceAmount = isDistanceRequest && iouAmount === 0; const taxRates = policy?.taxRates; diff --git a/src/components/MoneyTemporaryForRefactorRequestConfirmationList.tsx b/src/components/MoneyTemporaryForRefactorRequestConfirmationList.tsx index 21815f00253b..b48e4b31eb0d 100755 --- a/src/components/MoneyTemporaryForRefactorRequestConfirmationList.tsx +++ b/src/components/MoneyTemporaryForRefactorRequestConfirmationList.tsx @@ -222,7 +222,7 @@ function MoneyTemporaryForRefactorRequestConfirmationList({ rate: 0, currency: 'USD', }; - const distance = transaction?.routes?.route0.distance ?? 0; + const distance = transaction?.comment?.customUnit?.quantity ?? 0; const shouldCalculateDistanceAmount = isDistanceRequest && iouAmount === 0; const taxRates = policy?.taxRates; From 9c2f5565ac9d4eaf297dff0bdb6631fbae196068 Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 30 Apr 2024 08:22:04 -0700 Subject: [PATCH 3/4] Fix lint by removing type assertions --- src/components/ReportActionItem/MoneyRequestView.tsx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index 9373df336e78..6fc23e427bc0 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -195,11 +195,11 @@ function MoneyRequestView({ const currency = policy ? policy.outputCurrency : PolicyUtils.getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD; - const mileageRate = TransactionUtils.isCustomUnitRateIDForP2P(transaction) ? DistanceRequestUtils.getRateForP2P(currency) : distanceRates[rateID as string] ?? {}; + const mileageRate = TransactionUtils.isCustomUnitRateIDForP2P(transaction) ? DistanceRequestUtils.getRateForP2P(currency) : distanceRates[rateID] ?? {}; const {unit} = mileageRate; - const rate = (transaction?.comment?.customUnit?.defaultP2PRate as number) ?? mileageRate.rate; + const rate = transaction?.comment?.customUnit?.defaultP2PRate ?? mileageRate.rate; - const distance = DistanceRequestUtils.convertToDistanceInMeters((transaction?.comment?.customUnit?.quantity as number) ?? 0, unit); + const distance = DistanceRequestUtils.convertToDistanceInMeters(transaction?.comment?.customUnit?.quantity ?? 0, unit); const rateToDisplay = DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, isOffline); const distanceToDisplay = DistanceRequestUtils.getDistanceForDisplay(hasRoute, distance, unit, rate, translate); let merchantTitle = isEmptyMerchant ? '' : transactionMerchant; From 2859ec2125612e48c858f64e4ad2f7e5c90a974f Mon Sep 17 00:00:00 2001 From: neil-marcellini Date: Tue, 30 Apr 2024 08:40:44 -0700 Subject: [PATCH 4/4] Update getDistance util and use it more --- src/components/MoneyRequestConfirmationList.tsx | 2 +- src/components/ReportActionItem/MoneyRequestView.tsx | 2 +- src/libs/TransactionUtils.ts | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index a4005d6c63f1..ec740a4e9843 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -267,7 +267,7 @@ function MoneyRequestConfirmationList({ const currency = (mileageRate as MileageRate)?.currency ?? policyCurrency; - const distance = transaction?.comment?.customUnit?.quantity ?? 0; + const distance = TransactionUtils.getDistance(transaction); const taxRates = policy?.taxRates ?? null; // A flag for showing the categories field diff --git a/src/components/ReportActionItem/MoneyRequestView.tsx b/src/components/ReportActionItem/MoneyRequestView.tsx index 6fc23e427bc0..f71a220d0c99 100644 --- a/src/components/ReportActionItem/MoneyRequestView.tsx +++ b/src/components/ReportActionItem/MoneyRequestView.tsx @@ -199,7 +199,7 @@ function MoneyRequestView({ const {unit} = mileageRate; const rate = transaction?.comment?.customUnit?.defaultP2PRate ?? mileageRate.rate; - const distance = DistanceRequestUtils.convertToDistanceInMeters(transaction?.comment?.customUnit?.quantity ?? 0, unit); + const distance = DistanceRequestUtils.convertToDistanceInMeters(TransactionUtils.getDistance(transaction), unit); const rateToDisplay = DistanceRequestUtils.getRateForDisplay(unit, rate, currency, translate, toLocaleDigit, isOffline); const distanceToDisplay = DistanceRequestUtils.getDistanceForDisplay(hasRoute, distance, unit, rate, translate); let merchantTitle = isEmptyMerchant ? '' : transactionMerchant; diff --git a/src/libs/TransactionUtils.ts b/src/libs/TransactionUtils.ts index 74bc15f32487..36cbb4f1ecc5 100644 --- a/src/libs/TransactionUtils.ts +++ b/src/libs/TransactionUtils.ts @@ -356,8 +356,8 @@ function getMerchant(transaction: OnyxEntry): string { return transaction?.modifiedMerchant ? transaction.modifiedMerchant : transaction?.merchant ?? ''; } -function getDistance(transaction: Transaction): number { - return transaction?.routes?.route0?.distance ?? 0; +function getDistance(transaction: Transaction | null): number { + return transaction?.comment?.customUnit?.quantity ?? 0; } /**