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
3 changes: 2 additions & 1 deletion src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ import {
isManagedCardTransaction,
isManualDistanceRequest as isManualDistanceRequestTransactionUtils,
isMapDistanceRequest,
isOdometerDistanceRequest,
isOnHold as isOnHoldTransactionUtils,
isPayAtEndExpense,
isPending,
Expand Down Expand Up @@ -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)
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: we also call open reports "Draft" in the UI, so this reads like it's about the report status. Maybe "Odometer photos are only stitched into the receipt during the create flow, 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;
Expand Down
193 changes: 193 additions & 0 deletions tests/ui/TransactionReceiptOdometerReplaceTest.tsx
Original file line number Diff line number Diff line change
@@ -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<AuthScreensParamList>();

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(
<ComposeProviders components={[OnyxListItemProvider, LocaleContextProvider, AttachmentModalContextProvider, CurrentReportIDContextProvider, PortalProvider, PlaybackContextProvider]}>
<NavigationContainer>
<Stack.Navigator initialRouteName={SCREENS.TRANSACTION_RECEIPT}>
<Stack.Screen
name={SCREENS.TRANSACTION_RECEIPT}
component={AttachmentModalScreen}
initialParams={initialParams}
/>
</Stack.Navigator>
</NavigationContainer>
</ComposeProviders>,
);

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: "the stitcher no longer owns the receipt" is a bit hard to parse. Maybe "because odometer photos are only stitched during the create flow"?

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();
});
});
122 changes: 122 additions & 0 deletions tests/unit/canEditFieldOfMoneyRequestTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ 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';

Expand Down Expand Up @@ -654,6 +656,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<typeof CONST.IOU.REQUEST_TYPE>) => ({
...createRandomTransaction(Number(DISTANCE_TRANSACTION_ID)),
transactionID: DISTANCE_TRANSACTION_ID,
reportID: DISTANCE_REPORT_ID,
iouRequestType,
});

const setUpOnyx = async (transaction: ReturnType<typeof createDistanceTransaction>) => {
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NAB: the description says GPS stays locked too, could we add a GPS case next to the map one? An it.each over DISTANCE_MAP and DISTANCE_GPS would cover it.

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';
Expand Down
Loading