@@ -5207,7 +5207,11 @@ function deleteAppReport(reportID: string | undefined) {
52075207 * @param policyID - The ID of the policy to move the report to
52085208 * @param isFromSettlementButton - Whether the action is from report preview
52095209 */
5210- function moveIOUReportToPolicy ( reportID : string , policyID : string , isFromSettlementButton ?: boolean ) {
5210+ function moveIOUReportToPolicy (
5211+ reportID : string ,
5212+ policyID : string ,
5213+ isFromSettlementButton ?: boolean ,
5214+ ) : { policyExpenseChatReportID ?: string ; useTemporaryOptimisticExpenseChatReportID : boolean } | undefined {
52115215 const iouReport = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ] ;
52125216 // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850
52135217 // eslint-disable-next-line deprecation/deprecation
@@ -5229,10 +5233,8 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
52295233 const iouReportID = iouReport . reportID ;
52305234 const employeeAccountID = iouReport . ownerAccountID ;
52315235 const expenseChatReportId = getPolicyExpenseChat ( employeeAccountID , policyID ) ?. reportID ;
5232-
5233- if ( ! expenseChatReportId ) {
5234- return ;
5235- }
5236+ const useTemporaryOptimisticExpenseChatReportID = ! expenseChatReportId ;
5237+ const optimisticExpenseChatReportID = expenseChatReportId ?? generateReportID ( ) ;
52365238
52375239 const optimisticData : OnyxUpdate [ ] = [ ] ;
52385240
@@ -5300,7 +5302,7 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53005302 } ) ;
53015303
53025304 // We need to move the report preview action from the DM to the expense chat.
5303- const parentReportActions = allReportActions ?. [ ` ${ iouReport . parentReportID } ` ] ;
5305+ const parentReportActions = iouReport ?. parentReportID ? allReportActions ?. [ iouReport . parentReportID ] : undefined ;
53045306 const parentReportActionID = iouReport . parentReportActionID ;
53055307 const reportPreview = iouReport ?. parentReportID && parentReportActionID ? parentReportActions ?. [ parentReportActionID ] : undefined ;
53065308 const oldChatReportID = iouReport . chatReportID ;
@@ -5320,18 +5322,18 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53205322 // Add the reportPreview action to expense chat
53215323 optimisticData . push ( {
53225324 onyxMethod : Onyx . METHOD . MERGE ,
5323- key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ expenseChatReportId } ` ,
5325+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ optimisticExpenseChatReportID } ` ,
53245326 value : { [ reportPreview . reportActionID ] : { ...reportPreview , childReportName : expenseReport . reportName , created : DateUtils . getDBTime ( ) } } ,
53255327 } ) ;
53265328 failureData . push ( {
53275329 onyxMethod : Onyx . METHOD . MERGE ,
5328- key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ expenseChatReportId } ` ,
5330+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ optimisticExpenseChatReportID } ` ,
53295331 value : { [ reportPreview . reportActionID ] : null } ,
53305332 } ) ;
53315333 }
53325334
53335335 // Create MOVED report action and add it to the expense report which indicates to the user where the report has been moved
5334- const movedExpenseReportAction = buildOptimisticMovedReportAction ( iouReport . policyID , policyID , expenseChatReportId , iouReportID , policyName , true ) ;
5336+ const movedExpenseReportAction = buildOptimisticMovedReportAction ( iouReport . policyID , policyID , expenseChatReportId ?? '' , iouReportID , policyName , true ) ;
53355337 optimisticData . push ( {
53365338 onyxMethod : Onyx . METHOD . MERGE ,
53375339 key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ reportID } ` ,
@@ -5372,7 +5374,7 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53725374 } ) ;
53735375
53745376 // Create the MOVED report action and add it to the DM chat which indicates to the user where the report has been moved
5375- const movedReportAction = buildOptimisticMovedReportAction ( iouReport . policyID , policyID , expenseChatReportId , iouReportID , policyName ) ;
5377+ const movedReportAction = buildOptimisticMovedReportAction ( iouReport . policyID , policyID , optimisticExpenseChatReportID , iouReportID , policyName ) ;
53765378 optimisticData . push ( {
53775379 onyxMethod : Onyx . METHOD . MERGE ,
53785380 key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ oldChatReportID } ` ,
@@ -5389,10 +5391,11 @@ function moveIOUReportToPolicy(reportID: string, policyID: string, isFromSettlem
53895391 policyID,
53905392 changePolicyReportActionID : movedExpenseReportAction . reportActionID ,
53915393 dmMovedReportActionID : movedReportAction . reportActionID ,
5392- optimisticReportID : expenseChatReportId ,
5394+ optimisticReportID : optimisticExpenseChatReportID ,
53935395 } ;
53945396
53955397 API . write ( WRITE_COMMANDS . MOVE_IOU_REPORT_TO_EXISTING_POLICY , parameters , { optimisticData, successData, failureData} ) ;
5398+ return { policyExpenseChatReportID : optimisticExpenseChatReportID , useTemporaryOptimisticExpenseChatReportID} ;
53965399}
53975400
53985401/**
0 commit comments