@@ -22,6 +22,7 @@ import {
2222 buildOptimisticUnHoldReportAction ,
2323 buildOptimisticUnreportedTransactionAction ,
2424 buildTransactionThread ,
25+ findSelfDMReportID ,
2526 getReportTransactions ,
2627 getTransactionDetails ,
2728 hasViolations as hasViolationsReportUtils ,
@@ -700,7 +701,6 @@ function changeTransactionsReport(
700701 policy ?: OnyxEntry < Policy > ,
701702 reportNextStep ?: OnyxEntry < ReportNextStepDeprecated > ,
702703 policyCategories ?: OnyxEntry < PolicyCategories > ,
703- selfDMReportIDParam ?: string ,
704704) {
705705 const reportID = newReport ?. reportID ?? CONST . REPORT . UNREPORTED_REPORT_ID ;
706706
@@ -720,16 +720,15 @@ function changeTransactionsReport(
720720 const failureData : OnyxUpdate [ ] = [ ] ;
721721 const successData : OnyxUpdate [ ] = [ ] ;
722722
723- let selfDMReportID = selfDMReportIDParam ;
724- let selfDMReport : Report | undefined = allReports ?. [ ` ${ ONYXKEYS . COLLECTION . REPORT } ${ selfDMReportID } ` ] ;
723+ const existingSelfDMReportID = findSelfDMReportID ( ) ;
724+ let selfDMReport : Report | undefined ;
725725 let selfDMCreatedReportAction : ReportAction | undefined ;
726726 const currentUserAccountID = getCurrentUserAccountID ( ) ;
727727
728- if ( ! selfDMReport && reportID === CONST . REPORT . UNREPORTED_REPORT_ID ) {
728+ if ( ! existingSelfDMReportID && reportID === CONST . REPORT . UNREPORTED_REPORT_ID ) {
729729 const currentTime = DateUtils . getDBTime ( ) ;
730730 selfDMReport = buildOptimisticSelfDMReport ( currentTime ) ;
731731 selfDMCreatedReportAction = buildOptimisticCreatedReportAction ( email ?? '' , currentTime ) ;
732- selfDMReportID = selfDMReport . reportID ;
733732
734733 // Add optimistic updates for self DM report
735734 optimisticData . push (
@@ -812,6 +811,8 @@ function changeTransactionsReport(
812811 for ( const transaction of transactions ) {
813812 const isUnreportedExpense = ! transaction . reportID || transaction . reportID === CONST . REPORT . UNREPORTED_REPORT_ID ;
814813
814+ const selfDMReportID = existingSelfDMReportID ?? selfDMReport ?. reportID ;
815+
815816 const oldIOUAction = getIOUActionForReportID ( isUnreportedExpense ? selfDMReportID : transaction . reportID , transaction . transactionID ) ;
816817 if ( ! transaction . reportID || transaction . reportID === reportID ) {
817818 continue ;
@@ -1178,11 +1179,11 @@ function changeTransactionsReport(
11781179 : { } ) ,
11791180 } ;
11801181
1181- if ( reportID === CONST . REPORT . UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction ) {
1182+ if ( ! existingSelfDMReportID && reportID === CONST . REPORT . UNREPORTED_REPORT_ID && selfDMReport && selfDMCreatedReportAction ) {
11821183 // Add self DM data to transaction data
11831184 transactionIDToReportActionAndThreadData [ transaction . transactionID ] = {
11841185 ...baseTransactionData ,
1185- selfDMReportID,
1186+ selfDMReportID : selfDMReport . reportID ,
11861187 selfDMCreatedReportActionID : selfDMCreatedReportAction . reportActionID ,
11871188 } ;
11881189 } else {
@@ -1294,7 +1295,7 @@ function changeTransactionsReport(
12941295 }
12951296
12961297 // 9. Update next step for report
1297- const destinationReportID = reportID === CONST . REPORT . UNREPORTED_REPORT_ID ? selfDMReportID : reportID ;
1298+ const destinationReportID = reportID === CONST . REPORT . UNREPORTED_REPORT_ID ? ( existingSelfDMReportID ?? selfDMReport ?. reportID ) : reportID ;
12981299 const destinationReport = destinationReportID
12991300 ? ( allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ destinationReportID } ` ] ??
13001301 ( destinationReportID === newReport ?. reportID ? newReport : undefined ) ??
0 commit comments