Skip to content

Commit

Permalink
Merge pull request #38258 from Expensify/tgolen-refactor-sideloading-…
Browse files Browse the repository at this point in the history
…transactionutils

Remove deprecated methods from transaction utils that sideload data from Onyx
  • Loading branch information
techievivek authored Mar 15, 2024
2 parents f1f22c7 + 64867e5 commit 4e599b2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 35 deletions.
33 changes: 29 additions & 4 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2346,6 +2346,31 @@ function hasMissingSmartscanFields(iouReportID: string): boolean {
return TransactionUtils.getAllReportTransactions(iouReportID).some((transaction) => TransactionUtils.hasMissingSmartscanFields(transaction));
}

/**
* Get the transactions related to a report preview with receipts
* Get the details linked to the IOU reportAction
*
* NOTE: This method is only meant to be used inside this action file. Do not export and use it elsewhere. Use withOnyx or Onyx.connect() instead.
*/
function getLinkedTransaction(reportAction: OnyxEntry<ReportAction | OptimisticIOUReportAction>): Transaction | EmptyObject {
let transactionID = '';

if (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU) {
transactionID = (reportAction?.originalMessage as IOUMessage)?.IOUTransactionID ?? '';
}

return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}

/**
* Retrieve the particular transaction object given its ID.
*
* NOTE: This method is only meant to be used inside this action file. Do not export and use it elsewhere. Use withOnyx or Onyx.connect() instead.
*/
function getTransaction(transactionID: string): OnyxEntry<Transaction> | EmptyObject {
return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}

/**
* Given a parent IOU report action get report name for the LHN.
*/
Expand All @@ -2358,7 +2383,7 @@ function getTransactionReportName(reportAction: OnyxEntry<ReportAction | Optimis
return Localize.translateLocal('parentReportAction.deletedRequest');
}

const transaction = TransactionUtils.getLinkedTransaction(reportAction);
const transaction = getLinkedTransaction(reportAction);
if (isEmptyObject(transaction)) {
// Transaction data might be empty on app's first load, if so we fallback to Request
return Localize.translateLocal('iou.request');
Expand Down Expand Up @@ -2407,7 +2432,7 @@ function getReportPreviewMessage(

if (!isEmptyObject(reportAction) && !isIOUReport(report) && reportAction && ReportActionsUtils.isSplitBillAction(reportAction)) {
// This covers group chats where the last action is a split bill action
const linkedTransaction = TransactionUtils.getLinkedTransaction(reportAction);
const linkedTransaction = getLinkedTransaction(reportAction);
if (isEmptyObject(linkedTransaction)) {
return reportActionMessage;
}
Expand Down Expand Up @@ -2443,7 +2468,7 @@ function getReportPreviewMessage(

let linkedTransaction;
if (!isEmptyObject(reportAction) && shouldConsiderScanningReceiptOrPendingRoute && reportAction && ReportActionsUtils.isMoneyRequestAction(reportAction)) {
linkedTransaction = TransactionUtils.getLinkedTransaction(reportAction);
linkedTransaction = getLinkedTransaction(reportAction);
}

if (!isEmptyObject(linkedTransaction) && TransactionUtils.hasReceipt(linkedTransaction) && TransactionUtils.isReceiptBeingScanned(linkedTransaction)) {
Expand Down Expand Up @@ -4955,7 +4980,7 @@ function getIOUReportActionDisplayMessage(reportAction: OnyxEntry<ReportAction>)
return Localize.translateLocal(translationKey, {amount: formattedAmount, payer: ''});
}

const transaction = TransactionUtils.getTransaction(originalMessage.IOUTransactionID ?? '');
const transaction = getTransaction(originalMessage.IOUTransactionID ?? '');
const transactionDetails = getTransactionDetails(!isEmptyObject(transaction) ? transaction : null);
const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency);
const isRequestSettled = isSettled(originalMessage.IOUReportID);
Expand Down
32 changes: 1 addition & 31 deletions src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,13 @@ import Onyx from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {RecentWaypoint, Report, ReportAction, TaxRate, TaxRates, Transaction, TransactionViolation} from '@src/types/onyx';
import type {IOUMessage} from '@src/types/onyx/OriginalMessage';
import type {RecentWaypoint, Report, TaxRate, TaxRates, Transaction, TransactionViolation} from '@src/types/onyx';
import type {Comment, Receipt, TransactionChanges, TransactionPendingFieldsKey, Waypoint, WaypointCollection} from '@src/types/onyx/Transaction';
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import {isCorporateCard, isExpensifyCard} from './CardUtils';
import DateUtils from './DateUtils';
import * as NumberUtils from './NumberUtils';
import {getCleanedTagName} from './PolicyUtils';
import type {OptimisticIOUReportAction} from './ReportUtils';

let allTransactions: OnyxCollection<Transaction> = {};

Expand Down Expand Up @@ -248,15 +245,6 @@ function getUpdatedTransaction(transaction: Transaction, transactionChanges: Tra
return updatedTransaction;
}

/**
* Retrieve the particular transaction object given its ID.
*
* @deprecated Use withOnyx() or Onyx.connect() instead
*/
function getTransaction(transactionID: string): OnyxEntry<Transaction> | EmptyObject {
return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}

/**
* Return the comment field (referred to as description in the App) from the transaction.
* The comment does not have its modifiedComment counterpart.
Expand Down Expand Up @@ -493,22 +481,6 @@ function hasRoute(transaction: OnyxEntry<Transaction>): boolean {
return !!transaction?.routes?.route0?.geometry?.coordinates;
}

/**
* Get the transactions related to a report preview with receipts
* Get the details linked to the IOU reportAction
*
* @deprecated Use Onyx.connect() or withOnyx() instead
*/
function getLinkedTransaction(reportAction: OnyxEntry<ReportAction | OptimisticIOUReportAction>): Transaction | EmptyObject {
let transactionID = '';

if (reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.IOU) {
transactionID = (reportAction?.originalMessage as IOUMessage)?.IOUTransactionID ?? '';
}

return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}

function getAllReportTransactions(reportID?: string): Transaction[] {
// `reportID` from the `/CreateDistanceRequest` endpoint return's number instead of string for created `transaction`.
// For reference, https://github.com/Expensify/App/pull/26536#issuecomment-1703573277.
Expand Down Expand Up @@ -617,7 +589,6 @@ export {
calculateTaxAmount,
getEnabledTaxRateCount,
getUpdatedTransaction,
getTransaction,
getDescription,
getHeaderTitleTranslationKey,
getRequestType,
Expand All @@ -638,7 +609,6 @@ export {
getTagArrayFromName,
getTagForDisplay,
getTransactionViolations,
getLinkedTransaction,
getAllReportTransactions,
hasReceipt,
hasEReceipt,
Expand Down

0 comments on commit 4e599b2

Please sign in to comment.