Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 37 additions & 14 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {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';
Expand Down Expand Up @@ -264,8 +264,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) &&
Expand All @@ -275,17 +275,25 @@ 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,
),
);
});
const firstParticipant = transaction?.participants?.at(0);

// Check if user manually selected a recipient different from default workspace
const {hasManualSelection} = hasManuallySelectedParticipant(firstParticipant, activePolicyExpenseChat?.reportID);

if (hasManualSelection) {
const targetReportID = transaction?.reportID;
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));
});
} 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));
});
}
} else {
Navigation.setNavigationActionToMicrotaskQueue(() => {
navigateToParticipantPage(iouType, transactionID, reportID);
Expand Down Expand Up @@ -376,10 +384,25 @@ function IOURequestStepAmount({

IOURequestStepAmount.displayName = 'IOURequestStepAmount';

/**
* Determines if user has manually selected a participant different from the default workspace
*/
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);
// eslint-disable-next-line rulesdir/no-negated-variables
const IOURequestStepAmountWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepAmountWithWritableReportOrNotFound);

export default IOURequestStepAmountWithFullTransactionOrNotFound;
export {hasManuallySelectedParticipant};
98 changes: 98 additions & 0 deletions tests/unit/IOURequestStepAmountTest.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
});