Repository navigation
Detach workspace bank accounts when disabling Payments #106102
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
951192f
7587f13
f40561b
eb0b7cb
3986bf7
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 |
|---|---|---|
|
|
@@ -1503,6 +1503,7 @@ function setWorkspaceReimbursement({ | |
| bankAccountList = {}, | ||
| }: SetWorkspaceReimbursementActionParams) { | ||
| const lastUsedPaymentMethod = typeof lastPaymentMethod === 'string' ? lastPaymentMethod : lastPaymentMethod?.expense?.name; | ||
| const shouldClearBankAccountID = reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO; | ||
|
|
||
| const oldBankAccountID = Object.keys(bankAccountList ?? {}).find((accountID) => { | ||
| const account = bankAccountList?.[accountID]; | ||
|
|
@@ -1511,7 +1512,7 @@ function setWorkspaceReimbursement({ | |
|
|
||
| // A bank account can list the workspace in its policyIDs while the workspace itself points at another account, | ||
| // so the selection is only redundant when the workspace's own bank account is the selected one too. | ||
| if (oldBankAccountID !== undefined && String(bankAccountID) === oldBankAccountID && currentAchAccount?.bankAccountID === bankAccountID) { | ||
| if (!shouldClearBankAccountID && oldBankAccountID !== undefined && String(bankAccountID) === oldBankAccountID && currentAchAccount?.bankAccountID === bankAccountID) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -1523,14 +1524,14 @@ function setWorkspaceReimbursement({ | |
| reimbursementChoice, | ||
| isLoadingWorkspaceReimbursement: true, | ||
| reimburser: reimburserEmail, | ||
| achAccount: {reimburser: reimburserEmail, bankAccountID, accountNumber, addressName, bankName, state}, | ||
| achAccount: shouldClearBankAccountID ? null : {reimburser: reimburserEmail, bankAccountID, accountNumber, addressName, bankName, state}, | ||
| errorFields: {reimbursementChoice: null}, | ||
| pendingFields: {reimbursementChoice: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| if (bankAccountID !== undefined && bankAccountList !== undefined) { | ||
| if (!shouldClearBankAccountID && bankAccountID !== undefined && bankAccountList !== undefined) { | ||
| const optimisticBankAccountList: NullishDeep<BankAccountList> = {}; | ||
| if (oldBankAccountID) { | ||
| optimisticBankAccountList[oldBankAccountID] = { | ||
|
|
@@ -1596,21 +1597,23 @@ function setWorkspaceReimbursement({ | |
| value: { | ||
| isLoadingWorkspaceReimbursement: false, | ||
| reimbursementChoice: currentReimbursementChoice ?? null, | ||
| achAccount: { | ||
| reimburser: currentAchAccount?.reimburser ?? null, | ||
| bankAccountID: currentAchAccount?.bankAccountID ?? null, | ||
| accountNumber: currentAchAccount?.accountNumber ?? null, | ||
| addressName: currentAchAccount?.addressName ?? null, | ||
| bankName: currentAchAccount?.bankName ?? null, | ||
| state: currentAchAccount?.state ?? null, | ||
| }, | ||
| achAccount: shouldClearBankAccountID | ||
| ? (currentAchAccount ?? null) | ||
|
Comment on lines
1599
to
+1601
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.
When a user toggles Payments back on before an offline or slow disable request finishes—a flow explicitly exercised by the added test—the second call applies its optimistic manual/no-bank state immediately, but a failure of the first request unconditionally restores the previous reimbursement choice, ACH account, and bank associations. The later re-enable success only clears pending flags, so it never reapplies its state; the old bank therefore appears connected even though the server processed the later enable without a bank ID. Make this rollback conflict-aware or prevent another toggle while the request is pending. AGENTS.md reference: AGENTS.md:L97-L102 Useful? React with 👍 / 👎. |
||
| : { | ||
| reimburser: currentAchAccount?.reimburser ?? null, | ||
| bankAccountID: currentAchAccount?.bankAccountID ?? null, | ||
| accountNumber: currentAchAccount?.accountNumber ?? null, | ||
| addressName: currentAchAccount?.addressName ?? null, | ||
| bankName: currentAchAccount?.bankName ?? null, | ||
| state: currentAchAccount?.state ?? null, | ||
| }, | ||
| errorFields: {reimbursementChoice: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('common.genericErrorMessage')}, | ||
| pendingFields: {reimbursementChoice: null}, | ||
| }, | ||
| }, | ||
| ]; | ||
|
|
||
| if (bankAccountID !== undefined && bankAccountList !== undefined) { | ||
| if (!shouldClearBankAccountID && bankAccountID !== undefined && bankAccountList !== undefined) { | ||
| const failureBankAccountList: NullishDeep<BankAccountList> = {}; | ||
| if (oldBankAccountID) { | ||
| failureBankAccountList[oldBankAccountID] = { | ||
|
|
@@ -1636,7 +1639,47 @@ function setWorkspaceReimbursement({ | |
| } | ||
|
|
||
| // Turning payments off also stops collecting employee bank details and clears the reimbursement countries | ||
| if (reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO) { | ||
| if (shouldClearBankAccountID) { | ||
| const optimisticBankAccountList: NullishDeep<BankAccountList> = {}; | ||
| const failureBankAccountList: NullishDeep<BankAccountList> = {}; | ||
|
|
||
| // Incomplete setups can link a workspace without an ACH account or a policyIDs entry. | ||
| // Clear both association forms without detaching other workspaces that use the same account. | ||
| for (const [accountID, account] of Object.entries(bankAccountList ?? {})) { | ||
| const policyIDs = account?.accountData?.policyIDs; | ||
| const isLinkedToWorkspace = policyIDs?.includes(policyID); | ||
| const isSetupForWorkspace = account?.accountData?.additionalData?.policyID === policyID; | ||
| if (!isLinkedToWorkspace && !isSetupForWorkspace) { | ||
| continue; | ||
| } | ||
|
|
||
| optimisticBankAccountList[accountID] = { | ||
| accountData: { | ||
| ...(isLinkedToWorkspace ? {policyIDs: policyIDs?.filter((id) => id !== policyID)} : {}), | ||
| ...(isSetupForWorkspace ? {additionalData: {policyID: null}} : {}), | ||
| }, | ||
| }; | ||
| failureBankAccountList[accountID] = { | ||
| accountData: { | ||
| ...(isLinkedToWorkspace ? {policyIDs} : {}), | ||
| ...(isSetupForWorkspace ? {additionalData: {policyID}} : {}), | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| if (Object.keys(optimisticBankAccountList).length > 0) { | ||
| optimisticData.push({ | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: ONYXKEYS.BANK_ACCOUNT_LIST, | ||
| value: optimisticBankAccountList, | ||
| }); | ||
| failureData.push({ | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: ONYXKEYS.BANK_ACCOUNT_LIST, | ||
| value: failureBankAccountList, | ||
| }); | ||
| } | ||
|
|
||
| optimisticData.push({ | ||
| onyxMethod: Onyx.METHOD.MERGE, | ||
| key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, | ||
|
|
@@ -1652,7 +1695,6 @@ function setWorkspaceReimbursement({ | |
| }); | ||
| } | ||
|
|
||
| const shouldClearBankAccountID = reimbursementChoice === CONST.POLICY.REIMBURSEMENT_CHOICES.REIMBURSEMENT_NO; | ||
| const params: SetWorkspaceReimbursementParams = { | ||
| policyID, | ||
| reimbursementChoice, | ||
|
|
||
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.
❌ CONSISTENCY-18 (docs)
The early-return guard gained two new clauses (
!shouldClearBankAccountIDandreimbursementChoice === currentReimbursementChoice), but the comment directly above it only explains the original "the bank account selection is only redundant when the workspace's own bank account is selected" reasoning. The newreimbursementChoice === currentReimbursementChoiceclause is non-obvious — it is specifically there so a reimbursement-choice change (e.g. switching MANUAL -> YES with the same bank) is not mistakenly skipped as a redundant bank selection, which is exactly what the newdoes not skip a reimbursement choice change when the bank account stays the sametest covers. Without a note, the next reader cannot tell why unchanged-bank-but-changed-choice must still fall through.Extend the comment to cover the new conditions, e.g.:
Reviewed at: eb0b7cb | Please rate this suggestion with 👍 or 👎 to help us improve! Reactions are used to monitor reviewer efficiency.