From a52ade1142fddf0213c497ba708a49f37e961bec Mon Sep 17 00:00:00 2001 From: Kacper Falat Date: Mon, 18 Sep 2023 16:11:51 +0200 Subject: [PATCH] Implemented requested changes. --- src/libs/models/BankAccount.ts | 118 ++++++++++++++------------------- src/types/onyx/BankAccount.ts | 5 +- 2 files changed, 53 insertions(+), 70 deletions(-) diff --git a/src/libs/models/BankAccount.ts b/src/libs/models/BankAccount.ts index 73453f5382a6..c1557cdef324 100644 --- a/src/libs/models/BankAccount.ts +++ b/src/libs/models/BankAccount.ts @@ -1,30 +1,12 @@ import Str from 'expensify-common/lib/str'; import CONST from '../../CONST'; import { ValueOf } from "type-fest"; +import BankAccountJSON, {AdditionalData} from "../../types/onyx/BankAccount"; type State = ValueOf; type BankAccountType = "withdrawal" | "direct-deposit"; -type BankAccountJSON = { - methodID: number; - validateCodeExpectedDate: string; - isSavings: boolean; - accountData: { - accountNumber: string; - addressName: string; - processor: string; - routingNumber: string; - sharees: string[]; - state: State; - country: string; - defaultCredit: boolean; - allowDebit: boolean; - riskChecked: boolean; - additionalData: Record; - } -}; - type ACHData = { routingNumber: string, accountNumber: string, @@ -53,7 +35,7 @@ class BankAccount { /** * @returns - the ID of the reimbursement account */ - getID() { + getID(): number | undefined { return this.json.methodID; } @@ -61,50 +43,50 @@ class BankAccount { * @returns - account number obfuscated by the backend. * @example "XXXXXX3956" */ - getMaskedAccountNumber() { - return this.json.accountData.accountNumber; + getMaskedAccountNumber(): string | undefined { + return this.json.accountData?.accountNumber; } /** * @returns - the display name for the account. */ - getAddressName() { - return this.json.accountData.addressName; + getAddressName(): string | undefined { + return this.json.accountData?.addressName; } /** * @returns - processor of the bank account. */ - getProcessor() { - return this.json.accountData.processor; + getProcessor(): string | undefined { + return this.json.accountData?.processor; } /** * @returns - routing number of the bank account. */ - getRoutingNumber() { - return this.json.accountData.routingNumber; + getRoutingNumber(): string | undefined { + return this.json.accountData?.routingNumber; } /** * @return - all user emails that have access to this bank account. */ - getSharees() { - return this.json.accountData.sharees; + getSharees(): string[] | undefined { + return this.json.accountData?.sharees; } /** * @returns - current state of the bank account. * @private */ - private getState(): State { - return this.json.accountData.state; + private getState(): State | undefined { + return this.json.accountData?.state; } /** * @returns - if the bank account is open. */ - isOpen() { + isOpen(): boolean { return this.getState() === BankAccount.STATE.OPEN; } @@ -112,50 +94,50 @@ class BankAccount { * @deprecated Use !isPending instead. * @returns - if the bank account is verified. */ - isVerified() { + isVerified(): boolean { return !this.isPending(); } /** * @returns - if he user still needs to enter the 3 micro deposit amounts. */ - isPending() { + isPending(): boolean { return this.getState() === BankAccount.STATE.PENDING; } /** * @returns - if success team is currently verifying the bank account data provided by the user. */ - isVerifying() { + isVerifying(): boolean { return this.getState() === BankAccount.STATE.VERIFYING; } /** * @returns - if the user didn't finish entering all their info. */ - isInSetup() { + isInSetup(): boolean { return this.getState() === BankAccount.STATE.SETUP; } /** * @returns - if the bank account is locked. */ - isLocked() { + isLocked(): boolean { return this.getState() === BankAccount.STATE.LOCKED; } /** * @returns - if the account is the default credit account. */ - isDefaultCredit() { - return this.json.accountData.defaultCredit; + isDefaultCredit(): boolean | undefined { + return this.json.accountData?.defaultCredit; } /** * @returns - if the account can be used for paying other people. */ - isWithdrawal() { - return this.json.accountData.allowDebit; + isWithdrawal(): boolean | undefined { + return this.json.accountData?.allowDebit; } /** @@ -164,7 +146,7 @@ class BankAccount { */ 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.getType()}`; } /** @@ -178,83 +160,81 @@ class BankAccount { /** * @returns - Return the internal json data structure used by auth. */ - getJSON() { + getJSON(): BankAccountJSON { return this.json; } /** * @returns - Whether or not this bank account has been risk checked */ - isRiskChecked() { - return this.json.accountData.riskChecked; + isRiskChecked(): boolean | undefined { + return this.json.accountData?.riskChecked; } /** * @returns - date when the 3 micro amounts for validation were supposed to reach the bank account. */ - getValidateCodeExpectedDate() { + getValidateCodeExpectedDate(): string { return this.json.validateCodeExpectedDate || ''; } /** * @returns - country of the bank account. */ - getCountry() { + getCountry(): string { return this.json.accountData?.additionalData?.country as string || CONST.COUNTRY.US; } /** * @returns - currency of the bank account. */ - getCurrency() { + getCurrency(): string { return this.json.accountData?.additionalData?.currency as string || "USD"; } /** * @returns - bank name of the bank account. */ - getBankName() { + getBankName(): string { return this.json.accountData?.additionalData?.bankName as string || ""; } /** * @returns - Information if did we get bank account details for local transfer or international wire. */ - hasInternationalWireDetails() { + hasInternationalWireDetails(): boolean { return this.json.accountData?.additionalData?.fieldsType as string === 'international'; } /** - * @returns {Unknown} Additional data of a bankAccount. + * @returns - Additional data of a bankAccount. */ - getAdditionalData() { + getAdditionalData(): Partial { return this.json.accountData?.additionalData || {}; } /** * @returns - A map needed to set up a withdrawal account. */ - toACHData(): ACHData { - return Object.assign( - { - routingNumber: this.getRoutingNumber(), - accountNumber: this.getMaskedAccountNumber(), - addressName: this.getAddressName(), - isSavings: this.json.isSavings, - bankAccountID: this.getID(), - state: this.getState(), - validateCodeExpectedDate: this.getValidateCodeExpectedDate(), - needsToUpgrade: this.needsToUpgrade(), - }, - this.getAdditionalData(), - ); + toACHData(): Partial { + return { + routingNumber: this.getRoutingNumber(), + accountNumber: this.getMaskedAccountNumber(), + addressName: this.getAddressName(), + isSavings: this.json.accountData?.isSavings, + bankAccountID: this.getID(), + state: this.getState(), + validateCodeExpectedDate: this.getValidateCodeExpectedDate(), + needsToUpgrade: this.needsToUpgrade(), + ...this.getAdditionalData(), + }; } /** * @returns - Information if user hasn't upgraded their bank account yet. */ - needsToUpgrade() { - return !this.isInSetup() && !(typeof this.json?.accountData?.additionalData?.beneficialOwners !== 'undefined'); + needsToUpgrade(): boolean { + return !this.isInSetup() && !this.json?.accountData?.additionalData?.beneficialOwners; } } diff --git a/src/types/onyx/BankAccount.ts b/src/types/onyx/BankAccount.ts index ccaaa7ebab78..5f29fb9c90b0 100644 --- a/src/types/onyx/BankAccount.ts +++ b/src/types/onyx/BankAccount.ts @@ -1,4 +1,4 @@ -type AdditionalData = { +export type AdditionalData = { isP2PDebitCard?: boolean; beneficialOwners?: string[]; currency?: string; @@ -56,6 +56,9 @@ type BankAccount = { isDefault?: boolean; + /** Date when the 3 micro amounts for validation were supposed to reach the bank account. */ + validateCodeExpectedDate?: string; + /** string like 'bankAccount-{}' where is the bankAccountID */ key?: string;