@@ -5,7 +5,7 @@ import {calculateDefaultReimbursable, navigateToConfirmationPage, navigateToPart
55import Log from '@libs/Log' ;
66import Navigation from '@libs/Navigation/Navigation' ;
77import { getManagerMcTestParticipant , getParticipantsOption , getReportOption } from '@libs/OptionsListUtils' ;
8- import { findSelfDMReportID , generateReportID , getPolicyExpenseChat } from '@libs/ReportUtils' ;
8+ import { generateReportID , getPolicyExpenseChat , isSelfDM } from '@libs/ReportUtils' ;
99import type { OptionData } from '@libs/ReportUtils' ;
1010import shouldUseDefaultExpensePolicy from '@libs/shouldUseDefaultExpensePolicy' ;
1111import { getValidWaypoints } from '@libs/TransactionUtils' ;
@@ -101,6 +101,7 @@ type MoneyRequestStepScanParticipantsFlowParams = {
101101 isTestTransaction ?: boolean ;
102102 locationPermissionGranted ?: boolean ;
103103 shouldGenerateTransactionThreadReport : boolean ;
104+ selfDMReport : OnyxEntry < Report > ;
104105 isSelfTourViewed : boolean ;
105106 betas : OnyxEntry < Beta [ ] > ;
106107} ;
@@ -136,6 +137,7 @@ type MoneyRequestStepDistanceNavigationParams = {
136137 introSelected ?: IntroSelected ;
137138 activePolicyID ?: string ;
138139 privateIsArchived ?: string ;
140+ selfDMReport : OnyxEntry < Report > ;
139141 gpsCoordinates ?: string ;
140142 gpsDistance ?: number ;
141143 betas : OnyxEntry < Beta [ ] > ;
@@ -284,6 +286,7 @@ function handleMoneyRequestStepScanParticipants({
284286 files,
285287 isTestTransaction = false ,
286288 locationPermissionGranted = false ,
289+ selfDMReport,
287290 isSelfTourViewed,
288291 betas,
289292} : MoneyRequestStepScanParticipantsFlowParams ) {
@@ -452,19 +455,21 @@ function handleMoneyRequestStepScanParticipants({
452455 // If there was no reportID, then that means the user started this flow from the global + menu
453456 // and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense.
454457 if ( shouldUseDefaultExpensePolicy ( iouType , defaultExpensePolicy ) ) {
455- const activePolicyExpenseChat = getPolicyExpenseChat ( currentUserAccountID , defaultExpensePolicy ?. id ) ;
456458 const shouldAutoReport = ! ! defaultExpensePolicy ?. autoReporting || isAutoReporting ;
457- const transactionReportID = shouldAutoReport ? activePolicyExpenseChat ?. reportID : CONST . REPORT . UNREPORTED_REPORT_ID ;
459+ const targetReport = shouldAutoReport ? getPolicyExpenseChat ( currentUserAccountID , defaultExpensePolicy ?. id ) : selfDMReport ;
460+ const transactionReportID = isSelfDM ( targetReport ) ? CONST . REPORT . UNREPORTED_REPORT_ID : targetReport ?. reportID ;
461+ const iouTypeTrackOrSubmit = transactionReportID === CONST . REPORT . UNREPORTED_REPORT_ID ? CONST . IOU . TYPE . TRACK : CONST . IOU . TYPE . SUBMIT ;
458462
459463 // If the initial transaction has different participants selected that means that the user has changed the participant in the confirmation step
460- if ( initialTransaction ?. participants && initialTransaction ?. participants ?. at ( 0 ) ?. reportID !== activePolicyExpenseChat ?. reportID ) {
461- const selfDMReportID = findSelfDMReportID ( ) ;
462- const isTrackExpense = initialTransaction ?. participants ?. at ( 0 ) ?. reportID === selfDMReportID ;
464+ if ( initialTransaction ?. participants && initialTransaction ?. participants ?. at ( 0 ) ?. reportID !== targetReport ?. reportID ) {
465+ const isTrackExpense = initialTransaction ?. participants ?. at ( 0 ) ?. reportID === selfDMReport ?. reportID ;
463466
464467 const setParticipantsPromises = files . map ( ( receiptFile ) => setMoneyRequestParticipants ( receiptFile . transactionID , initialTransaction ?. participants ) ) ;
465468 Promise . all ( setParticipantsPromises ) . then ( ( ) => {
466469 if ( isTrackExpense ) {
467- Navigation . navigate ( ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( CONST . IOU . ACTION . CREATE , CONST . IOU . TYPE . TRACK , initialTransaction . transactionID , selfDMReportID ) ) ;
470+ Navigation . navigate (
471+ ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( CONST . IOU . ACTION . CREATE , CONST . IOU . TYPE . TRACK , initialTransaction . transactionID , selfDMReport ?. reportID ) ,
472+ ) ;
468473 } else {
469474 navigateToConfirmationPage ( iouType , initialTransaction . transactionID , reportID , backToReport , iouType === CONST . IOU . TYPE . CREATE , initialTransaction ?. reportID ) ;
470475 }
@@ -474,17 +479,10 @@ function handleMoneyRequestStepScanParticipants({
474479
475480 const setParticipantsPromises = files . map ( ( receiptFile ) => {
476481 setTransactionReport ( receiptFile . transactionID , { reportID : transactionReportID } , true ) ;
477- return setMoneyRequestParticipantsFromReport ( receiptFile . transactionID , activePolicyExpenseChat , currentUserAccountID ) ;
482+ return setMoneyRequestParticipantsFromReport ( receiptFile . transactionID , targetReport , currentUserAccountID ) ;
478483 } ) ;
479484 Promise . all ( setParticipantsPromises ) . then ( ( ) =>
480- Navigation . navigate (
481- ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute (
482- CONST . IOU . ACTION . CREATE ,
483- iouType === CONST . IOU . TYPE . CREATE ? CONST . IOU . TYPE . SUBMIT : iouType ,
484- initialTransaction . transactionID ,
485- activePolicyExpenseChat ?. reportID ,
486- ) ,
487- ) ,
485+ Navigation . navigate ( ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( CONST . IOU . ACTION . CREATE , iouTypeTrackOrSubmit , initialTransaction . transactionID , targetReport ?. reportID ) ) ,
488486 ) ;
489487 } else {
490488 navigateToParticipantPage ( iouType , initialTransaction . transactionID , reportID ) ;
@@ -521,6 +519,7 @@ function handleMoneyRequestStepDistanceNavigation({
521519 introSelected,
522520 activePolicyID,
523521 privateIsArchived,
522+ selfDMReport,
524523 gpsCoordinates,
525524 gpsDistance,
526525 policyForMovingExpenses,
@@ -650,9 +649,11 @@ function handleMoneyRequestStepDistanceNavigation({
650649 // If there was no reportID, then that means the user started this flow from the global menu
651650 // and an optimistic reportID was generated. In that case, the next step is to select the participants for this expense.
652651 if ( defaultExpensePolicy && shouldUseDefaultExpensePolicy ( iouType , defaultExpensePolicy ) ) {
653- const activePolicyExpenseChat = getPolicyExpenseChat ( currentUserAccountID , defaultExpensePolicy ?. id ) ;
654652 const shouldAutoReport = ! ! defaultExpensePolicy ?. autoReporting || isAutoReporting ;
655- const transactionReportID = shouldAutoReport ? activePolicyExpenseChat ?. reportID : CONST . REPORT . UNREPORTED_REPORT_ID ;
653+ const targetReport = shouldAutoReport ? getPolicyExpenseChat ( currentUserAccountID , defaultExpensePolicy ?. id ) : selfDMReport ;
654+ const transactionReportID = isSelfDM ( targetReport ) ? CONST . REPORT . UNREPORTED_REPORT_ID : targetReport ?. reportID ;
655+ const iouTypeTrackOrSubmit = transactionReportID === CONST . REPORT . UNREPORTED_REPORT_ID ? CONST . IOU . TYPE . TRACK : CONST . IOU . TYPE . SUBMIT ;
656+
656657 const rateID = DistanceRequestUtils . getCustomUnitRateID ( {
657658 reportID : transactionReportID ,
658659 isPolicyExpenseChat : true ,
@@ -661,15 +662,8 @@ function handleMoneyRequestStepDistanceNavigation({
661662 } ) ;
662663 setTransactionReport ( transactionID , { reportID : transactionReportID } , true ) ;
663664 setCustomUnitRateID ( transactionID , rateID ) ;
664- setMoneyRequestParticipantsFromReport ( transactionID , activePolicyExpenseChat , currentUserAccountID ) . then ( ( ) => {
665- Navigation . navigate (
666- ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute (
667- CONST . IOU . ACTION . CREATE ,
668- iouType === CONST . IOU . TYPE . CREATE ? CONST . IOU . TYPE . SUBMIT : iouType ,
669- transactionID ,
670- activePolicyExpenseChat ?. reportID ,
671- ) ,
672- ) ;
665+ setMoneyRequestParticipantsFromReport ( transactionID , targetReport , currentUserAccountID ) . then ( ( ) => {
666+ Navigation . navigate ( ROUTES . MONEY_REQUEST_STEP_CONFIRMATION . getRoute ( CONST . IOU . ACTION . CREATE , iouTypeTrackOrSubmit , transactionID , targetReport ?. reportID ) ) ;
673667 } ) ;
674668 } else {
675669 navigateToParticipantPage ( iouType , transactionID , reportID ) ;
0 commit comments