diff --git a/src/libs/models/BankAccount.ts b/src/libs/models/BankAccount.ts index f7f4a925a7e4..7096f4bf3f8a 100644 --- a/src/libs/models/BankAccount.ts +++ b/src/libs/models/BankAccount.ts @@ -1,12 +1,11 @@ import Str from 'expensify-common/lib/str'; import {ValueOf} from 'type-fest'; import CONST from '../../CONST'; + import BankAccountJSON, {AdditionalData} from '../../types/onyx/BankAccount'; type State = ValueOf; -type BankAccountType = 'withdrawal' | 'direct-deposit'; - type ACHData = { routingNumber: string; accountNumber: string; @@ -19,7 +18,9 @@ type ACHData = { }; class BankAccount { - static readonly STATE = { + json: BankAccountJSON; + + static STATE = { PENDING: 'PENDING', OPEN: 'OPEN', DELETED: 'DELETED', @@ -28,215 +29,204 @@ class BankAccount { VERIFYING: 'VERIFYING', }; - private readonly json: BankAccountJSON; - constructor(accountJSON: BankAccountJSON) { this.json = accountJSON; } /** - * @returns - the ID of the reimbursement account + * Return the ID of the reimbursement account */ - getID(): number | undefined { + getID() { return this.json.methodID; } /** - * @returns - account number obfuscated by the backend. - * @example "XXXXXX3956" + * Return the account number, which has been obfuscated by the back end + * example "XXXXXX3956" */ - getMaskedAccountNumber(): string | undefined { + getMaskedAccountNumber() { return this.json.accountData?.accountNumber; } /** - * @returns - the display name for the account. + * Used as the display name for the account... */ - getAddressName(): string | undefined { + getAddressName() { return this.json.accountData?.addressName; } - /** - * @returns - processor of the bank account. - */ - getProcessor(): string | undefined { + getProcessor() { return this.json.accountData?.processor; } - /** - * @returns - routing number of the bank account. - */ - getRoutingNumber(): string | undefined { + getRoutingNumber() { return this.json.accountData?.routingNumber; } /** - * @return - all user emails that have access to this bank account. + * Get all user emails that have access to this bank account */ - getSharees(): string[] | undefined { + getSharees() { return this.json.accountData?.sharees; } - /** - * @returns - current state of the bank account. - * @private - */ - private getState(): State | undefined { + getState() { return this.json.accountData?.state; } - /** - * @returns - if the bank account is open. - */ - isOpen(): boolean { + isOpen() { return this.getState() === BankAccount.STATE.OPEN; } /** * @deprecated Use !isPending instead. - * @returns - if the bank account is verified. */ - isVerified(): boolean { + isVerified() { return !this.isPending(); } /** - * @returns - if he user still needs to enter the 3 micro deposit amounts. + * If the user still needs to enter the 3 micro deposit amounts. */ - isPending(): boolean { + isPending() { return this.getState() === BankAccount.STATE.PENDING; } /** - * @returns - if success team is currently verifying the bank account data provided by the user. + * If success team is currently verifying the bank account data provided by the user. */ - isVerifying(): boolean { + isVerifying() { return this.getState() === BankAccount.STATE.VERIFYING; } /** - * @returns - if the user didn't finish entering all their info. + * If the user didn't finish entering all their info. */ - isInSetup(): boolean { + isInSetup() { return this.getState() === BankAccount.STATE.SETUP; } - /** - * @returns - if the bank account is locked. - */ - isLocked(): boolean { + isLocked() { return this.getState() === BankAccount.STATE.LOCKED; } /** - * @returns - if the account is the default credit account. + * Is it the account to use by default to receive money? */ - isDefaultCredit(): boolean | undefined { - return this.json.accountData?.defaultCredit; + isDefaultCredit() { + return this.json.accountData?.defaultCredit === true; } /** - * @returns - if the account can be used for paying other people. + * Can we use this account to pay other people? */ - isWithdrawal(): boolean | undefined { - return this.json.accountData?.allowDebit; + isWithdrawal() { + return this.json.accountData?.allowDebit === true; + } + + getType() { + return this.json.accountData?.type; } /** + * Return the client ID of this bank account + * * @NOTE WARNING KEEP IN SYNC WITH THE PHP - * @returns - client ID of the bank account. */ getClientID() { // eslint-disable-next-line max-len - return `${Str.makeID(this.getMaskedAccountNumber() ?? '')}${Str.makeID(this.getAddressName() ?? '')}${Str.makeID(this.getRoutingNumber() ?? '')}${this.getType()}`; + return `${Str.makeID(this.getMaskedAccountNumber() ?? '')}${Str.makeID(this.getAddressName() ?? '')}${Str.makeID(this.getRoutingNumber() ?? '')}${this.getTransactionType()}`; } - /** - * @returns - type of the bank account. - * @private - */ - private getType(): BankAccountType { + private getTransactionType() { return this.isWithdrawal() ? 'withdrawal' : 'direct-deposit'; } /** - * @returns - Return the internal json data structure used by auth. + * Return the internal json data structure used by auth */ - getJSON(): BankAccountJSON { + getJSON() { return this.json; } /** - * @returns - Whether or not this bank account has been risk checked + * Return whether this bank account has been risk checked */ - isRiskChecked(): boolean | undefined { - return this.json.accountData?.riskChecked; + isRiskChecked() { + return Boolean(this.json.accountData?.riskChecked); } /** - * @returns - date when the 3 micro amounts for validation were supposed to reach the bank account. + * Return when the 3 micro amounts for validation were supposed to reach the bank account. */ - getValidateCodeExpectedDate(): string { + getValidateCodeExpectedDate() { return this.json.validateCodeExpectedDate ?? ''; } /** - * @returns - country of the bank account. + * In which country is the bank account? */ - getCountry(): string { + getCountry() { return this.json.accountData?.additionalData?.country ?? CONST.COUNTRY.US; } /** - * @returns - currency of the bank account. + * In which currency is the bank account? */ - getCurrency(): string { + getCurrency() { return this.json.accountData?.additionalData?.currency ?? 'USD'; } /** - * @returns - bank name of the bank account. + * In which bank is the bank account? */ - getBankName(): string { + getBankName() { return this.json.accountData?.additionalData?.bankName ?? ''; } /** - * @returns - Information if did we get bank account details for local transfer or international wire. + * Did we get bank account details for local transfer or international wire? */ - hasInternationalWireDetails(): boolean { - return this.json.accountData?.additionalData?.fieldsType === 'international'; + hasInternationalWireDetails() { + return (this.json.accountData?.additionalData?.fieldsType ?? 'local') === 'international'; } /** - * @returns - Additional data of a bankAccount. + * Get the additional data of a bankAccount */ getAdditionalData(): Partial { return this.json.accountData?.additionalData ?? {}; } /** - * @returns - A map needed to set up a withdrawal account. + * Get the pending action of the bank account + */ + getPendingAction() { + return this.json.pendingAction ?? ''; + } + + /** + * Return a map needed to set up a withdrawal account */ - toACHData(): Partial { + toACHData(): Partial { return { routingNumber: this.getRoutingNumber(), accountNumber: this.getMaskedAccountNumber(), addressName: this.getAddressName(), - isSavings: this.json.accountData?.isSavings, + isSavings: this.json.isSavings, bankAccountID: this.getID(), state: this.getState(), validateCodeExpectedDate: this.getValidateCodeExpectedDate(), needsToUpgrade: this.needsToUpgrade(), ...this.getAdditionalData(), - }; + } as ACHData; } /** - * @returns - Information if user hasn't upgraded their bank account yet. + * Check if user hasn't upgraded their bank account yet. */ - needsToUpgrade(): boolean { - return !this.isInSetup() && !this.json?.accountData?.additionalData?.beneficialOwners; + needsToUpgrade() { + return !this.isInSetup() && this.json.accountData?.additionalData?.beneficialOwners === undefined; } } diff --git a/src/types/onyx/BankAccount.ts b/src/types/onyx/BankAccount.ts index ff61cab805f1..70a2a0495b35 100644 --- a/src/types/onyx/BankAccount.ts +++ b/src/types/onyx/BankAccount.ts @@ -2,6 +2,15 @@ import CONST from '../../CONST'; import AccountData from './AccountData'; import * as OnyxCommon from './OnyxCommon'; +type AdditionalData = { + isP2PDebitCard?: boolean; + beneficialOwners?: string[]; + currency?: string; + bankName?: string; + fieldsType?: string; + country?: string; +}; + type BankAccount = { /** The bank account type */ accountType?: typeof CONST.PAYMENT_METHODS.BANK_ACCOUNT; @@ -11,6 +20,9 @@ type BankAccount = { isDefault?: boolean; + /* Determines if the bank account is a savings account */ + isSavings?: boolean; + /** Date when the 3 micro amounts for validation were supposed to reach the bank account. */ validateCodeExpectedDate?: string; @@ -34,4 +46,4 @@ type BankAccount = { }; export default BankAccount; -export type {AdditionalData, AccountData}; +export type {AccountData, AdditionalData};