@@ -3097,6 +3097,98 @@ function createDraftWorkspace(
30973097 return params ;
30983098}
30993099
3100+ function buildOptimisticDuplicatePolicy ( sourcePolicy : Policy , policyOptions : DuplicatePolicyDataOptions ) {
3101+ const {
3102+ policyName : duplicatedPolicyName = '' ,
3103+ targetPolicyID : duplicatedPolicyID ,
3104+ file : duplicatedPolicyFile ,
3105+ parts : duplicatedParts ,
3106+ localCurrency : duplicatedLocalCurrency ,
3107+ } = policyOptions ;
3108+
3109+ const isMemberFeatureSelected = duplicatedParts ?. people ;
3110+ const isReportsFeatureSelected = duplicatedParts ?. reports ;
3111+ const isConnectionsFeatureSelected = duplicatedParts ?. connections ;
3112+ const isTaxesFeatureSelected = duplicatedParts ?. taxes ;
3113+ const isTagsFeatureSelected = duplicatedParts ?. tags ;
3114+ const isInvoicesFeatureSelected = duplicatedParts ?. invoices ;
3115+ const isDistanceRatesFeatureSelected = duplicatedParts ?. distance ;
3116+ const isRulesFeatureSelected = duplicatedParts ?. expenses ;
3117+ const isWorkflowsFeatureSelected = duplicatedParts ?. exportLayouts ;
3118+ const isPerDiemFeatureSelected = duplicatedParts ?. perDiem ;
3119+ const isOverviewFeatureSelected = duplicatedParts ?. overview ;
3120+ const isTravelFeatureSelected = duplicatedParts ?. travel ;
3121+ const isCodingRulesFeatureSelected = duplicatedParts ?. codingRules ;
3122+ const duplicatedOutputCurrency = isOverviewFeatureSelected ? sourcePolicy ?. outputCurrency : duplicatedLocalCurrency ;
3123+ const { customUnitID : duplicatedDistanceCustomUnitID } = buildOptimisticDistanceRateCustomUnits ( duplicatedOutputCurrency ) ;
3124+ const duplicatedPerDiemCustomUnitID = generateCustomUnitID ( ) ;
3125+
3126+ const filterPendingDeleteData = < T > ( data ?: Record < string , T > ) : Record < string , T > | undefined =>
3127+ data
3128+ ? ( Object . fromEntries (
3129+ Object . entries ( data ) . filter ( ( [ , value ] ) => {
3130+ if ( ! value || typeof value !== 'object' || ! ( 'pendingAction' in value ) ) {
3131+ return true ;
3132+ }
3133+ return value . pendingAction !== CONST . RED_BRICK_ROAD_PENDING_ACTION . DELETE ;
3134+ } ) ,
3135+ ) as Record < string , T > )
3136+ : undefined ;
3137+
3138+ const codingRulesWithoutPendingDelete = filterPendingDeleteData ( sourcePolicy ?. rules ?. codingRules ) ;
3139+ const employeeListWithoutPendingDelete = filterPendingDeleteData ( sourcePolicy ?. employeeList ) ;
3140+ const fieldListWithoutPendingDelete = filterPendingDeleteData ( sourcePolicy ?. fieldList ) ;
3141+ const connectionsWithoutPendingDelete = filterPendingDeleteData ( sourcePolicy ?. connections ) ;
3142+ const taxRatesWithoutPendingDelete = {
3143+ ...sourcePolicy ?. taxRates ,
3144+ taxes : filterPendingDeleteData ( sourcePolicy ?. taxRates ?. taxes ) ,
3145+ } ;
3146+
3147+ return {
3148+ ...sourcePolicy ,
3149+ areCategoriesEnabled : true ,
3150+ areTagsEnabled : isTagsFeatureSelected ,
3151+ areDistanceRatesEnabled : isDistanceRatesFeatureSelected ,
3152+ areInvoicesEnabled : isInvoicesFeatureSelected ,
3153+ areRulesEnabled : isRulesFeatureSelected ,
3154+ areWorkflowsEnabled : isWorkflowsFeatureSelected ,
3155+ areReportFieldsEnabled : isReportsFeatureSelected ,
3156+ areConnectionsEnabled : isConnectionsFeatureSelected ,
3157+ arePerDiemRatesEnabled : isPerDiemFeatureSelected ,
3158+ isTravelEnabled : isTravelFeatureSelected ? sourcePolicy ?. isTravelEnabled : undefined ,
3159+ travelSettings : undefined ,
3160+ workspaceAccountID : undefined ,
3161+ tax : isTaxesFeatureSelected ? sourcePolicy ?. tax : undefined ,
3162+ employeeList : isMemberFeatureSelected ? employeeListWithoutPendingDelete : { [ sourcePolicy . owner ] : sourcePolicy ?. employeeList ?. [ sourcePolicy . owner ] } ,
3163+ id : duplicatedPolicyID ,
3164+ name : duplicatedPolicyName ,
3165+ fieldList : isReportsFeatureSelected ? fieldListWithoutPendingDelete : undefined ,
3166+ connections : isConnectionsFeatureSelected ? connectionsWithoutPendingDelete : undefined ,
3167+ customUnits : getCustomUnitsForDuplication ( sourcePolicy , isDistanceRatesFeatureSelected , isPerDiemFeatureSelected , {
3168+ distanceCustomUnitID : duplicatedDistanceCustomUnitID ,
3169+ perDiemCustomUnitID : duplicatedPerDiemCustomUnitID ,
3170+ } ) ,
3171+ taxRates : isTaxesFeatureSelected ? taxRatesWithoutPendingDelete : undefined ,
3172+ rules : isCodingRulesFeatureSelected ? { codingRules : codingRulesWithoutPendingDelete } : undefined ,
3173+ pendingAction : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3174+ pendingFields : {
3175+ autoReporting : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3176+ approvalMode : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3177+ reimbursementChoice : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3178+ name : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3179+ outputCurrency : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3180+ address : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3181+ description : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3182+ type : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3183+ areReportFieldsEnabled : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3184+ } ,
3185+ avatarURL : duplicatedPolicyFile ?. uri ,
3186+ originalFileName : duplicatedPolicyFile ?. name ,
3187+ outputCurrency : duplicatedOutputCurrency ,
3188+ address : isOverviewFeatureSelected ? sourcePolicy ?. address : undefined ,
3189+ } ;
3190+ }
3191+
31003192function buildDuplicatePolicyData ( policy : Policy , options : DuplicatePolicyDataOptions ) {
31013193 const { policyName = '' , policyID = generatePolicyID ( ) , file, welcomeNote, parts, targetPolicyID = generatePolicyID ( ) , policyCategories, localCurrency} = options ;
31023194
@@ -3112,19 +3204,8 @@ function buildDuplicatePolicyData(policy: Policy, options: DuplicatePolicyDataOp
31123204 pendingChatMembers,
31133205 } = ReportUtils . buildOptimisticWorkspaceChats ( targetPolicyID , policyName ) ;
31143206 const isMemberOptionSelected = parts ?. people ;
3115- const isReportsOptionSelected = parts ?. reports ;
3116- const isConnectionsOptionSelected = parts ?. connections ;
31173207 const isCategoriesOptionSelected = parts ?. categories ;
3118- const isTaxesOptionSelected = parts ?. taxes ;
3119- const isTagsOptionSelected = parts ?. tags ;
3120- const isInvoicesOptionSelected = parts ?. invoices ;
3121- const isDistanceRatesOptionSelected = parts ?. distance ;
3122- const isRulesOptionSelected = parts ?. expenses ;
3123- const isWorkflowsOptionSelected = parts ?. exportLayouts ;
3124- const isPerDiemOptionSelected = parts ?. perDiem ;
31253208 const isOverviewOptionSelected = parts ?. overview ;
3126- const isTravelOptionSelected = parts ?. travel ;
3127- const isCodingRulesOptionSelected = parts ?. codingRules ;
31283209
31293210 const outputCurrency = isOverviewOptionSelected ? policy ?. outputCurrency : localCurrency ;
31303211
@@ -3157,46 +3238,7 @@ function buildDuplicatePolicyData(policy: Policy, options: DuplicatePolicyDataOp
31573238 {
31583239 onyxMethod : Onyx . METHOD . SET ,
31593240 key : `${ ONYXKEYS . COLLECTION . POLICY } ${ targetPolicyID } ` ,
3160- value : {
3161- ...policy ,
3162- areCategoriesEnabled : true ,
3163- areTagsEnabled : isTagsOptionSelected ,
3164- areDistanceRatesEnabled : isDistanceRatesOptionSelected ,
3165- areInvoicesEnabled : isInvoicesOptionSelected ,
3166- areRulesEnabled : isRulesOptionSelected ,
3167- areWorkflowsEnabled : isWorkflowsOptionSelected ,
3168- areReportFieldsEnabled : isReportsOptionSelected ,
3169- areConnectionsEnabled : isConnectionsOptionSelected ,
3170- arePerDiemRatesEnabled : isPerDiemOptionSelected ,
3171- isTravelEnabled : isTravelOptionSelected ? policy ?. isTravelEnabled : undefined ,
3172- travelSettings : undefined ,
3173- workspaceAccountID : undefined ,
3174- tax : isTaxesOptionSelected ? policy ?. tax : undefined ,
3175- employeeList : isMemberOptionSelected ? policy . employeeList : { [ policy . owner ] : policy ?. employeeList ?. [ policy . owner ] } ,
3176- id : targetPolicyID ,
3177- name : policyName ,
3178- fieldList : isReportsOptionSelected ? policy ?. fieldList : undefined ,
3179- connections : isConnectionsOptionSelected ? policy ?. connections : undefined ,
3180- customUnits : getCustomUnitsForDuplication ( policy , isDistanceRatesOptionSelected , isPerDiemOptionSelected , { distanceCustomUnitID, perDiemCustomUnitID} ) ,
3181- taxRates : isTaxesOptionSelected ? policy ?. taxRates : undefined ,
3182- rules : isCodingRulesOptionSelected ? { codingRules : policy ?. rules ?. codingRules } : undefined ,
3183- pendingAction : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3184- pendingFields : {
3185- autoReporting : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3186- approvalMode : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3187- reimbursementChoice : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3188- name : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3189- outputCurrency : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3190- address : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3191- description : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3192- type : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3193- areReportFieldsEnabled : CONST . RED_BRICK_ROAD_PENDING_ACTION . ADD ,
3194- } ,
3195- avatarURL : file ?. uri ,
3196- originalFileName : file ?. name ,
3197- outputCurrency,
3198- address : isOverviewOptionSelected ? policy ?. address : undefined ,
3199- } ,
3241+ value : buildOptimisticDuplicatePolicy ( policy , { ...options , targetPolicyID} ) ,
32003242 } ,
32013243 {
32023244 onyxMethod : Onyx . METHOD . MERGE ,
0 commit comments