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
33 changes: 8 additions & 25 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {useFocusEffect} from '@react-navigation/native';
import reportsSelector from '@selectors/Attributes';
import React, {useCallback, useEffect, useMemo, useRef} from 'react';
import type {OnyxEntry} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import withCurrentUserPersonalDetails from '@components/withCurrentUserPersonalDetails';
import type {WithCurrentUserPersonalDetailsProps} from '@components/withCurrentUserPersonalDetails';
Expand Down Expand Up @@ -44,7 +43,6 @@ import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {SelectedTabRequest} from '@src/types/onyx';
import type {PaymentMethodType} from '@src/types/onyx/OriginalMessage';
import type Policy from '@src/types/onyx/Policy';
import type Transaction from '@src/types/onyx/Transaction';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import StepScreenWrapper from './StepScreenWrapper';
Expand All @@ -66,26 +64,6 @@ type IOURequestStepAmountProps = WithCurrentUserPersonalDetailsProps &
shouldKeepUserInput?: boolean;
};

function shouldAutoNavigateToDefaultWorkspace(
iouType: ValueOf<typeof CONST.IOU.TYPE>,
defaultExpensePolicy: Policy | null | undefined,
personalPolicy: OnyxEntry<Pick<Policy, 'autoReporting'>>,
): boolean {
if (!defaultExpensePolicy?.id) {
return false;
}

const hasAutoReporting = !!(defaultExpensePolicy?.autoReporting && personalPolicy?.autoReporting);

return (
iouType === CONST.IOU.TYPE.CREATE &&
isPaidGroupPolicy(defaultExpensePolicy) &&
defaultExpensePolicy?.isPolicyExpenseChatEnabled &&
hasAutoReporting &&
!shouldRestrictUserBillableActions(defaultExpensePolicy.id)
);
}

function IOURequestStepAmount({
report,
route: {
Expand Down Expand Up @@ -288,8 +266,13 @@ function IOURequestStepAmount({

// Starting from global + menu means no participant context exists yet,
// so we need to handle participant selection based on available workspace settings
if (shouldAutoNavigateToDefaultWorkspace(iouType, defaultExpensePolicy, personalPolicy)) {
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserPersonalDetails.accountID, defaultExpensePolicy?.id);
if (
iouType === CONST.IOU.TYPE.CREATE &&
isPaidGroupPolicy(defaultExpensePolicy) &&
defaultExpensePolicy?.isPolicyExpenseChatEnabled &&
!shouldRestrictUserBillableActions(defaultExpensePolicy.id)
) {
const activePolicyExpenseChat = getPolicyExpenseChat(currentUserAccountIDParam, defaultExpensePolicy?.id);
const shouldAutoReport = !!defaultExpensePolicy?.autoReporting || !!personalPolicy?.autoReporting;
const transactionReportID = shouldAutoReport ? activePolicyExpenseChat?.reportID : CONST.REPORT.UNREPORTED_REPORT_ID;
const isReturningFromConfirmationPage = !!transaction?.participants?.length;
Expand Down Expand Up @@ -428,4 +411,4 @@ const IOURequestStepAmountWithWritableReportOrNotFound = withWritableReportOrNot
const IOURequestStepAmountWithFullTransactionOrNotFound = withFullTransactionOrNotFound(IOURequestStepAmountWithWritableReportOrNotFound);

export default IOURequestStepAmountWithFullTransactionOrNotFound;
export {isParticipantP2P, shouldAutoNavigateToDefaultWorkspace};
export {isParticipantP2P};
62 changes: 1 addition & 61 deletions tests/unit/IOURequestStepAmountTest.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
import {shouldRestrictUserBillableActions} from '@libs/SubscriptionUtils';
import {isParticipantP2P, shouldAutoNavigateToDefaultWorkspace} from '@pages/iou/request/step/IOURequestStepAmount';
import CONST from '@src/CONST';
import type Policy from '@src/types/onyx/Policy';
import {isParticipantP2P} 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('@libs/SubscriptionUtils', () => {
const actual: {
shouldRestrictUserBillableActions: typeof shouldRestrictUserBillableActions;
[key: string]: unknown;
} = jest.requireActual('@libs/SubscriptionUtils');

return {
...actual,
shouldRestrictUserBillableActions: jest.fn(),
};
});
jest.mock('@react-navigation/native', () => ({
useFocusEffect: jest.fn(),
createNavigationContainerRef: jest.fn(() => ({
Expand All @@ -28,52 +14,6 @@ jest.mock('@react-navigation/native', () => ({
}));

describe('IOURequestStepAmount', () => {
const mockShouldRestrictUserBillableActions = shouldRestrictUserBillableActions as jest.MockedFunction<typeof shouldRestrictUserBillableActions>;

const defaultExpensePolicy: Policy = {
id: '123',
name: 'Test Workspace',
role: CONST.POLICY.ROLE.ADMIN,
type: CONST.POLICY.TYPE.TEAM,
owner: 'owner@test.com',
outputCurrency: CONST.CURRENCY.USD,
isPolicyExpenseChatEnabled: true,
autoReporting: true,
};

const personalPolicy: Policy = {
id: '456',
name: 'Personal Policy',
role: CONST.POLICY.ROLE.ADMIN,
type: CONST.POLICY.TYPE.PERSONAL,
owner: 'owner@test.com',
outputCurrency: CONST.CURRENCY.USD,
isPolicyExpenseChatEnabled: true,
autoReporting: true,
};

beforeEach(() => {
mockShouldRestrictUserBillableActions.mockReset();
mockShouldRestrictUserBillableActions.mockReturnValue(false);
});

describe('shouldAutoNavigateToDefaultWorkspace', () => {
it('returns true when both workspace and personal policy have auto reporting enabled', () => {
const shouldNavigate = shouldAutoNavigateToDefaultWorkspace(CONST.IOU.TYPE.CREATE, defaultExpensePolicy, personalPolicy);

expect(shouldNavigate).toBe(true);
});

it('returns false when personal policy auto reporting is disabled', () => {
const shouldNavigate = shouldAutoNavigateToDefaultWorkspace(CONST.IOU.TYPE.CREATE, defaultExpensePolicy, {
...personalPolicy,
autoReporting: false,
});

expect(shouldNavigate).toBe(false);
});
});

describe('isParticipantP2P', () => {
it('should return true for P2P participant with accountID and isPolicyExpenseChat false', () => {
const participant = {
Expand Down
Loading