diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index ee7991dd2960..3c5545c01c20 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -361,7 +361,7 @@ 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 activePolicies = getActiveAdminWorkspaces(policies, currentUserLogin); if (!activePolicies) { return false; } diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 02c76022fb8f..732d23141e62 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,7 +1720,7 @@ 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 user with already shared bank account', async () => { @@ -1711,6 +1730,7 @@ describe('PolicyUtils', () => { 1: { methodID: 12345, accountData: { + additionalData: {policyID: '1'}, sharees: [adminEmail], }, }, @@ -1732,6 +1752,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 +1768,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); }); });