Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions src/libs/actions/BankAccounts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import ROUTES from '@src/ROUTES';
import type {Route} from '@src/ROUTES';
import type {InternationalBankAccountForm, PersonalBankAccountForm} from '@src/types/form';
import type {ACHContractStepProps, BeneficialOwnersStepProps, CompanyStepProps, ReimbursementAccountForm, RequestorStepProps} from '@src/types/form/ReimbursementAccountForm';
import type {BankAccountList, LastPaymentMethod, LastPaymentMethodType, PersonalBankAccount, Policy} from '@src/types/onyx';
import type {BankAccountList, LastPaymentMethod, LastPaymentMethodType, PersonalBankAccount} from '@src/types/onyx';
import type PlaidBankAccount from '@src/types/onyx/PlaidBankAccount';
import type {BankAccountStep, ReimbursementAccountStep, ReimbursementAccountSubStep} from '@src/types/onyx/ReimbursementAccount';
import type {OnyxData} from '@src/types/onyx/Request';
Expand Down Expand Up @@ -264,7 +264,13 @@ function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAcc
*
* TODO: offline pattern for this command will have to be added later once the pattern B design doc is complete
*/
function addPersonalBankAccount(account: PlaidBankAccount, policyID?: string, source?: string, lastPaymentMethod?: LastPaymentMethodType | string | undefined, personalPolicy?: Policy) {
function addPersonalBankAccount(
account: PlaidBankAccount,
personalPolicyID: string | undefined,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a unit test for this new parameter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgolen I don't know how to write a test case for the BA feature, as there are no tests present to mock PlaidBankAccount. Can you help me here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At a very minimum, I would add a test that does this:

  • Mock all the parameters to addPersonalBankAccount() with test data
  • Mock the network layer so that API.write() doesn't do anything
  • Verify that the Onyx updates have the correct personal policy ID

I know that's very far from 100% coverage, but at least it's more than 0%.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tgolen I have talked about tests here https://expensify.slack.com/archives/C08CZDJFJ77/p1768229592680389 and it seems like someone will write the tests in another PR. So, we could merge the PR without any issues.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, thank you.

policyID?: string,
source?: string,
lastPaymentMethod?: LastPaymentMethodType | string | undefined,
) {
const parameters: AddPersonalBankAccountParams = {
addressName: account.addressName ?? '',
routingNumber: account.routingNumber,
Expand Down Expand Up @@ -324,12 +330,12 @@ function addPersonalBankAccount(account: PlaidBankAccount, policyID?: string, so
],
};

if (personalPolicy?.id && !lastPaymentMethod) {
if (personalPolicyID && !lastPaymentMethod) {
onyxData.optimisticData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD,
value: {
[personalPolicy?.id]: {
[personalPolicyID]: {
iou: {
name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY,
},
Expand All @@ -343,7 +349,7 @@ function addPersonalBankAccount(account: PlaidBankAccount, policyID?: string, so
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD,
value: {
[personalPolicy?.id]: {
[personalPolicyID]: {
iou: {
name: CONST.IOU.PAYMENT_TYPE.EXPENSIFY,
},
Expand All @@ -357,15 +363,15 @@ function addPersonalBankAccount(account: PlaidBankAccount, policyID?: string, so
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD,
value: {
[personalPolicy?.id]: null,
[personalPolicyID]: null,
},
});
}

API.write(WRITE_COMMANDS.ADD_PERSONAL_BANK_ACCOUNT, parameters, onyxData);
}

function deletePaymentBankAccount(bankAccountID: number, lastUsedPaymentMethods?: LastPaymentMethod, bankAccount?: OnyxEntry<PersonalBankAccount>, personalPolicy?: Policy) {
function deletePaymentBankAccount(bankAccountID: number, personalPolicyID: string | undefined, lastUsedPaymentMethods?: LastPaymentMethod, bankAccount?: OnyxEntry<PersonalBankAccount>) {
const parameters: DeletePaymentBankAccountParams = {bankAccountID};

const bankAccountFailureData = {
Expand Down Expand Up @@ -411,14 +417,14 @@ function deletePaymentBankAccount(bankAccountID: number, lastUsedPaymentMethods?
continue;
}

if (personalPolicy?.id === paymentMethodID && lastUsedPaymentMethod.iou?.name === CONST.IOU.PAYMENT_TYPE.EXPENSIFY) {
if (personalPolicyID === paymentMethodID && lastUsedPaymentMethod.iou?.name === CONST.IOU.PAYMENT_TYPE.EXPENSIFY) {
const revertedLastUsedPaymentMethod = lastUsedPaymentMethod.lastUsed?.name !== CONST.IOU.PAYMENT_TYPE.EXPENSIFY ? lastUsedPaymentMethod.lastUsed?.name : null;

onyxData.successData?.push({
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD,
value: {
[personalPolicy?.id]: {
[personalPolicyID]: {
iou: {
name: revertedLastUsedPaymentMethod,
bankAccountID: null,
Expand All @@ -432,7 +438,7 @@ function deletePaymentBankAccount(bankAccountID: number, lastUsedPaymentMethods?
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.NVP_LAST_PAYMENT_METHOD,
value: {
[personalPolicy?.id]: {
[personalPolicyID]: {
expense: {
name: lastUsedPaymentMethod.iou?.name,
bankAccountID: lastUsedPaymentMethod.iou?.bankAccountID,
Expand Down
6 changes: 2 additions & 4 deletions src/pages/AddPersonalBankAccountPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePolicy from '@hooks/usePolicy';
import useThemeStyles from '@hooks/useThemeStyles';
import getPlaidOAuthReceivedRedirectURI from '@libs/getPlaidOAuthReceivedRedirectURI';
import {isFullScreenName} from '@libs/Navigation/helpers/isNavigatorName';
Expand All @@ -29,7 +28,6 @@ function AddPersonalBankAccountPage() {
const [personalBankAccount] = useOnyx(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {canBeMissing: true});
const [plaidData] = useOnyx(ONYXKEYS.PLAID_DATA, {canBeMissing: true});
const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID, {canBeMissing: true});
const personalPolicy = usePolicy(personalPolicyID);
const shouldShowSuccess = personalBankAccount?.shouldShowSuccess ?? false;
const topmostFullScreenRoute = navigationRef.current?.getRootState()?.routes.findLast((route) => isFullScreenName(route.name));
const kycWallRef = useContext(KYCWallContext);
Expand Down Expand Up @@ -62,9 +60,9 @@ function AddPersonalBankAccountPage() {
...selectedPlaidBankAccount,
plaidAccessToken: plaidData?.plaidAccessToken ?? '',
};
addPersonalBankAccount(bankAccountWithToken, policyID, source, undefined, personalPolicy);
addPersonalBankAccount(bankAccountWithToken, personalPolicyID, policyID, source);
}
}, [plaidData?.bankAccounts, plaidData?.plaidAccessToken, personalBankAccount?.policyID, personalBankAccount?.source, selectedPlaidAccountId, personalPolicy]);
}, [plaidData?.bankAccounts, plaidData?.plaidAccessToken, selectedPlaidAccountId, personalPolicyID, personalBankAccount?.policyID, personalBankAccount?.source]);

const exitFlow = useCallback(
(shouldContinue = false) => {
Expand Down
6 changes: 2 additions & 4 deletions src/pages/EnablePayments/AddBankAccount/AddBankAccount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {KYCWallContext} from '@components/KYCWall/KYCWallContext';
import ScreenWrapper from '@components/ScreenWrapper';
import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePolicy from '@hooks/usePolicy';
import useSubStep from '@hooks/useSubStep';
import type {SubStepProps} from '@hooks/useSubStep/types';
import useThemeStyles from '@hooks/useThemeStyles';
Expand All @@ -27,7 +26,6 @@ function AddBankAccount() {
const [personalBankAccount] = useOnyx(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {canBeMissing: true});
const [personalBankAccountDraft] = useOnyx(ONYXKEYS.FORMS.PERSONAL_BANK_ACCOUNT_FORM_DRAFT, {canBeMissing: true});
const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID, {canBeMissing: true});
const personalPolicy = usePolicy(personalPolicyID);
const {translate} = useLocalize();
const styles = useThemeStyles();
const kycWallRef = useContext(KYCWallContext);
Expand All @@ -43,9 +41,9 @@ function AddBankAccount() {
...selectedPlaidBankAccount,
plaidAccessToken: plaidData?.plaidAccessToken ?? '',
};
addPersonalBankAccount(bankAccountWithToken, undefined, undefined, undefined, personalPolicy);
addPersonalBankAccount(bankAccountWithToken, personalPolicyID);
}
}, [personalBankAccountDraft?.plaidAccountID, personalPolicy, plaidData?.bankAccounts, plaidData?.plaidAccessToken]);
}, [personalBankAccountDraft?.plaidAccountID, plaidData?.bankAccounts, plaidData?.plaidAccessToken, personalPolicyID]);

const isSetupTypeChosen = personalBankAccountDraft?.setupType === CONST.BANK_ACCOUNT.SETUP_TYPE.PLAID;

Expand Down
6 changes: 2 additions & 4 deletions src/pages/settings/Wallet/WalletPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import useNetwork from '@hooks/useNetwork';
import useOnyx from '@hooks/useOnyx';
import usePaymentMethodState from '@hooks/usePaymentMethodState';
import type {FormattedSelectedPaymentMethod} from '@hooks/usePaymentMethodState/types';
import usePolicy from '@hooks/usePolicy';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -69,7 +68,6 @@ function WalletPage() {
const [userAccount] = useOnyx(ONYXKEYS.ACCOUNT, {canBeMissing: true});
const [lastUsedPaymentMethods] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {canBeMissing: true});
const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID, {canBeMissing: true});
const personalPolicy = usePolicy(personalPolicyID);
const isUserValidated = userAccount?.validated ?? false;
const {isAccountLocked, showLockedAccountModal} = useContext(LockedAccountContext);
const {login: currentUserLogin} = useCurrentUserPersonalDetails();
Expand Down Expand Up @@ -217,7 +215,7 @@ function WalletPage() {
const fundID = paymentMethod.selectedPaymentMethod.fundID;
if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT && bankAccountID) {
const bankAccount = bankAccountList?.[paymentMethod.methodID] ?? {};
deletePaymentBankAccount(bankAccountID, lastUsedPaymentMethods, bankAccount, personalPolicy);
deletePaymentBankAccount(bankAccountID, personalPolicyID, lastUsedPaymentMethods, bankAccount);
} else if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.DEBIT_CARD && fundID) {
deletePaymentCard(fundID);
}
Expand All @@ -231,7 +229,7 @@ function WalletPage() {
resetSelectedPaymentMethodData,
bankAccountList,
lastUsedPaymentMethods,
personalPolicy,
personalPolicyID,
]);

/**
Expand Down
6 changes: 2 additions & 4 deletions src/pages/workspace/invoices/WorkspaceInvoiceVBASection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import useLocalize from '@hooks/useLocalize';
import useOnyx from '@hooks/useOnyx';
import usePaymentMethodState from '@hooks/usePaymentMethodState';
import type {FormattedSelectedPaymentMethod} from '@hooks/usePaymentMethodState/types';
import usePolicy from '@hooks/usePolicy';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useThemeStyles from '@hooks/useThemeStyles';
import useWindowDimensions from '@hooks/useWindowDimensions';
Expand Down Expand Up @@ -50,7 +49,6 @@ function WorkspaceInvoiceVBASection({policyID}: WorkspaceInvoiceVBASectionProps)
const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: true});
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {canBeMissing: true});
const [personalPolicyID] = useOnyx(ONYXKEYS.PERSONAL_POLICY_ID, {canBeMissing: true});
const personalPolicy = usePolicy(personalPolicyID);
const {paymentMethod, setPaymentMethod, resetSelectedPaymentMethodData} = usePaymentMethodState();
const paymentMethodButtonRef = useRef<HTMLDivElement | null>(null);
const [showConfirmDeleteModal, setShowConfirmDeleteModal] = useState(false);
Expand Down Expand Up @@ -157,9 +155,9 @@ function WorkspaceInvoiceVBASection({policyID}: WorkspaceInvoiceVBASectionProps)
const deletePaymentMethod = useCallback(() => {
const bankAccountID = paymentMethod.selectedPaymentMethod.bankAccountID;
if (paymentMethod.selectedPaymentMethodType === CONST.PAYMENT_METHODS.PERSONAL_BANK_ACCOUNT && bankAccountID) {
deletePaymentBankAccount(bankAccountID, undefined, undefined, personalPolicy);
deletePaymentBankAccount(bankAccountID, personalPolicyID);
}
}, [paymentMethod.selectedPaymentMethod.bankAccountID, paymentMethod.selectedPaymentMethodType, personalPolicy]);
}, [paymentMethod.selectedPaymentMethod.bankAccountID, paymentMethod.selectedPaymentMethodType, personalPolicyID]);

const makeDefaultPaymentMethod = useCallback(() => {
// Find the previous default payment method so we can revert if the MakeDefaultPaymentMethod command errors
Expand Down
Loading