-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[No QA][TS migration] Migrate 'models' lib to TypeScript #27667
Changes from all commits
104d34d
12c860c
a52ade1
b0f2d57
5b93413
0589037
75f8cf9
b99bb38
8986ffe
91b3bb0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,24 @@ | ||
import Str from 'expensify-common/lib/str'; | ||
import lodashGet from 'lodash/get'; | ||
import lodashHas from 'lodash/has'; | ||
import _ from 'underscore'; | ||
import {ValueOf} from 'type-fest'; | ||
import CONST from '@src/CONST'; | ||
import BankAccountJSON, {AdditionalData} from '@src/types/onyx/BankAccount'; | ||
|
||
type State = ValueOf<typeof BankAccount.STATE>; | ||
|
||
type ACHData = { | ||
fvlvte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
routingNumber: string; | ||
accountNumber: string; | ||
addressName: string; | ||
isSavings: boolean; | ||
bankAccountID: number; | ||
state: State; | ||
validateCodeExpectedDate: string; | ||
needsToUpgrade: boolean; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: Can we make this optionally have |
||
}; | ||
|
||
class BankAccount { | ||
json: BankAccountJSON; | ||
|
||
static STATE = { | ||
PENDING: 'PENDING', | ||
OPEN: 'OPEN', | ||
|
@@ -14,14 +28,12 @@ class BankAccount { | |
VERIFYING: 'VERIFYING', | ||
}; | ||
|
||
constructor(accountJSON) { | ||
constructor(accountJSON: BankAccountJSON) { | ||
this.json = accountJSON; | ||
} | ||
|
||
/** | ||
* Return the ID of the reimbursement account | ||
* | ||
* @returns {Number} | ||
*/ | ||
getID() { | ||
fvlvte marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return this.json.methodID; | ||
|
@@ -30,236 +42,190 @@ class BankAccount { | |
/** | ||
* Return the account number, which has been obfuscated by the back end | ||
* example "XXXXXX3956" | ||
* | ||
* @returns {String} | ||
*/ | ||
getMaskedAccountNumber() { | ||
return this.json.accountData.accountNumber; | ||
return this.json.accountData?.accountNumber; | ||
} | ||
|
||
/** | ||
* Used as the display name for the account... | ||
* @returns {String} | ||
*/ | ||
getAddressName() { | ||
return this.json.accountData.addressName; | ||
return this.json.accountData?.addressName; | ||
} | ||
|
||
/** | ||
* @returns {String} | ||
*/ | ||
getProcessor() { | ||
return this.json.accountData.processor; | ||
return this.json.accountData?.processor; | ||
} | ||
|
||
/** | ||
* @returns {String} | ||
*/ | ||
getRoutingNumber() { | ||
return this.json.accountData.routingNumber; | ||
return this.json.accountData?.routingNumber; | ||
} | ||
|
||
/** | ||
* Get all user emails that have access to this bank account | ||
* @return {String[]} | ||
*/ | ||
getSharees() { | ||
return this.json.accountData.sharees; | ||
return this.json.accountData?.sharees; | ||
} | ||
|
||
/** | ||
* @returns {String} | ||
* @private | ||
*/ | ||
getState() { | ||
return this.json.accountData.state; | ||
return this.json.accountData?.state; | ||
} | ||
|
||
/** | ||
* @returns {Boolean} | ||
*/ | ||
isOpen() { | ||
return this.getState() === BankAccount.STATE.OPEN; | ||
} | ||
|
||
/** | ||
* @deprecated Use !isPending instead. | ||
* @returns {Boolean} | ||
*/ | ||
isVerified() { | ||
return !this.isPending(); | ||
} | ||
|
||
/** | ||
* If the user still needs to enter the 3 micro deposit amounts. | ||
* @returns {Boolean} | ||
*/ | ||
isPending() { | ||
return this.getState() === BankAccount.STATE.PENDING; | ||
} | ||
|
||
/** | ||
* If success team is currently verifying the bank account data provided by the user. | ||
* @returns {Boolean} | ||
*/ | ||
isVerifying() { | ||
return this.getState() === BankAccount.STATE.VERIFYING; | ||
} | ||
|
||
/** | ||
* If the user didn't finish entering all their info. | ||
* @returns {Boolean} | ||
*/ | ||
isInSetup() { | ||
return this.getState() === BankAccount.STATE.SETUP; | ||
} | ||
|
||
/** | ||
* @returns {Boolean} | ||
*/ | ||
isLocked() { | ||
return this.getState() === BankAccount.STATE.LOCKED; | ||
} | ||
|
||
/** | ||
* Is it the account to use by default to receive money? | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isDefaultCredit() { | ||
return this.json.accountData.defaultCredit === true; | ||
return this.json.accountData?.defaultCredit === true; | ||
} | ||
|
||
/** | ||
* Can we use this account to pay other people? | ||
* | ||
* @returns {Boolean} | ||
*/ | ||
isWithdrawal() { | ||
return this.json.accountData.allowDebit === true; | ||
return this.json.accountData?.allowDebit === true; | ||
} | ||
|
||
getType() { | ||
return this.json.accountData.type; | ||
return this.json.accountData?.type; | ||
} | ||
|
||
/** | ||
* Return the client ID of this bank account | ||
* | ||
* @NOTE WARNING KEEP IN SYNC WITH THE PHP | ||
* @returns {String} | ||
*/ | ||
getClientID() { | ||
// eslint-disable-next-line max-len | ||
return `${Str.makeID(this.getMaskedAccountNumber())}${Str.makeID(this.getAddressName())}${Str.makeID(this.getRoutingNumber())}${this.getTransactionType()}`; | ||
return `${Str.makeID(this.getMaskedAccountNumber() ?? '')}${Str.makeID(this.getAddressName() ?? '')}${Str.makeID(this.getRoutingNumber() ?? '')}${this.getTransactionType()}`; | ||
} | ||
|
||
/** | ||
* @returns {String} | ||
* @private | ||
*/ | ||
getTransactionType() { | ||
private getTransactionType() { | ||
return this.isWithdrawal() ? 'withdrawal' : 'direct-deposit'; | ||
} | ||
|
||
/** | ||
* Return the internal json data structure used by auth | ||
* @returns {Object} | ||
*/ | ||
getJSON() { | ||
return this.json; | ||
} | ||
|
||
/** | ||
* Return whether or not this bank account has been risk checked | ||
* @returns {Boolean} | ||
* Return whether this bank account has been risk checked | ||
*/ | ||
isRiskChecked() { | ||
return Boolean(this.json.accountData.riskChecked); | ||
return Boolean(this.json.accountData?.riskChecked); | ||
} | ||
|
||
/** | ||
* Return when the 3 micro amounts for validation were supposed to reach the bank account. | ||
* @returns {String} | ||
*/ | ||
getValidateCodeExpectedDate() { | ||
return this.json.validateCodeExpectedDate || ''; | ||
return this.json.validateCodeExpectedDate ?? ''; | ||
} | ||
|
||
/** | ||
* In which country is the bank account? | ||
* @returns {string} | ||
*/ | ||
getCountry() { | ||
return lodashGet(this.json, ['accountData', 'additionalData', 'country'], CONST.COUNTRY.US); | ||
return this.json.accountData?.additionalData?.country ?? CONST.COUNTRY.US; | ||
} | ||
|
||
/** | ||
* In which currency is the bank account? | ||
* @returns {String} | ||
*/ | ||
getCurrency() { | ||
return lodashGet(this.json, ['accountData', 'additionalData', 'currency'], 'USD'); | ||
return this.json.accountData?.additionalData?.currency ?? 'USD'; | ||
} | ||
|
||
/** | ||
* In which bank is the bank account? | ||
* @returns {String} | ||
*/ | ||
getBankName() { | ||
return lodashGet(this.json, ['accountData', 'additionalData', 'bankName'], ''); | ||
return this.json.accountData?.additionalData?.bankName ?? ''; | ||
} | ||
|
||
/** | ||
* Did we get bank account details for local transfer or international wire? | ||
* @returns {Boolean} | ||
*/ | ||
hasInternationalWireDetails() { | ||
return lodashGet(this.json, ['accountData', 'additionalData', 'fieldsType'], 'local') === 'international'; | ||
return (this.json.accountData?.additionalData?.fieldsType ?? 'local') === 'international'; | ||
} | ||
|
||
/** | ||
* Get the additional data of a bankAccount | ||
* @returns {Object} | ||
*/ | ||
getAdditionalData() { | ||
return this.json.accountData.additionalData || {}; | ||
getAdditionalData(): Partial<AdditionalData> { | ||
return this.json.accountData?.additionalData ?? {}; | ||
} | ||
|
||
/** | ||
* Get the pending action of the bank account | ||
* @returns {String} | ||
*/ | ||
getPendingAction() { | ||
return lodashGet(this.json, 'pendingAction', ''); | ||
return this.json.pendingAction ?? ''; | ||
} | ||
|
||
/** | ||
* Return a map needed to setup a withdrawal account | ||
* @returns {Object} | ||
* Return a map needed to set up a withdrawal account | ||
*/ | ||
toACHData() { | ||
return _.extend( | ||
{ | ||
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<ACHData> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB: I don't really understand why we need |
||
return { | ||
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(), | ||
} as ACHData; | ||
} | ||
|
||
/** | ||
* Check if user hasn't upgraded their bank account yet. | ||
* @return {Boolean} | ||
*/ | ||
needsToUpgrade() { | ||
return !this.isInSetup() && !lodashHas(this.json, ['accountData', 'additionalData', 'beneficialOwners']); | ||
return !this.isInSetup() && this.json.accountData?.additionalData?.beneficialOwners === undefined; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NAB: I would rather not call this
BankAccountJSON
because that makes me think it's a string that needs to be parsed. Would you please instead match the name here with the type declarationBankAccount
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh I do see that it's already called json in this class even though it's an object. I still like the name change.