diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index fef788ddf6ff..a40617f27cbd 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -19,6 +19,7 @@ import enhanceParameters from '@libs/Network/enhanceParameters'; import Parser from '@libs/Parser'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as PhoneNumber from '@libs/PhoneNumber'; +import {getDefaultApprover} from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; @@ -637,7 +638,12 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccount const successMembersState: OnyxCollectionInputValue = {}; const failureMembersState: OnyxCollectionInputValue = {}; logins.forEach((email) => { - optimisticMembersState[email] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, role: CONST.POLICY.ROLE.USER}; + optimisticMembersState[email] = { + email, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + role: CONST.POLICY.ROLE.USER, + submitsTo: getDefaultApprover(allPolicies?.[policyKey]), + }; successMembersState[email] = {pendingAction: null}; failureMembersState[email] = { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.people.error.genericAdd'), diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index f9ac32010a23..6efbeece1cdb 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -228,4 +228,40 @@ describe('actions/PolicyMember', () => { }); }); }); + + describe('addMembersToWorkspace', () => { + it('Add a new member to a workspace', async () => { + const policyID = '1'; + const defaultApprover = 'approver@gmail.com'; + const newUserEmail = 'user@gmail.com'; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { + ...createRandomPolicy(Number(policyID)), + approver: defaultApprover, + }); + + mockFetch?.pause?.(); + Member.addMembersToWorkspace({[newUserEmail]: 1234}, 'Welcome', policyID, []); + + await waitForBatchedUpdates(); + + await new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connection); + const newEmployee = policy?.employeeList?.[newUserEmail]; + expect(newEmployee).not.toBeUndefined(); + expect(newEmployee?.email).toBe(newUserEmail); + expect(newEmployee?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + expect(newEmployee?.role).toBe(CONST.POLICY.ROLE.USER); + expect(newEmployee?.submitsTo).toBe(defaultApprover); + resolve(); + }, + }); + }); + await mockFetch?.resume?.(); + }); + }); });