@@ -49,7 +49,6 @@ import {getCategoryApproverRule} from './CategoryUtils';
4949import { convertToBackendAmount } from './CurrencyUtils' ;
5050import Navigation from './Navigation/Navigation' ;
5151import { getIsOffline } from './NetworkState' ;
52- import { generateHexadecimalValue } from './NumberUtils' ;
5352import { formatMemberForList } from './OptionsListUtils' ;
5453import type { MemberForList } from './OptionsListUtils' ;
5554import { getAccountIDsByLogins , getLoginByAccountID , getLoginsByAccountIDs , getPersonalDetailByEmail } from './PersonalDetailsUtils' ;
@@ -308,19 +307,22 @@ function hasEligibleActiveAdminFromWorkspaces(policies: OnyxCollection<Policy> |
308307 return false ;
309308}
310309
311- function cloneCustomUnitWithNewIDs ( unit : CustomUnit , newCustomUnitID : string ) : CustomUnit {
312- const newRates : Record < string , Rate > = { } ;
313- for ( const rate of Object . values ( unit . rates ) ) {
314- const newRateID = generateHexadecimalValue ( 13 ) ;
315- newRates [ newRateID ] = {
316- ...rate ,
317- customUnitRateID : newRateID ,
310+ function cloneCustomUnitWithNewIDs ( unit : CustomUnit , newCustomUnitID : string , newDefaultRateID ?: string ) : CustomUnit {
311+ if ( newDefaultRateID ) {
312+ // For distance units: use the provided rate ID for the default rate,
313+ // matching the customUnitRateID sent to the DUPLICATE_POLICY API
314+ const defaultRate = Object . values ( unit . rates ) . find ( ( rate ) => rate . enabled ) ?? Object . values ( unit . rates ) . at ( 0 ) ;
315+ return {
316+ ...unit ,
317+ customUnitID : newCustomUnitID ,
318+ rates : defaultRate ? { [ newDefaultRateID ] : { ...defaultRate , customUnitRateID : newDefaultRateID } } : { } ,
318319 } ;
319320 }
321+
322+ // For other units (per diem): only update the customUnitID
320323 return {
321324 ...unit ,
322325 customUnitID : newCustomUnitID ,
323- rates : newRates ,
324326 } ;
325327}
326328
@@ -331,10 +333,11 @@ function getCustomUnitsForDuplication(
331333 customUnitIDs : {
332334 distanceCustomUnitID : string ;
333335 perDiemCustomUnitID : string ;
336+ customUnitRateID : string ;
334337 } ,
335338) : Record < string , CustomUnit > | undefined {
336339 const customUnits = policy ?. customUnits ;
337- const { distanceCustomUnitID, perDiemCustomUnitID} = customUnitIDs ?? { } ;
340+ const { distanceCustomUnitID, perDiemCustomUnitID, customUnitRateID } = customUnitIDs ?? { } ;
338341
339342 if ( ( ! isDistanceRatesOptionSelected && ! isPerDiemOptionSelected ) || ! customUnits || Object . keys ( customUnits ) . length === 0 ) {
340343 return undefined ;
@@ -349,7 +352,7 @@ function getCustomUnitsForDuplication(
349352 }
350353
351354 return {
352- [ distanceCustomUnitID ] : cloneCustomUnitWithNewIDs ( distanceCustomUnit , distanceCustomUnitID ) ,
355+ [ distanceCustomUnitID ] : cloneCustomUnitWithNewIDs ( distanceCustomUnit , distanceCustomUnitID , customUnitRateID ) ,
353356 [ perDiemCustomUnitID ] : cloneCustomUnitWithNewIDs ( perDiemUnit , perDiemCustomUnitID ) ,
354357 } ;
355358 }
@@ -359,7 +362,7 @@ function getCustomUnitsForDuplication(
359362 if ( ! distanceCustomUnit ) {
360363 return undefined ;
361364 }
362- return { [ distanceCustomUnitID ] : cloneCustomUnitWithNewIDs ( distanceCustomUnit , distanceCustomUnitID ) } ;
365+ return { [ distanceCustomUnitID ] : cloneCustomUnitWithNewIDs ( distanceCustomUnit , distanceCustomUnitID , customUnitRateID ) } ;
363366 }
364367
365368 const perDiemUnit = Object . values ( customUnits ) . find ( ( customUnit ) => customUnit . name === CONST . CUSTOM_UNITS . NAME_PER_DIEM_INTERNATIONAL ) ;
0 commit comments