diff --git a/src/CONST.ts b/src/CONST.ts index 30ba4d4e98b9..448af0b44569 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -1037,6 +1037,7 @@ 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 7ade1df2713b..b313dc694a21 100644 --- a/src/ROUTES.ts +++ b/src/ROUTES.ts @@ -82,12 +82,14 @@ 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'); @@ -104,6 +106,10 @@ 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); }, @@ -369,10 +375,19 @@ 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) => { + getRoute: ( + reportID: string | undefined, + reportActionID?: string, + referrer?: string, + moneyRequestReportActionID?: string, + transactionID?: string, + backTo?: string, + iouReportID?: 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[] = []; @@ -386,6 +401,10 @@ 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); @@ -567,10 +586,15 @@ const ROUTES = { }, MONEY_REQUEST_HOLD_REASON: { route: ':type/edit/reason/:transactionID?/:searchHash?', - 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); + 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; + } + return route; }, }, diff --git a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx index bfb37070d64c..1354ff233bfe 100644 --- a/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx +++ b/src/components/MoneyRequestReportView/MoneyRequestReportTransactionList.tsx @@ -25,14 +25,17 @@ import {convertToDisplayString} from '@libs/CurrencyUtils'; import {getThreadReportIDsForTransactions} from '@libs/MoneyRequestReportUtils'; import {navigationRef} from '@libs/Navigation/Navigation'; import {getIOUActionForTransactionID} from '@libs/ReportActionsUtils'; -import {getMoneyRequestSpendBreakdown} from '@libs/ReportUtils'; +import {generateReportID, 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'; @@ -72,6 +75,8 @@ 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) => { @@ -150,19 +155,27 @@ function MoneyRequestReportTransactionList({report, transactions, reportActions, const navigateToTransaction = useCallback( (activeTransaction: OnyxTypes.Transaction) => { const iouAction = getIOUActionForTransactionID(reportActions, activeTransaction.transactionID); - const reportIDToNavigate = iouAction?.childReportID; - if (!reportIDToNavigate) { - return; - } + const reportIDToNavigate = iouAction?.childReportID ?? generateReportID(); - const backTo = Navigation.getActiveRoute(); + const backTo = Navigation.getActiveRoute() as Route; // 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); - Navigation.navigate(ROUTES.SEARCH_REPORT.getRoute({reportID: reportIDToNavigate, backTo})); + 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)); }, [reportActions, sortedTransactions], ); diff --git a/src/components/ReportActionItem/MoneyRequestAction.tsx b/src/components/ReportActionItem/MoneyRequestAction.tsx index 6850a53b73e0..3f106138485c 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())); + Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(optimisticReportID, undefined, undefined, action.reportActionID, transactionID, Navigation.getActiveRoute(), requestReportID)); return; } diff --git a/src/libs/API/parameters/CreatePerDiemRequestParams.ts b/src/libs/API/parameters/CreatePerDiemRequestParams.ts index 480acd9ca23d..24f13f203adb 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 357194d7ae56..b22e421898d1 100644 --- a/src/libs/API/parameters/HoldMoneyRequestParams.ts +++ b/src/libs/API/parameters/HoldMoneyRequestParams.ts @@ -3,6 +3,8 @@ 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 b5ef56a52502..e1cdfd0903c6 100644 --- a/src/libs/API/parameters/MergeDuplicatesParams.ts +++ b/src/libs/API/parameters/MergeDuplicatesParams.ts @@ -13,6 +13,8 @@ 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 74a435600cd2..029df6eae5b7 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 a18884f2257c..c163aead379f 100644 --- a/src/libs/Navigation/types.ts +++ b/src/libs/Navigation/types.ts @@ -1632,6 +1632,7 @@ type ReportsSplitNavigatorParamList = { backTo?: Routes; moneyRequestReportActionID?: string; transactionID?: string; + iouReportID?: string; }; }; diff --git a/src/libs/ReportActionsUtils.ts b/src/libs/ReportActionsUtils.ts index 1a3d58d3dc07..b13bca41c964 100644 --- a/src/libs/ReportActionsUtils.ts +++ b/src/libs/ReportActionsUtils.ts @@ -1234,7 +1234,6 @@ 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 @@ -1262,8 +1261,8 @@ function getOneTransactionThreadReportID( return; } - // Ensure we have a childReportID associated with the IOU report action - return singleAction?.childReportID; + // Since we don't always create transaction thread optimistically, we return CONST.FAKE_REPORT_ID + return singleAction?.childReportID ?? CONST.FAKE_REPORT_ID; } /** diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e86d74acf89a..8c3a35f34c89 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -600,7 +600,7 @@ type OptimisticModifiedExpenseReportAction = Pick< | 'delegateAccountID' > & {reportID?: string}; -type OptimisticMoneyRequestEntities = { +type BaseOptimisticMoneyRequestEntities = { iouReport: Report; type: ValueOf; amount: number; @@ -618,6 +618,10 @@ type OptimisticMoneyRequestEntities = { linkedTrackedExpenseReportAction?: ReportAction; }; +type OptimisticMoneyRequestEntities = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread?: boolean}; +type OptimisticMoneyRequestEntitiesWithTransactionThreadFlag = BaseOptimisticMoneyRequestEntities & {shouldGenerateOptimisticTransactionThread: boolean}; +type OptimisticMoneyRequestEntitiesWithoutTransactionThreadFlag = BaseOptimisticMoneyRequestEntities; + type OptimisticTaskReport = SetRequired< Pick< Report, @@ -3982,8 +3986,8 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry, sea const transactionID = getOriginalMessage(reportAction)?.IOUTransactionID; - if (!transactionID || !reportAction.childReportID) { - Log.warn('Missing transactionID and reportAction.childReportID during the change of the money request hold status'); + if (!transactionID) { + Log.warn('Missing transactionID during the change of the money request hold status'); return; } @@ -3991,7 +3995,7 @@ const changeMoneyRequestHoldStatus = (reportAction: OnyxEntry, sea const isOnHold = isOnHoldTransactionUtils(transaction); const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${moneyRequestReport.policyID}`] ?? null; - if (isOnHold) { + if (isOnHold && reportAction.childReportID) { unholdRequest(transactionID, reportAction.childReportID, searchHash); } else { const activeRoute = encodeURIComponent(Navigation.getActiveRoute()); @@ -7298,6 +7302,7 @@ 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); @@ -7320,6 +7325,7 @@ function buildTransactionThread( notificationPreference: CONST.REPORT.NOTIFICATION_PREFERENCE.HIDDEN, parentReportActionID: reportAction?.reportActionID, parentReportID: moneyRequestReport?.reportID, + optimisticReportID: optimisticTransactionThreadReportID, }); } @@ -7332,6 +7338,12 @@ 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, @@ -7345,10 +7357,17 @@ function buildOptimisticMoneyRequestEntities({ isSettlingUp = false, isSendMoneyFlow = false, isOwnPolicyExpenseChat = false, + shouldGenerateOptimisticTransactionThread = true, isPersonalTrackingExpense, existingTransactionThreadReportID, linkedTrackedExpenseReportAction, -}: OptimisticMoneyRequestEntities): [OptimisticCreatedReportAction, OptimisticCreatedReportAction, OptimisticIOUReportAction, OptimisticChatReport, OptimisticCreatedReportAction | null] { +}: OptimisticMoneyRequestEntities): [ + OptimisticCreatedReportAction, + OptimisticCreatedReportAction, + OptimisticIOUReportAction, + OptimisticChatReport | undefined, + 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. @@ -7373,11 +7392,11 @@ function buildOptimisticMoneyRequestEntities({ }); // Create optimistic transactionThread and the `CREATED` action for it, if existingTransactionThreadReportID is undefined - const transactionThread = buildTransactionThread(iouAction, iouReport, existingTransactionThreadReportID); - const createdActionForTransactionThread = existingTransactionThreadReportID ? null : buildOptimisticCreatedReportAction(payeeEmail); + const transactionThread = shouldGenerateOptimisticTransactionThread ? buildTransactionThread(iouAction, iouReport, existingTransactionThreadReportID) : undefined; + const createdActionForTransactionThread = !!existingTransactionThreadReportID || !shouldGenerateOptimisticTransactionThread ? 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]; } @@ -9166,12 +9185,12 @@ function getAllAncestorReportActionIDs(report: Report | null | undefined, includ /** * Get optimistic data of parent report action - * @param reportID The reportID of the report that is updated + * @param reportOrID The reportID of the report that is updated or the optimistic report on its own * @param lastVisibleActionCreated Last visible action created of the child report * @param type The type of action in the child report */ -function getOptimisticDataForParentReportAction(reportID: string | undefined, lastVisibleActionCreated: string, type: string): Array { - const report = getReportOrDraftReport(reportID); +function getOptimisticDataForParentReportAction(reportOrID: Report | string | undefined, lastVisibleActionCreated: string, type: string): Array { + const report = typeof reportOrID === 'string' ? getReportOrDraftReport(reportOrID) : reportOrID; if (!report || isEmptyObject(report)) { return []; diff --git a/src/libs/actions/IOU.ts b/src/libs/actions/IOU.ts index 92f6f05c2e62..c7199ac121a7 100644 --- a/src/libs/actions/IOU.ts +++ b/src/libs/actions/IOU.ts @@ -16,6 +16,7 @@ import type { CreateWorkspaceParams, DeleteMoneyRequestParams, DetachReceiptParams, + HoldMoneyRequestParams, MergeDuplicatesParams, PayInvoiceParams, PayMoneyRequestParams, @@ -75,6 +76,7 @@ import { } from '@libs/PolicyUtils'; import { getAllReportActions, + getIOUActionForReportID, getIOUReportIDFromReportActionPreview, getLastVisibleAction, getLastVisibleMessage, @@ -112,8 +114,10 @@ import { buildOptimisticSubmittedReportAction, buildOptimisticUnapprovedReportAction, buildOptimisticUnHoldReportAction, + buildTransactionThread, canBeAutoReimbursed, canUserPerformWriteAction as canUserPerformWriteActionReportUtils, + generateReportID, getAllHeldTransactions as getAllHeldTransactionsReportUtils, getAllPolicyReports, getApprovalChain, @@ -245,7 +249,7 @@ type MoneyRequestInformation = { createdChatReportActionID?: string; createdIOUReportActionID?: string; reportPreviewAction: OnyxTypes.ReportAction; - transactionThreadReportID: string; + transactionThreadReportID?: string; createdReportActionIDForThread: string | undefined; onyxData: OnyxData; billable?: boolean; @@ -416,6 +420,7 @@ type MoneyRequestInformationParams = { existingTransactionID?: string; existingTransaction?: OnyxEntry; retryParams?: StartSplitBilActionParams | CreateTrackExpenseParams | RequestMoneyInformation | ReplaceReceipt; + shouldGenerateOptimisticTransactionThread?: boolean; }; type MoneyRequestOptimisticParams = { @@ -431,8 +436,8 @@ type MoneyRequestOptimisticParams = { }; transactionParams: { transaction: OnyxTypes.Transaction; - transactionThreadReport: OptimisticChatReport | null; - transactionThreadCreatedReportAction: OptimisticCreatedReportAction | null; + transactionThreadReport?: OptimisticChatReport | null; + transactionThreadCreatedReportAction?: OptimisticCreatedReportAction | null; }; policyRecentlyUsed: { categories?: string[]; @@ -3080,6 +3085,7 @@ function getMoneyRequestInformation(moneyRequestInformation: MoneyRequestInforma existingTransactionID, moneyRequestReportID = '', retryParams, + shouldGenerateOptimisticTransactionThread = true, } = moneyRequestInformation; const {payeeAccountID = userAccountID, payeeEmail = currentUserEmail, participant} = participantParams; const {policy, policyCategories, policyTagList} = policyParams; @@ -3199,6 +3205,7 @@ 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); @@ -4663,30 +4670,32 @@ const getConvertTrackedExpenseInformation = ( // Build modified expense report action with the transaction changes const modifiedExpenseReportAction = buildOptimisticMovedTransactionAction(transactionThreadReportID, moneyRequestReportID ?? CONST.REPORT.UNREPORTED_REPORT_ID); - 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'), + 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'), + }, + }, + }); + } return {optimisticData, successData, failureData, modifiedExpenseReportActionID: modifiedExpenseReportAction.reportActionID}; }; @@ -4735,7 +4744,7 @@ type ConvertTrackedExpenseToRequestParams = { merchant: string; created: string; attendees?: Attendee[]; - transactionThreadReportID: string; + transactionThreadReportID?: string; }; chatParams: { reportID: string; @@ -5038,29 +5047,18 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) { }, }; - 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 {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 activeReportID = isMoneyRequestReport ? report?.reportID : chatReport.reportID; switch (action) { @@ -5098,7 +5096,6 @@ function requestMoney(requestMoneyInformation: RequestMoneyInformation) { actionableWhisperReportActionID, linkedTrackedExpenseReportAction, linkedTrackedExpenseReportID, - transactionThreadReportID, }, chatParams: { reportID: chatReport.reportID, @@ -5140,8 +5137,6 @@ 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, @@ -10034,17 +10029,29 @@ function adjustRemainingSplitShares(transaction: NonNullable notifyNewAction(currentReportID, userAccountID)); @@ -10380,7 +10440,9 @@ function getIOUActionForTransactions(transactionIDList: Array { + const transactionThreadReportID = reportAction?.childReportID ?? generateReportID(); + if (!reportAction?.childReportID) { - return; + transactionsMergeParams.transactionThreadReportID = transactionThreadReportID; } + IOU.mergeDuplicates(transactionsMergeParams); if (canUseTableReportView) { Navigation.dismissModal(); return; } - if (!reportAction?.childReportID) { - return; - } - Navigation.dismissModalWithReport({reportID: reportAction.childReportID}); + Navigation.dismissModalWithReport({reportID: transactionThreadReportID}); }, [reportAction?.childReportID, transactionsMergeParams, canUseTableReportView]); const resolveDuplicates = useCallback(() => { diff --git a/src/pages/home/ReportScreen.tsx b/src/pages/home/ReportScreen.tsx index ce6b26989a9d..73691c688b15 100644 --- a/src/pages/home/ReportScreen.tsx +++ b/src/pages/home/ReportScreen.tsx @@ -40,7 +40,9 @@ import {getPersonalDetailsForAccountIDs} from '@libs/OptionsListUtils'; import {getDisplayNameOrDefault} from '@libs/PersonalDetailsUtils'; import { getCombinedReportActions, + getIOUActionForReportID, getOneTransactionThreadReportID, + getReportAction, isCreatedAction, isDeletedParentAction, isMoneyRequestAction, @@ -48,11 +50,15 @@ import { shouldReportActionBeVisible, } from '@libs/ReportActionsUtils'; import { + buildTransactionThread, canEditReportAction, canUserPerformWriteAction, findLastAccessedReport, + generateReportID, getParticipantsAccountIDsForDisplay, getReportOfflinePendingActionAndErrors, + getReportOrDraftReport, + getReportTransactions, isChatThread, isConciergeChatReport, isGroupChat, @@ -298,6 +304,7 @@ 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)); @@ -447,17 +454,30 @@ function ReportScreen({route, navigation}: ReportScreenProps) { return; } - const moneyRequestReportActionID: string | undefined = route.params?.moneyRequestReportActionID; - const transactionID: string | undefined = route.params?.transactionID; + const {moneyRequestReportActionID, transactionID, iouReportID} = route.params; // 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) { - openReport(reportIDFromRoute, '', [currentUserEmail], undefined, moneyRequestReportActionID, false, [], undefined, undefined, transactionID); + 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); 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?.moneyRequestReportActionID, route.params?.transactionID, reportIDFromRoute, reportActionIDFromRoute, currentUserEmail]); + }, [reportMetadata.isOptimisticReport, route.params, reportIDFromRoute, reportActionIDFromRoute, currentUserEmail, report, reportID, transactionThreadReport, transactionThreadReportID]); useEffect(() => { if (!reportID || !isFocused) { @@ -523,7 +543,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]); + }, [route, isLinkedMessagePageReady, reportActionIDFromRoute, transactionThreadReportID]); const prevReportActions = usePrevious(reportActions); useEffect(() => { diff --git a/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx b/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx index a614867109e0..bc97ea5b6428 100755 --- a/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx +++ b/src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx @@ -20,12 +20,13 @@ import useResponsiveLayout from '@hooks/useResponsiveLayout'; import useRestoreInputFocus from '@hooks/useRestoreInputFocus'; import useStyleUtils from '@hooks/useStyleUtils'; import {getExpensifyCardFromReportAction} from '@libs/CardMessageUtils'; -import {getLinkedTransactionID, getOneTransactionThreadReportID, getReportAction} from '@libs/ReportActionsUtils'; +import {getLinkedTransactionID, getOneTransactionThreadReportID, getOriginalMessage, getReportActions, isMoneyRequestAction} from '@libs/ReportActionsUtils'; import { chatIncludesChronosWithID, getSourceIDFromReportAction, isArchivedNonExpenseReport, isArchivedNonExpenseReportWithID, + isIOUReport, isInvoiceReport as ReportUtilsIsInvoiceReport, isMoneyRequest as ReportUtilsIsMoneyRequest, isMoneyRequestReport as ReportUtilsIsMoneyRequestReport, @@ -163,7 +164,7 @@ function BaseReportActionContextMenu({ const [download] = useOnyx(`${ONYXKEYS.COLLECTION.DOWNLOAD}${sourceID}`, {canBeMissing: true}); const [childReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${reportAction?.childReportID}`, {canBeMissing: true}); - const parentReportAction = getReportAction(childReport?.parentReportID, childReport?.parentReportActionID); + const childReportActions = childReport ? getReportActions(childReport) : undefined; const {reportActions: paginatedReportActions} = usePaginatedReportActions(childReport?.reportID); const transactionThreadReportID = useMemo( @@ -178,24 +179,29 @@ function BaseReportActionContextMenu({ const requestParentReportAction = useMemo(() => { if (isMoneyRequestReport || isInvoiceReport) { - if (!paginatedReportActions || !transactionThreadReport?.parentReportActionID) { + if (!paginatedReportActions) { return undefined; } - return paginatedReportActions.find((action) => action.reportActionID === transactionThreadReport.parentReportActionID); + 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 parentReportAction; - }, [parentReportAction, isMoneyRequestReport, isInvoiceReport, paginatedReportActions, transactionThreadReport?.parentReportActionID]); + return reportAction; + }, [childReportActions, transactionThreadReportID, reportAction, isMoneyRequestReport, isInvoiceReport, paginatedReportActions, transactionThreadReport?.parentReportActionID]); - const moneyRequestAction = transactionThreadReportID ? requestParentReportAction : parentReportAction; + const moneyRequestAction = transactionThreadReportID ? requestParentReportAction : reportAction; 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}`, {canBeMissing: true}); + const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${childReport?.parentReportID ?? (isMoneyRequestAction(reportAction) && getOriginalMessage(reportAction)?.IOUReportID)}`, { + canBeMissing: true, + }); const isMoneyRequest = useMemo(() => ReportUtilsIsMoneyRequest(childReport), [childReport]); const isTrackExpenseReport = ReportUtilsIsTrackExpenseReport(childReport); const isSingleTransactionView = isMoneyRequest || isTrackExpenseReport; - const isMoneyRequestOrReport = isMoneyRequestReport || isSingleTransactionView; + const isMoneyRequestOrReport = isMoneyRequestReport || isSingleTransactionView || isIOUReport(parentReport); const areHoldRequirementsMet = !isInvoiceReport && diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 762ac3979b84..786c189fb486 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -620,8 +620,6 @@ describe('actions/IOU', () => { let createdAction: OnyxEntry; let iouAction: OnyxEntry>; let transactionID: string | undefined; - let transactionThread: OnyxEntry; - let transactionThreadCreatedAction: OnyxEntry; mockFetch?.pause?.(); requestMoney({ report: {reportID: ''}, @@ -649,16 +647,14 @@ describe('actions/IOU', () => { callback: (allReports) => { Onyx.disconnect(connection); - // A chat report, a transaction thread, and an iou report should be created + // A chat report 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(2); + expect(Object.keys(chatReports).length).toBe(1); 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}, @@ -721,29 +717,6 @@ 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) => { @@ -1206,8 +1179,6 @@ describe('actions/IOU', () => { let createdAction: OnyxEntry; let iouAction: OnyxEntry>; let transactionID: string | undefined; - let transactionThreadReport: OnyxEntry; - let transactionThreadAction: OnyxEntry; mockFetch?.pause?.(); requestMoney({ report: {reportID: ''}, @@ -1236,14 +1207,13 @@ describe('actions/IOU', () => { callback: (allReports) => { Onyx.disconnect(connection); - // A chat report, transaction thread and an iou report should be created + // A chat report 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(2); + expect(Object.values(chatReports).length).toBe(1); 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; @@ -1356,24 +1326,6 @@ 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) => { @@ -1440,22 +1392,6 @@ 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( () => @@ -1479,9 +1415,6 @@ describe('actions/IOU', () => { if (chatReportID) { deleteReport(chatReportID); } - if (transactionThreadReport?.reportID) { - deleteReport(transactionThreadReport?.reportID); - } resolve(); }), )