From ccfbff11e0e8f6efb8536654f8710788b8f7e625 Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Thu, 23 Oct 2025 22:43:25 +0300 Subject: [PATCH 01/11] fix: Revisiting create expense page from confirm details page changes the selected user #72593 --- .../iou/request/step/IOURequestStepAmount.tsx | 43 ++++++++++++++----- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 59ca04cf8cee..51d10ff382a8 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -269,17 +269,38 @@ function IOURequestStepAmount({ const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, defaultExpensePolicy?.id); const shouldAutoReport = !!defaultExpensePolicy?.autoReporting || !!personalPolicy?.autoReporting; const transactionReportID = shouldAutoReport ? activePolicyExpenseChat?.reportID : CONST.REPORT.UNREPORTED_REPORT_ID; - setTransactionReport(transactionID, {reportID: transactionReportID}, true); - setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - activePolicyExpenseChat?.reportID, - ), - ); - }); + + // Check if manually selected participants differ from default workspace + const hasExistingParticipants = transaction?.participants && transaction.participants.length > 0; + const existingParticipantReportID = transaction?.participants?.at(0)?.reportID; + const shouldKeepExistingParticipants = hasExistingParticipants && existingParticipantReportID !== activePolicyExpenseChat?.reportID; + + if (shouldKeepExistingParticipants) { + // Navigate with manually selected participants + Navigation.setNavigationActionToMicrotaskQueue(() => { + Navigation.navigate( + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + existingParticipantReportID, + ), + ); + }); + } else { + // Use default workspace participants + setTransactionReport(transactionID, {reportID: transactionReportID}, true); + setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { + Navigation.navigate( + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( + CONST.IOU.ACTION.CREATE, + iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, + transactionID, + activePolicyExpenseChat?.reportID, + ), + ); + }); + } } else { Navigation.setNavigationActionToMicrotaskQueue(() => { navigateToParticipantPage(iouType, transactionID, reportID); From 5a23bb469c6c7c21918205d06152a387066cdb6c Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Fri, 24 Oct 2025 20:09:56 +0300 Subject: [PATCH 02/11] fix: Submitting an expense to self DM fails and other related issues --- .../iou/request/step/IOURequestStepAmount.tsx | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 51d10ff382a8..dba7372ee711 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -20,7 +20,7 @@ import {navigateToParticipantPage} from '@libs/IOUUtils'; import Navigation from '@libs/Navigation/Navigation'; import {getParticipantsOption, getReportOption} from '@libs/OptionsListUtils'; import {isPaidGroupPolicy} from '@libs/PolicyUtils'; -import {getPolicyExpenseChat, getTransactionDetails, isPolicyExpenseChat, shouldEnableNegative} from '@libs/ReportUtils'; +import {generateReportID, getPolicyExpenseChat, getReportOrDraftReport, getTransactionDetails, isPolicyExpenseChat, isSelfDM, shouldEnableNegative} from '@libs/ReportUtils'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import {calculateTaxAmount, getAmount, getCurrency, getDefaultTaxCode, getRequestType, getTaxValue} from '@libs/TransactionUtils'; import MoneyRequestAmountForm from '@pages/iou/MoneyRequestAmountForm'; @@ -258,8 +258,8 @@ function IOURequestStepAmount({ return; } - // If there was no reportID, then that means the user started this flow from the global + menu - // and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense. + // User started from global + menu with CREATE flow + // If they manually selected a recipient, keep it; otherwise auto-assign default workspace if ( iouType === CONST.IOU.TYPE.CREATE && isPaidGroupPolicy(defaultExpensePolicy) && @@ -270,34 +270,36 @@ function IOURequestStepAmount({ const shouldAutoReport = !!defaultExpensePolicy?.autoReporting || !!personalPolicy?.autoReporting; const transactionReportID = shouldAutoReport ? activePolicyExpenseChat?.reportID : CONST.REPORT.UNREPORTED_REPORT_ID; - // Check if manually selected participants differ from default workspace - const hasExistingParticipants = transaction?.participants && transaction.participants.length > 0; - const existingParticipantReportID = transaction?.participants?.at(0)?.reportID; - const shouldKeepExistingParticipants = hasExistingParticipants && existingParticipantReportID !== activePolicyExpenseChat?.reportID; - - if (shouldKeepExistingParticipants) { - // Navigate with manually selected participants + const firstParticipant = transaction?.participants?.at(0); + + // Check if user manually selected a recipient different from default workspace + const hasManuallySelectedParticipant = transaction?.participantsAutoAssigned === false || + (firstParticipant && ( + (firstParticipant.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID) || + (firstParticipant.accountID && !firstParticipant.isPolicyExpenseChat) + )); + + if (hasManuallySelectedParticipant) { + const targetReportID = firstParticipant?.reportID ?? transaction?.reportID ?? generateReportID(); + + if (!firstParticipant?.reportID && targetReportID) { + setTransactionReport(transactionID, {reportID: targetReportID}, true); + } + + // Self DM uses TRACK, all others use SUBMIT + const selectedReport = targetReportID ? getReportOrDraftReport(targetReportID) : null; + const navigationIOUType = isSelfDM(selectedReport) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; + Navigation.setNavigationActionToMicrotaskQueue(() => { Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - existingParticipantReportID, - ), + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, navigationIOUType, transactionID, targetReportID), ); }); } else { - // Use default workspace participants setTransactionReport(transactionID, {reportID: transactionReportID}, true); setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute( - CONST.IOU.ACTION.CREATE, - iouType === CONST.IOU.TYPE.CREATE ? CONST.IOU.TYPE.SUBMIT : iouType, - transactionID, - activePolicyExpenseChat?.reportID, - ), + ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, activePolicyExpenseChat?.reportID), ); }); } From 363cf00ebf4a6286bca8736cd45822459d3333f4 Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Fri, 24 Oct 2025 20:15:37 +0300 Subject: [PATCH 03/11] chore: run Prettier to fix formatting --- .../iou/request/step/IOURequestStepAmount.tsx | 26 ++++++++----------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index dba7372ee711..71081f827679 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -271,36 +271,32 @@ function IOURequestStepAmount({ const transactionReportID = shouldAutoReport ? activePolicyExpenseChat?.reportID : CONST.REPORT.UNREPORTED_REPORT_ID; const firstParticipant = transaction?.participants?.at(0); - + // Check if user manually selected a recipient different from default workspace - const hasManuallySelectedParticipant = transaction?.participantsAutoAssigned === false || - (firstParticipant && ( - (firstParticipant.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID) || - (firstParticipant.accountID && !firstParticipant.isPolicyExpenseChat) - )); + const hasManuallySelectedParticipant = + transaction?.participantsAutoAssigned === false || + (firstParticipant && + ((firstParticipant.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID) || + (firstParticipant.accountID && !firstParticipant.isPolicyExpenseChat))); if (hasManuallySelectedParticipant) { const targetReportID = firstParticipant?.reportID ?? transaction?.reportID ?? generateReportID(); - + if (!firstParticipant?.reportID && targetReportID) { setTransactionReport(transactionID, {reportID: targetReportID}, true); } - + // Self DM uses TRACK, all others use SUBMIT const selectedReport = targetReportID ? getReportOrDraftReport(targetReportID) : null; const navigationIOUType = isSelfDM(selectedReport) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; - + Navigation.setNavigationActionToMicrotaskQueue(() => { - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, navigationIOUType, transactionID, targetReportID), - ); + Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, navigationIOUType, transactionID, targetReportID)); }); } else { setTransactionReport(transactionID, {reportID: transactionReportID}, true); setMoneyRequestParticipantsFromReport(transactionID, activePolicyExpenseChat).then(() => { - Navigation.navigate( - ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, activePolicyExpenseChat?.reportID), - ); + Navigation.navigate(ROUTES.MONEY_REQUEST_STEP_CONFIRMATION.getRoute(CONST.IOU.ACTION.CREATE, CONST.IOU.TYPE.SUBMIT, transactionID, activePolicyExpenseChat?.reportID)); }); } } else { From 35ca9cc1cb4205c25a0ecbf63ee121763716d999 Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Fri, 24 Oct 2025 21:40:54 +0300 Subject: [PATCH 04/11] fix: ESLint errors --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 71081f827679..a3541e3785c3 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -273,11 +273,10 @@ function IOURequestStepAmount({ const firstParticipant = transaction?.participants?.at(0); // Check if user manually selected a recipient different from default workspace - const hasManuallySelectedParticipant = - transaction?.participantsAutoAssigned === false || - (firstParticipant && - ((firstParticipant.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID) || - (firstParticipant.accountID && !firstParticipant.isPolicyExpenseChat))); + const isManuallyAssigned = transaction?.participantsAutoAssigned === false; + const hasDifferentWorkspace = !!(firstParticipant?.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID); + const isP2PChat = !!(firstParticipant?.accountID && !firstParticipant.isPolicyExpenseChat); + const hasManuallySelectedParticipant = isManuallyAssigned || hasDifferentWorkspace || isP2PChat; if (hasManuallySelectedParticipant) { const targetReportID = firstParticipant?.reportID ?? transaction?.reportID ?? generateReportID(); From f553f7fb43c2f0ba88e47f56e819019fe670d46e Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Wed, 29 Oct 2025 10:00:38 +0200 Subject: [PATCH 05/11] fix: Remove unreachable participantsAutoAssigned check --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index a3541e3785c3..8401b5158b9c 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -273,10 +273,9 @@ function IOURequestStepAmount({ const firstParticipant = transaction?.participants?.at(0); // Check if user manually selected a recipient different from default workspace - const isManuallyAssigned = transaction?.participantsAutoAssigned === false; const hasDifferentWorkspace = !!(firstParticipant?.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID); const isP2PChat = !!(firstParticipant?.accountID && !firstParticipant.isPolicyExpenseChat); - const hasManuallySelectedParticipant = isManuallyAssigned || hasDifferentWorkspace || isP2PChat; + const hasManuallySelectedParticipant = hasDifferentWorkspace || isP2PChat; if (hasManuallySelectedParticipant) { const targetReportID = firstParticipant?.reportID ?? transaction?.reportID ?? generateReportID(); From 0ce9ec01636c563f852cba3a0860410e3e6ee6aa Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Wed, 29 Oct 2025 21:10:03 +0200 Subject: [PATCH 06/11] fix: Prevent navigation to not found page when selecting P2P participant without existing chat --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 8401b5158b9c..a0b628fac0f9 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -20,7 +20,7 @@ import {navigateToParticipantPage} from '@libs/IOUUtils'; import Navigation from '@libs/Navigation/Navigation'; import {getParticipantsOption, getReportOption} from '@libs/OptionsListUtils'; import {isPaidGroupPolicy} from '@libs/PolicyUtils'; -import {generateReportID, getPolicyExpenseChat, getReportOrDraftReport, getTransactionDetails, isPolicyExpenseChat, isSelfDM, shouldEnableNegative} from '@libs/ReportUtils'; +import {getPolicyExpenseChat, getReportOrDraftReport, getTransactionDetails, isPolicyExpenseChat, isSelfDM, shouldEnableNegative} from '@libs/ReportUtils'; import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils'; import {calculateTaxAmount, getAmount, getCurrency, getDefaultTaxCode, getRequestType, getTaxValue} from '@libs/TransactionUtils'; import MoneyRequestAmountForm from '@pages/iou/MoneyRequestAmountForm'; @@ -278,9 +278,10 @@ function IOURequestStepAmount({ const hasManuallySelectedParticipant = hasDifferentWorkspace || isP2PChat; if (hasManuallySelectedParticipant) { - const targetReportID = firstParticipant?.reportID ?? transaction?.reportID ?? generateReportID(); + const participantReportID = firstParticipant?.reportID || undefined; + const targetReportID = participantReportID ?? transaction?.reportID ?? reportID; - if (!firstParticipant?.reportID && targetReportID) { + if (targetReportID && targetReportID !== transaction?.reportID) { setTransactionReport(transactionID, {reportID: targetReportID}, true); } From 3210d25f260fc4cb40955132e497dd4899be4c7b Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Wed, 29 Oct 2025 21:18:26 +0200 Subject: [PATCH 07/11] fix: ESLint errors --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index a0b628fac0f9..37804e3e1485 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -278,14 +278,13 @@ function IOURequestStepAmount({ const hasManuallySelectedParticipant = hasDifferentWorkspace || isP2PChat; if (hasManuallySelectedParticipant) { - const participantReportID = firstParticipant?.reportID || undefined; + const participantReportID = firstParticipant?.reportID ? firstParticipant.reportID : undefined; const targetReportID = participantReportID ?? transaction?.reportID ?? reportID; if (targetReportID && targetReportID !== transaction?.reportID) { setTransactionReport(transactionID, {reportID: targetReportID}, true); } - // Self DM uses TRACK, all others use SUBMIT const selectedReport = targetReportID ? getReportOrDraftReport(targetReportID) : null; const navigationIOUType = isSelfDM(selectedReport) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; From 460df05c4d16d38a171bcda7c0ef99d5eb0145cd Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Thu, 30 Oct 2025 19:49:29 +0200 Subject: [PATCH 08/11] fix: Remove redundant if-block --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 37804e3e1485..16b11c8bd35a 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -280,11 +280,6 @@ function IOURequestStepAmount({ if (hasManuallySelectedParticipant) { const participantReportID = firstParticipant?.reportID ? firstParticipant.reportID : undefined; const targetReportID = participantReportID ?? transaction?.reportID ?? reportID; - - if (targetReportID && targetReportID !== transaction?.reportID) { - setTransactionReport(transactionID, {reportID: targetReportID}, true); - } - const selectedReport = targetReportID ? getReportOrDraftReport(targetReportID) : null; const navigationIOUType = isSelfDM(selectedReport) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; From eec84e774ed4a0d35c908dbcf34c987e0b41ae77 Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Thu, 30 Oct 2025 20:58:48 +0200 Subject: [PATCH 09/11] refactor: Simplify targetReportID logic --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 16b11c8bd35a..1ede9f8fd200 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -278,8 +278,7 @@ function IOURequestStepAmount({ const hasManuallySelectedParticipant = hasDifferentWorkspace || isP2PChat; if (hasManuallySelectedParticipant) { - const participantReportID = firstParticipant?.reportID ? firstParticipant.reportID : undefined; - const targetReportID = participantReportID ?? transaction?.reportID ?? reportID; + const targetReportID = transaction?.reportID; const selectedReport = targetReportID ? getReportOrDraftReport(targetReportID) : null; const navigationIOUType = isSelfDM(selectedReport) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; From 31fbc4a54cc69590752b4bee79c0f1201f4ff59b Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Thu, 6 Nov 2025 19:46:23 +0200 Subject: [PATCH 10/11] refactor: Extract participant selection logic into separate function for testability and add unit tests --- .../iou/request/step/IOURequestStepAmount.tsx | 21 +++- tests/unit/IOURequestStepAmountTest.ts | 98 +++++++++++++++++++ 2 files changed, 114 insertions(+), 5 deletions(-) create mode 100644 tests/unit/IOURequestStepAmountTest.ts diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 1ede9f8fd200..85e16fdd914f 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -269,15 +269,12 @@ function IOURequestStepAmount({ const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, defaultExpensePolicy?.id); const shouldAutoReport = !!defaultExpensePolicy?.autoReporting || !!personalPolicy?.autoReporting; const transactionReportID = shouldAutoReport ? activePolicyExpenseChat?.reportID : CONST.REPORT.UNREPORTED_REPORT_ID; - const firstParticipant = transaction?.participants?.at(0); // Check if user manually selected a recipient different from default workspace - const hasDifferentWorkspace = !!(firstParticipant?.reportID && firstParticipant.reportID !== activePolicyExpenseChat?.reportID); - const isP2PChat = !!(firstParticipant?.accountID && !firstParticipant.isPolicyExpenseChat); - const hasManuallySelectedParticipant = hasDifferentWorkspace || isP2PChat; + const {hasManualSelection} = hasManuallySelectedParticipant(firstParticipant, activePolicyExpenseChat?.reportID); - if (hasManuallySelectedParticipant) { + if (hasManualSelection) { const targetReportID = transaction?.reportID; const selectedReport = targetReportID ? getReportOrDraftReport(targetReportID) : null; const navigationIOUType = isSelfDM(selectedReport) ? CONST.IOU.TYPE.TRACK : CONST.IOU.TYPE.SUBMIT; @@ -378,6 +375,20 @@ function IOURequestStepAmount({ IOURequestStepAmount.displayName = 'IOURequestStepAmount'; +/** + * Determines if user has manually selected a participant different from the default workspace + */ +export function hasManuallySelectedParticipant( + firstParticipant: {reportID?: string; accountID?: number; isPolicyExpenseChat?: boolean} | undefined, + activePolicyExpenseChatReportID: string | undefined, +): {hasDifferentWorkspace: boolean; isP2PChat: boolean; hasManualSelection: boolean} { + const hasDifferentWorkspace = !!(firstParticipant?.reportID && firstParticipant.reportID !== activePolicyExpenseChatReportID); + const isP2PChat = !!(firstParticipant?.accountID && !firstParticipant.isPolicyExpenseChat); + const hasManualSelection = hasDifferentWorkspace || isP2PChat; + + return {hasDifferentWorkspace, isP2PChat, hasManualSelection}; +} + const IOURequestStepAmountWithCurrentUserPersonalDetails = withCurrentUserPersonalDetails(IOURequestStepAmount); // eslint-disable-next-line rulesdir/no-negated-variables const IOURequestStepAmountWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepAmountWithCurrentUserPersonalDetails, true); diff --git a/tests/unit/IOURequestStepAmountTest.ts b/tests/unit/IOURequestStepAmountTest.ts new file mode 100644 index 000000000000..2acb77bfedee --- /dev/null +++ b/tests/unit/IOURequestStepAmountTest.ts @@ -0,0 +1,98 @@ +import {hasManuallySelectedParticipant} from '@pages/iou/request/step/IOURequestStepAmount'; + +jest.mock('@components/withCurrentUserPersonalDetails', () => (Component: unknown) => Component); +jest.mock('@pages/iou/request/step/withWritableReportOrNotFound', () => () => (Component: unknown) => Component); +jest.mock('@pages/iou/request/step/withFullTransactionOrNotFound', () => (Component: unknown) => Component); +jest.mock('@src/hooks/useResponsiveLayout'); +jest.mock('@react-navigation/native', () => ({ + useFocusEffect: jest.fn(), + createNavigationContainerRef: jest.fn(() => ({ + current: null, + isReady: jest.fn(() => true), + navigate: jest.fn(), + })), +})); + +describe('IOURequestStepAmount - Participant Selection Logic', () => { + describe('hasManuallySelectedParticipant', () => { + const DEFAULT_WORKSPACE_REPORT_ID = '100'; + const DIFFERENT_WORKSPACE_REPORT_ID = '200'; + const P2P_ACCOUNT_ID = 5; + + it('should detect manually selected different workspace', () => { + const firstParticipant = { + reportID: DIFFERENT_WORKSPACE_REPORT_ID, + isPolicyExpenseChat: true, + }; + + const result = hasManuallySelectedParticipant(firstParticipant, DEFAULT_WORKSPACE_REPORT_ID); + + expect(result.hasDifferentWorkspace).toBe(true); + expect(result.isP2PChat).toBe(false); + expect(result.hasManualSelection).toBe(true); + }); + + it('should detect manually selected P2P recipient', () => { + const firstParticipant = { + accountID: P2P_ACCOUNT_ID, + isPolicyExpenseChat: false, + }; + + const result = hasManuallySelectedParticipant(firstParticipant, DEFAULT_WORKSPACE_REPORT_ID); + + expect(result.hasDifferentWorkspace).toBe(false); + expect(result.isP2PChat).toBe(true); + expect(result.hasManualSelection).toBe(true); + }); + + it('should NOT detect manual selection when no participant is selected', () => { + const firstParticipant = undefined; + + const result = hasManuallySelectedParticipant(firstParticipant, DEFAULT_WORKSPACE_REPORT_ID); + + expect(result.hasDifferentWorkspace).toBe(false); + expect(result.isP2PChat).toBe(false); + expect(result.hasManualSelection).toBe(false); + }); + + it('should NOT detect manual selection when default workspace is selected', () => { + const firstParticipant = { + reportID: DEFAULT_WORKSPACE_REPORT_ID, + isPolicyExpenseChat: true, + }; + + const result = hasManuallySelectedParticipant(firstParticipant, DEFAULT_WORKSPACE_REPORT_ID); + + expect(result.hasDifferentWorkspace).toBe(false); + expect(result.isP2PChat).toBe(false); + expect(result.hasManualSelection).toBe(false); + }); + + it('should handle participant with both accountID and reportID (workspace chat)', () => { + const firstParticipant = { + accountID: P2P_ACCOUNT_ID, + reportID: DIFFERENT_WORKSPACE_REPORT_ID, + isPolicyExpenseChat: true, + }; + + const result = hasManuallySelectedParticipant(firstParticipant, DEFAULT_WORKSPACE_REPORT_ID); + + expect(result.hasDifferentWorkspace).toBe(true); + expect(result.isP2PChat).toBe(false); + expect(result.hasManualSelection).toBe(true); + }); + + it('should handle participant with accountID but isPolicyExpenseChat is true', () => { + const firstParticipant = { + accountID: P2P_ACCOUNT_ID, + isPolicyExpenseChat: true, + }; + + const result = hasManuallySelectedParticipant(firstParticipant, DEFAULT_WORKSPACE_REPORT_ID); + + expect(result.hasDifferentWorkspace).toBe(false); + expect(result.isP2PChat).toBe(false); + expect(result.hasManualSelection).toBe(false); + }); + }); +}); From fc2e3f7cfbddd2f1dc8b3b2ff247d6cb332a09c6 Mon Sep 17 00:00:00 2001 From: ganzz4 Date: Thu, 6 Nov 2025 19:55:24 +0200 Subject: [PATCH 11/11] fix: Move named export to end of file per lint rules --- src/pages/iou/request/step/IOURequestStepAmount.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepAmount.tsx b/src/pages/iou/request/step/IOURequestStepAmount.tsx index 85e16fdd914f..f55a5ce62bf5 100644 --- a/src/pages/iou/request/step/IOURequestStepAmount.tsx +++ b/src/pages/iou/request/step/IOURequestStepAmount.tsx @@ -378,7 +378,7 @@ IOURequestStepAmount.displayName = 'IOURequestStepAmount'; /** * Determines if user has manually selected a participant different from the default workspace */ -export function hasManuallySelectedParticipant( +function hasManuallySelectedParticipant( firstParticipant: {reportID?: string; accountID?: number; isPolicyExpenseChat?: boolean} | undefined, activePolicyExpenseChatReportID: string | undefined, ): {hasDifferentWorkspace: boolean; isP2PChat: boolean; hasManualSelection: boolean} { @@ -396,3 +396,4 @@ const IOURequestStepAmountWithWritableReportOrNotFound = withWritableReportOrNot const IOURequestStepAmountWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepAmountWithWritableReportOrNotFound); export default IOURequestStepAmountWithFullTransactionOrNotFound; +export {hasManuallySelectedParticipant};