Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS migration] Migrate 'BankAccounts.js' lib to TypeScript #28316

Merged
merged 17 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import * as PlaidDataProps from '../../pages/ReimbursementAccount/plaidDataPropT
import Navigation from '../Navigation/Navigation';
import ROUTES from '../../ROUTES';
import * as ReimbursementAccount from './ReimbursementAccount';
import PlaidBankAccount from '../../types/onyx/PlaidBankAccount';
import {ACHContractStepProps, BankAccountStepProps, CompanyStepProps, OnfidoData, ReimbursementAccountProps, RequestorStepProps} from '../../types/onyx/ReimbursementAccountDraft';
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
import {OnyxData} from '../../types/onyx/Request';

export {
goToWithdrawalAccountSetupStep,
Expand All @@ -23,7 +26,9 @@ export {
export {openPlaidBankAccountSelector, openPlaidBankLogin} from './Plaid';
export {openOnfidoFlow, answerQuestionsForWallet, verifyIdentity, acceptWalletTerms} from './Wallet';

function clearPlaid() {
type BankAccountCompanyInformation = BankAccountStepProps & CompanyStepProps & ReimbursementAccountProps;

function clearPlaid(): Promise<void> {
Onyx.set(ONYXKEYS.PLAID_LINK_TOKEN, '');

return Onyx.set(ONYXKEYS.PLAID_DATA, PlaidDataProps.plaidDataDefaultProps);
Expand All @@ -35,14 +40,13 @@ function openPlaidView() {

/**
* Open the personal bank account setup flow, with an optional exitReportID to redirect to once the flow is finished.
* @param {String} exitReportID
*/
function openPersonalBankAccountSetupView(exitReportID) {
function openPersonalBankAccountSetupView(exitReportID: string) {
clearPlaid().then(() => {
if (exitReportID) {
Onyx.merge(ONYXKEYS.PERSONAL_BANK_ACCOUNT, {exitReportID});
}
Navigation.navigate(ROUTES.SETTINGS_ADD_BANK_ACCOUNT);
Navigation.navigate(ROUTES.SETTINGS_ADD_BANK_ACCOUNT, '');
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
});
}

Expand All @@ -57,18 +61,16 @@ function clearOnfidoToken() {

/**
* Helper method to build the Onyx data required during setup of a Verified Business Bank Account
*
* @returns {Object}
*/
function getVBBADataForOnyx() {
function getVBBADataForOnyx(): OnyxData {
return {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: true,
errors: null,
errors: undefined,
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
Expand All @@ -78,7 +80,7 @@ function getVBBADataForOnyx() {
key: ONYXKEYS.REIMBURSEMENT_ACCOUNT,
value: {
isLoading: false,
errors: null,
errors: undefined,
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
},
},
],
Expand All @@ -97,11 +99,8 @@ function getVBBADataForOnyx() {

/**
* Submit Bank Account step with Plaid data so php can perform some checks.
*
* @param {Number} bankAccountID
* @param {Object} selectedPlaidBankAccount
*/
function connectBankAccountWithPlaid(bankAccountID, selectedPlaidBankAccount) {
function connectBankAccountWithPlaid(bankAccountID: number, selectedPlaidBankAccount: PlaidBankAccount) {
const commandName = 'ConnectBankAccountWithPlaid';

const parameters = {
Expand All @@ -119,10 +118,9 @@ function connectBankAccountWithPlaid(bankAccountID, selectedPlaidBankAccount) {
/**
* Adds a bank account via Plaid
*
* @param {Object} account
* @TODO offline pattern for this command will have to be added later once the pattern B design doc is complete
*/
function addPersonalBankAccount(account) {
function addPersonalBankAccount(account: PlaidBankAccount) {
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
const commandName = 'AddPersonalBankAccount';

const parameters = {
Expand Down Expand Up @@ -174,7 +172,7 @@ function addPersonalBankAccount(account) {
API.write(commandName, parameters, onyxData);
}

function deletePaymentBankAccount(bankAccountID) {
function deletePaymentBankAccount(bankAccountID: number) {
API.write(
'DeletePaymentBankAccount',
{
Expand Down Expand Up @@ -206,30 +204,12 @@ function deletePaymentBankAccount(bankAccountID) {
* Update the user's personal information on the bank account in database.
*
* This action is called by the requestor step in the Verified Bank Account flow
*
* @param {Object} params
*
* @param {String} [params.dob]
* @param {String} [params.firstName]
* @param {String} [params.lastName]
* @param {String} [params.requestorAddressStreet]
* @param {String} [params.requestorAddressCity]
* @param {String} [params.requestorAddressState]
* @param {String} [params.requestorAddressZipCode]
* @param {String} [params.ssnLast4]
* @param {String} [params.isControllingOfficer]
* @param {Object} [params.onfidoData]
* @param {Boolean} [params.isOnfidoSetupComplete]
*/
function updatePersonalInformationForBankAccount(params) {
function updatePersonalInformationForBankAccount(params: RequestorStepProps) {
API.write('UpdatePersonalInformationForBankAccount', params, getVBBADataForOnyx());
}

/**
* @param {Number} bankAccountID
* @param {String} validateCode
*/
function validateBankAccount(bankAccountID, validateCode) {
function validateBankAccount(bankAccountID: number, validateCode: string) {
API.write(
'ValidateBankAccountWithTransactions',
{
Expand Down Expand Up @@ -269,7 +249,7 @@ function validateBankAccount(bankAccountID, validateCode) {
);
}

function openReimbursementAccountPage(stepToOpen, subStep, localCurrentStep) {
function openReimbursementAccountPage(stepToOpen: string, subStep: string, localCurrentStep: string) {
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like stepToOpen comes from this:

switch (lodashGet(this.props.route, ['params', 'stepToOpen'], '')) {
case 'new':
return CONST.BANK_ACCOUNT.STEP.BANK_ACCOUNT;
case 'company':
return CONST.BANK_ACCOUNT.STEP.COMPANY;
case 'personal-information':
return CONST.BANK_ACCOUNT.STEP.REQUESTOR;
case 'contract':
return CONST.BANK_ACCOUNT.STEP.ACH_CONTRACT;
case 'validate':
return CONST.BANK_ACCOUNT.STEP.VALIDATION;
case 'enable':
return CONST.BANK_ACCOUNT.STEP.ENABLE;
default:
return '';
}

Instead of using a type of string, how about using something more like ValueOf<typeof CONST.BANK_ACCOUNT.STEP>

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@tgolen Updated

const onyxData = {
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
optimisticData: [
{
Expand Down Expand Up @@ -311,63 +291,23 @@ function openReimbursementAccountPage(stepToOpen, subStep, localCurrentStep) {

/**
* Updates the bank account in the database with the company step data
*
* @param {Object} bankAccount
* @param {Number} [bankAccount.bankAccountID]
*
* Fields from BankAccount step
* @param {String} [bankAccount.routingNumber]
* @param {String} [bankAccount.accountNumber]
* @param {String} [bankAccount.bankName]
* @param {String} [bankAccount.plaidAccountID]
* @param {String} [bankAccount.plaidAccessToken]
* @param {Boolean} [bankAccount.isSavings]
*
* Fields from Company step
* @param {String} [bankAccount.companyName]
* @param {String} [bankAccount.addressStreet]
* @param {String} [bankAccount.addressCity]
* @param {String} [bankAccount.addressState]
* @param {String} [bankAccount.addressZipCode]
* @param {String} [bankAccount.companyPhone]
* @param {String} [bankAccount.website]
* @param {String} [bankAccount.companyTaxID]
* @param {String} [bankAccount.incorporationType]
* @param {String} [bankAccount.incorporationState]
* @param {String} [bankAccount.incorporationDate]
* @param {Boolean} [bankAccount.hasNoConnectionToCannabis]
* @param {String} policyID
*/
function updateCompanyInformationForBankAccount(bankAccount, policyID) {
function updateCompanyInformationForBankAccount(bankAccount: BankAccountCompanyInformation, policyID: string) {
API.write('UpdateCompanyInformationForBankAccount', {...bankAccount, policyID}, getVBBADataForOnyx());
}

/**
* Add beneficial owners for the bank account, accept the ACH terms and conditions and verify the accuracy of the information provided
*
* @param {Object} params
*
* // ACH Contract Step
* @param {Boolean} [params.ownsMoreThan25Percent]
* @param {Boolean} [params.hasOtherBeneficialOwners]
* @param {Boolean} [params.acceptTermsAndConditions]
* @param {Boolean} [params.certifyTrueInformation]
* @param {String} [params.beneficialOwners]
*/
function updateBeneficialOwnersForBankAccount(params) {
function updateBeneficialOwnersForBankAccount(params: ACHContractStepProps) {
API.write('UpdateBeneficialOwnersForBankAccount', {...params}, getVBBADataForOnyx());
}

/**
* Create the bank account with manually entered data.
*
* @param {number} [bankAccountID]
* @param {String} [accountNumber]
* @param {String} [routingNumber]
* @param {String} [plaidMask]
*
*/
function connectBankAccountManually(bankAccountID, accountNumber, routingNumber, plaidMask) {
function connectBankAccountManually(bankAccountID: number, accountNumber: string, routingNumber: string, plaidMask: string) {
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
API.write(
'ConnectBankAccountManually',
{
Expand All @@ -382,11 +322,8 @@ function connectBankAccountManually(bankAccountID, accountNumber, routingNumber,

/**
* Verify the user's identity via Onfido
*
* @param {Number} bankAccountID
* @param {Object} onfidoData
*/
function verifyIdentityForBankAccount(bankAccountID, onfidoData) {
function verifyIdentityForBankAccount(bankAccountID: number, onfidoData: OnfidoData) {
API.write(
'VerifyIdentityForBankAccount',
{
Expand All @@ -398,24 +335,23 @@ function verifyIdentityForBankAccount(bankAccountID, onfidoData) {
}

function openWorkspaceView() {
API.read('OpenWorkspaceView');
API.read('OpenWorkspaceView', {}, {});
}

function handlePlaidError(bankAccountID, error, error_description, plaidRequestID) {
function handlePlaidError(bankAccountID: number, error: string, errorDescription: string, plaidRequestID: string) {
API.write('BankAccount_HandlePlaidError', {
bankAccountID,
error,
error_description,
// eslint-disable-next-line @typescript-eslint/naming-convention
error_description: errorDescription,
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
plaidRequestID,
});
}

/**
* Set the reimbursement account loading so that it happens right away, instead of when the API command is processed.
*
* @param {Boolean} isLoading
*/
function setReimbursementAccountLoading(isLoading) {
function setReimbursementAccountLoading(isLoading: boolean) {
Onyx.merge(ONYXKEYS.REIMBURSEMENT_ACCOUNT, {isLoading});
}

Expand Down
1 change: 1 addition & 0 deletions src/types/onyx/BankAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,4 @@ type BankAccount = {
};

export default BankAccount;
export type {AccountData};
3 changes: 3 additions & 0 deletions src/types/onyx/PersonalBankAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ type PersonalBankAccount = {

/** The account ID of the selected bank account from Plaid */
plaidAccountID?: string;

/** Any reportID we should redirect to at the end of the flow */
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
exitReportID?: string;
};

export default PersonalBankAccount;
3 changes: 3 additions & 0 deletions src/types/onyx/PlaidBankAccount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ type PlaidBankAccount = {

/** Plaid access token, used to then retrieve Assets and Balances */
plaidAccessToken: string;

/** Name of the bank */
bankName?: string;
};

export default PlaidBankAccount;
33 changes: 32 additions & 1 deletion src/types/onyx/ReimbursementAccountDraft.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,52 @@
type ReimbursementAccountDraft = {
type OnfidoData = Record<string, unknown>;

type BankAccountStepProps = {
accountNumber?: string;
routingNumber?: string;
acceptTerms?: boolean;
plaidAccountID?: string;
plaidMask?: string;
};

type CompanyStepProps = {
companyName?: string;
addressStreet?: string;
addressCity?: string;
addressState?: string;
addressZipCode?: string;
companyPhone?: string;
website?: string;
companyTaxID?: string;
incorporationType?: string;
incorporationDate?: string | Date;
incorporationState?: string;
hasNoConnectionToCannabis?: boolean;
};

type RequestorStepProps = {
firstName?: string;
lastName?: string;
requestorAddressStreet?: string;
requestorAddressCity?: string;
requestorAddressState?: string;
requestorAddressZipCode?: string;
dob?: string | Date;
ssnLast4?: string;
isControllingOfficer?: boolean;
isOnfidoSetupComplete?: boolean;
onfidoData?: OnfidoData;
};

type ACHContractStepProps = {
ownsMoreThan25Percent?: boolean;
hasOtherBeneficialOwners?: boolean;
acceptTermsAndConditions?: boolean;
certifyTrueInformation?: boolean;
beneficialOwners?: string[];
};

type ReimbursementAccountProps = {
bankAccountID?: number;
isSavings?: boolean;
bankName?: string;
plaidAccessToken?: string;
Expand All @@ -27,4 +55,7 @@ type ReimbursementAccountDraft = {
amount3?: string;
};

type ReimbursementAccountDraft = BankAccountStepProps & CompanyStepProps & RequestorStepProps & ACHContractStepProps & ReimbursementAccountProps;

export default ReimbursementAccountDraft;
export type {ACHContractStepProps, RequestorStepProps, OnfidoData, BankAccountStepProps, CompanyStepProps, ReimbursementAccountProps};
13 changes: 10 additions & 3 deletions src/types/onyx/Request.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
import {OnyxUpdate} from 'react-native-onyx';

type Request = {
type OnyxData = {
VickyStash marked this conversation as resolved.
Show resolved Hide resolved
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
optimisticData?: OnyxUpdate[];
};

type RequestData = {
command: string;
data?: Record<string, unknown>;
type?: string;
shouldUseSecure?: boolean;
successData?: OnyxUpdate[];
failureData?: OnyxUpdate[];
};

type Request = RequestData & OnyxData;

export default Request;
export type {OnyxData};
Loading