From 674f9f84aab3777ae9c3e3969efc365abc65b4fc Mon Sep 17 00:00:00 2001 From: Nicolay Arefyeu Date: Mon, 29 Sep 2025 17:52:08 +0300 Subject: [PATCH 1/3] Use correct custom units for both Distance Rate and PerDiem during duplication --- src/libs/PolicyUtils.ts | 25 +++++++++++++++++++++++++ src/libs/actions/Policy/Policy.ts | 11 +++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 598af1b8d380..a8eac5a61858 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -190,6 +190,30 @@ function getDistanceRateCustomUnitRate(policy: OnyxEntry, customUnitRate return distanceUnit?.rates[customUnitRateID]; } +function getCustomUnitsForDuplication(policy: Policy, isCustomUnitsOptionSelected: boolean, isPerDiemOptionSelected: boolean): Record | undefined { + if ((!isCustomUnitsOptionSelected && !isPerDiemOptionSelected) || !policy?.customUnits || Object.keys(policy.customUnits).length === 0) { + return undefined; + } + + if (isCustomUnitsOptionSelected && isPerDiemOptionSelected) { + return policy?.customUnits; + } + + if (isCustomUnitsOptionSelected) { + const distanceCustomUnit = Object.values(policy.customUnits).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); + if (!distanceCustomUnit) { + return undefined; + } + return {[distanceCustomUnit.customUnitID]: distanceCustomUnit}; + } + + const perDiemUnit = Object.values(policy.customUnits).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_PER_DIEM_INTERNATIONAL); + if (!perDiemUnit) { + return undefined; + } + return {[perDiemUnit.customUnitID]: perDiemUnit}; +} + /** * Retrieves custom unit rate object from the given customUnitRateID */ @@ -1668,6 +1692,7 @@ export { getDescriptionForPolicyDomainCard, getManagerAccountID, isPreferredExporter, + getCustomUnitsForDuplication, areAllGroupPoliciesExpenseChatDisabled, getCountOfRequiredTagLists, getActiveEmployeeWorkspaces, diff --git a/src/libs/actions/Policy/Policy.ts b/src/libs/actions/Policy/Policy.ts index 6b6b3e0c99c4..2d011204b32b 100644 --- a/src/libs/actions/Policy/Policy.ts +++ b/src/libs/actions/Policy/Policy.ts @@ -84,7 +84,14 @@ import * as NumberUtils from '@libs/NumberUtils'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as PhoneNumber from '@libs/PhoneNumber'; import * as PolicyUtils from '@libs/PolicyUtils'; -import {getMemberAccountIDsForWorkspace, goBackWhenEnableFeature, isControlPolicy, navigateToExpensifyCardPage, navigateToReceiptPartnersPage} from '@libs/PolicyUtils'; +import { + getCustomUnitsForDuplication, + getMemberAccountIDsForWorkspace, + goBackWhenEnableFeature, + isControlPolicy, + navigateToExpensifyCardPage, + navigateToReceiptPartnersPage, +} from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import type {PolicySelector} from '@pages/home/sidebar/FloatingActionButtonAndPopover'; import type {Feature} from '@pages/OnboardingInterestedFeatures/types'; @@ -2576,7 +2583,7 @@ function buildDuplicatePolicyData(policy: Policy, options: DuplicatePolicyDataOp name: policyName, fieldList: isReportsOptionSelected ? policy?.fieldList : undefined, connections: isConnectionsOptionSelected ? policy?.connections : undefined, - customUnits: isCustomUnitsOptionSelected ? policy?.customUnits : undefined, + customUnits: getCustomUnitsForDuplication(policy, isCustomUnitsOptionSelected, isPerDiemOptionSelected), taxRates: isTaxesOptionSelected ? policy?.taxRates : undefined, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, pendingFields: { From 869e6b5af674ef5ac2ea2affe95094bb8c146524 Mon Sep 17 00:00:00 2001 From: Nicolay Arefyeu Date: Mon, 29 Sep 2025 17:56:39 +0300 Subject: [PATCH 2/3] clean up --- src/libs/PolicyUtils.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index a8eac5a61858..360b5a64266e 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -191,23 +191,24 @@ function getDistanceRateCustomUnitRate(policy: OnyxEntry, customUnitRate } function getCustomUnitsForDuplication(policy: Policy, isCustomUnitsOptionSelected: boolean, isPerDiemOptionSelected: boolean): Record | undefined { - if ((!isCustomUnitsOptionSelected && !isPerDiemOptionSelected) || !policy?.customUnits || Object.keys(policy.customUnits).length === 0) { + const customUnits = policy?.customUnits; + if ((!isCustomUnitsOptionSelected && !isPerDiemOptionSelected) || !customUnits || Object.keys(customUnits).length === 0) { return undefined; } if (isCustomUnitsOptionSelected && isPerDiemOptionSelected) { - return policy?.customUnits; + return customUnits; } if (isCustomUnitsOptionSelected) { - const distanceCustomUnit = Object.values(policy.customUnits).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); + const distanceCustomUnit = Object.values(customUnits).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_DISTANCE); if (!distanceCustomUnit) { return undefined; } return {[distanceCustomUnit.customUnitID]: distanceCustomUnit}; } - const perDiemUnit = Object.values(policy.customUnits).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_PER_DIEM_INTERNATIONAL); + const perDiemUnit = Object.values(customUnits).find((customUnit) => customUnit.name === CONST.CUSTOM_UNITS.NAME_PER_DIEM_INTERNATIONAL); if (!perDiemUnit) { return undefined; } From 2494401c0dea2610ed94f8bed0ced3ac75eea38a Mon Sep 17 00:00:00 2001 From: Nicolay Arefyeu Date: Tue, 30 Sep 2025 15:35:46 +0300 Subject: [PATCH 3/3] add unit tests --- tests/unit/PolicyUtilsTest.ts | 48 +++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 95cbd5488767..a941d83d9cf6 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -6,6 +6,7 @@ import useDefaultFundID from '@hooks/useDefaultFundID'; import DateUtils from '@libs/DateUtils'; import { getActivePolicies, + getCustomUnitsForDuplication, getManagerAccountID, getPolicyEmployeeAccountIDs, getPolicyNameByID, @@ -272,6 +273,53 @@ describe('PolicyUtils', () => { expect(getActivePolicies(policies, undefined)).toHaveLength(1); }); }); + describe('getCustomUnitsForDuplication', () => { + const perDiemUnit = { + customUnitID: '123', + name: CONST.CUSTOM_UNITS.NAME_PER_DIEM_INTERNATIONAL, + enabled: true, + rates: {}, + }; + const otherUnit = { + customUnitID: '456', + name: CONST.CUSTOM_UNITS.NAME_DISTANCE, + enabled: true, + rates: {}, + }; + const policy: Policy = { + ...createRandomPolicy(0), + customUnits: { + [perDiemUnit.customUnitID]: perDiemUnit, + [otherUnit.customUnitID]: otherUnit, + }, + }; + + const policyWithoutCustomUnits: Policy = { + ...createRandomPolicy(0), + }; + + it('returns undefined if neither option is selected', () => { + expect(getCustomUnitsForDuplication(policy, false, false)).toBeUndefined(); + }); + + it('returns all custom units if both options are selected', () => { + const result = getCustomUnitsForDuplication(policy, true, true); + expect(result).toEqual(policy.customUnits); + }); + it('returns only non-per-diem units if only custom units option is selected', () => { + const result = getCustomUnitsForDuplication(policy, true, false); + expect(result).toEqual({[otherUnit.customUnitID]: otherUnit}); + }); + + it('returns only per diem unit if only per diem option is selected', () => { + const result = getCustomUnitsForDuplication(policy, false, true); + expect(result).toEqual({[perDiemUnit.customUnitID]: perDiemUnit}); + }); + + it('returns undefined if customUnits is empty', () => { + expect(getCustomUnitsForDuplication(policyWithoutCustomUnits, true, true)).toBeUndefined(); + }); + }); describe('getRateDisplayValue', () => { it('should return an empty string for NaN', () => { const rate = getRateDisplayValue('invalid' as unknown as number, toLocaleDigitMock);