diff --git a/src/CONST.ts b/src/CONST.ts index e3ca655f0b380..c42fb547ab71e 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1041,7 +1041,6 @@ const CONST = { EMPTY_ARRAY, EMPTY_OBJECT, DEFAULT_NUMBER_ID, - FAKE_REPORT_ID: 'FAKE_REPORT_ID', USE_EXPENSIFY_URL, EXPENSIFY_URL, GOOGLE_MEET_URL_ANDROID: 'https://meet.google.com', diff --git a/src/ROUTES.ts b/src/ROUTES.ts index 57f7580556e3b..399af77a7415d 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -83,14 +83,12 @@ const ROUTES = { backTo, moneyRequestReportActionID, transactionID, - iouReportID, }: { reportID: string | undefined; reportActionID?: string; backTo?: string; moneyRequestReportActionID?: string; transactionID?: string; - iouReportID?: string; }) => { if (!reportID) { Log.warn('Invalid reportID is used to build the SEARCH_REPORT route'); @@ -107,10 +105,6 @@ const ROUTES = { queryParams.push(`moneyRequestReportActionID=${moneyRequestReportActionID}`); } - if (iouReportID) { - queryParams.push(`iouReportID=${iouReportID}`); - } - const queryString = queryParams.length > 0 ? (`${baseRoute}?${queryParams.join('&')}` as const) : baseRoute; return getUrlWithBackToParam(queryString, backTo); }, @@ -382,19 +376,10 @@ const ROUTES = { REPORT: 'r', REPORT_WITH_ID: { route: 'r/:reportID?/:reportActionID?', - getRoute: ( - reportID: string | undefined, - reportActionID?: string, - referrer?: string, - moneyRequestReportActionID?: string, - transactionID?: string, - backTo?: string, - iouReportID?: string, - ) => { + getRoute: (reportID: string | undefined, reportActionID?: string, referrer?: string, moneyRequestReportActionID?: string, transactionID?: string, backTo?: string) => { if (!reportID) { Log.warn('Invalid reportID is used to build the REPORT_WITH_ID route'); } - const baseRoute = reportActionID ? (`r/${reportID}/${reportActionID}` as const) : (`r/${reportID}` as const); const queryParams: string[] = []; @@ -408,10 +393,6 @@ const ROUTES = { queryParams.push(`transactionID=${transactionID}`); } - if (iouReportID) { - queryParams.push(`iouReportID=${iouReportID}`); - } - const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; return getUrlWithBackToParam(`${baseRoute}${queryString}` as const, backTo); @@ -593,15 +574,10 @@ const ROUTES = { }, MONEY_REQUEST_HOLD_REASON: { route: ':type/edit/reason/:transactionID?/:searchHash?', - getRoute: (type: ValueOf, transactionID: string, reportID: string | undefined, backTo: string, searchHash?: number) => { - let route = searchHash - ? (`${type as string}/edit/reason/${transactionID}/${searchHash}/?backTo=${backTo}` as const) - : (`${type as string}/edit/reason/${transactionID}/?backTo=${backTo}` as const); - - if (reportID) { - route = `${route}&reportID=${reportID}` as const; - } - + getRoute: (type: ValueOf, transactionID: string, reportID: string, backTo: string, searchHash?: number) => { + const route = searchHash + ? (`${type as string}/edit/reason/${transactionID}/${searchHash}/?backTo=${backTo}&reportID=${reportID}` as const) + : (`${type as string}/edit/reason/${transactionID}/?backTo=${backTo}&reportID=${reportID}` as const); return route; }, }, diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index f77b791ccfc2e..d0b8bc6c8b2dd 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -25,17 +25,14 @@ import {convertToDisplayString} from '@libs/CurrencyUtils'; import {getThreadReportIDsForTransactions} from '@libs/MoneyRequestReportUtils'; import {navigationRef} from '@libs/Navigation/Navigation'; import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils'; -import {generateReportID, getMoneyRequestSpendBreakdown} from '@libs/ReportUtils'; +import {getMoneyRequestSpendBreakdown} from '@libs/ReportUtils'; import {compareValues} from '@libs/SearchUIUtils'; import shouldShowTransactionYear from '@libs/TransactionUtils/shouldShowTransactionYear'; import Navigation from '@navigation/Navigation'; -import type {ReportsSplitNavigatorParamList} from '@navigation/types'; import variables from '@styles/variables'; import CONST from '@src/CONST'; import NAVIGATORS from '@src/NAVIGATORS'; -import type {Route} from '@src/ROUTES'; import ROUTES from '@src/ROUTES'; -import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; import {useMoneyRequestReportContext} from './MoneyRequestReportContext'; import MoneyRequestReportTableHeader from './MoneyRequestReportTableHeader'; @@ -75,8 +72,6 @@ type SortedTransactions = { sortOrder: SortOrder; }; -type ReportScreenNavigationProps = ReportsSplitNavigatorParamList[typeof SCREENS.REPORT]; - const isSortableColumnName = (key: unknown): key is SortableColumnName => !!sortableColumnNames.find((val) => val === key); const getTransactionKey = (transaction: OnyxTypes.Transaction, key: SortableColumnName) => { @@ -155,27 +150,19 @@ function MoneyRequestReportTransactionList({report, transactions, reportActions, const navigateToTransaction = useCallback( (activeTransaction: OnyxTypes.Transaction) => { const iouAction = getIOUActionForTransactionID(reportActions, activeTransaction.transactionID); - const reportIDToNavigate = iouAction?.childReportID ?? generateReportID(); + const reportIDToNavigate = iouAction?.childReportID; + if (!reportIDToNavigate) { + return; + } - const backTo = Navigation.getActiveRoute() as Route; + const backTo = Navigation.getActiveRoute(); // Single transaction report will open in RHP, and we need to find every other report ID for the rest of transactions // to display prev/next arrows in RHP for navigating between transactions const sortedSiblingTransactionReportIDs = getThreadReportIDsForTransactions(reportActions, sortedTransactions); setActiveTransactionThreadIDs(sortedSiblingTransactionReportIDs); - const routeParams = { - reportID: reportIDToNavigate, - backTo, - } as ReportScreenNavigationProps; - - if (!iouAction?.childReportID) { - routeParams.moneyRequestReportActionID = iouAction?.reportActionID; - routeParams.transactionID = activeTransaction.transactionID; - routeParams.iouReportID = activeTransaction.reportID; - } - - Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute(routeParams)); + Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: reportIDToNavigate, backTo})); }, [reportActions, sortedTransactions], ); diff --git a/src/components/ReportActionItem/MoneyRequestAction.tsx b/src/components/ReportActionItem/MoneyRequestAction.tsx index 3f106138485c9..6850a53b73e0c 100644 --- a/src/components/ReportActionItem/MoneyRequestAction.tsx +++ b/src/components/ReportActionItem/MoneyRequestAction.tsx @@ -98,7 +98,7 @@ function MoneyRequestAction({ const transactionID = isMoneyRequestAction(action) ? getOriginalMessage(action)?.IOUTransactionID : CONST.DEFAULT_NUMBER_ID; if (!action?.childReportID && transactionID && action.reportActionID) { const optimisticReportID = generateReportID(); - Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID, Navigation.getActiveRoute(), requestReportID)); + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID, Navigation.getActiveRoute())); return; } diff --git a/src/libs/API/parameters/CreatePerDiemRequestParams.ts b/src/libs/API/parameters/CreatePerDiemRequestParams.ts index 24f13f203adb3..480acd9ca23db 100644 --- a/src/libs/API/parameters/CreatePerDiemRequestParams.ts +++ b/src/libs/API/parameters/CreatePerDiemRequestParams.ts @@ -17,7 +17,7 @@ type CreatePerDiemRequestParams = { createdChatReportActionID?: string; createdIOUReportActionID?: string; reportPreviewReportActionID: string; - transactionThreadReportID?: string; + transactionThreadReportID: string; createdReportActionIDForThread: string | undefined; billable?: boolean; attendees?: string; diff --git a/src/libs/API/parameters/HoldMoneyRequestParams.ts b/src/libs/API/parameters/HoldMoneyRequestParams.ts index b22e421898d10..357194d7ae56a 100644 --- a/src/libs/API/parameters/HoldMoneyRequestParams.ts +++ b/src/libs/API/parameters/HoldMoneyRequestParams.ts @@ -3,8 +3,6 @@ type HoldMoneyRequestParams = { comment: string; reportActionID: string; commentReportActionID: string; - transactionThreadReportID?: string; - createdReportActionIDForThread?: string; }; export default HoldMoneyRequestParams; diff --git a/src/libs/API/parameters/MergeDuplicatesParams.ts b/src/libs/API/parameters/MergeDuplicatesParams.ts index e1cdfd0903c61..b5ef56a52502a 100644 --- a/src/libs/API/parameters/MergeDuplicatesParams.ts +++ b/src/libs/API/parameters/MergeDuplicatesParams.ts @@ -13,8 +13,6 @@ type MergeDuplicatesParams = { receiptID: number; reportID: string | undefined; reportActionID?: string | undefined; - transactionThreadReportID?: string; - createdReportActionIDForThread?: string; }; export default MergeDuplicatesParams; diff --git a/src/libs/API/parameters/RequestMoneyParams.ts b/src/libs/API/parameters/RequestMoneyParams.ts index 029df6eae5b71..74a435600cd29 100644 --- a/src/libs/API/parameters/RequestMoneyParams.ts +++ b/src/libs/API/parameters/RequestMoneyParams.ts @@ -25,8 +25,8 @@ type RequestMoneyParams = { taxAmount: number; billable?: boolean; receiptGpsPoints?: string; - transactionThreadReportID?: string; - createdReportActionIDForThread?: string | undefined; + transactionThreadReportID: string; + createdReportActionIDForThread: string | undefined; reimbursible?: boolean; description?: string; attendees?: string; diff --git a/src/libs/Navigation/types.ts b/src/libs/Navigation/types.ts index 443e99361d12f..a9b6de2afda8a 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -1644,7 +1644,6 @@ type ReportsSplitNavigatorParamList = { backTo?: Routes; moneyRequestReportActionID?: string; transactionID?: string; - iouReportID?: string; }; }; diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index ea5cb1c57d5a8..25aa61f87b3ea 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1238,6 +1238,7 @@ function getOneTransactionThreadReportID( if ( actionType && iouRequestTypesSet.has(actionType) && + action.childReportID && // Include deleted IOU reportActions if: // - they have an assocaited IOU transaction ID or // - they have visibile childActions (like comments) that we'd want to display @@ -1265,8 +1266,8 @@ function getOneTransactionThreadReportID( return; } - // Since we don't always create transaction thread optimistically, we return CONST.FAKE_REPORT_ID - return singleAction?.childReportID ?? CONST.FAKE_REPORT_ID; + // Ensure we have a childReportID associated with the IOU report action + return singleAction?.childReportID; } /** diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index 039cbc3a021a6..7a0c2a7481376 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -609,7 +609,7 @@ type OptimisticModifiedExpenseReportAction = Pick< | 'delegateAccountID' > & {reportID?: string}; -type BaseOptimisticMoneyRequestEntities = { +type OptimisticMoneyRequestEntities = { iouReport: Report; type: ValueOf; amount: number; @@ -627,10 +627,6 @@ type BaseOptimisticMoneyRequestEntities = { linkedTrackedExpenseReportAction?: ReportAction; }; -type OptimisticMoneyRequestEntities = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread?: boolean}; -type OptimisticMoneyRequestEntitiesWithTransactionThreadFlag = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread: boolean}; -type OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag = BaseOptimisticMoneyRequestEntities; - type OptimisticTaskReport = SetRequired< Pick< Report, @@ -3998,8 +3994,8 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry, sea const transactionID = getOriginalMessage(reportAction)?.IOUTransactionID; - if (!transactionID) { - Log.warn('Missing transactionID during the change of the money request hold status'); + if (!transactionID || !reportAction.childReportID) { + Log.warn('Missing transactionID and reportAction.childReportID during the change of the money request hold status'); return; } @@ -4007,7 +4003,7 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry, sea const isOnHold = isOnHoldTransactionUtils(transaction); const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport.policyID}`] ?? null; - if (isOnHold && reportAction.childReportID) { + if (isOnHold) { unholdRequest(transactionID, reportAction.childReportID, searchHash); } else { const activeRoute = encodeURIComponent(Navigation.getActiveRoute()); @@ -7348,7 +7344,6 @@ function buildTransactionThread( reportAction: OnyxEntry, moneyRequestReport: OnyxEntry, existingTransactionThreadReportID?: string, - optimisticTransactionThreadReportID?: string, ): OptimisticChatReport { const participantAccountIDs = [...new Set([currentUserAccountID, Number(reportAction?.actorAccountID)])].filter(Boolean) as number[]; const existingTransactionThreadReport = getReportOrDraftReport(existingTransactionThreadReportID); @@ -7371,7 +7366,6 @@ function buildTransactionThread( notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, parentReportActionID: reportAction?.reportActionID, parentReportID: moneyRequestReport?.reportID, - optimisticReportID: optimisticTransactionThreadReportID, }); } @@ -7384,12 +7378,6 @@ function buildTransactionThread( * 4. Transaction Thread linked to the IOU action via `parentReportActionID` * 5. CREATED action for the Transaction Thread */ -function buildOptimisticMoneyRequestEntities( - optimisticMoneyRequestEntities: OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag, -): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null]; -function buildOptimisticMoneyRequestEntities( - optimisticMoneyRequestEntities: OptimisticMoneyRequestEntitiesWithTransactionThreadFlag, -): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport | undefined, OptimisticCreatedReportAction | null]; function buildOptimisticMoneyRequestEntities({ iouReport, type, @@ -7403,17 +7391,10 @@ function buildOptimisticMoneyRequestEntities({ isSettlingUp = false, isSendMoneyFlow = false, isOwnPolicyExpenseChat = false, - shouldGenerateOptimisticTransactionThread = true, isPersonalTrackingExpense, existingTransactionThreadReportID, linkedTrackedExpenseReportAction, -}: OptimisticMoneyRequestEntities): [ - OptimisticCreatedReportAction, - OptimisticCreatedReportAction, - OptimisticIOUReportAction, - OptimisticChatReport | undefined, - OptimisticCreatedReportAction | null, -] { +}: OptimisticMoneyRequestEntities): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null] { const createdActionForChat = buildOptimisticCreatedReportAction(payeeEmail); // The `CREATED` action must be optimistically generated before the IOU action so that it won't appear after the IOU action in the chat. @@ -7438,11 +7419,11 @@ function buildOptimisticMoneyRequestEntities({ }); // Create optimistic transactionThread and the `CREATED` action for it, if existingTransactionThreadReportID is undefined - const transactionThread = shouldGenerateOptimisticTransactionThread ? buildTransactionThread(iouAction, iouReport, existingTransactionThreadReportID) : undefined; - const createdActionForTransactionThread = !!existingTransactionThreadReportID || !shouldGenerateOptimisticTransactionThread ? null : buildOptimisticCreatedReportAction(payeeEmail); + const transactionThread = buildTransactionThread(iouAction, iouReport, existingTransactionThreadReportID); + const createdActionForTransactionThread = existingTransactionThreadReportID ? null : buildOptimisticCreatedReportAction(payeeEmail); // The IOU action and the transactionThread are co-dependent as parent-child, so we need to link them together - iouAction.childReportID = existingTransactionThreadReportID ?? transactionThread?.reportID; + iouAction.childReportID = existingTransactionThreadReportID ?? transactionThread.reportID; return [createdActionForChat, createdActionForIOUReport, iouAction, transactionThread, createdActionForTransactionThread]; } @@ -9231,12 +9212,12 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined, includ /** * Get optimistic data of parent report action - * @param reportOrID The reportID of the report that is updated or the optimistic report on its own + * @param reportID The reportID of the report that is updated * @param lastVisibleActionCreated Last visible action created of the child report * @param type The type of action in the child report */ -function getOptimisticDataForParentReportAction(reportOrID: Report | string | undefined, lastVisibleActionCreated: string, type: string): Array { - const report = typeof reportOrID === 'string' ? getReportOrDraftReport(reportOrID) : reportOrID; +function getOptimisticDataForParentReportAction(reportID: string | undefined, lastVisibleActionCreated: string, type: string): Array { + const report = getReportOrDraftReport(reportID); if (!report || isEmptyObject(report)) { return []; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index f438b9d474884..ea3ba50723b09 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -16,7 +16,6 @@ import type { CreateWorkspaceParams, DeleteMoneyRequestParams, DetachReceiptParams, - HoldMoneyRequestParams, MergeDuplicatesParams, PayInvoiceParams, PayMoneyRequestParams, @@ -77,7 +76,6 @@ import { } from '@libs/PolicyUtils'; import { getAllReportActions, - getIOUActionForReportID, getIOUReportIDFromReportActionPreview, getLastVisibleAction, getLastVisibleMessage, @@ -116,10 +114,8 @@ import { buildOptimisticSubmittedReportAction, buildOptimisticUnapprovedReportAction, buildOptimisticUnHoldReportAction, - buildTransactionThread, canBeAutoReimbursed, canUserPerformWriteAction as canUserPerformWriteActionReportUtils, - generateReportID, getAllHeldTransactions as getAllHeldTransactionsReportUtils, getAllPolicyReports, getApprovalChain, @@ -251,7 +247,7 @@ type MoneyRequestInformation = { createdChatReportActionID?: string; createdIOUReportActionID?: string; reportPreviewAction: OnyxTypes.ReportAction; - transactionThreadReportID?: string; + transactionThreadReportID: string; createdReportActionIDForThread: string | undefined; onyxData: OnyxData; billable?: boolean; @@ -423,7 +419,6 @@ type MoneyRequestInformationParams = { existingTransactionID?: string; existingTransaction?: OnyxEntry; retryParams?: StartSplitBilActionParams | CreateTrackExpenseParams | RequestMoneyInformation | ReplaceReceipt; - shouldGenerateOptimisticTransactionThread?: boolean; }; type MoneyRequestOptimisticParams = { @@ -439,8 +434,8 @@ type MoneyRequestOptimisticParams = { }; transactionParams: { transaction: OnyxTypes.Transaction; - transactionThreadReport?: OptimisticChatReport | null; - transactionThreadCreatedReportAction?: OptimisticCreatedReportAction | null; + transactionThreadReport: OptimisticChatReport | null; + transactionThreadCreatedReportAction: OptimisticCreatedReportAction | null; }; policyRecentlyUsed: { categories?: string[]; @@ -3089,7 +3084,6 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma existingTransactionID, moneyRequestReportID = '', retryParams, - shouldGenerateOptimisticTransactionThread = true, } = moneyRequestInformation; const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams; const {policy, policyCategories, policyTagList} = policyParams; @@ -3209,7 +3203,6 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma paymentType: isSelectedManagerMcTest(participant.login) ? CONST.IOU.PAYMENT_TYPE.ELSEWHERE : undefined, existingTransactionThreadReportID: linkedTrackedExpenseReportAction?.childReportID, linkedTrackedExpenseReportAction, - shouldGenerateOptimisticTransactionThread, }); let reportPreviewAction = shouldCreateNewMoneyRequestReport ? null : getReportPreviewAction(chatReport.reportID, iouReport.reportID); @@ -4674,32 +4667,30 @@ const getConvertTrackedExpenseInformation = ( // Build modified expense report action with the transaction changes const modifiedExpenseReportAction = buildOptimisticMovedTransactionAction(transactionThreadReportID, moneyRequestReportID ?? CONST.REPORT.UNREPORTED_REPORT_ID); - if (transactionThreadReportID) { - optimisticData?.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, - value: { - [modifiedExpenseReportAction.reportActionID]: modifiedExpenseReportAction, - }, - }); - successData?.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, - value: { - [modifiedExpenseReportAction.reportActionID]: {pendingAction: null}, - }, - }); - failureData?.push({ - onyxMethod: Onyx.METHOD.MERGE, - key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, - value: { - [modifiedExpenseReportAction.reportActionID]: { - ...modifiedExpenseReportAction, - errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericEditFailureMessage'), - }, + optimisticData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, + value: { + [modifiedExpenseReportAction.reportActionID]: modifiedExpenseReportAction, + }, + }); + successData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, + value: { + [modifiedExpenseReportAction.reportActionID]: {pendingAction: null}, + }, + }); + failureData?.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, + value: { + [modifiedExpenseReportAction.reportActionID]: { + ...modifiedExpenseReportAction, + errors: getMicroSecondOnyxErrorWithTranslationKey('iou.error.genericEditFailureMessage'), }, - }); - } + }, + }); return {optimisticData, successData, failureData, modifiedExpenseReportActionID: modifiedExpenseReportAction.reportActionID}; }; @@ -4748,7 +4739,7 @@ type ConvertTrackedExpenseToRequestParams = { merchant: string; created: string; attendees?: Attendee[]; - transactionThreadReportID?: string; + transactionThreadReportID: string; }; chatParams: { reportID: string; @@ -5051,18 +5042,29 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) { }, }; - const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} = - getMoneyRequestInformation({ - parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport, - participantParams, - policyParams, - transactionParams, - moneyRequestReportID, - existingTransactionID, - existingTransaction: isDistanceRequestTransactionUtils(existingTransaction) ? existingTransaction : undefined, - retryParams, - shouldGenerateOptimisticTransactionThread: false, - }); + const { + payerAccountID, + payerEmail, + iouReport, + chatReport, + transaction, + iouAction, + createdChatReportActionID, + createdIOUReportActionID, + reportPreviewAction, + transactionThreadReportID, + createdReportActionIDForThread, + onyxData, + } = getMoneyRequestInformation({ + parentChatReport: isMovingTransactionFromTrackExpense ? undefined : currentChatReport, + participantParams, + policyParams, + transactionParams, + moneyRequestReportID, + existingTransactionID, + existingTransaction: isDistanceRequestTransactionUtils(existingTransaction) ? existingTransaction : undefined, + retryParams, + }); const activeReportID = isMoneyRequestReport ? report?.reportID : chatReport.reportID; switch (action) { @@ -5100,6 +5102,7 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) { actionableWhisperReportActionID, linkedTrackedExpenseReportAction, linkedTrackedExpenseReportID, + transactionThreadReportID, }, chatParams: { reportID: chatReport.reportID, @@ -5141,6 +5144,8 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) { billable, // This needs to be a string of JSON because of limitations with the fetch() API and nested objects receiptGpsPoints: gpsPoints ? JSON.stringify(gpsPoints) : undefined, + transactionThreadReportID, + createdReportActionIDForThread, reimbursible, description: parsedComment, attendees: attendees ? JSON.stringify(attendees) : undefined, @@ -10163,29 +10168,17 @@ function adjustRemainingSplitShares(transaction: NonNullable notifyNewAction(currentReportID, userAccountID)); @@ -10574,9 +10514,7 @@ function getIOUActionForTransactions(transactionIDList: Array { - const transactionThreadReportID = reportAction?.childReportID ?? generateReportID(); - if (!reportAction?.childReportID) { - transactionsMergeParams.transactionThreadReportID = transactionThreadReportID; + return; } - IOU.mergeDuplicates(transactionsMergeParams); if (canUseTableReportView) { Navigation.dismissModal(); return; } - Navigation.dismissModalWithReport({reportID: transactionThreadReportID}); + if (!reportAction?.childReportID) { + return; + } + Navigation.dismissModalWithReport({reportID: reportAction.childReportID}); }, [reportAction?.childReportID, transactionsMergeParams, canUseTableReportView]); const resolveDuplicates = useCallback(() => { diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index 73691c688b158..ce6b26989a9d6 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -40,9 +40,7 @@ import {getPersonalDetailsForAccountIDs} from '@libs/OptionsListUtils'; import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils'; import { getCombinedReportActions, - getIOUActionForReportID, getOneTransactionThreadReportID, - getReportAction, isCreatedAction, isDeletedParentAction, isMoneyRequestAction, @@ -50,15 +48,11 @@ import { shouldReportActionBeVisible, } from '@libs/ReportActionsUtils'; import { - buildTransactionThread, canEditReportAction, canUserPerformWriteAction, findLastAccessedReport, - generateReportID, getParticipantsAccountIDsForDisplay, getReportOfflinePendingActionAndErrors, - getReportOrDraftReport, - getReportTransactions, isChatThread, isConciergeChatReport, isGroupChat, @@ -304,7 +298,6 @@ function ReportScreen({route, navigation}: ReportScreenProps) { canBeMissing: false, }); const transactionThreadReportID = getOneTransactionThreadReportID(reportID, reportActions ?? [], isOffline); - const [transactionThreadReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${transactionThreadReportID}`, {canBeMissing: true}); const [transactionThreadReportActions = {}] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReportID}`, {canBeMissing: true}); const combinedReportActions = getCombinedReportActions(reportActions, transactionThreadReportID ?? null, Object.values(transactionThreadReportActions)); const lastReportAction = [...combinedReportActions, parentReportAction].find((action) => canEditReportAction(action) && !isMoneyRequestAction(action)); @@ -454,30 +447,17 @@ function ReportScreen({route, navigation}: ReportScreenProps) { return; } - const {moneyRequestReportActionID, transactionID, iouReportID} = route.params; + const moneyRequestReportActionID: string | undefined = route.params?.moneyRequestReportActionID; + const transactionID: string | undefined = route.params?.transactionID; // When we get here with a moneyRequestReportActionID and a transactionID from the route it means we don't have the transaction thread created yet // so we have to call OpenReport in a way that the transaction thread will be created and attached to the parentReportAction - if (transactionID && currentUserEmail && !report) { - const iouReport = getReportOrDraftReport(iouReportID); - const iouAction = getReportAction(iouReportID, moneyRequestReportActionID); - const optimisticTransactionThread = buildTransactionThread(iouAction, iouReport, undefined, reportIDFromRoute); - openReport(reportIDFromRoute, undefined, [currentUserEmail], optimisticTransactionThread, moneyRequestReportActionID, false, [], undefined, undefined, transactionID); + if (transactionID && currentUserEmail) { + openReport(reportIDFromRoute, '', [currentUserEmail], undefined, moneyRequestReportActionID, false, [], undefined, undefined, transactionID); return; } - - // If there is one transaction thread that has not yet been created, we should create it. - if (transactionThreadReportID === CONST.FAKE_REPORT_ID && !transactionThreadReport && currentUserEmail) { - const optimisticTransactionThreadReportID = generateReportID(); - const transactions = getReportTransactions(reportID); - const oneTransactionID = transactions.at(0)?.transactionID; - const iouAction = getIOUActionForReportID(reportID, oneTransactionID); - const optimisticTransactionThread = buildTransactionThread(iouAction, report, undefined, optimisticTransactionThreadReportID); - openReport(optimisticTransactionThreadReportID, undefined, [currentUserEmail], optimisticTransactionThread, iouAction?.reportActionID); - } - openReport(reportIDFromRoute, reportActionIDFromRoute); - }, [reportMetadata.isOptimisticReport, route.params, reportIDFromRoute, reportActionIDFromRoute, currentUserEmail, report, reportID, transactionThreadReport, transactionThreadReportID]); + }, [reportMetadata.isOptimisticReport, route.params?.moneyRequestReportActionID, route.params?.transactionID, reportIDFromRoute, reportActionIDFromRoute, currentUserEmail]); useEffect(() => { if (!reportID || !isFocused) { @@ -543,7 +523,7 @@ function ReportScreen({route, navigation}: ReportScreenProps) { // There should be only one openReport execution per page start or navigating fetchReport(); // eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps - }, [route, isLinkedMessagePageReady, reportActionIDFromRoute, transactionThreadReportID]); + }, [route, isLinkedMessagePageReady, reportActionIDFromRoute]); const prevReportActions = usePrevious(reportActions); useEffect(() => { diff --git a/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx b/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx index 75d36d9bf31ce..2a236649aa07e 100755 --- a/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx +++ b/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx @@ -20,13 +20,12 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useRestoreInputFocus from '@hooks/useRestoreInputFocus'; import useStyleUtils from '@hooks/useStyleUtils'; import {getExpensifyCardFromReportAction} from '@libs/CardMessageUtils'; -import {getLinkedTransactionID, getOneTransactionThreadReportID, getOriginalMessage, getReportActions, isMoneyRequestAction} from '@libs/ReportActionsUtils'; +import {getLinkedTransactionID, getOneTransactionThreadReportID, getReportAction} from '@libs/ReportActionsUtils'; import { chatIncludesChronosWithID, getSourceIDFromReportAction, isArchivedNonExpenseReport, isArchivedNonExpenseReportWithID, - isIOUReport, isInvoiceReport as ReportUtilsIsInvoiceReport, isMoneyRequest as ReportUtilsIsMoneyRequest, isMoneyRequestReport as ReportUtilsIsMoneyRequestReport, @@ -164,7 +163,7 @@ function BaseReportActionContextMenu({ const [download] = useOnyx(`${ONYXKEYS.COLLECTION.DOWNLOAD}${sourceID}`, {canBeMissing: true}); const [childReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportAction?.childReportID}`, {canBeMissing: true}); - const childReportActions = childReport ? getReportActions(childReport) : undefined; + const parentReportAction = getReportAction(childReport?.parentReportID, childReport?.parentReportActionID); const {reportActions: paginatedReportActions} = usePaginatedReportActions(childReport?.reportID); const transactionThreadReportID = useMemo( @@ -179,29 +178,24 @@ function BaseReportActionContextMenu({ const requestParentReportAction = useMemo(() => { if (isMoneyRequestReport || isInvoiceReport) { - if (!paginatedReportActions) { + if (!paginatedReportActions || !transactionThreadReport?.parentReportActionID) { return undefined; } - if (transactionThreadReportID === CONST.FAKE_REPORT_ID) { - return Object.values(childReportActions ?? {}).find((action) => action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU); - } - return paginatedReportActions.find((action) => action.reportActionID === transactionThreadReport?.parentReportActionID); + return paginatedReportActions.find((action) => action.reportActionID === transactionThreadReport.parentReportActionID); } - return reportAction; - }, [childReportActions, transactionThreadReportID, reportAction, isMoneyRequestReport, isInvoiceReport, paginatedReportActions, transactionThreadReport?.parentReportActionID]); + return parentReportAction; + }, [parentReportAction, isMoneyRequestReport, isInvoiceReport, paginatedReportActions, transactionThreadReport?.parentReportActionID]); - const moneyRequestAction = transactionThreadReportID ? requestParentReportAction : reportAction; + const moneyRequestAction = transactionThreadReportID ? requestParentReportAction : parentReportAction; const [childReportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${childReport?.reportID}`, {canBeMissing: true}); const [parentReportNameValuePairs] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${childReport?.parentReportID}`, {canBeMissing: true}); - const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${childReport?.parentReportID ?? (isMoneyRequestAction(reportAction) && getOriginalMessage(reportAction)?.IOUReportID)}`, { - canBeMissing: true, - }); + const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${childReport?.parentReportID}`, {canBeMissing: true}); const isMoneyRequest = useMemo(() => ReportUtilsIsMoneyRequest(childReport), [childReport]); const isTrackExpenseReport = ReportUtilsIsTrackExpenseReport(childReport); const isSingleTransactionView = isMoneyRequest || isTrackExpenseReport; - const isMoneyRequestOrReport = isMoneyRequestReport || isSingleTransactionView || isIOUReport(parentReport); + const isMoneyRequestOrReport = isMoneyRequestReport || isSingleTransactionView; const areHoldRequirementsMet = !isInvoiceReport && diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 4bfe8452ec7eb..9543ae0d412f6 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -620,6 +620,8 @@ describe('actions/IOU', () => { let createdAction: OnyxEntry; let iouAction: OnyxEntry>; let transactionID: string | undefined; + let transactionThread: OnyxEntry; + let transactionThreadCreatedAction: OnyxEntry; mockFetch?.pause?.(); requestMoney({ report: {reportID: ''}, @@ -647,14 +649,16 @@ describe('actions/IOU', () => { callback: (allReports) => { Onyx.disconnect(connection); - // A chat report and an iou report should be created + // A chat report, a transaction thread, and an iou report should be created const chatReports = Object.values(allReports ?? {}).filter((report) => report?.type === CONST.REPORT.TYPE.CHAT); const iouReports = Object.values(allReports ?? {}).filter((report) => report?.type === CONST.REPORT.TYPE.IOU); - expect(Object.keys(chatReports).length).toBe(1); + expect(Object.keys(chatReports).length).toBe(2); expect(Object.keys(iouReports).length).toBe(1); const chatReport = chatReports.at(0); + const transactionThreadReport = chatReports.at(1); const iouReport = iouReports.at(0); iouReportID = iouReport?.reportID; + transactionThread = transactionThreadReport; expect(iouReport?.participants).toEqual({ [RORY_ACCOUNT_ID]: {notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN}, @@ -717,6 +721,29 @@ describe('actions/IOU', () => { }); }), ) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThread?.reportID}`, + waitForCollectionCallback: false, + callback: (reportActionsForTransactionThread) => { + Onyx.disconnect(connection); + + // The transaction thread should have a CREATED action + expect(Object.values(reportActionsForTransactionThread ?? {}).length).toBe(1); + const createdActions = Object.values(reportActionsForTransactionThread ?? {}).filter( + (reportAction) => reportAction?.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED, + ); + expect(Object.values(createdActions).length).toBe(1); + transactionThreadCreatedAction = createdActions.at(0); + + expect(transactionThreadCreatedAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + resolve(); + }, + }); + }), + ) .then( () => new Promise((resolve) => { @@ -1179,6 +1206,8 @@ describe('actions/IOU', () => { let createdAction: OnyxEntry; let iouAction: OnyxEntry>; let transactionID: string | undefined; + let transactionThreadReport: OnyxEntry; + let transactionThreadAction: OnyxEntry; mockFetch?.pause?.(); requestMoney({ report: {reportID: ''}, @@ -1207,13 +1236,14 @@ describe('actions/IOU', () => { callback: (allReports) => { Onyx.disconnect(connection); - // A chat report and an iou report should be created + // A chat report, transaction thread and an iou report should be created const chatReports = Object.values(allReports ?? {}).filter((report) => report?.type === CONST.REPORT.TYPE.CHAT); const iouReports = Object.values(allReports ?? {}).filter((report) => report?.type === CONST.REPORT.TYPE.IOU); - expect(Object.values(chatReports).length).toBe(1); + expect(Object.values(chatReports).length).toBe(2); expect(Object.values(iouReports).length).toBe(1); const chatReport = chatReports.at(0); chatReportID = chatReport?.reportID; + transactionThreadReport = chatReports.at(1); const iouReport = iouReports.at(0); iouReportID = iouReport?.reportID; @@ -1326,6 +1356,24 @@ describe('actions/IOU', () => { }); }), ) + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, + waitForCollectionCallback: true, + callback: (reportActionsForTransactionThread) => { + Onyx.disconnect(connection); + expect(Object.values(reportActionsForTransactionThread ?? {}).length).toBe(3); + transactionThreadAction = Object.values( + reportActionsForTransactionThread?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport?.reportID}`] ?? {}, + ).find((reportAction) => reportAction.actionName === CONST.REPORT.ACTIONS.TYPE.CREATED); + expect(transactionThreadAction?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + resolve(); + }, + }); + }), + ) .then( () => new Promise((resolve) => { @@ -1392,6 +1440,22 @@ describe('actions/IOU', () => { }), ) + // Then the reportAction from transaction report should be removed from Onyx + .then( + () => + new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${transactionThreadReport?.reportID}`, + waitForCollectionCallback: false, + callback: (reportActionsForReport) => { + Onyx.disconnect(connection); + expect(reportActionsForReport).toMatchObject({}); + resolve(); + }, + }); + }), + ) + // Along with the associated transaction .then( () => @@ -1415,6 +1479,9 @@ describe('actions/IOU', () => { if (chatReportID) { deleteReport(chatReportID); } + if (transactionThreadReport?.reportID) { + deleteReport(transactionThreadReport?.reportID); + } resolve(); }), )