Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type {OnyxEntry} from 'react-native-onyx';
import {useCurrencyListActions} from '@hooks/useCurrencyList';
import {isValidPerDiemExpenseAmount} from '@libs/actions/IOU/PerDiem';
import {getIsMissingAttendeesViolation} from '@libs/AttendeeUtils';
import {validateAmount} from '@libs/MoneyRequestUtils';
import {isValidMoneyRequestAmount, validateAmount} from '@libs/MoneyRequestUtils';
import type {getTagLists as getTagListsFn} from '@libs/PolicyUtils';
import {isAttendeeTrackingEnabled} from '@libs/PolicyUtils';
import {hasEnabledTags, hasMatchingTag} from '@libs/TagsOptionsListUtils';
Expand Down Expand Up @@ -157,15 +157,28 @@ function useConfirmationValidation({
}

const firstParticipant = transaction?.participants?.at(0);
const isP2P = !!(firstParticipant?.accountID && !firstParticipant?.isPolicyExpenseChat);
const isP2P = !!(firstParticipant?.accountID && !firstParticipant?.isPolicyExpenseChat && !firstParticipant?.isSelfDM);

// P2P manual submit: $0 is invalid unless scan/time/distance (same guard as legacy inline confirm).
if (!isScanRequestUtil(transaction) && !isTimeRequest && !isDistanceRequest && iouAmount === 0 && isP2P) {
return {errorKey: 'common.error.invalidAmount'};
}
if (isNewManualExpenseFlowEnabled && !transaction?.isAmountSet) {
// isAmountSet only applies to manual expenses — scan, per diem, distance, and time set amount programmatically.
if (isNewManualExpenseFlowEnabled && transaction?.iouRequestType === CONST.IOU.REQUEST_TYPE.MANUAL && !transaction?.isAmountSet) {
return {errorKey: 'common.error.fieldRequired'};
}
if (
isNewManualExpenseFlowEnabled &&
transaction?.iouRequestType === CONST.IOU.REQUEST_TYPE.MANUAL &&
transaction?.isAmountSet &&
!isScanRequestUtil(transaction) &&
!isTimeRequest &&
!isDistanceRequest &&
!isEditingSplitBill &&
!isValidMoneyRequestAmount(iouAmount, iouType, true, isP2P)
) {
return {errorKey: 'common.error.invalidAmount'};
}
const merchantValue = iouMerchant ?? '';
const {isValid: isMerchantLengthValid} = isValidInputLength(merchantValue, CONST.MERCHANT_NAME_MAX_BYTES);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,10 @@ function AmountField({
const decimals = getCurrencyDecimals(effectiveCurrency);
// In the new manual expense flow the amount field starts empty (transaction.amount defaults to 0 before the user
// touches it). Once the user explicitly sets an amount – including 0 – isAmountSet becomes true and we show the
// real value. This avoids showing "$0.00" as a pre-filled default.
const transactionAmount = isNewManualExpenseFlowEnabled && !transactionSlice?.isAmountSet ? '' : convertToFrontendAmountAsString(amount, decimals);
// real value. This avoids showing "$0.00" as a pre-filled default. Scan and other non-manual flows populate
// amount programmatically and never set isAmountSet.
const shouldShowEmptyAmount = isNewManualExpenseFlowEnabled && !transactionSlice?.isAmountSet && transactionSlice?.iouRequestType === CONST.IOU.REQUEST_TYPE.MANUAL;
const transactionAmount = shouldShowEmptyAmount ? '' : convertToFrontendAmountAsString(amount, decimals);
const allowNegative = shouldEnableNegative(report, policy, iouType, transactionSlice?.participants);

// `autoFocus` on our TextInput only runs on mount. Closing and reopening the RHP often keeps the same mounted
Expand Down
4 changes: 2 additions & 2 deletions src/pages/iou/request/step/IOURequestStepAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -570,8 +570,8 @@ function IOURequestStepAmount({
/**
* Check if the participant is a P2P chat
*/
function isParticipantP2P(participant: {accountID?: number; isPolicyExpenseChat?: boolean} | undefined): boolean {
return !!(participant?.accountID && !participant.isPolicyExpenseChat);
function isParticipantP2P(participant: {accountID?: number; isPolicyExpenseChat?: boolean; isSelfDM?: boolean} | undefined): boolean {
return !!(participant?.accountID && !participant.isPolicyExpenseChat && !participant.isSelfDM);
}

const IOURequestStepAmountWithWritableReportOrNotFound = withWritableReportOrNotFound(IOURequestStepAmount, true);
Expand Down
58 changes: 57 additions & 1 deletion tests/actions/IOU/MoneyRequestTest.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import type {MoneyRequestStepScanParticipantsFlowParams} from '@libs/actions/IOU/MoneyRequest';
import {createTransaction, getMoneyRequestParticipantOptions, handleMoneyRequestStepDistanceNavigation, handleMoneyRequestStepScanParticipants} from '@libs/actions/IOU/MoneyRequest';
import {
clearMoneyRequestAmount,
createTransaction,
getMoneyRequestParticipantOptions,
handleMoneyRequestStepDistanceNavigation,
handleMoneyRequestStepScanParticipants,
setMoneyRequestAmount,
} from '@libs/actions/IOU/MoneyRequest';
import getCurrentPosition from '@libs/getCurrentPosition';
import {GeolocationErrorCode} from '@libs/getCurrentPosition/getCurrentPosition.types';
import Navigation from '@libs/Navigation/Navigation';
Expand Down Expand Up @@ -1753,4 +1760,53 @@ describe('MoneyRequest', () => {
await Onyx.clear();
});
});

describe('setMoneyRequestAmount and clearMoneyRequestAmount', () => {
const transactionID = 'amount-test-txn';

beforeEach(async () => {
await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`, {
transactionID,
amount: 0,
currency: 'USD',
comment: {},
iouRequestType: CONST.IOU.REQUEST_TYPE.MANUAL,
});
});

afterEach(async () => {
await Onyx.clear();
});

it('sets isAmountSet to true when the user enters an amount', async () => {
setMoneyRequestAmount(transactionID, 1500, 'USD');
await waitForBatchedUpdates();

const transaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`);
expect(transaction?.amount).toBe(1500);
expect(transaction?.currency).toBe('USD');
expect(transaction?.isAmountSet).toBe(true);
});

it('allows explicitly setting zero as a valid amount via isAmountSet', async () => {
setMoneyRequestAmount(transactionID, 0, 'USD');
await waitForBatchedUpdates();

const transaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`);
expect(transaction?.amount).toBe(0);
expect(transaction?.isAmountSet).toBe(true);
});

it('clears isAmountSet when the user deletes the amount input', async () => {
setMoneyRequestAmount(transactionID, 2500, 'USD');
await waitForBatchedUpdates();

clearMoneyRequestAmount(transactionID);
await waitForBatchedUpdates();

const transaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${transactionID}`);
expect(transaction?.amount).toBe(0);
expect(transaction?.isAmountSet).toBe(false);
});
});
});
10 changes: 10 additions & 0 deletions tests/unit/IOURequestStepAmountTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,15 @@ describe('IOURequestStepAmount', () => {

expect(isParticipantP2P(participant)).toBe(true);
});

it('should return false for self-DM participant', () => {
const participant = {
accountID: 123,
isPolicyExpenseChat: false,
isSelfDM: true,
};

expect(isParticipantP2P(participant)).toBe(false);
});
});
});
Loading
Loading