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