@@ -5,18 +5,20 @@ import type {EmitterSubscription, GestureResponderEvent, View} from 'react-nativ
55import AddPaymentMethodMenu from '@components/AddPaymentMethodMenu' ;
66import useOnyx from '@hooks/useOnyx' ;
77import { openPersonalBankAccountSetupView } from '@libs/actions/BankAccounts' ;
8- import { completePaymentOnboarding } from '@libs/actions/IOU' ;
8+ import { completePaymentOnboarding , savePreferredPaymentMethod } from '@libs/actions/IOU' ;
9+ import { moveIOUReportToPolicy , moveIOUReportToPolicyAndInviteSubmitter } from '@libs/actions/Report' ;
910import getClickedTargetLocation from '@libs/getClickedTargetLocation' ;
1011import Log from '@libs/Log' ;
1112import Navigation from '@libs/Navigation/Navigation' ;
1213import { hasExpensifyPaymentMethod } from '@libs/PaymentUtils' ;
13- import { isExpenseReport as isExpenseReportReportUtils , isIOUReport } from '@libs/ReportUtils' ;
14+ import { getPolicyExpenseChat , isExpenseReport as isExpenseReportReportUtils , isIOUReport } from '@libs/ReportUtils' ;
1415import { kycWallRef } from '@userActions/PaymentMethods' ;
1516import { createWorkspaceFromIOUPayment } from '@userActions/Policy/Policy' ;
1617import { setKYCWallSource } from '@userActions/Wallet' ;
1718import CONST from '@src/CONST' ;
1819import ONYXKEYS from '@src/ONYXKEYS' ;
1920import ROUTES from '@src/ROUTES' ;
21+ import type { Policy } from '@src/types/onyx' ;
2022import type { PaymentMethodType } from '@src/types/onyx/OriginalMessage' ;
2123import viewRef from '@src/types/utils/viewRef' ;
2224import type { AnchorPosition , DomRect , KYCWallProps , PaymentMethod } from './types' ;
@@ -45,11 +47,11 @@ function KYCWall({
4547 source,
4648 shouldShowPersonalBankAccountOption = false ,
4749} : KYCWallProps ) {
48- const [ userWallet ] = useOnyx ( ONYXKEYS . USER_WALLET ) ;
49- const [ walletTerms ] = useOnyx ( ONYXKEYS . WALLET_TERMS ) ;
50- const [ fundList ] = useOnyx ( ONYXKEYS . FUND_LIST ) ;
51- const [ bankAccountList = { } ] = useOnyx ( ONYXKEYS . BANK_ACCOUNT_LIST ) ;
52- const [ reimbursementAccount ] = useOnyx ( ONYXKEYS . REIMBURSEMENT_ACCOUNT ) ;
50+ const [ userWallet ] = useOnyx ( ONYXKEYS . USER_WALLET , { canBeMissing : true } ) ;
51+ const [ walletTerms ] = useOnyx ( ONYXKEYS . WALLET_TERMS , { canBeMissing : true } ) ;
52+ const [ fundList ] = useOnyx ( ONYXKEYS . FUND_LIST , { canBeMissing : true } ) ;
53+ const [ bankAccountList = { } ] = useOnyx ( ONYXKEYS . BANK_ACCOUNT_LIST , { canBeMissing : true } ) ;
54+ const [ reimbursementAccount ] = useOnyx ( ONYXKEYS . REIMBURSEMENT_ACCOUNT , { canBeMissing : true } ) ;
5355
5456 const anchorRef = useRef < HTMLDivElement | View > ( null ) ;
5557 const transferBalanceButtonRef = useRef < HTMLDivElement | View | null > ( null ) ;
@@ -100,24 +102,48 @@ function KYCWall({
100102 } , [ getAnchorPosition ] ) ;
101103
102104 const selectPaymentMethod = useCallback (
103- ( paymentMethod : PaymentMethod ) => {
104- onSelectPaymentMethod ( paymentMethod ) ;
105+ ( paymentMethod ?: PaymentMethod , policy ?: Policy ) => {
106+ if ( paymentMethod ) {
107+ onSelectPaymentMethod ( paymentMethod ) ;
108+ }
105109
106110 if ( paymentMethod === CONST . PAYMENT_METHODS . PERSONAL_BANK_ACCOUNT ) {
107111 openPersonalBankAccountSetupView ( ) ;
108112 } else if ( paymentMethod === CONST . PAYMENT_METHODS . DEBIT_CARD ) {
109113 Navigation . navigate ( addDebitCardRoute ?? ROUTES . HOME ) ;
110- } else if ( paymentMethod === CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT ) {
114+ } else if ( paymentMethod === CONST . PAYMENT_METHODS . BUSINESS_BANK_ACCOUNT || policy ) {
111115 if ( iouReport && isIOUReport ( iouReport ) ) {
116+ if ( policy ) {
117+ const policyExpenseChatReportID = getPolicyExpenseChat ( iouReport . ownerAccountID , policy . id ) ?. reportID ;
118+ if ( ! policyExpenseChatReportID ) {
119+ const { policyExpenseChatReportID : newPolicyExpenseChatReportID } = moveIOUReportToPolicyAndInviteSubmitter ( iouReport . reportID , policy . id ) ?? { } ;
120+ savePreferredPaymentMethod ( iouReport . policyID , policy . id , CONST . LAST_PAYMENT_METHOD . IOU ) ;
121+ Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( newPolicyExpenseChatReportID ) ) ;
122+ } else {
123+ moveIOUReportToPolicy ( iouReport . reportID , policy . id , true ) ;
124+ savePreferredPaymentMethod ( iouReport . policyID , policy . id , CONST . LAST_PAYMENT_METHOD . IOU ) ;
125+ Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( policyExpenseChatReportID ) ) ;
126+ }
127+
128+ if ( policy ?. achAccount ) {
129+ return ;
130+ }
131+ // Navigate to the bank account set up flow for this specific policy
132+ Navigation . navigate ( ROUTES . BANK_ACCOUNT_WITH_STEP_TO_OPEN . getRoute ( policy . id ) ) ;
133+ return ;
134+ }
135+
112136 const { policyID, workspaceChatReportID, reportPreviewReportActionID, adminsChatReportID} = createWorkspaceFromIOUPayment ( iouReport ) ?? { } ;
137+ if ( policyID ) {
138+ savePreferredPaymentMethod ( iouReport . policyID , policyID , CONST . LAST_PAYMENT_METHOD . IOU ) ;
139+ }
113140 completePaymentOnboarding ( CONST . PAYMENT_SELECTED . BBA , adminsChatReportID , policyID ) ;
114141 if ( workspaceChatReportID ) {
115142 Navigation . navigate ( ROUTES . REPORT_WITH_ID . getRoute ( workspaceChatReportID , reportPreviewReportActionID ) ) ;
116143 }
117144
118145 // Navigate to the bank account set up flow for this specific policy
119146 Navigation . navigate ( ROUTES . BANK_ACCOUNT_WITH_STEP_TO_OPEN . getRoute ( policyID ) ) ;
120-
121147 return ;
122148 }
123149 Navigation . navigate ( addBankAccountRoute ) ;
@@ -133,7 +159,7 @@ function KYCWall({
133159 *
134160 */
135161 const continueAction = useCallback (
136- ( event ?: GestureResponderEvent | KeyboardEvent , iouPaymentType ?: PaymentMethodType ) => {
162+ ( event ?: GestureResponderEvent | KeyboardEvent , iouPaymentType ?: PaymentMethodType , paymentMethod ?: PaymentMethod , policy ?: Policy ) => {
137163 const currentSource = walletTerms ?. source ?? source ;
138164
139165 /**
@@ -170,6 +196,13 @@ function KYCWall({
170196 const clickedElementLocation = getClickedTargetLocation ( targetElement as HTMLDivElement ) ;
171197 const position = getAnchorPosition ( clickedElementLocation ) ;
172198
199+ // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
200+ if ( paymentMethod || policy ) {
201+ setShouldShowAddPaymentMenu ( false ) ;
202+ selectPaymentMethod ( paymentMethod , policy ) ;
203+ return ;
204+ }
205+
173206 setPositionAddPaymentMenu ( position ) ;
174207 setShouldShowAddPaymentMenu ( true ) ;
175208
0 commit comments