From 893a837987ea5f1c1ace14b2486ee6fa67be44da Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Tue, 10 Feb 2026 10:51:05 +0700 Subject: [PATCH 1/7] fix: Incorrect currency displayed when creating Distance expense in selfDM --- src/components/MoneyRequestConfirmationList.tsx | 3 ++- src/hooks/usePolicyForMovingExpenses.ts | 6 +++++- src/pages/iou/request/step/IOURequestStepConfirmation.tsx | 7 ++++--- 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index 6bf4357c8182..e3e4928c3a59 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -279,7 +279,6 @@ function MoneyRequestConfirmationList({ selector: mileageRateSelector, canBeMissing: true, }); - const {policyForMovingExpenses} = usePolicyForMovingExpenses(); const isMovingTransactionFromTrackExpense = isMovingTransactionFromTrackExpenseUtil(action); const [defaultMileageRateReal] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { selector: mileageRateSelector, @@ -309,6 +308,8 @@ function MoneyRequestConfirmationList({ ); const isTrackExpense = iouType === CONST.IOU.TYPE.TRACK; + const isTrackDistanceRequest = isTrackExpense && isDistanceRequest; + const {policyForMovingExpenses} = usePolicyForMovingExpenses(undefined, undefined, isTrackDistanceRequest, isMovingTransactionFromTrackExpense); const policy = isTrackExpense ? policyForMovingExpenses : (policyReal ?? policyDraft); const policyCategories = policyCategoriesReal ?? policyCategoriesDraft; const defaultMileageRate = defaultMileageRateDraft ?? defaultMileageRateReal; diff --git a/src/hooks/usePolicyForMovingExpenses.ts b/src/hooks/usePolicyForMovingExpenses.ts index 83a5d95f5d75..1265715b22b8 100644 --- a/src/hooks/usePolicyForMovingExpenses.ts +++ b/src/hooks/usePolicyForMovingExpenses.ts @@ -30,7 +30,7 @@ function isPolicyValidForMovingExpenses(policy: OnyxEntry, login: string ); } -function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, expensePolicyID?: string) { +function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, expensePolicyID?: string, isTrackDistanceRequest?: boolean, isMovingFromTrackExpense?: boolean) { const [allPolicies] = useOnyx(ONYXKEYS.COLLECTION.POLICY, {canBeMissing: true}); const [activePolicyID] = useOnyx(ONYXKEYS.NVP_ACTIVE_POLICY_ID, {canBeMissing: true}); const [activePolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${activePolicyID}`, { @@ -41,6 +41,10 @@ function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, expensePolicyID? const session = useSession(); const login = session?.email ?? ''; + if (isTrackDistanceRequest && !isMovingFromTrackExpense) { + return {policyForMovingExpensesID: undefined, policyForMovingExpenses: undefined, shouldSelectPolicy: false}; + } + // Early exit optimization: only need to check if we have 0, 1, or >1 policies let singleUserPolicy; let isMemberOfMoreThanOnePolicy = false; diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 2bc7ba1cb060..7ae6ed1e17bd 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -147,7 +147,10 @@ function IOURequestStepConfirmation({ ); const isUnreported = transaction?.reportID === CONST.REPORT.UNREPORTED_REPORT_ID; const isCreatingTrackExpense = action === CONST.IOU.ACTION.CREATE && iouType === CONST.IOU.TYPE.TRACK; - const {policyForMovingExpenses, policyForMovingExpensesID} = usePolicyForMovingExpenses(); + const isDistanceRequest = isDistanceRequestTransactionUtils(transaction); + const isMovingTransactionFromTrackExpense = isMovingTransactionFromTrackExpenseIOUUtils(action); + const isTrackDistanceRequest = isCreatingTrackExpense && isDistanceRequest; + const {policyForMovingExpenses, policyForMovingExpensesID} = usePolicyForMovingExpenses(undefined, undefined, isTrackDistanceRequest, isMovingTransactionFromTrackExpense); const realPolicyID = isCreatingTrackExpense || isUnreported ? policyForMovingExpensesID : getIOURequestPolicyID(initialTransaction, reportReal); const draftPolicyID = getIOURequestPolicyID(initialTransaction, reportDraft); const [policyDraft] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY_DRAFTS}${draftPolicyID}`, {canBeMissing: true}); @@ -236,7 +239,6 @@ function IOURequestStepConfirmation({ const [receiptFiles, setReceiptFiles] = useState>({}); const requestType = getRequestType(transaction); - const isDistanceRequest = isDistanceRequestTransactionUtils(transaction); const isManualDistanceRequest = isManualDistanceRequestTransactionUtils(transaction); const isOdometerDistanceRequest = isOdometerDistanceRequestTransactionUtils(transaction); const isGPSDistanceRequest = isGPSDistanceRequestTransactionUtils(transaction); @@ -262,7 +264,6 @@ function IOURequestStepConfirmation({ const transactionTaxAmount = transaction?.taxAmount ?? 0; const isSharingTrackExpense = action === CONST.IOU.ACTION.SHARE; const isCategorizingTrackExpense = action === CONST.IOU.ACTION.CATEGORIZE; - const isMovingTransactionFromTrackExpense = isMovingTransactionFromTrackExpenseIOUUtils(action); const isTestTransaction = transaction?.participants?.some((participant) => isSelectedManagerMcTest(participant.login)); const gpsRequired = transaction?.amount === 0 && iouType !== CONST.IOU.TYPE.SPLIT && Object.values(receiptFiles).length && !isTestTransaction; From bbda3e36cd503b912ced0538550f44f7091a7e77 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 13 Feb 2026 09:46:26 +0700 Subject: [PATCH 2/7] fix lint --- src/components/MoneyRequestConfirmationList.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/MoneyRequestConfirmationList.tsx b/src/components/MoneyRequestConfirmationList.tsx index fa299e75a3f8..b4f3e3f925fc 100755 --- a/src/components/MoneyRequestConfirmationList.tsx +++ b/src/components/MoneyRequestConfirmationList.tsx @@ -345,7 +345,7 @@ function MoneyRequestConfirmationList({ const prevCurrency = usePrevious(currency); const prevSubRates = usePrevious(subRates); - const {shouldSelectPolicy} = usePolicyForMovingExpenses(); + const {shouldSelectPolicy, policyForMovingExpenses} = usePolicyForMovingExpenses(); // A flag for showing the categories field const shouldShowCategories = isTrackExpense From 2fe40c748af036ae8fdda3853a02a2bac0e0d465 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 13 Feb 2026 10:00:28 +0700 Subject: [PATCH 3/7] remove duplicate import --- src/hooks/usePolicyForTransaction.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/hooks/usePolicyForTransaction.ts b/src/hooks/usePolicyForTransaction.ts index 54bb1d793597..9cdc95bf6ae2 100644 --- a/src/hooks/usePolicyForTransaction.ts +++ b/src/hooks/usePolicyForTransaction.ts @@ -2,8 +2,7 @@ import {useMemo} from 'react'; import type {OnyxEntry} from 'react-native-onyx'; import {isMovingTransactionFromTrackExpense as isMovingTransactionFromTrackExpenseIOUUtils} from '@libs/IOUUtils'; import {getPolicyByCustomUnitID} from '@libs/PolicyUtils'; -import {isExpenseUnreported} from '@libs/TransactionUtils'; -import {isDistanceRequest as isDistanceRequestTransactionUtils} from '@libs/TransactionUtils'; +import {isDistanceRequest as isDistanceRequestTransactionUtils, isExpenseUnreported} from '@libs/TransactionUtils'; import CONST from '@src/CONST'; import type {IOUAction} from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; From 2b1872a3c776739f474f8963c35826f4af05522e Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Fri, 13 Feb 2026 10:18:31 +0700 Subject: [PATCH 4/7] fix lint --- src/pages/iou/request/step/IOURequestStepConfirmation.tsx | 1 - 1 file changed, 1 deletion(-) diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index 02d29c4b74a2..9389d2ea3777 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -26,7 +26,6 @@ import useOptimisticDraftTransactions from '@hooks/useOptimisticDraftTransaction import useParentReportAction from '@hooks/useParentReportAction'; import useParticipantsInvoiceReport from '@hooks/useParticipantsInvoiceReport'; import usePermissions from '@hooks/usePermissions'; -import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses'; import usePolicyForTransaction from '@hooks/usePolicyForTransaction'; import usePrivateIsArchivedMap from '@hooks/usePrivateIsArchivedMap'; import useTheme from '@hooks/useTheme'; From 9d5a2c5f11f09a4d90e73746c57a602e857c5e1a Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Sun, 15 Feb 2026 23:12:37 +0700 Subject: [PATCH 5/7] write unit test for usePolicyForMovingExpenses --- tests/unit/usePolicyForMovingExpensesTest.ts | 212 +++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 tests/unit/usePolicyForMovingExpensesTest.ts diff --git a/tests/unit/usePolicyForMovingExpensesTest.ts b/tests/unit/usePolicyForMovingExpensesTest.ts new file mode 100644 index 000000000000..c8fbcb9ed45f --- /dev/null +++ b/tests/unit/usePolicyForMovingExpensesTest.ts @@ -0,0 +1,212 @@ +import {renderHook} from '@testing-library/react-native'; +import Onyx from 'react-native-onyx'; +import OnyxListItemProvider from '@components/OnyxListItemProvider'; +import usePolicyForMovingExpenses from '@hooks/usePolicyForMovingExpenses'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Policy} from '@src/types/onyx'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; + +const USER_EMAIL = 'user@example.com'; + +function createValidPolicyForMovingExpenses(id: string, overrides: Partial = {}): Policy { + return { + id, + name: `Workspace ${id}`, + type: CONST.POLICY.TYPE.TEAM, + role: CONST.POLICY.ROLE.ADMIN, + pendingAction: undefined, + employeeList: {}, + isPolicyExpenseChatEnabled: true, + customUnits: {}, + outputCurrency: 'USD', + ...overrides, + } as Policy; +} + +describe('usePolicyForMovingExpenses', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + }); + return waitForBatchedUpdates(); + }); + + beforeEach(async () => { + await Onyx.clear(); + return waitForBatchedUpdates(); + }); + + it('should return default values when no policies and no session', () => { + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBeUndefined(); + expect(result.current.policyForMovingExpenses).toBeUndefined(); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should return undefined policy when isTrackDistanceRequest is true and isMovingFromTrackExpense is false', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy = createValidPolicyForMovingExpenses('1'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(false, undefined, true, false), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBeUndefined(); + expect(result.current.policyForMovingExpenses).toBeUndefined(); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should return single valid policy when user is member of one policy', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy = createValidPolicyForMovingExpenses('policy1'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBe('policy1'); + expect(result.current.policyForMovingExpenses?.id).toBe('policy1'); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should return shouldSelectPolicy true when user is member of more than one valid policy', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy1 = createValidPolicyForMovingExpenses('policy1'); + const policy2 = createValidPolicyForMovingExpenses('policy2'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy1.id}`, policy1); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy2.id}`, policy2); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBeUndefined(); + expect(result.current.policyForMovingExpenses).toBeUndefined(); + expect(result.current.shouldSelectPolicy).toBe(true); + }); + + it('should prefer expensePolicyID when provided and valid', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + await Onyx.set(ONYXKEYS.NVP_ACTIVE_POLICY_ID, 'activePolicy'); + const activePolicy = createValidPolicyForMovingExpenses('activePolicy'); + const expensePolicy = createValidPolicyForMovingExpenses('expensePolicy'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${activePolicy.id}`, activePolicy); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${expensePolicy.id}`, expensePolicy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(false, 'expensePolicy'), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBe('expensePolicy'); + expect(result.current.policyForMovingExpenses?.id).toBe('expensePolicy'); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should return active policy when expensePolicyID not provided and active policy is valid', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + await Onyx.set(ONYXKEYS.NVP_ACTIVE_POLICY_ID, 'activePolicy'); + const activePolicy = createValidPolicyForMovingExpenses('activePolicy'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${activePolicy.id}`, activePolicy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBe('activePolicy'); + expect(result.current.policyForMovingExpenses?.id).toBe('activePolicy'); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should not return policy with pendingAction DELETE', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy = createValidPolicyForMovingExpenses('policy1', { + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBeUndefined(); + expect(result.current.policyForMovingExpenses).toBeUndefined(); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should not return personal policy as valid for moving expenses', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy = createValidPolicyForMovingExpenses('policy1', { + type: CONST.POLICY.TYPE.PERSONAL, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBeUndefined(); + expect(result.current.policyForMovingExpenses).toBeUndefined(); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should return single policy when only one of two policies is valid', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const validPolicy = createValidPolicyForMovingExpenses('valid'); + const invalidPolicy = createValidPolicyForMovingExpenses('invalid', { + type: CONST.POLICY.TYPE.PERSONAL, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${validPolicy.id}`, validPolicy); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${invalidPolicy.id}`, invalidPolicy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBe('valid'); + expect(result.current.shouldSelectPolicy).toBe(false); + }); + + it('should return undefined when expensePolicyID is provided but policy is invalid', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy = createValidPolicyForMovingExpenses('policy1', { + type: CONST.POLICY.TYPE.PERSONAL, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(false, 'policy1'), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBeUndefined(); + expect(result.current.policyForMovingExpenses).toBeUndefined(); + }); + + it('should return policy when isTrackDistanceRequest is true but isMovingFromTrackExpense is true', async () => { + await Onyx.set(ONYXKEYS.SESSION, {email: USER_EMAIL}); + const policy = createValidPolicyForMovingExpenses('policy1'); + await Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${policy.id}`, policy); + await waitForBatchedUpdates(); + + const {result} = renderHook(() => usePolicyForMovingExpenses(false, undefined, true, true), { + wrapper: OnyxListItemProvider, + }); + + expect(result.current.policyForMovingExpensesID).toBe('policy1'); + expect(result.current.shouldSelectPolicy).toBe(false); + }); +}); From 36d9bc7f5e80226285edaf94f1efcbd7d37fb804 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Sun, 15 Feb 2026 23:14:47 +0700 Subject: [PATCH 6/7] add explain comment --- src/hooks/usePolicyForMovingExpenses.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hooks/usePolicyForMovingExpenses.ts b/src/hooks/usePolicyForMovingExpenses.ts index 1265715b22b8..f8ac551f78fe 100644 --- a/src/hooks/usePolicyForMovingExpenses.ts +++ b/src/hooks/usePolicyForMovingExpenses.ts @@ -41,6 +41,8 @@ function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, expensePolicyID? const session = useSession(); const login = session?.email ?? ''; + + // Creating a track distance request always uses the P2P custom unit, so we don't use the active policy if (isTrackDistanceRequest && !isMovingFromTrackExpense) { return {policyForMovingExpensesID: undefined, policyForMovingExpenses: undefined, shouldSelectPolicy: false}; } From 7eba64ce763ccad4812e8a1f5983a5e211c1b7c3 Mon Sep 17 00:00:00 2001 From: nkdengineer Date: Sun, 15 Feb 2026 23:17:50 +0700 Subject: [PATCH 7/7] fix prettier --- src/hooks/usePolicyForMovingExpenses.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hooks/usePolicyForMovingExpenses.ts b/src/hooks/usePolicyForMovingExpenses.ts index f8ac551f78fe..ac3c17638e22 100644 --- a/src/hooks/usePolicyForMovingExpenses.ts +++ b/src/hooks/usePolicyForMovingExpenses.ts @@ -41,7 +41,6 @@ function usePolicyForMovingExpenses(isPerDiemRequest?: boolean, expensePolicyID? const session = useSession(); const login = session?.email ?? ''; - // Creating a track distance request always uses the P2P custom unit, so we don't use the active policy if (isTrackDistanceRequest && !isMovingFromTrackExpense) { return {policyForMovingExpensesID: undefined, policyForMovingExpenses: undefined, shouldSelectPolicy: false};