From 5f33a42e3d21bd1cbb6d30e88369ef059f03e1ac Mon Sep 17 00:00:00 2001 From: "Nyoman Jyotisa (via MelvinBot)" Date: Mon, 19 Oct 2026 17:08:12 +0000 Subject: [PATCH 1/2] Let users attach and replace receipts on created odometer expenses The receipt permission check only allowed manual distance expenses, so the receipt area on an odometer expense was disabled and a dropped file created a new scan expense. The Replace button was also hidden for every odometer receipt, though the photo-stitched receipt only exists while the expense is a draft. Co-authored-by: Nyoman Jyotisa --- src/libs/ReportUtils.ts | 3 +- .../routes/TransactionReceiptModalContent.tsx | 3 +- .../TransactionReceiptOdometerReplaceTest.tsx | 193 ++++++++++++++++++ tests/unit/canEditFieldOfMoneyRequestTest.ts | 121 +++++++++++ 4 files changed, 318 insertions(+), 2 deletions(-) create mode 100644 tests/ui/TransactionReceiptOdometerReplaceTest.tsx diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index da39566f0418..731da56605d1 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -297,6 +297,7 @@ import { isManagedCardTransaction, isManualDistanceRequest as isManualDistanceRequestTransactionUtils, isMapDistanceRequest, + isOdometerDistanceRequest, isOnHold as isOnHoldTransactionUtils, isPayAtEndExpense, isPending, @@ -6146,7 +6147,7 @@ function canEditFieldOfMoneyRequest({ !isInvoiceReport(moneyRequestReport) && !isReceiptBeingScanned(transaction) && !isPerDiemRequest(transaction) && - (!isDistanceRequest(transaction) || isManualDistanceRequestTransactionUtils(transaction)) && + (!isDistanceRequest(transaction) || isManualDistanceRequestTransactionUtils(transaction) || isOdometerDistanceRequest(transaction)) && canEditExpense && (isDeleteAction ? isRequestor : true) ); diff --git a/src/pages/media/AttachmentModalScreen/routes/TransactionReceiptModalContent.tsx b/src/pages/media/AttachmentModalScreen/routes/TransactionReceiptModalContent.tsx index 7d7bddf4f976..4f28b9153b63 100644 --- a/src/pages/media/AttachmentModalScreen/routes/TransactionReceiptModalContent.tsx +++ b/src/pages/media/AttachmentModalScreen/routes/TransactionReceiptModalContent.tsx @@ -188,7 +188,8 @@ function TransactionReceiptModalContent({navigation, route}: AttachmentModalScre }); const receiptFilename = transaction?.receipt?.filename; - const isStitchedOdometerReceipt = isOdometerDistanceRequest(transaction) && !imageType; + // Odometer photos are stitched into the receipt only while the expense is a draft, so a created expense's receipt can be replaced like any other + const isStitchedOdometerReceipt = isDraftTransaction && isOdometerDistanceRequest(transaction) && !imageType; const canEditReceiptButtons = ((canEditReceipt && !readonly) || isDraftTransaction) && !transaction?.receipt?.isTestDriveReceipt; const shouldShowReplaceReceiptButton = canEditReceiptButtons && !isStitchedOdometerReceipt; diff --git a/tests/ui/TransactionReceiptOdometerReplaceTest.tsx b/tests/ui/TransactionReceiptOdometerReplaceTest.tsx new file mode 100644 index 000000000000..26f633d9cbdc --- /dev/null +++ b/tests/ui/TransactionReceiptOdometerReplaceTest.tsx @@ -0,0 +1,193 @@ +import {act, render, screen} from '@testing-library/react-native'; + +import ComposeProviders from '@components/ComposeProviders'; +import {LocaleContextProvider} from '@components/LocaleContextProvider'; +import OnyxListItemProvider from '@components/OnyxListItemProvider'; +import {PlaybackContextProvider} from '@components/VideoPlayerContexts/PlaybackContext'; + +import {CurrentReportIDContextProvider} from '@hooks/useCurrentReportID'; + +import createPlatformStackNavigator from '@libs/Navigation/PlatformStackNavigation/createPlatformStackNavigator'; +import {waitForIdle} from '@libs/Network/SequentialQueue'; + +import type {AuthScreensParamList} from '@navigation/types'; + +import AttachmentModalScreen from '@pages/media/AttachmentModalScreen'; +import {AttachmentModalContextProvider} from '@pages/media/AttachmentModalScreen/AttachmentModalContext'; + +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import SCREENS from '@src/SCREENS'; +import type {Policy, Report, ReportAction, Transaction} from '@src/types/onyx'; + +import {PortalProvider} from '@gorhom/portal'; +import {NavigationContainer} from '@react-navigation/native'; +import React from 'react'; +import Onyx from 'react-native-onyx'; + +import {getGlobalFetchMock, setupGlobalFetchMock, signInWithTestUser, translateLocal} from '../utils/TestHelper'; +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; +import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; + +const Stack = createPlatformStackNavigator(); + +setupGlobalFetchMock(); + +jest.mock('@rnmapbox/maps', () => { + return { + default: jest.fn(), + MarkerView: jest.fn(), + setAccessToken: jest.fn(), + }; +}); +jest.mock('@src/components/Attachments/AttachmentCarousel/Pager/usePageScrollHandler', () => jest.fn()); +jest.mock('@libs/fetchImage', () => jest.fn((source: string) => Promise.resolve(source))); + +const TEST_USER_LOGIN = 'test@test.com'; +const TEST_USER_ACCOUNT_ID = 1; +const POLICY_ID = 'odometerPolicy'; +const EXPENSE_REPORT_ID = '100'; +const TRANSACTION_THREAD_REPORT_ID = '101'; +const IOU_ACTION_ID = '102'; +const TRANSACTION_ID = '103'; + +const policy: Policy = { + id: POLICY_ID, + name: 'Odometer Workspace', + role: CONST.POLICY.ROLE.USER, + type: CONST.POLICY.TYPE.TEAM, + owner: TEST_USER_LOGIN, + outputCurrency: CONST.CURRENCY.USD, + approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL, +}; + +const expenseReport: Report = { + reportID: EXPENSE_REPORT_ID, + policyID: POLICY_ID, + type: CONST.REPORT.TYPE.EXPENSE, + ownerAccountID: TEST_USER_ACCOUNT_ID, + managerID: TEST_USER_ACCOUNT_ID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + currency: CONST.CURRENCY.USD, +}; + +const transactionThreadReport: Report = { + reportID: TRANSACTION_THREAD_REPORT_ID, + policyID: POLICY_ID, + type: CONST.REPORT.TYPE.CHAT, + parentReportID: EXPENSE_REPORT_ID, + parentReportActionID: IOU_ACTION_ID, +}; + +const iouAction: ReportAction = { + reportActionID: IOU_ACTION_ID, + actorAccountID: TEST_USER_ACCOUNT_ID, + actionName: CONST.REPORT.ACTIONS.TYPE.IOU, + childReportID: TRANSACTION_THREAD_REPORT_ID, + created: '2026-10-19 12:00:00.000', + originalMessage: { + IOUReportID: EXPENSE_REPORT_ID, + IOUTransactionID: TRANSACTION_ID, + amount: 1000, + currency: CONST.CURRENCY.USD, + type: CONST.IOU.REPORT_ACTION_TYPE.CREATE, + }, + message: [{type: 'COMMENT', html: 'USD 10.00 expense', text: 'USD 10.00 expense'}], +}; + +const odometerTransaction: Transaction = { + transactionID: TRANSACTION_ID, + reportID: EXPENSE_REPORT_ID, + amount: 1000, + currency: CONST.CURRENCY.USD, + created: '2026-10-19', + merchant: '10 mi @ $1.00 / mi', + iouRequestType: CONST.IOU.REQUEST_TYPE.DISTANCE_ODOMETER, + comment: {odometerStart: 100, odometerEnd: 110}, + receipt: {source: 'https://www.expensify.com/receipts/w_odometer.jpg', filename: 'w_odometer.jpg'}, +}; + +const renderReceiptModal = (initialParams: AuthScreensParamList[typeof SCREENS.TRANSACTION_RECEIPT]) => + render( + + + + + + + , + ); + +describe('TransactionReceiptModalContent - odometer receipt', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + initialKeyStates: { + [ONYXKEYS.SESSION]: {accountID: TEST_USER_ACCOUNT_ID, email: TEST_USER_LOGIN}, + [ONYXKEYS.PERSONAL_DETAILS_LIST]: {[TEST_USER_ACCOUNT_ID]: {accountID: TEST_USER_ACCOUNT_ID, login: TEST_USER_LOGIN}}, + }, + }); + }); + + beforeEach(async () => { + global.fetch = getGlobalFetchMock(); + wrapOnyxWithWaitForBatchedUpdates(Onyx); + await act(async () => { + await Onyx.merge(ONYXKEYS.IS_LOADING_APP, false); + }); + await signInWithTestUser(TEST_USER_ACCOUNT_ID, TEST_USER_LOGIN); + await waitForBatchedUpdatesWithAct(); + }); + + afterEach(async () => { + await waitForIdle(); + await act(async () => { + await Onyx.clear(); + }); + jest.clearAllMocks(); + }); + + it('shows Replace for the receipt of a created odometer expense', async () => { + // Given a created odometer expense with a receipt, submitted by the current user on an open report + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${POLICY_ID}`, policy); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${EXPENSE_REPORT_ID}`, expenseReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${TRANSACTION_THREAD_REPORT_ID}`, transactionThreadReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${EXPENSE_REPORT_ID}`, {[IOU_ACTION_ID]: iouAction}); + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION}${TRANSACTION_ID}`, odometerTransaction); + }); + await waitForBatchedUpdatesWithAct(); + + // When the receipt is opened from the expense + renderReceiptModal({reportID: TRANSACTION_THREAD_REPORT_ID, transactionID: TRANSACTION_ID}); + await waitForBatchedUpdatesWithAct(); + + // Then Replace is shown, because the stitcher no longer owns the receipt once the expense is created + expect(screen.getByText(translateLocal('common.replace'))).toBeOnTheScreen(); + }); + + it('hides Replace for the receipt of an odometer draft', async () => { + // Given an odometer draft whose receipt is built from the odometer photos + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.TRANSACTION_DRAFT}${TRANSACTION_ID}`, odometerTransaction); + }); + await waitForBatchedUpdatesWithAct(); + + // When the receipt is opened from the create flow + renderReceiptModal({ + reportID: EXPENSE_REPORT_ID, + transactionID: TRANSACTION_ID, + action: CONST.IOU.ACTION.CREATE, + iouType: CONST.IOU.TYPE.SUBMIT, + }); + await waitForBatchedUpdatesWithAct(); + + // Then Replace is hidden, so the user cannot overwrite the receipt the stitcher builds from the photos + expect(screen.queryByText(translateLocal('common.replace'))).toBeNull(); + }); +}); diff --git a/tests/unit/canEditFieldOfMoneyRequestTest.ts b/tests/unit/canEditFieldOfMoneyRequestTest.ts index eb187627db16..8b8be94a4504 100644 --- a/tests/unit/canEditFieldOfMoneyRequestTest.ts +++ b/tests/unit/canEditFieldOfMoneyRequestTest.ts @@ -10,6 +10,7 @@ import {toCollectionDataSet} from '@src/types/utils/CollectionDataSet'; import Onyx from 'react-native-onyx'; import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'; +import type {ValueOf} from 'type-fest'; import createRandomPolicy from '../utils/collections/policies'; import createRandomReportAction from '../utils/collections/reportActions'; @@ -654,6 +655,126 @@ describe('canEditFieldOfMoneyRequest', () => { }); }); + describe('receipt on a distance expense', () => { + const DISTANCE_POLICY_ID = '33'; + const DISTANCE_REPORT_ID = '44'; + const DISTANCE_TRANSACTION_ID = '45'; + + const distanceReportAction = { + ...createRandomReportAction(8), + reportID: DISTANCE_REPORT_ID, + actionName: CONST.REPORT.ACTIONS.TYPE.IOU, + actorAccountID: currentUserAccountID, + originalMessage: { + IOUTransactionID: DISTANCE_TRANSACTION_ID, + type: CONST.IOU.ACTION.CREATE, + amount: 100, + currency: CONST.CURRENCY.USD, + }, + }; + + const distancePolicy: Policy = { + ...createRandomPolicy(Number(DISTANCE_POLICY_ID), CONST.POLICY.TYPE.CORPORATE), + id: DISTANCE_POLICY_ID, + role: CONST.POLICY.ROLE.USER, + approvalMode: CONST.POLICY.APPROVAL_MODE.OPTIONAL, + }; + + const openReport = { + ...createExpenseReport(Number(DISTANCE_REPORT_ID)), + policyID: DISTANCE_POLICY_ID, + ownerAccountID: currentUserAccountID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + }; + + const createDistanceTransaction = (iouRequestType: ValueOf) => ({ + ...createRandomTransaction(Number(DISTANCE_TRANSACTION_ID)), + transactionID: DISTANCE_TRANSACTION_ID, + reportID: DISTANCE_REPORT_ID, + iouRequestType, + }); + + const setUpOnyx = async (transaction: ReturnType) => { + const policyCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.POLICY, [distancePolicy], (p) => p.id); + await Onyx.multiSet({ + [ONYXKEYS.SESSION]: {email: currentUserEmail, accountID: currentUserAccountID}, + [`${ONYXKEYS.COLLECTION.TRANSACTION}${DISTANCE_TRANSACTION_ID}`]: transaction, + [`${ONYXKEYS.COLLECTION.REPORT}${DISTANCE_REPORT_ID}`]: openReport, + ...policyCollectionDataSet, + }); + await waitForBatchedUpdates(); + }; + + afterEach(() => { + Onyx.clear(); + return waitForBatchedUpdates(); + }); + + it('should let the submitter add, replace, and delete the receipt on an odometer expense', async () => { + // Given an odometer expense the current user submitted on an open report + const odometerTransaction = createDistanceTransaction(CONST.IOU.REQUEST_TYPE.DISTANCE_ODOMETER); + await setUpOnyx(odometerTransaction); + + // When checking whether the receipt can be edited or deleted + const canEditReceipt = canEditFieldOfMoneyRequest({ + reportAction: distanceReportAction, + fieldToEdit: CONST.EDIT_REQUEST_FIELD.RECEIPT, + transaction: odometerTransaction, + rules: undefined, + reportNameValuePairs: undefined, + }); + const canDeleteReceipt = canEditFieldOfMoneyRequest({ + reportAction: distanceReportAction, + fieldToEdit: CONST.EDIT_REQUEST_FIELD.RECEIPT, + isDeleteAction: true, + transaction: odometerTransaction, + rules: undefined, + reportNameValuePairs: undefined, + }); + + // Then both are allowed, the same as for a manual distance expense, so the receipt can be attached by click or drop + expect(canEditReceipt).toBe(true); + expect(canDeleteReceipt).toBe(true); + }); + + it('should let the submitter edit the receipt on a manual distance expense', async () => { + // Given a manual distance expense the current user submitted on an open report + const manualTransaction = createDistanceTransaction(CONST.IOU.REQUEST_TYPE.DISTANCE_MANUAL); + await setUpOnyx(manualTransaction); + + // When checking whether the receipt can be edited + const canEditReceipt = canEditFieldOfMoneyRequest({ + reportAction: distanceReportAction, + fieldToEdit: CONST.EDIT_REQUEST_FIELD.RECEIPT, + transaction: manualTransaction, + rules: undefined, + reportNameValuePairs: undefined, + }); + + // Then it is allowed + expect(canEditReceipt).toBe(true); + }); + + it('should not let the submitter edit the receipt on a map distance expense', async () => { + // Given a map distance expense, whose receipt is generated from the route + const mapTransaction = createDistanceTransaction(CONST.IOU.REQUEST_TYPE.DISTANCE_MAP); + await setUpOnyx(mapTransaction); + + // When checking whether the receipt can be edited + const canEditReceipt = canEditFieldOfMoneyRequest({ + reportAction: distanceReportAction, + fieldToEdit: CONST.EDIT_REQUEST_FIELD.RECEIPT, + transaction: mapTransaction, + rules: undefined, + reportNameValuePairs: undefined, + }); + + // Then it is not allowed, because the map receipt must stay in sync with the route + expect(canEditReceipt).toBe(false); + }); + }); + describe('receipt on an approved expense', () => { const APPROVED_POLICY_ID = '55'; const APPROVED_REPORT_ID = '66'; From 3bf84dcd2a8a4366e6806a06f9a0c52a2639aab3 Mon Sep 17 00:00:00 2001 From: "Nyoman Jyotisa (via MelvinBot)" Date: Mon, 19 Oct 2026 17:14:35 +0000 Subject: [PATCH 2/2] Fix: Apply Oxfmt formatting to canEditFieldOfMoneyRequestTest Co-authored-by: Nyoman Jyotisa --- tests/unit/canEditFieldOfMoneyRequestTest.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/unit/canEditFieldOfMoneyRequestTest.ts b/tests/unit/canEditFieldOfMoneyRequestTest.ts index 8b8be94a4504..38f0114012e4 100644 --- a/tests/unit/canEditFieldOfMoneyRequestTest.ts +++ b/tests/unit/canEditFieldOfMoneyRequestTest.ts @@ -8,9 +8,10 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {Policy, Report, ReportAction} from '@src/types/onyx'; import {toCollectionDataSet} from '@src/types/utils/CollectionDataSet'; +import type {ValueOf} from 'type-fest'; + import Onyx from 'react-native-onyx'; import OnyxUtils from 'react-native-onyx/dist/OnyxUtils'; -import type {ValueOf} from 'type-fest'; import createRandomPolicy from '../utils/collections/policies'; import createRandomReportAction from '../utils/collections/reportActions';