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
26 changes: 26 additions & 0 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,31 @@ function getDistanceRateCustomUnitRate(policy: OnyxEntry<Policy>, customUnitRate
return distanceUnit?.rates[customUnitRateID];
}

function getCustomUnitsForDuplication(policy: Policy, isCustomUnitsOptionSelected: boolean, isPerDiemOptionSelected: boolean): Record<string, CustomUnit> | 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};

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.

Coming from this issue #86116, we have an issue when user is unable to create a manual distance expense right after they duplicate a workspace. More details here: #86116 (comment)

}

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
*/
Expand Down Expand Up @@ -1668,6 +1693,7 @@ export {
getDescriptionForPolicyDomainCard,
getManagerAccountID,
isPreferredExporter,
getCustomUnitsForDuplication,
areAllGroupPoliciesExpenseChatDisabled,
getCountOfRequiredTagLists,
getActiveEmployeeWorkspaces,
Expand Down
11 changes: 9 additions & 2 deletions src/libs/actions/Policy/Policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: {
Expand Down
48 changes: 48 additions & 0 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import useDefaultFundID from '@hooks/useDefaultFundID';
import DateUtils from '@libs/DateUtils';
import {
getActivePolicies,
getCustomUnitsForDuplication,
getManagerAccountID,
getPolicyEmployeeAccountIDs,
getPolicyNameByID,
Expand Down Expand Up @@ -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);
Expand Down
Loading