-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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 'PaymentUtils.js' lib to TypeScript #27923
Changes from 1 commit
bd31bf3
e795c85
03cfe9c
e2caf02
06376b2
b0d39cd
7ca844f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||
import {SvgProps} from 'react-native-svg'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import {FC} from 'react'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
// eslint-disable-next-line import/no-named-default | ||||||||||||||||||||||||||||||||||||||||||||||||||
import {default as BankAccountModel} from './models/BankAccount'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
import getBankIcon from '../components/Icon/BankIcons'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import CONST from '../CONST'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import * as Localize from './Localize'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import Fund from '../types/onyx/Fund'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
import BankAccount from '../types/onyx/BankAccount'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
Comment on lines
+6
to
+7
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. Change these types so that |
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
type AccountType = 'debitCard' | 'bankAccount'; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
type PaymentMethod = { | ||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
description: string; | ||||||||||||||||||||||||||||||||||||||||||||||||||
icon: FC<SvgProps>; | ||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
iconSize: number; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} & BankAccount & | ||||||||||||||||||||||||||||||||||||||||||||||||||
Fund; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Check to see if user has either a debit card or personal bank account added | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
function hasExpensifyPaymentMethod(fundList: Record<string, Fund>, bankAccountList: Record<string, BankAccount>): boolean { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const validBankAccount = Object.values(bankAccountList).some((bankAccountJSON) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const bankAccount = new BankAccountModel(bankAccountJSON); | ||||||||||||||||||||||||||||||||||||||||||||||||||
return bankAccount.isDefaultCredit(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
// Hide any billing cards that are not P2P debit cards for now because you cannot make them your default method, or delete them | ||||||||||||||||||||||||||||||||||||||||||||||||||
const validDebitCard = Object.values(fundList).some((card) => card?.accountData?.additionalData?.isP2PDebitCard ?? false); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return validBankAccount || validDebitCard; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
function getPaymentMethodDescription(accountType: AccountType, account: BankAccount['accountData'] & Fund['accountData']): string { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (account) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (accountType === CONST.PAYMENT_METHODS.BANK_ACCOUNT && account?.accountNumber) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return `${Localize.translateLocal('paymentMethodList.accountLastFour')} ${account?.accountNumber?.slice(-4)}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (accountType === CONST.PAYMENT_METHODS.DEBIT_CARD) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return `${Localize.translateLocal('paymentMethodList.cardLastFour')} ${account?.cardNumber?.slice(-4)}`; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
return ''; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
/** | ||||||||||||||||||||||||||||||||||||||||||||||||||
* Get the PaymentMethods list | ||||||||||||||||||||||||||||||||||||||||||||||||||
*/ | ||||||||||||||||||||||||||||||||||||||||||||||||||
function formatPaymentMethods(bankAccountList: Record<string, BankAccount>, fundList: Record<string, Fund>) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
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.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const combinedPaymentMethods: PaymentMethod[] = []; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Object.values(bankAccountList).forEach((bankAccount) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
// Add all bank accounts besides the wallet | ||||||||||||||||||||||||||||||||||||||||||||||||||
if (bankAccount?.accountData?.type === CONST.BANK_ACCOUNT_TYPES.WALLET) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
return; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
const {icon, iconSize} = getBankIcon(bankAccount?.accountData?.additionalData?.bankName ?? '', false); | ||||||||||||||||||||||||||||||||||||||||||||||||||
combinedPaymentMethods.push({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
...bankAccount, | ||||||||||||||||||||||||||||||||||||||||||||||||||
description: getPaymentMethodDescription(bankAccount?.accountType as AccountType, bankAccount.accountData), | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. No need for assertion after changes above. |
||||||||||||||||||||||||||||||||||||||||||||||||||
icon, | ||||||||||||||||||||||||||||||||||||||||||||||||||
iconSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
Object.values(fundList).forEach((card) => { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const {icon, iconSize} = getBankIcon(card?.accountData?.bank ?? '', true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
combinedPaymentMethods.push({ | ||||||||||||||||||||||||||||||||||||||||||||||||||
...card, | ||||||||||||||||||||||||||||||||||||||||||||||||||
description: getPaymentMethodDescription(card?.accountType as AccountType, card.accountData), | ||||||||||||||||||||||||||||||||||||||||||||||||||
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. No need for assertion after changes above. |
||||||||||||||||||||||||||||||||||||||||||||||||||
icon, | ||||||||||||||||||||||||||||||||||||||||||||||||||
iconSize, | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
return combinedPaymentMethods; | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
function calculateWalletTransferBalanceFee(currentBalance: number, methodType: string): number { | ||||||||||||||||||||||||||||||||||||||||||||||||||
const transferMethodTypeFeeStructure = | ||||||||||||||||||||||||||||||||||||||||||||||||||
methodType === CONST.WALLET.TRANSFER_METHOD_TYPE.INSTANT ? CONST.WALLET.TRANSFER_METHOD_TYPE_FEE.INSTANT : CONST.WALLET.TRANSFER_METHOD_TYPE_FEE.ACH; | ||||||||||||||||||||||||||||||||||||||||||||||||||
const calculateFee = Math.ceil(currentBalance * (transferMethodTypeFeeStructure.RATE / 100)); | ||||||||||||||||||||||||||||||||||||||||||||||||||
return Math.max(calculateFee, transferMethodTypeFeeStructure.MINIMUM_FEE); | ||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||
export {hasExpensifyPaymentMethod, getPaymentMethodDescription, formatPaymentMethods, calculateWalletTransferBalanceFee}; |
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.