@@ -974,13 +974,7 @@ function clearAvatarErrors(reportID: string) {
974974 * @param parentReportActionID The parent report action that a thread was created from (only passed for new threads)
975975 * @param isFromDeepLink Whether or not this report is being opened from a deep link
976976 * @param participantAccountIDList The list of accountIDs that are included in a new chat, not including the user creating it
977- * @param avatar The avatar file to upload for a new group chat
978- * @param isNewThread Whether this is a new thread being created
979- * @param transaction The transaction object for legacy transactions that don't have a transaction thread or money request preview yet
980- * @param transactionViolations The violations for the transaction, if any
981- * @param parentReportID The parent report ID for the transaction thread (optional, defaults to transaction.reportID)
982977 */
983- // eslint-disable-next-line @typescript-eslint/max-params
984978function openReport (
985979 reportID : string | undefined ,
986980 reportActionID ?: string ,
@@ -990,10 +984,8 @@ function openReport(
990984 isFromDeepLink = false ,
991985 participantAccountIDList : number [ ] = [ ] ,
992986 avatar ?: File | CustomRNImageManipulatorResult ,
987+ transactionID ?: string ,
993988 isNewThread = false ,
994- transaction ?: Transaction ,
995- transactionViolations ?: TransactionViolations ,
996- parentReportID ?: string ,
997989) {
998990 if ( ! reportID ) {
999991 return ;
@@ -1074,92 +1066,59 @@ function openReport(
10741066 emailList : participantLoginList ? participantLoginList . join ( ',' ) : '' ,
10751067 accountIDList : participantAccountIDList ? participantAccountIDList . join ( ',' ) : '' ,
10761068 parentReportActionID,
1077- transactionID : transaction ?. transactionID ,
1069+ transactionID,
10781070 } ;
10791071
1080- // This is a legacy transaction that doesn't have either a transaction thread or a money request preview
1081- if ( transaction && ! parentReportActionID ) {
1082- const transactionParentReportID = parentReportID ?? transaction ?. reportID ;
1083- const iouReportActionID = rand64 ( ) ;
1084-
1085- const optimisticIOUAction = buildOptimisticIOUReportAction ( {
1086- type : CONST . IOU . REPORT_ACTION_TYPE . CREATE ,
1087- amount : Math . abs ( transaction . amount ) ,
1088- currency : transaction . currency ,
1089- comment : transaction . comment ?. comment ?? '' ,
1090- participants : [ { accountID : currentUserAccountID , login : currentUserEmail ?? '' } ] ,
1091- transactionID : transaction . transactionID ,
1092- isOwnPolicyExpenseChat : true ,
1093- reportActionID : iouReportActionID ,
1094- iouReportID : transactionParentReportID ,
1095- } ) ;
1096-
1097- // We have a case where the transaction data is only available from the snapshot
1098- // So we need to add the transaction data to Onyx so it's available when opening the report
1099- optimisticData . push ( {
1100- onyxMethod : Onyx . METHOD . MERGE ,
1101- key : `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transaction . transactionID } ` ,
1102- value : transaction ,
1103- } ) ;
1104-
1105- // Add violations if they exist. This is needed when opening from search results where
1106- // violations are in the snapshot but not yet synced to the main collections, so we need
1107- // to add them to Onyx to ensure they show up in the transaction thread
1108- if ( transactionViolations ) {
1109- optimisticData . push ( {
1110- onyxMethod : Onyx . METHOD . MERGE ,
1111- key : `${ ONYXKEYS . COLLECTION . TRANSACTION_VIOLATIONS } ${ transaction . transactionID } ` ,
1112- value : transactionViolations ,
1113- } ) ;
1114- }
1072+ // This is a legacy transactions that doesn't have either a transaction thread or a money request preview
1073+ if ( transactionID && ! parentReportActionID ) {
1074+ const transaction = allTransactions ?. [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transactionID } ` ] ;
1075+
1076+ if ( transaction ) {
1077+ const selfDMReportID = findSelfDMReportID ( ) ;
1078+
1079+ if ( selfDMReportID ) {
1080+ const generatedReportActionID = rand64 ( ) ;
1081+ const optimisticParentAction = buildOptimisticIOUReportAction ( {
1082+ type : CONST . IOU . REPORT_ACTION_TYPE . CREATE ,
1083+ amount : Math . abs ( transaction . amount ) ,
1084+ currency : transaction . currency ,
1085+ comment : transaction . comment ?. comment ?? '' ,
1086+ participants : [ { accountID : currentUserAccountID , login : currentUserEmail ?? '' } ] ,
1087+ transactionID,
1088+ isOwnPolicyExpenseChat : true ,
1089+ } ) ;
11151090
1116- // Attach the optimistic IOU report action created for the transaction to the transaction thread
1117- optimisticData . push ( {
1118- onyxMethod : Onyx . METHOD . MERGE ,
1119- key : ` ${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ,
1120- value : {
1121- parentReportActionID : iouReportActionID ,
1122- } ,
1123- } ) ;
1091+ optimisticData . push ( {
1092+ onyxMethod : Onyx . METHOD . MERGE ,
1093+ key : ` ${ ONYXKEYS . COLLECTION . REPORT } ${ reportID } ` ,
1094+ value : {
1095+ parentReportID : selfDMReportID ,
1096+ parentReportActionID : generatedReportActionID ,
1097+ } ,
1098+ } ) ;
11241099
1125- optimisticData . push ( {
1126- onyxMethod : Onyx . METHOD . MERGE ,
1127- key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ transactionParentReportID } ` ,
1128- value : {
1129- [ iouReportActionID ] : {
1130- ...optimisticIOUAction ,
1131- childReportID : reportID ,
1132- } ,
1133- } ,
1134- } ) ;
1100+ // Log how often the legacy transaction fallback path is taken
1101+ Log . info ( '[Report] Legacy transaction fallback: creating money request preview in self DM' , false , {
1102+ selfDMReportID,
1103+ transactionID,
1104+ reportID,
1105+ } ) ;
11351106
1136- // Update the snapshot with the new transactionThreadReportID and moneyRequestReportActionID if we're coming from search
1137- // preventing duplicate reportActionID when moneyRequestReportActionID still empty
1138- const currentSearchQueryJSON = getCurrentSearchQueryJSON ( ) ;
1139- if ( currentSearchQueryJSON ?. hash ) {
1140- // @ts -expect-error - will be solved in https://github.com/Expensify/App/issues/73830
1141- optimisticData . push ( {
1142- onyxMethod : Onyx . METHOD . MERGE ,
1143- key : `${ ONYXKEYS . COLLECTION . SNAPSHOT } ${ currentSearchQueryJSON . hash } ` ,
1144- value : {
1145- data : {
1146- [ `${ ONYXKEYS . COLLECTION . TRANSACTION } ${ transaction . transactionID } ` ] : {
1147- transactionThreadReportID : reportID ,
1148- moneyRequestReportActionID : iouReportActionID ,
1107+ optimisticData . push ( {
1108+ onyxMethod : Onyx . METHOD . MERGE ,
1109+ key : `${ ONYXKEYS . COLLECTION . REPORT_ACTIONS } ${ selfDMReportID } ` ,
1110+ value : {
1111+ [ generatedReportActionID ] : {
1112+ ...optimisticParentAction ,
1113+ reportActionID : generatedReportActionID ,
1114+ childReportID : reportID ,
11491115 } ,
11501116 } ,
1151- } ,
1152- } ) ;
1153- }
1154-
1155- parameters . moneyRequestPreviewReportActionID = iouReportActionID ;
1117+ } ) ;
11561118
1157- // Log how often the legacy transaction fallback path is taken
1158- Log . info ( '[Report] Legacy transaction fallback: creating money request preview' , false , {
1159- transactionParentReportID,
1160- transactionID : transaction . transactionID ,
1161- reportID,
1162- } ) ;
1119+ parameters . moneyRequestPreviewReportActionID = generatedReportActionID ;
1120+ }
1121+ }
11631122 }
11641123
11651124 const isInviteOnboardingComplete = introSelected ?. isInviteOnboardingComplete ?? false ;
@@ -1396,20 +1355,16 @@ function getOptimisticChatReport(accountID: number): OptimisticChatReport {
13961355 } ) ;
13971356}
13981357
1399- function createTransactionThreadReport (
1400- iouReport ?: OnyxEntry < Report > ,
1401- iouReportAction ?: OnyxEntry < ReportAction > ,
1402- transaction ?: Transaction ,
1403- transactionViolations ?: TransactionViolations ,
1404- ) : OptimisticChatReport | undefined {
1405- // Determine if we need selfDM report (for track expenses or unreported transactions)
1406- const isTrackExpense = ! iouReport && ReportActionsUtils . isTrackExpenseAction ( iouReportAction ) ;
1407- const isUnreportedTransaction = transaction ?. reportID === CONST . REPORT . UNREPORTED_REPORT_ID ;
1408- const selfDMReportID = isTrackExpense || isUnreportedTransaction ? findSelfDMReportID ( ) : undefined ;
1358+ function createTransactionThreadReport ( iouReport : OnyxEntry < Report > , iouReportAction : OnyxEntry < ReportAction > ) : OptimisticChatReport | undefined {
1359+ if ( ! iouReportAction ) {
1360+ Log . warn ( 'Cannot build transaction thread report without iouReportAction parameter' ) ;
1361+ return ;
1362+ }
14091363
14101364 let reportToUse = iouReport ;
14111365 // For track expenses without iouReport, get the selfDM report
1412- if ( isTrackExpense && selfDMReportID ) {
1366+ if ( ! iouReport && ReportActionsUtils . isTrackExpenseAction ( iouReportAction ) ) {
1367+ const selfDMReportID = findSelfDMReportID ( ) ;
14131368 reportToUse = allReports ?. [ `${ ONYXKEYS . COLLECTION . REPORT } ${ selfDMReportID } ` ] ;
14141369 }
14151370
@@ -1418,29 +1373,9 @@ function createTransactionThreadReport(
14181373 return ;
14191374 }
14201375
1421- // Sync fresh report data from snapshot to allReports. When navigating from search,
1422- // allReports may be stale and missing parentReportID/parentReportActionID, causing
1423- // getAllAncestorReportActionIDs() to fail when adding comments optimistically.
1424- if ( iouReport ?. reportID && reportToUse . reportID === iouReport . reportID ) {
1425- Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT } ${ iouReport . reportID } ` , iouReport ) ;
1426- }
1427-
14281376 const optimisticTransactionThreadReportID = generateReportID ( ) ;
14291377 const optimisticTransactionThread = buildTransactionThread ( iouReportAction , reportToUse , undefined , optimisticTransactionThreadReportID ) ;
1430- openReport (
1431- optimisticTransactionThreadReportID ,
1432- undefined ,
1433- currentUserEmail ? [ currentUserEmail ] : [ ] ,
1434- optimisticTransactionThread ,
1435- iouReportAction ?. reportActionID ,
1436- false ,
1437- [ ] ,
1438- undefined ,
1439- false ,
1440- transaction ,
1441- transactionViolations ,
1442- selfDMReportID ,
1443- ) ;
1378+ openReport ( optimisticTransactionThreadReportID , undefined , currentUserEmail ? [ currentUserEmail ] : [ ] , optimisticTransactionThread , iouReportAction ?. reportActionID ) ;
14441379 return optimisticTransactionThread ;
14451380}
14461381
@@ -1555,7 +1490,7 @@ function navigateToAndOpenChildReport(childReportID: string | undefined, parentR
15551490
15561491 if ( ! childReportID ) {
15571492 const participantLogins = PersonalDetailsUtils . getLoginsByAccountIDs ( Object . keys ( newChat . participants ?? { } ) . map ( Number ) ) ;
1558- openReport ( newChat . reportID , '' , participantLogins , newChat , parentReportAction . reportActionID , undefined , undefined , undefined , true ) ;
1493+ openReport ( newChat . reportID , '' , participantLogins , newChat , parentReportAction . reportActionID , undefined , undefined , undefined , undefined , true ) ;
15591494 } else {
15601495 Onyx . merge ( `${ ONYXKEYS . COLLECTION . REPORT } ${ childReportID } ` , newChat ) ;
15611496 }
0 commit comments