diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 598af1b8d380..360b5a64266e 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -190,6 +190,31 @@ function getDistanceRateCustomUnitRate(policy: OnyxEntry, customUnitRate return distanceUnit?.rates[customUnitRateID]; } +function getCustomUnitsForDuplication(policy: Policy, isCustomUnitsOptionSelected: boolean, isPerDiemOptionSelected: boolean): Record | undefined { + const customUnits = policy?.customUnits; + if ((!isCustomUnitsOptionSelected && !isPerDiemOptionSelected) || !customUnits || Object.keys(customUnits).length === 0) { + return undefined; + } + + if (isCustomUnitsOptionSelected && isPerDiemOptionSelected) { + return customUnits; + } + + if (isCustomUnitsOptionSelected) { + 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(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 +1693,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 eef8929417ca..94bf8f83ff2e 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: { 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);