Skip to content
7 changes: 5 additions & 2 deletions src/hooks/useReportActionsPagination.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {getReportPreviewAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {getReportPreviewReportAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import getNonEmptyStringOnyxID from '@libs/getNonEmptyStringOnyxID';
import {getCombinedReportActions, getFilteredReportActionsForReportView, isCreatedAction} from '@libs/ReportActionsUtils';
import {isConciergeChatReport, isInvoiceReport, isMoneyRequestReport, isReportTransactionThread as isReportTransactionThreadUtil, shouldReportAlignToTop} from '@libs/ReportUtils';
Expand Down Expand Up @@ -71,7 +71,10 @@ function useReportActionsPagination(reportID: string | undefined, reportActionID
const shouldAddCreatedAction = !isCreatedAction(lastAction) && (isMoneyRequestReport(report) || isInvoiceReport(report) || isReportTransactionThread || isConciergeChat);

const [chatReportActions] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${getNonEmptyStringOnyxID(report?.chatReportID)}`);
const reportPreviewAction = useMemo(() => getReportPreviewAction(report?.chatReportID, report?.reportID, chatReportActions), [report?.chatReportID, report?.reportID, chatReportActions]);
const reportPreviewAction = useMemo(
() => getReportPreviewReportAction(report?.chatReportID, report?.reportID, chatReportActions),
[report?.chatReportID, report?.reportID, chatReportActions],
);

// When we are offline before opening an IOU/Expense report,
// the total of the report and sometimes the expense aren't displayed because these actions aren't returned until `OpenReport` API is complete.
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/IOU/DeleteMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import cloneDeep from 'lodash/cloneDeep';
import Onyx from 'react-native-onyx';

import {getAllReportActionsFromIOU, getAllReportNameValuePairs, getAllReports, getAllTransactions, getAllTransactionViolations} from '.';
import {getReportPreviewAction, maybeUpdateReportNameForFormulaTitle} from './MoneyRequestBuilder';
import {getReportPreviewReportAction, maybeUpdateReportNameForFormulaTitle} from './MoneyRequestBuilder';

type PrepareToCleanUpMoneyRequestResult = {
shouldDeleteTransactionThread: boolean;
Expand Down Expand Up @@ -115,7 +115,7 @@ function prepareToCleanUpMoneyRequest({

// STEP 1: Get all collections we're updating
const iouReportID = iouReport?.reportID;
const reportPreviewAction = getReportPreviewAction(iouReport?.chatReportID, iouReport?.reportID);
const reportPreviewAction = getReportPreviewReportAction(iouReport?.chatReportID, iouReport?.reportID);
const transaction = allTransactions[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`];
const isTransactionOnHold = isOnHold(transaction);
const transactionViolations = allTransactionViolations[`${ONYXKEYS.COLLECTION.TRANSACTION_VIOLATIONS}${transactionID}`];
Expand Down Expand Up @@ -693,7 +693,7 @@ function getCleanUpTransactionThreadReportOnyxData({
const iouReportID = isMoneyRequestAction(reportAction) ? reportAction?.reportID : undefined;
const iouReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`];
const chatReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${iouReport?.chatReportID}`];
const originalReportPreviewAction = getReportPreviewAction(chatReport?.reportID, iouReport?.reportID) ?? undefined;
const originalReportPreviewAction = getReportPreviewReportAction(chatReport?.reportID, iouReport?.reportID) ?? undefined;
let reportPreviewAction = updatedReportPreviewAction ?? originalReportPreviewAction;
if (
originalReportPreviewAction?.reportActionID &&
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/IOU/MoneyRequestBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ function buildMinimalTransactionForFormula(
};
}

function getReportPreviewAction(
function getReportPreviewReportAction(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are currently two getReportPreviewAction functions in separate files. Renaming one improves maintainability and prevents confusion during refactoring

chatReportID: string | undefined,
iouReportID: string | undefined,
chatReportActions?: OnyxEntry<OnyxTypes.ReportActions>,
Expand Down Expand Up @@ -1629,7 +1629,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma
delegateAccountIDParam: delegateAccountID,
});

let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID);
let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewReportAction(chatReport.reportID, iouReport.reportID);

if (reportPreviewAction) {
reportPreviewAction = updateReportPreview(iouReport, reportPreviewAction, getCurrencyDecimals, false, comment, optimisticTransaction);
Expand Down Expand Up @@ -1896,7 +1896,7 @@ export {
calculateDiffAmount,
getMoneyRequestInformation,
getReceiptError,
getReportPreviewAction,
getReportPreviewReportAction,
getTransactionWithPreservedLocalReceiptSource,
getUpdatedMoneyRequestReportData,
maybeUpdateReportNameForFormulaTitle,
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/IOU/PayMoneyRequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ import Onyx from 'react-native-onyx';

import {getAllPersonalDetails, getAllTransactionViolations} from '.';
import {getReportFromHoldRequestsOnyxData} from './Hold';
import {getReportPreviewAction} from './MoneyRequestBuilder';
import {getReportPreviewReportAction} from './MoneyRequestBuilder';

type PayInvoiceArgs = {
paymentMethodType: PaymentMethodType;
Expand Down Expand Up @@ -293,7 +293,7 @@ function getPayMoneyRequestParams({
// In some instances, the report preview action might not be available to the payer (only whispered to the requestor)
// hence we need to make the updates to the action safely.
let optimisticReportPreviewAction = null;
const reportPreviewAction = getReportPreviewAction(chatReport.reportID, iouReport?.reportID, chatReportActions);
const reportPreviewAction = getReportPreviewReportAction(chatReport.reportID, iouReport?.reportID, chatReportActions);
if (reportPreviewAction) {
optimisticReportPreviewAction = updateReportPreview(iouReport, reportPreviewAction, getCurrencyDecimals, true);
}
Expand Down Expand Up @@ -880,7 +880,7 @@ function markReportPaymentReceived(
const receivedPaymentMessage = getElsewherePaymentReportActionMessage(translateLocal, optimisticIOUReportAction.originalMessage);
optimisticIOUReportAction.message = [{html: receivedPaymentMessage, text: receivedPaymentMessage, isEdited: false, type: CONST.REPORT.MESSAGE.TYPE.COMMENT}];

const reportPreviewAction = getReportPreviewAction(chatReport.reportID, iouReport.reportID, chatReportActions);
const reportPreviewAction = getReportPreviewReportAction(chatReport.reportID, iouReport.reportID, chatReportActions);
const optimisticReportPreviewAction = reportPreviewAction ? updateReportPreview(iouReport, reportPreviewAction, getCurrencyDecimals, true) : null;
const optimisticNextStep = buildOptimisticNextStep({report: iouReport, predictedNextStatus: CONST.REPORT.STATUS_NUM.REIMBURSED, isTrackIntentUser});

Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/IOU/PerDiem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ import {getAllPersonalDetails, getAllReports} from '.';
import {
buildMinimalTransactionForFormula,
buildOnyxDataForMoneyRequest,
getReportPreviewAction,
getReportPreviewReportAction,
mergePolicyRecentlyUsedCategories,
mergePolicyRecentlyUsedCurrencies,
} from './MoneyRequestBuilder';
Expand Down Expand Up @@ -541,7 +541,7 @@ function getPerDiemExpenseInformation(perDiemExpenseInformation: PerDiemExpenseI
shouldGenerateTransactionThreadReport: false,
});

let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID);
let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewReportAction(chatReport.reportID, iouReport.reportID);

if (reportPreviewAction) {
reportPreviewAction = updateReportPreview(iouReport, reportPreviewAction, getCurrencyDecimals, false, comment, optimisticTransaction);
Expand Down
6 changes: 3 additions & 3 deletions src/libs/actions/IOU/Split.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ import {
buildOnyxDataForMoneyRequest,
getMoneyRequestInformation,
getReceiptError,
getReportPreviewAction,
getReportPreviewReportAction,
mergePolicyRecentlyUsedCategories,
mergePolicyRecentlyUsedCurrencies,
} from './MoneyRequestBuilder';
Expand Down Expand Up @@ -1142,7 +1142,7 @@ function completeSplitBill({
delegateAccountIDParam: delegateAccountID,
});

let oneOnOneReportPreviewAction = getReportPreviewAction(oneOnOneChatReport?.reportID, oneOnOneIOUReport?.reportID);
let oneOnOneReportPreviewAction = getReportPreviewReportAction(oneOnOneChatReport?.reportID, oneOnOneIOUReport?.reportID);
if (oneOnOneReportPreviewAction) {
oneOnOneReportPreviewAction = updateReportPreview(oneOnOneIOUReport, oneOnOneReportPreviewAction, getCurrencyDecimals);
} else {
Expand Down Expand Up @@ -1883,7 +1883,7 @@ function createSplitsAndOnyxData({
redundantParticipants[accountID] = null;
}

let oneOnOneReportPreviewAction = getReportPreviewAction(oneOnOneChatReport.reportID, oneOnOneIOUReport.reportID);
let oneOnOneReportPreviewAction = getReportPreviewReportAction(oneOnOneChatReport.reportID, oneOnOneIOUReport.reportID);
if (oneOnOneReportPreviewAction) {
oneOnOneReportPreviewAction = updateReportPreview(oneOnOneIOUReport, oneOnOneReportPreviewAction, getCurrencyDecimals);
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/libs/actions/IOU/SplitTransactionUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ import type {UpdateMoneyRequestDataKeys} from './UpdateMoneyRequest';
import {getCleanUpTransactionThreadReportOnyxData} from './DeleteMoneyRequest';
import {getAllReports} from './index';
import {getMoneyRequestParticipantsFromReport} from './MoneyRequest';
import {getMoneyRequestInformation, getReportPreviewAction} from './MoneyRequestBuilder';
import {getMoneyRequestInformation, getReportPreviewReportAction} from './MoneyRequestBuilder';
import {getDeleteTrackExpenseInformation} from './TrackExpense';
import {getUpdateMoneyRequestParams} from './UpdateMoneyRequest';

Expand Down Expand Up @@ -388,7 +388,7 @@ function updateSplitTransactions({
}

let updatedReportPreviewAction: Partial<OnyxTypes.ReportAction> | undefined;
const originalReportPreviewAction = getReportPreviewAction(
const originalReportPreviewAction = getReportPreviewReportAction(
expenseReport?.chatReportID,
expenseReport?.reportID,
allReportActionsList?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${expenseReport?.chatReportID}`],
Expand Down
10 changes: 8 additions & 2 deletions src/libs/actions/IOU/TrackExpense.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,13 @@ import type {

import {deleteMoneyRequest, getCleanUpTransactionThreadReportOnyxData, getNavigationUrlOnMoneyRequestDelete} from './DeleteMoneyRequest';
import {getAllReports, getAllTransactionDrafts, getAllTransactions, getAllTransactionViolations} from './index';
import {buildMinimalTransactionForFormula, getMoneyRequestInformation, getReceiptError, getReportPreviewAction, getTransactionWithPreservedLocalReceiptSource} from './MoneyRequestBuilder';
import {
buildMinimalTransactionForFormula,
getMoneyRequestInformation,
getReceiptError,
getReportPreviewReportAction,
getTransactionWithPreservedLocalReceiptSource,
} from './MoneyRequestBuilder';
import {highlightTransactionOnSearchRouteIfNeeded} from './NavigationHelpers';
import {addPendingNewTransactionIDs, isOneToTwoTransactionTransition} from './PendingNewTransactions';
import {getSearchOnyxUpdate} from './SearchUpdate';
Expand Down Expand Up @@ -1154,7 +1160,7 @@ function getTrackExpenseInformation(params: GetTrackExpenseInformationParams): T

let reportPreviewAction: OnyxInputValue<OnyxTypes.ReportAction<typeof CONST.REPORT.ACTIONS.TYPE.REPORT_PREVIEW>> = null;
if (shouldUseMoneyReport && iouReport) {
reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID);
reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewReportAction(chatReport.reportID, iouReport.reportID);

if (reportPreviewAction) {
reportPreviewAction = updateReportPreview(iouReport, reportPreviewAction, getCurrencyDecimals, false, comment, optimisticTransaction);
Expand Down
23 changes: 15 additions & 8 deletions src/libs/actions/OnyxDerived/configs/reportAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {LocalizedTranslate} from '@components/LocaleContextProvider';

import {getReportPreviewAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {getReportPreviewReportAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {translate as translateForLocale} from '@libs/Localize';
import {getIsOffline} from '@libs/NetworkState';
import {getLoginByAccountID} from '@libs/PersonalDetailsUtils';
Expand Down Expand Up @@ -29,7 +29,7 @@ import {hasKeyTriggeredCompute} from '@userActions/OnyxDerived/utils';
import CONST from '@src/CONST';
import IntlStore from '@src/languages/IntlStore';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetails, PersonalDetailsList, Policy, Report, ReportAttributesDerivedValue, Transaction, TransactionViolation} from '@src/types/onyx';
import type {PersonalDetails, PersonalDetailsList, Policy, Report, ReportActions, ReportAttributesDerivedValue, Transaction, TransactionViolation} from '@src/types/onyx';

import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';

Expand Down Expand Up @@ -163,14 +163,20 @@ const reportReferencesAccountIDs = (report: Report, accountIDs: Set<number>): bo

// Returns the report-preview action ID of the oldest child in `reportIDs` matching `predicate`
// (oldest by preview-action creation time), or undefined when none match.
const getOldestPreviewActionID = (chatReportID: string, reportIDs: string[] | undefined, reports: OnyxCollection<Report>, predicate?: (childReport: OnyxEntry<Report>) => boolean) => {
const getOldestPreviewActionID = (
chatReportID: string,
reportIDs: string[] | undefined,
reports: OnyxCollection<Report>,
chatReportActions: OnyxEntry<ReportActions>,
predicate?: (childReport: OnyxEntry<Report>) => boolean,
) => {
let oldestCreated: string | undefined;
let targetReportActionID: string | undefined;
for (const childReportID of reportIDs ?? []) {
if (predicate && !predicate(reports?.[`${ONYXKEYS.COLLECTION.REPORT}${childReportID}`])) {
continue;
}
const reportPreviewAction = getReportPreviewAction(chatReportID, childReportID);
const reportPreviewAction = getReportPreviewReportAction(chatReportID, childReportID, chatReportActions);
if (!reportPreviewAction) {
continue;
}
Expand Down Expand Up @@ -700,10 +706,11 @@ export default createOnyxDerivedValueConfig({

const chatAttributes = reportAttributes[chatReportID];
let actionTargetReportActionID = chatAttributes.actionTargetReportActionID;
const chatReportActions = reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`];

actionTargetReportActionID =
getOldestPreviewActionID(chatReportID, erroredChildReportIDs, reports, isActionable) ??
getOldestPreviewActionID(chatReportID, childReportIDsByChat.get(chatReportID), reports, (childReport) =>
getOldestPreviewActionID(chatReportID, erroredChildReportIDs, reports, chatReportActions, isActionable) ??
getOldestPreviewActionID(chatReportID, childReportIDsByChat.get(chatReportID), reports, chatReportActions, (childReport) =>
needsViolationFix(
childReport,
getLoginByAccountID(childReport?.ownerAccountID, personalDetails),
Expand All @@ -713,7 +720,7 @@ export default createOnyxDerivedValueConfig({
currentUserEmail,
),
) ??
getOldestPreviewActionID(chatReportID, erroredChildReportIDs, reports) ??
getOldestPreviewActionID(chatReportID, erroredChildReportIDs, reports, chatReportActions) ??
actionTargetReportActionID;

// Clone the entry before mutating — it may be a reference carried over from
Expand Down Expand Up @@ -741,4 +748,4 @@ export default createOnyxDerivedValueConfig({
},
});

export {hasPolicyRelevantFieldChanged};
export {hasPolicyRelevantFieldChanged, getOldestPreviewActionID};
12 changes: 6 additions & 6 deletions tests/actions/IOUTest/DeleteMoneyRequestTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {deleteMoneyRequest, getCleanUpTransactionThreadReportOnyxData} from '@libs/actions/IOU/DeleteMoneyRequest';
import {getReportPreviewAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {getReportPreviewReportAction} from '@libs/actions/IOU/MoneyRequestBuilder';
import {requestMoney} from '@libs/actions/IOU/TrackExpense';
import {updateMoneyRequestAmountAndCurrency} from '@libs/actions/IOU/UpdateMoneyRequest';
import initOnyxDerivedValues from '@libs/actions/OnyxDerived';
Expand Down Expand Up @@ -1159,7 +1159,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
expect(iouReport).toHaveProperty('chatReportID');
expect(iouReport?.total).toBe(30000);

const iouPreview = chatReport?.reportID && iouReport?.reportID ? getReportPreviewAction(chatReport.reportID, iouReport.reportID) : undefined;
const iouPreview = chatReport?.reportID && iouReport?.reportID ? getReportPreviewReportAction(chatReport.reportID, iouReport.reportID) : undefined;
expect(iouPreview).toBeTruthy();
expect(getReportActionText(iouPreview)).toBe('rory@expensifail.com owes $300.00');

Expand Down Expand Up @@ -1469,7 +1469,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
jest.advanceTimersByTime(10);

// When a comment is added
let iouPreview = getReportPreviewAction(chatReport?.reportID, iouReport?.reportID);
let iouPreview = getReportPreviewReportAction(chatReport?.reportID, iouReport?.reportID);
const ancestors = [];
ancestors.push(...(iouReport && createIOUAction ? [{report: iouReport, reportAction: createIOUAction, shouldDisplayNewMarker: false}] : []));
ancestors.push(...(chatReport && iouPreview ? [{report: chatReport, reportAction: iouPreview, shouldDisplayNewMarker: false}] : []));
Expand Down Expand Up @@ -1513,7 +1513,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
expect(createIOUAction?.childVisibleActionCount).toEqual(1);
expect(createIOUAction?.childCommenterCount).toEqual(1);

iouPreview = getReportPreviewAction(chatReport?.reportID, iouReport?.reportID);
iouPreview = getReportPreviewReportAction(chatReport?.reportID, iouReport?.reportID);
expect(iouPreview).toBeTruthy();
expect(iouPreview?.childVisibleActionCount).toEqual(1);
expect(iouPreview?.childCommenterCount).toEqual(1);
Expand Down Expand Up @@ -1541,7 +1541,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {

// Then we expect the reportPreview to update with new childVisibleActionCount

iouPreview = getReportPreviewAction(chatReport?.reportID, iouReport?.reportID);
iouPreview = getReportPreviewReportAction(chatReport?.reportID, iouReport?.reportID);
expect(iouPreview).toBeTruthy();
expect(iouPreview?.childVisibleActionCount).toEqual(0);
expect(iouPreview?.childCommenterCount).toEqual(0);
Expand All @@ -1551,7 +1551,7 @@ describe('actions/IOU/DeleteMoneyRequest', () => {
await waitForBatchedUpdates();

// Then we expect the reportPreview to update with new childVisibleActionCount
iouPreview = getReportPreviewAction(chatReport?.reportID, iouReport?.reportID);
iouPreview = getReportPreviewReportAction(chatReport?.reportID, iouReport?.reportID);
expect(iouPreview).toBeTruthy();
expect(iouPreview?.childVisibleActionCount).toEqual(0);
expect(iouPreview?.childCommenterCount).toEqual(0);
Expand Down
Loading
Loading