Repository navigation
fix: 79312 better existing acc detection #81185
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
Changes from all commits
a2747ba
d5b15bd
5383489
8b9e3e8
7867ebd
b53280e
05f1716
1217955
f4eebc7
a330463
250cece
fb95d38
f51b6e8
967978e
08261c5
0a95681
5f96410
3093035
2504568
2b03dc4
4ef746e
c7ad2b3
d2cd3b6
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 |
|---|---|---|
|
|
@@ -15,8 +15,19 @@ function isBankAccountPartiallySetup(state: string | undefined) { | |
| return state === CONST.BANK_ACCOUNT.STATE.SETUP || state === CONST.BANK_ACCOUNT.STATE.VERIFYING || state === CONST.BANK_ACCOUNT.STATE.PENDING; | ||
| } | ||
|
|
||
| function doesPolicyHavePartiallySetupBankAccount(bankAccountList: OnyxEntry<OnyxTypes.BankAccountList>, policyID: string) { | ||
|
Contributor
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. ❌ PERF-2 (docs)The function performs expensive work ( Suggested fix: function doesPolicyHavePartiallySetupBankAccount(bankAccountList: OnyxEntry<OnyxTypes.BankAccountList>, policyID: string) {
if (!bankAccountList) {
return false;
}
const bankAccounts = Object.values(bankAccountList);
const matchingBankAccount = bankAccounts.find((bankAccount) => bankAccount.accountData?.policyIDs?.includes(policyID));
return isBankAccountPartiallySetup(matchingBankAccount?.accountData?.state);
}This avoids calling Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency. |
||
| if (!bankAccountList) { | ||
| return false; | ||
| } | ||
|
|
||
| const bankAccounts = Object.values(bankAccountList); | ||
| const matchingBankAccount = bankAccounts.find((bankAccount) => bankAccount.accountData?.policyIDs?.includes(policyID)); | ||
|
|
||
| return isBankAccountPartiallySetup(matchingBankAccount?.accountData?.state); | ||
| } | ||
|
|
||
| function hasPartiallySetupBankAccount(bankAccountList: OnyxEntry<OnyxTypes.BankAccountList>): boolean { | ||
| return Object.values(bankAccountList ?? {}).some((bankAccount) => isBankAccountPartiallySetup(bankAccount?.accountData?.state)); | ||
| } | ||
|
|
||
| export {getDefaultCompanyWebsite, getLastFourDigits, hasPartiallySetupBankAccount, isBankAccountPartiallySetup}; | ||
| export {getDefaultCompanyWebsite, getLastFourDigits, hasPartiallySetupBankAccount, isBankAccountPartiallySetup, doesPolicyHavePartiallySetupBankAccount}; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -226,6 +226,7 @@ | |
| state?: string; | ||
| lastPaymentMethod?: LastPaymentMethodType | string; | ||
| shouldUpdateLastPaymentMethod?: boolean; | ||
| bankAccountList?: BankAccountList; | ||
| }; | ||
|
|
||
| type SetWorkspaceApprovalModeAdditionalData = { | ||
|
|
@@ -235,7 +236,7 @@ | |
| }; | ||
|
|
||
| const deprecatedAllPolicies: OnyxCollection<Policy> = {}; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.POLICY, | ||
| callback: (val, key) => { | ||
| if (!key) { | ||
|
|
@@ -251,7 +252,7 @@ | |
| }); | ||
|
|
||
| let deprecatedAllReportActions: OnyxCollection<ReportActions>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, | ||
| waitForCollectionCallback: true, | ||
| callback: (actions) => { | ||
|
|
@@ -261,7 +262,7 @@ | |
|
|
||
| let deprecatedSessionEmail = ''; | ||
| let deprecatedSessionAccountID = 0; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.SESSION, | ||
| callback: (val) => { | ||
| deprecatedSessionEmail = val?.email ?? ''; | ||
|
|
@@ -270,7 +271,7 @@ | |
| }); | ||
|
|
||
| let deprecatedAllPersonalDetails: OnyxEntry<PersonalDetailsList>; | ||
| Onyx.connect({ | ||
| key: ONYXKEYS.PERSONAL_DETAILS_LIST, | ||
| callback: (val) => (deprecatedAllPersonalDetails = val), | ||
| }); | ||
|
|
@@ -1102,13 +1103,23 @@ | |
| state, | ||
| lastPaymentMethod, | ||
| shouldUpdateLastPaymentMethod, | ||
| bankAccountList = {}, | ||
| }: SetWorkspaceReimbursementActionParams) { | ||
| // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850 | ||
| // eslint-disable-next-line @typescript-eslint/no-deprecated | ||
| const policy = getPolicy(policyID); | ||
| const lastUsedPaymentMethod = typeof lastPaymentMethod === 'string' ? lastPaymentMethod : lastPaymentMethod?.expense?.name; | ||
|
|
||
| const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [ | ||
| const oldBankAccountID = Object.keys(bankAccountList ?? {}).find((accountID) => { | ||
|
Contributor
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. I think we need to early return if
Contributor
Author
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. Is there a particular scenario you're concerned about?
Contributor
Author
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. This was already handled in ConnectExistingBusinessBankAccountPage but I've added the requested early return here as well. |
||
| const account = bankAccountList?.[accountID]; | ||
| return account?.accountData?.policyIDs?.includes(policyID); | ||
| }); | ||
|
|
||
| if (oldBankAccountID !== undefined && String(bankAccountID) === oldBankAccountID) { | ||
| return; | ||
| } | ||
|
|
||
| const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY | typeof ONYXKEYS.BANK_ACCOUNT_LIST>> = [ | ||
| { | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
|
|
@@ -1123,6 +1134,31 @@ | |
| }, | ||
| ]; | ||
|
|
||
| if (bankAccountID !== undefined && bankAccountList !== undefined) { | ||
|
Contributor
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. ❌ PERF-2 (docs)The code performs expensive work ( Suggested fix: if (bankAccountID === undefined || \!bankAccountList) {
return;
}
const optimisticBankAccountList: BankAccountList = {};
const oldBankAccountID = Object.keys(bankAccountList).find((accountID) => {
const account = bankAccountList[accountID];
return account?.accountData?.policyIDs?.includes(policyID);
});
// ... rest of the logicThis ensures early return before performing expensive iterations. Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
Author
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. Not only this case is almost identical to the one that written but it's wrong. If returned early here we would alter the logic of the method and wouldnt perform API call. |
||
| const optimisticBankAccountList: BankAccountList = {}; | ||
| if (oldBankAccountID) { | ||
| optimisticBankAccountList[oldBankAccountID] = { | ||
| ...optimisticBankAccountList[oldBankAccountID], | ||
| accountData: { | ||
| policyIDs: bankAccountList?.[oldBankAccountID]?.accountData?.policyIDs?.filter((id) => id !== policyID) ?? [], | ||
| }, | ||
| }; | ||
| } | ||
| const currentPolicyIDs = bankAccountList?.[bankAccountID]?.accountData?.policyIDs ?? []; | ||
| optimisticBankAccountList[bankAccountID] = { | ||
| ...optimisticBankAccountList[bankAccountID], | ||
| accountData: { | ||
| policyIDs: [...new Set([...currentPolicyIDs, policyID])], | ||
| }, | ||
| }; | ||
|
|
||
| optimisticData.push({ | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: ONYXKEYS.BANK_ACCOUNT_LIST, | ||
| value: optimisticBankAccountList, | ||
| }); | ||
| } | ||
|
|
||
| const successData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY | typeof ONYXKEYS.NVP_LAST_PAYMENT_METHOD>> = [ | ||
| { | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
|
|
@@ -1155,7 +1191,7 @@ | |
| }); | ||
| } | ||
|
|
||
| const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [ | ||
| const failureData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY | typeof ONYXKEYS.BANK_ACCOUNT_LIST>> = [ | ||
| { | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
|
|
@@ -1176,6 +1212,29 @@ | |
| }, | ||
| ]; | ||
|
|
||
| if (bankAccountID !== undefined && bankAccountList !== undefined) { | ||
| const failureBankAccountList: BankAccountList = {}; | ||
| if (oldBankAccountID) { | ||
| failureBankAccountList[oldBankAccountID] = { | ||
| ...failureBankAccountList[oldBankAccountID], | ||
| accountData: { | ||
| policyIDs: bankAccountList?.[oldBankAccountID]?.accountData?.policyIDs ?? [], | ||
| }, | ||
| }; | ||
| } | ||
| failureBankAccountList[bankAccountID] = { | ||
| ...failureBankAccountList[bankAccountID], | ||
| accountData: { | ||
| policyIDs: bankAccountList?.[bankAccountID]?.accountData?.policyIDs ?? [], | ||
| }, | ||
| }; | ||
| failureData.push({ | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: ONYXKEYS.BANK_ACCOUNT_LIST, | ||
| value: failureBankAccountList, | ||
| }); | ||
| } | ||
|
|
||
| const params: SetWorkspaceReimbursementParams = {policyID, reimbursementChoice, bankAccountID}; | ||
|
|
||
| API.write(WRITE_COMMANDS.SET_WORKSPACE_REIMBURSEMENT, params, {optimisticData, failureData, successData}); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ import type {PlatformStackScreenProps} from '@navigation/PlatformStackNavigation | |
| import type {ConnectExistingBankAccountNavigatorParamList} from '@navigation/types'; | ||
| import PaymentMethodList from '@pages/settings/Wallet/PaymentMethodList'; | ||
| import type {PaymentMethodPressHandlerParams} from '@pages/settings/Wallet/WalletPage/types'; | ||
| import {setReimbursementAccountLoading} from '@userActions/BankAccounts'; | ||
| import {setWorkspaceReimbursement} from '@userActions/Policy/Policy'; | ||
| import {navigateToBankAccountRoute} from '@userActions/ReimbursementAccount'; | ||
| import CONST from '@src/CONST'; | ||
|
|
@@ -27,6 +28,7 @@ function ConnectExistingBusinessBankAccountPage({route}: ConnectExistingBusiness | |
| const policyID = route.params?.policyID; | ||
| const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`); | ||
| const [lastPaymentMethod] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD); | ||
| const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST); | ||
| const policyName = policy?.name ?? ''; | ||
| const policyCurrency = policy?.outputCurrency ?? ''; | ||
| const {shouldUseNarrowLayout} = useResponsiveLayout(); | ||
|
|
@@ -44,23 +46,29 @@ function ConnectExistingBusinessBankAccountPage({route}: ConnectExistingBusiness | |
| } | ||
|
|
||
| const newReimburserEmail = policy?.achAccount?.reimburser ?? policy?.owner ?? ''; | ||
| setWorkspaceReimbursement({ | ||
| policyID, | ||
| reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, | ||
| bankAccountID: methodID ?? CONST.DEFAULT_NUMBER_ID, | ||
| reimburserEmail: newReimburserEmail, | ||
| accountNumber: accountData?.accountNumber, | ||
| addressName: accountData?.addressName, | ||
| bankName: accountData?.additionalData?.bankName, | ||
| state: accountData?.state, | ||
| lastPaymentMethod: lastPaymentMethod?.[policyID], | ||
| shouldUpdateLastPaymentMethod: accountData?.state === CONST.BANK_ACCOUNT.STATE.OPEN, | ||
| }); | ||
|
|
||
| if (bankAccountList && methodID && !bankAccountList[methodID]?.accountData?.policyIDs?.includes(policyID)) { | ||
|
Contributor
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. ❌ CONSISTENCY-6 (docs)The code checks if Suggested fix: if (bankAccountList && methodID && !bankAccountList[methodID]?.accountData?.policyIDs?.includes(policyID)) {
setWorkspaceReimbursement({
// ... existing code
});
setReimbursementAccountLoading(true);
} else {
// Log or notify when bank account is already linked
Log.info([ConnectExistingBusinessBankAccountPage] Bank account already linked to policy, {policyID, methodID});
}Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
Contributor
Author
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. If it's already linked then we do not link it again and do nothing |
||
| setWorkspaceReimbursement({ | ||
| policyID, | ||
| reimbursementChoice: CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_YES, | ||
| bankAccountID: methodID ?? CONST.DEFAULT_NUMBER_ID, | ||
| reimburserEmail: newReimburserEmail, | ||
| accountNumber: accountData?.accountNumber, | ||
| addressName: accountData?.addressName, | ||
| bankName: accountData?.additionalData?.bankName, | ||
| state: accountData?.state, | ||
| lastPaymentMethod: lastPaymentMethod?.[policyID], | ||
| shouldUpdateLastPaymentMethod: accountData?.state === CONST.BANK_ACCOUNT.STATE.OPEN, | ||
| bankAccountList, | ||
| }); | ||
| setReimbursementAccountLoading(true); | ||
| } | ||
|
|
||
| Navigation.setNavigationActionToMicrotaskQueue(() => { | ||
| if (isBankAccountPartiallySetup(accountData?.state)) { | ||
| navigateToBankAccountRoute({policyID: route.params.policyID}); | ||
| navigateToBankAccountRoute({policyID: route.params.policyID, navigationOptions: {forceReplace: true}}); | ||
| } else { | ||
| setReimbursementAccountLoading(false); | ||
| Navigation.closeRHPFlow(); | ||
| } | ||
| }); | ||
|
|
||
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.
❌ PERF-2 (docs)
The check
if (policy?.id !== undefinedhappens after accessingdoesPolicyHavePartiallySetupBankAccount(bankAccountList, policy.id), which could potentially cause issues ifpolicy.idis undefined.Suggested fix:
This ensures early return before calling functions with potentially undefined values.
Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
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.
It does not need to be explicitly formatted as standalone early return clause, short-circuit happens in correct order and PERF-2 can be followed this way too.