Repository navigation
fix: 79312 better existing acc detection - #81185
srikarparsi merged 23 commits into
Conversation
…2-better-existing-acc-detection
Codecov Report❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.
|
…2-better-existing-acc-detection
…2-better-existing-acc-detection
|
🚧 @joekaufmanexpensify has triggered a test Expensify/App build. You can view the workflow run here. |
This comment has been minimized.
This comment has been minimized.
|
@dominictb Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button] |
| // - account already present on policy is partially setup | ||
| // - account is being connected 'on the spot' while trying to pay for an expense (it won't be linked to policy yet but will appear as reimbursementAccount) | ||
| if (policy !== undefined && (isBankAccountPartiallySetup(policy?.achAccount?.state) || isBankAccountPartiallySetup(reimbursementAccount?.achData?.state))) { | ||
| if (policy?.id !== undefined && doesPolicyHavePartiallySetupBankAccount(bankAccountList, policy.id)) { |
There was a problem hiding this comment.
❌ PERF-2 (docs)
The check if (policy?.id !== undefined happens after accessing doesPolicyHavePartiallySetupBankAccount(bankAccountList, policy.id), which could potentially cause issues if policy.id is undefined.
Suggested fix:
if (!policy?.id) {
return;
}
if (doesPolicyHavePartiallySetupBankAccount(bankAccountList, policy.id)) {
navigateToBankAccountRoute(policy.id);
return;
}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.
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.
| 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) { |
There was a problem hiding this comment.
❌ PERF-2 (docs)
The function performs expensive work (Object.values, .find()) before checking if the result exists.
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 Object.values on a potentially null/undefined value.
Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
| shouldUpdateLastPaymentMethod: accountData?.state === CONST.BANK_ACCOUNT.STATE.OPEN, | ||
| }); | ||
|
|
||
| if (bankAccountList && methodID && !bankAccountList[methodID]?.accountData?.policyIDs?.includes(policyID)) { |
There was a problem hiding this comment.
❌ CONSISTENCY-6 (docs)
The code checks if bankAccountList && methodID && !bankAccountList[methodID]?.accountData?.policyIDs?.includes(policyID) but there is no error handling for the case when these conditions are false. This creates a silent failure path where the user action does not provide feedback.
Suggested fix:
Add logging or user feedback when the bank account is already connected to the policy:
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.
There was a problem hiding this comment.
If it's already linked then we do not link it again and do nothing
| }, | ||
| ]; | ||
|
|
||
| if (bankAccountID !== undefined && bankAccountList !== undefined) { |
There was a problem hiding this comment.
❌ PERF-2 (docs)
The code performs expensive work (Object.keys(bankAccountList ?? {}).find()) before checking if bankAccountID and bankAccountList are defined.
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.
There was a problem hiding this comment.
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.
|
|
||
| /** Returns true if VBBA flow is in progress */ | ||
| const hasInProgressVBBA = (achData?: ACHDataReimbursementAccount, isNonUSDWorkspace?: boolean, nonUSDCountryDraftValue?: string) => { | ||
| const hasInProgressVBBA = (achData?: ACHDataReimbursementAccount, isNonUSDWorkspace?: boolean, nonUSDCountryDraftValue?: string, policyID?: string) => { |
There was a problem hiding this comment.
❌ PERF-2 (docs)
The policyID check should be performed earlier, before calling the more expensive hasInProgressNonUSDVBBA or hasInProgressUSDVBBA functions.
Suggested fix:
const hasInProgressVBBA = (achData?: ACHDataReimbursementAccount, isNonUSDWorkspace?: boolean, nonUSDCountryDraftValue?: string, policyID?: string) => {
if (policyID && achData?.policyID !== policyID) {
return false;
}
if (isNonUSDWorkspace) {
return hasInProgressNonUSDVBBA(achData, nonUSDCountryDraftValue);
}
return hasInProgressUSDVBBA(achData);
};This checks the simple condition first before performing more complex validations.
Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
There was a problem hiding this comment.
It's not really changing anything
| @@ -27,6 +28,7 @@ function ConnectExistingBusinessBankAccountPage({route}: ConnectExistingBusiness | |||
| const policyID = route.params?.policyID; | |||
| const [policy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, {canBeMissing: false}); | |||
| const [lastPaymentMethod] = useOnyx(ONYXKEYS.NVP_LAST_PAYMENT_METHOD, {canBeMissing: true}); | |||
There was a problem hiding this comment.
❌ PERF-11 (docs)
The useOnyx call for ONYXKEYS.BANK_ACCOUNT_LIST fetches the entire bank account list without a selector, which may cause unnecessary re-renders when unrelated bank accounts change.
Suggested fix:
Consider using a selector to only subscribe to relevant changes:
const [bankAccountList] = useOnyx(ONYXKEYS.BANK_ACCOUNT_LIST, {
canBeMissing: true,
selector: (list) => {
if (!list) return undefined;
// Only include accounts relevant to this policy or all accounts if needed
return list;
},
});However, if the entire list is needed for the logic, this is acceptable.
Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.
There was a problem hiding this comment.
False positive. We need full list as we can freely swap accounts between different policies
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f4eebc7537
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| if (policy?.id !== undefined && doesPolicyHavePartiallySetupBankAccount(bankAccountList, policy.id)) { | ||
| navigateToBankAccountRoute(policy.id); |
There was a problem hiding this comment.
Restore fallback for unlinked in-progress VBBA flows
This check now gates continuation solely on bankAccountList[*].accountData.policyIDs, but the previous logic also handled the in-progress VBBA case stored in reimbursementAccount.achData before the account is linked to a policy. In that flow, policyIDs is not yet populated, so users who started setup from KYCWall and return can be sent to the connect/add flow instead of resuming navigateToBankAccountRoute(policy.id), effectively restarting setup.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
If user made substantial progress in the flow (saved some info) the account will always be present in bankAccountList
| key: ONYXKEYS.BANK_ACCOUNT_LIST, | ||
| value: bankAccountList, |
There was a problem hiding this comment.
Avoid rolling back the entire bank account list on failure
Reverting BANK_ACCOUNT_LIST with the full pre-request snapshot means any unrelated bank-account updates that happen while SetWorkspaceReimbursement is in flight (for example, other optimistic/server merges on existing accounts) are overwritten if this request fails. The rollback should be scoped to only the account entries touched by this action to prevent stale data from clobbering concurrent updates.
Useful? React with 👍 / 👎.
|
🚧 @joekaufmanexpensify has triggered a test Expensify/App build. You can view the workflow run here. |
joekaufmanexpensify
left a comment
There was a problem hiding this comment.
Good from my perspective 👍
…2-better-existing-acc-detection
|
|
||
| const isDefaultReimbursementAccountData = deepEqual(reimbursementAccount, CONST.REIMBURSEMENT_ACCOUNT.DEFAULT_DATA); | ||
| const hasLoadedData = reimbursementAccount?.achData && !isDefaultReimbursementAccountData && reimbursementAccount?.isLoading === false; | ||
| // reimbursementAccount?.isLoading can be undefined and data can be already fetched (returned via different command). Because of that we need to check whether isLoading it's not truthy |
There was a problem hiding this comment.
| // reimbursementAccount?.isLoading can be undefined and data can be already fetched (returned via different command). Because of that we need to check whether isLoading it's not truthy |
I think we don't need this comment
| const lastUsedPaymentMethod = typeof lastPaymentMethod === 'string' ? lastPaymentMethod : lastPaymentMethod?.expense?.name; | ||
|
|
||
| const optimisticData: Array<OnyxUpdate<typeof ONYXKEYS.COLLECTION.POLICY>> = [ | ||
| const oldBankAccountID = Object.keys(bankAccountList ?? {}).find((accountID) => { |
There was a problem hiding this comment.
I think we need to early return if oldBankAccountID === bankAccountID. The check in ConnectExistingBusinessBankAccountPage.tsx is not sufficient.
There was a problem hiding this comment.
Is there a particular scenario you're concerned about?
There was a problem hiding this comment.
This was already handled in ConnectExistingBusinessBankAccountPage but I've added the requested early return here as well.
If somebody would like to use this action in a different place it might've become a problem
| optimisticBankAccountList[bankAccountID] = { | ||
| ...optimisticBankAccountList[bankAccountID], | ||
| accountData: { | ||
| policyIDs: [...currentPolicyIDs, policyID], |
There was a problem hiding this comment.
We can use Set here to avoid duplication.
…2-better-existing-acc-detection
…2-better-existing-acc-detection
|
@MrMuzyk I think the test steps in PR description is outdated. Now once linked, we cannot go back to the existing bank account page to link a new account. Can you update it? |
|
I think a good one is: |
Reviewer Checklist
Screenshots/VideosAndroid: HybridAppAndroid: mWeb ChromeiOS: HybridAppScreen.Recording.2026-02-26.at.21.07.02.moviOS: mWeb SafariMacOS: Chrome / SafariScreen.Recording.2026-02-26.at.21.13.15.mov |
|
@dominictb Fixed |
|
Nice, on to you @srikarparsi |
…2-better-existing-acc-detection
…2-better-existing-acc-detection
|
🚧 @srikarparsi has triggered a test Expensify/App build. You can view the workflow run here. |
|
🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
|
|
🚀 Deployed to staging by https://github.com/srikarparsi in version: 9.3.31-0 🚀
|
|
🚀 Deployed to production by https://github.com/blimpich in version: 9.3.31-12 🚀
|
Explanation of Change
Improve detection of connected account basing it off policyIDs inside bankAccountList
Preemptively set loading indicator for reimbursementAccount when we know we will be fetching the data right after navigate
Fixed Issues
$ #79312
PROPOSAL:
Tests
Same as QA steps
Offline tests
QA Steps
Scenario A:
Precondition:
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectioncanBeMissingparam foruseOnyxtoggleReportand notonIconClick)src/languages/*files and using the translation methodSTYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.ScrollViewcomponent to make it scrollable when more elements are added to the page.mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
android.native.mp4
Android: mWeb Chrome
android.web.mp4
iOS: Native
ios.native.mp4
iOS: mWeb Safari
ios.web.mp4
MacOS: Chrome / Safari
web.mp4