From c75ad9db2c34dfc4bad1465acfd75f9d07b926ad Mon Sep 17 00:00:00 2001 From: "Bernhard Owen Josephus (via MelvinBot)" Date: Wed, 10 Jun 2026 08:13:42 +0000 Subject: [PATCH 1/2] Scope bank account Share eligibility to the account's own workspace Both hasEligibleActiveAdminFromWorkspaces and getEligibleBankAccountShareRecipients now use getActiveAdminWorkspaces filtered to the bank account's additionalData.policyID, so the Share option and recipient list only consider admins from the workspace that owns the bank account. Co-authored-by: Bernhard Owen Josephus --- src/libs/PolicyUtils.ts | 6 +- tests/unit/PolicyUtilsTest.ts | 142 ++++++++++++++++++++++++++++++++-- 2 files changed, 138 insertions(+), 10 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index ee7991dd2960..d251e142ceab 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -320,7 +320,8 @@ function getDistanceRateCustomUnitRate(policy: OnyxEntry, customUnitRate */ function getEligibleBankAccountShareRecipients(policies: OnyxCollection | null, currentUserLogin: string | undefined, bankAccountID: string | undefined): MemberForList[] { const currentBankAccount = getBankAccountFromID(Number(bankAccountID)); - const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin); + const bankAccountPolicyID = currentBankAccount?.accountData?.additionalData?.policyID; + const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin).filter((policy) => policy.id === bankAccountPolicyID); if (!activePolicies) { return []; } @@ -361,7 +362,8 @@ function getEligibleBankAccountShareRecipients(policies: OnyxCollection */ function hasEligibleActiveAdminFromWorkspaces(policies: OnyxCollection | null, currentUserLogin: string | undefined, bankAccountID: string | undefined): boolean { const currentBankAccount = getBankAccountFromID(Number(bankAccountID)); - const activePolicies = getActivePolicies(policies, currentUserLogin); + const bankAccountPolicyID = currentBankAccount?.accountData?.additionalData?.policyID; + const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin).filter((policy) => policy.id === bankAccountPolicyID); if (!activePolicies) { return false; } diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 02c76022fb8f..0f0464c28387 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -34,6 +34,7 @@ import { hasConfiguredRules, hasDependentTags, hasDynamicExternalWorkflow, + hasEligibleActiveAdminFromWorkspaces, hasIndependentTags, hasOnlyPersonalPolicies, hasOtherControlWorkspaces, @@ -1679,16 +1680,34 @@ describe('PolicyUtils', () => { await Onyx.clear(); await waitForBatchedUpdatesWithAct(); }); - it('should return empty array if no admins in policies', () => { + it('should return empty array if no admins in policies', async () => { + const bankAccountID = '1'; + await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { + 1: { + methodID: 12345, + accountData: { + additionalData: {policyID: '1'}, + }, + }, + }); const policies = { '1': {...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM), pendingAction: undefined}, '2': {...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), pendingAction: undefined}, }; - const result = getEligibleBankAccountShareRecipients(policies, approverEmail, '1'); + const result = getEligibleBankAccountShareRecipients(policies, approverEmail, bankAccountID); expect(result).toHaveLength(0); }); - it('should return array with admins', () => { + it('should return array with admins from the bank account workspace', async () => { + const bankAccountID = '1'; const currentUserLogin = adminEmail; + await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { + 1: { + methodID: 12345, + accountData: { + additionalData: {policyID: '1'}, + }, + }, + }); const policies = { '1': { @@ -1701,9 +1720,43 @@ describe('PolicyUtils', () => { }, '2': {...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), pendingAction: undefined}, }; - const result = getEligibleBankAccountShareRecipients(policies, approverEmail, '1'); + const result = getEligibleBankAccountShareRecipients(policies, approverEmail, bankAccountID); expect(result).toHaveLength(1); }); + it('should not return admins from workspaces other than the bank account workspace', async () => { + const bankAccountID = '1'; + await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { + 1: { + methodID: 12345, + accountData: { + additionalData: {policyID: '1'}, + }, + }, + }); + + const policies = { + // The bank account's own workspace - current user is the only admin + '1': { + ...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM), + pendingAction: undefined, + role: CONST.POLICY.ROLE.ADMIN, + employeeList: { + [adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN}, + }, + }, + // An unrelated workspace with another admin - must not leak into the recipient list + '2': { + ...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), + pendingAction: undefined, + role: CONST.POLICY.ROLE.ADMIN, + employeeList: { + [approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN}, + }, + }, + }; + const result = getEligibleBankAccountShareRecipients(policies, adminEmail, bankAccountID); + expect(result).toHaveLength(0); + }); it('should not return user with already shared bank account', async () => { const bankAccountID = '1'; const currentUserLogin = adminEmail; @@ -1711,6 +1764,7 @@ describe('PolicyUtils', () => { 1: { methodID: 12345, accountData: { + additionalData: {policyID: '1'}, sharees: [adminEmail], }, }, @@ -1732,6 +1786,14 @@ describe('PolicyUtils', () => { }); it('should not return current user for sharing account', async () => { const bankAccountID = '1'; + await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { + 1: { + methodID: 12345, + accountData: { + additionalData: {policyID: '1'}, + }, + }, + }); const policies = { '1': { @@ -1740,19 +1802,83 @@ describe('PolicyUtils', () => { role: CONST.POLICY.ROLE.ADMIN, employeeList: { [adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN}, + [approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN}, }, }, - '2': { - ...createRandomPolicy(2, CONST.POLICY.TYPE.CORPORATE), + }; + const result = getEligibleBankAccountShareRecipients(policies, adminEmail, bankAccountID); + expect(result).toHaveLength(1); + }); + }); + + describe('hasEligibleActiveAdminFromWorkspaces', () => { + beforeEach(() => { + wrapOnyxWithWaitForBatchedUpdates(Onyx); + Onyx.set(ONYXKEYS.PERSONAL_DETAILS_LIST, personalDetails); + }); + afterEach(async () => { + await Onyx.clear(); + await waitForBatchedUpdatesWithAct(); + }); + it('should return true when another admin is available in the bank account workspace', async () => { + const bankAccountID = '1'; + await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { + 1: { + methodID: 12345, + accountData: { + additionalData: {policyID: '1'}, + }, + }, + }); + + const policies = { + '1': { + ...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM), pendingAction: undefined, role: CONST.POLICY.ROLE.ADMIN, employeeList: { + [adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN}, [approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN}, }, }, }; - const result = getEligibleBankAccountShareRecipients(policies, adminEmail, bankAccountID); - expect(result).toHaveLength(1); + const result = hasEligibleActiveAdminFromWorkspaces(policies, adminEmail, bankAccountID); + expect(result).toBe(true); + }); + it('should return false when the user only joined another workspace as a member', async () => { + const bankAccountID = '1'; + await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { + 1: { + methodID: 12345, + accountData: { + additionalData: {policyID: '1'}, + }, + }, + }); + + const policies = { + // The bank account's own workspace - current user is the only admin, no one to share with + '1': { + ...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM), + pendingAction: undefined, + role: CONST.POLICY.ROLE.ADMIN, + employeeList: { + [adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN}, + }, + }, + // Another user's workspace the current user only joined as a member - has its own admin + '2': { + ...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), + pendingAction: undefined, + role: CONST.POLICY.ROLE.USER, + employeeList: { + [approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN}, + [adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.USER}, + }, + }, + }; + const result = hasEligibleActiveAdminFromWorkspaces(policies, adminEmail, bankAccountID); + expect(result).toBe(false); }); }); From 3d1bcde9d30451448008375bf3e99787bd1c7c62 Mon Sep 17 00:00:00 2001 From: "Bernhard Owen Josephus (via MelvinBot)" Date: Wed, 10 Jun 2026 09:10:52 +0000 Subject: [PATCH 2/2] Revert workspace filtering, keep both functions using getActiveAdminWorkspaces Drop the policyID-based filtering that scoped Share eligibility to the bank account's own workspace. Both functions now consistently use getActiveAdminWorkspaces without changing the recipient-list behavior. Co-authored-by: Bernhard Owen Josephus --- src/libs/PolicyUtils.ts | 6 ++---- tests/unit/PolicyUtilsTest.ts | 34 ---------------------------------- 2 files changed, 2 insertions(+), 38 deletions(-) diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index d251e142ceab..3c5545c01c20 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -320,8 +320,7 @@ function getDistanceRateCustomUnitRate(policy: OnyxEntry, customUnitRate */ function getEligibleBankAccountShareRecipients(policies: OnyxCollection | null, currentUserLogin: string | undefined, bankAccountID: string | undefined): MemberForList[] { const currentBankAccount = getBankAccountFromID(Number(bankAccountID)); - const bankAccountPolicyID = currentBankAccount?.accountData?.additionalData?.policyID; - const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin).filter((policy) => policy.id === bankAccountPolicyID); + const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin); if (!activePolicies) { return []; } @@ -362,8 +361,7 @@ function getEligibleBankAccountShareRecipients(policies: OnyxCollection */ function hasEligibleActiveAdminFromWorkspaces(policies: OnyxCollection | null, currentUserLogin: string | undefined, bankAccountID: string | undefined): boolean { const currentBankAccount = getBankAccountFromID(Number(bankAccountID)); - const bankAccountPolicyID = currentBankAccount?.accountData?.additionalData?.policyID; - const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin).filter((policy) => policy.id === bankAccountPolicyID); + const activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin); if (!activePolicies) { return false; } diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 0f0464c28387..732d23141e62 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -1723,40 +1723,6 @@ describe('PolicyUtils', () => { const result = getEligibleBankAccountShareRecipients(policies, approverEmail, bankAccountID); expect(result).toHaveLength(1); }); - it('should not return admins from workspaces other than the bank account workspace', async () => { - const bankAccountID = '1'; - await Onyx.set(ONYXKEYS.BANK_ACCOUNT_LIST, { - 1: { - methodID: 12345, - accountData: { - additionalData: {policyID: '1'}, - }, - }, - }); - - const policies = { - // The bank account's own workspace - current user is the only admin - '1': { - ...createRandomPolicy(1, CONST.POLICY.TYPE.TEAM), - pendingAction: undefined, - role: CONST.POLICY.ROLE.ADMIN, - employeeList: { - [adminEmail]: {email: adminEmail, role: CONST.POLICY.ROLE.ADMIN}, - }, - }, - // An unrelated workspace with another admin - must not leak into the recipient list - '2': { - ...createRandomPolicy(2, CONST.POLICY.TYPE.TEAM), - pendingAction: undefined, - role: CONST.POLICY.ROLE.ADMIN, - employeeList: { - [approverEmail]: {email: approverEmail, role: CONST.POLICY.ROLE.ADMIN}, - }, - }, - }; - const result = getEligibleBankAccountShareRecipients(policies, adminEmail, bankAccountID); - expect(result).toHaveLength(0); - }); it('should not return user with already shared bank account', async () => { const bankAccountID = '1'; const currentUserLogin = adminEmail;