From c3d94dbd46b3752a0aa3c356b8d25f149d35120c Mon Sep 17 00:00:00 2001 From: Lorenzo Bloedow Date: Sun, 23 Nov 2025 20:24:17 -0300 Subject: [PATCH 1/3] Fix "detached" user in employeeList when inviting using a secondary login --- src/libs/actions/Policy/Member.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 86a68aef6716..f783083a6ee7 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -20,6 +20,7 @@ import Log from '@libs/Log'; import enhanceParameters from '@libs/Network/enhanceParameters'; import Parser from '@libs/Parser'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; +import {getPersonalDetailByEmail} from '@libs/PersonalDetailsUtils'; import * as PhoneNumber from '@libs/PhoneNumber'; import {getDefaultApprover, isUserPolicyAdmin} from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; @@ -388,7 +389,23 @@ function removeMembers(policyID: string, selectedMemberEmails: string[], policyM const optimisticMembersState: OnyxCollectionInputValue = {}; const successMembersState: OnyxCollectionInputValue = {}; const failureMembersState: OnyxCollectionInputValue = {}; - for (const email of selectedMemberEmails) { + // Handles the case when there are multiple logins for the same account. + // Currently, the only known case where this happens is when a user gets invited + // with their secondary login. + // This happens because we only have the secondary login when + // we're inviting the user, but the backend always returns the primary login, + // so we end up with both stored in Onyx. + const selectedMemberEmailsWithDuplicates: string[] = [...selectedMemberEmails]; + for (const employeeEmail of Object.keys(policy?.employeeList ?? {})) { + const personalDetails = getPersonalDetailByEmail(employeeEmail); + // If we don't have the personal details, it means it's a secondary login + if (personalDetails) { + continue; + } + selectedMemberEmailsWithDuplicates.push(employeeEmail); + } + + for (const email of selectedMemberEmailsWithDuplicates) { optimisticMembersState[email] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.DELETE}; successMembersState[email] = null; failureMembersState[email] = { From 9889bcfc6ce37a754cea3c69732b17b1de4fc400 Mon Sep 17 00:00:00 2001 From: Lorenzo Bloedow Date: Tue, 2 Dec 2025 08:36:41 -0300 Subject: [PATCH 2/3] Add test for detached secondary login bug --- tests/actions/PolicyMemberTest.ts | 44 ++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index b921d377416c..b13042a0dca1 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -7,7 +7,7 @@ import * as Member from '@src/libs/actions/Policy/Member'; import * as Policy from '@src/libs/actions/Policy/Policy'; import * as ReportActionsUtils from '@src/libs/ReportActionsUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {ImportedSpreadsheet, Policy as PolicyType, Report, ReportAction, ReportMetadata} from '@src/types/onyx'; +import {type ImportedSpreadsheet, PolicyEmployeeList, type Policy as PolicyType, type Report, type ReportAction, type ReportMetadata} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import createPersonalDetails from '../utils/collections/personalDetails'; import createRandomPolicy from '../utils/collections/policies'; @@ -590,6 +590,48 @@ describe('actions/PolicyMember', () => { expect(failedMember?.errors).toBeTruthy(); expect(Object.keys(failedMember?.errors ?? {}).length).toBeGreaterThan(0); }); + + // For more details on what a detached member is, see https://github.com/Expensify/App/issues/75514#issuecomment-3568453686 + it('should remove "detached" members', async () => { + const policyID = 'ABCD123456'; + const ownerEmail = 'owner@gmail.com'; + const userEmail = 'user@gmail.com'; + const detachedUserEmail = 'detacheduser@gmail.com'; + const ownerAccountID = 1; + const userAccountID = 4321; + + await Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { + [ownerAccountID]: {login: ownerEmail}, + [userAccountID]: {login: userEmail}, + }); + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { + ...createRandomPolicy(Number(policyID)), + employeeList: { + [ownerEmail]: {role: CONST.POLICY.ROLE.ADMIN}, + [userEmail]: {role: CONST.POLICY.ROLE.USER}, + [detachedUserEmail]: {role: CONST.POLICY.ROLE.USER}, + }, + }); + + Member.removeMembers(policyID, [userEmail], {[userEmail]: userAccountID}); + + await waitForBatchedUpdates(); + + const employeeList = await new Promise((resolve) => { + const connection = Onyx.connectWithoutView({ + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + callback: (policy) => { + Onyx.disconnect(connection); + resolve(policy?.employeeList); + }, + }); + }); + + expect(employeeList?.[userEmail]).toBeUndefined(); + expect(employeeList?.[detachedUserEmail]).toBeUndefined(); + expect(employeeList?.[ownerEmail]).toBeDefined(); + }); }); describe('importPolicyMembers', () => { From f58c6c7fdc9f5fa823dcc49c7467c9e0335f517b Mon Sep 17 00:00:00 2001 From: Lorenzo Bloedow Date: Tue, 2 Dec 2025 09:41:27 -0300 Subject: [PATCH 3/3] Fix lint errors --- tests/actions/PolicyMemberTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index b13042a0dca1..ea849dace5d0 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -7,7 +7,7 @@ import * as Member from '@src/libs/actions/Policy/Member'; import * as Policy from '@src/libs/actions/Policy/Policy'; import * as ReportActionsUtils from '@src/libs/ReportActionsUtils'; import ONYXKEYS from '@src/ONYXKEYS'; -import {type ImportedSpreadsheet, PolicyEmployeeList, type Policy as PolicyType, type Report, type ReportAction, type ReportMetadata} from '@src/types/onyx'; +import type {ImportedSpreadsheet, PolicyEmployeeList, Policy as PolicyType, Report, ReportAction, ReportMetadata} from '@src/types/onyx'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import createPersonalDetails from '../utils/collections/personalDetails'; import createRandomPolicy from '../utils/collections/policies';