Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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';
Expand Down Expand Up @@ -48,7 +49,7 @@
};

const allPolicies: OnyxCollection<Policy> = {};
Onyx.connect({

Check warning on line 52 in src/libs/actions/Policy/Member.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.POLICY,
callback: (val, key) => {
if (!key) {
Expand All @@ -64,14 +65,14 @@
});

let allReportActions: OnyxCollection<ReportActions>;
Onyx.connect({

Check warning on line 68 in src/libs/actions/Policy/Member.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.COLLECTION.REPORT_ACTIONS,
waitForCollectionCallback: true,
callback: (actions) => (allReportActions = actions),
});

let policyOwnershipChecks: Record<string, PolicyOwnershipChangeChecks>;
Onyx.connect({

Check warning on line 75 in src/libs/actions/Policy/Member.ts

View workflow job for this annotation

GitHub Actions / Changed files ESLint check

Onyx.connect() is deprecated. Use useOnyx() hook instead and pass the data as parameters to a pure function
key: ONYXKEYS.POLICY_OWNERSHIP_CHANGE_CHECKS,
callback: (value) => {
policyOwnershipChecks = value ?? {};
Expand Down Expand Up @@ -388,7 +389,23 @@
const optimisticMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
const successMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
const failureMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
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] = {
Expand Down
44 changes: 43 additions & 1 deletion tests/actions/PolicyMemberTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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';
Expand Down Expand Up @@ -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<PolicyEmployeeList | undefined>((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', () => {
Expand Down
Loading