Skip to content

Commit

Permalink
check vbba
Browse files Browse the repository at this point in the history
  • Loading branch information
luacmartins committed Nov 15, 2024
1 parent 63b164a commit 2805d3b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
35 changes: 30 additions & 5 deletions src/libs/actions/Search.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Onyx from 'react-native-onyx';
import type {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import type {FormOnyxValues} from '@components/Form/types';
import type {PaymentData, SearchQueryJSON} from '@components/Search/types';
Expand All @@ -14,7 +14,7 @@ import {isReportListItemType, isTransactionListItemType} from '@libs/SearchUIUti
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import FILTER_KEYS from '@src/types/form/SearchAdvancedFiltersForm';
import type {LastPaymentMethod} from '@src/types/onyx';
import type {LastPaymentMethod, SearchResults} from '@src/types/onyx';
import type {SearchReport, SearchTransaction} from '@src/types/onyx/SearchResults';
import * as Report from './Report';

Expand All @@ -34,16 +34,23 @@ Onyx.connect({
},
});

let allSnapshots: OnyxCollection<SearchResults>;
Onyx.connect({
key: ONYXKEYS.COLLECTION.SNAPSHOT,
callback: (val) => {
allSnapshots = val;
},
waitForCollectionCallback: true,
});

function handleActionButtonPress(hash: number, item: TransactionListItemType | ReportListItemType, goToItem: () => void) {
// The transactionID is needed to handle actions taken on `status:all` where transactions on single expense reports can be approved/paid.
// We need the transactionID to display the loading indicator for that list item's action.
const transactionID = isTransactionListItemType(item) ? item.transactionID : undefined;

switch (item.action) {
case CONST.SEARCH.ACTION_TYPES.PAY: {
const lastPolicyPaymentMethod = item.policyID ? (lastPaymentMethod?.[item.policyID] as ValueOf<typeof CONST.IOU.PAYMENT_TYPE>) : null;
const amount = isReportListItemType(item) ? item.total ?? 0 : item.formattedTotal;
return lastPolicyPaymentMethod ? payMoneyRequestOnSearch(hash, [{reportID: item.reportID, amount, paymentType: lastPolicyPaymentMethod}], transactionID) : goToItem();
return getPayActionCallback(hash, item, goToItem);
}
case CONST.SEARCH.ACTION_TYPES.APPROVE:
return approveMoneyRequestOnSearch(hash, [item.reportID], transactionID);
Expand All @@ -52,6 +59,24 @@ function handleActionButtonPress(hash: number, item: TransactionListItemType | R
}
}

function getPayActionCallback(hash: number, item: TransactionListItemType | ReportListItemType, goToItem: () => void) {
const lastPolicyPaymentMethod = item.policyID ? (lastPaymentMethod?.[item.policyID] as ValueOf<typeof CONST.IOU.PAYMENT_TYPE>) : null;

if (!lastPolicyPaymentMethod) {
return goToItem();
}

const amount = isReportListItemType(item) ? item.total ?? 0 : item.formattedTotal;
const transactionID = isTransactionListItemType(item) ? item.transactionID : undefined;

if (lastPolicyPaymentMethod === CONST.IOU.PAYMENT_TYPE.ELSEWHERE) {
return payMoneyRequestOnSearch(hash, [{reportID: item.reportID, amount, paymentType: lastPolicyPaymentMethod}], transactionID);
}

const hasVBBA = !!allSnapshots?.[`${ONYXKEYS.COLLECTION.SNAPSHOT}${hash}`]?.data?.[`${ONYXKEYS.COLLECTION.POLICY}${item.policyID}`]?.achAccount?.bankAccountID;
return hasVBBA ? payMoneyRequestOnSearch(hash, [{reportID: item.reportID, amount, paymentType: lastPolicyPaymentMethod}], transactionID) : goToItem();
}

function getOnyxLoadingData(hash: number): {optimisticData: OnyxUpdate[]; finallyData: OnyxUpdate[]} {
const optimisticData: OnyxUpdate[] = [
{
Expand Down
3 changes: 2 additions & 1 deletion src/types/onyx/SearchResults.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type TransactionListItem from '@components/SelectionList/Search/Transacti
import type {ReportActionListItemType, ReportListItemType, TransactionListItemType} from '@components/SelectionList/types';
import type CONST from '@src/CONST';
import type ONYXKEYS from '@src/ONYXKEYS';
import type {ACHAccount} from './Policy';
import type {InvoiceReceiver} from './Report';
import type ReportActionName from './ReportActionName';
import type ReportNameValuePairs from './ReportNameValuePairs';
Expand Down Expand Up @@ -209,7 +210,7 @@ type SearchPolicy = {
autoReimbursementLimit?: number;

/** The verified bank account linked to the policy */
achAccount?: Record<string, string>;
achAccount?: ACHAccount;

/** The current user's role in the policy */
role: ValueOf<typeof CONST.POLICY.ROLE>;
Expand Down

0 comments on commit 2805d3b

Please sign in to comment.