diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index af51936e932a..39fbe07febfd 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -267,15 +267,15 @@ function removeOptimisticRoomMembers( * @param [policyID] The id of the policy. * @param [loginList] The logins of the users whose roles are being updated to non-admin role or are removed from a workspace */ -function resetAccountingPreferredExporter(policyID: string, loginList: string[]): OnyxDataReturnType { - // This will be fixed as part of https://github.com/Expensify/Expensify/issues/507850 - // eslint-disable-next-line @typescript-eslint/no-deprecated - const policy = getPolicy(policyID); - const owner = policy?.owner ?? ReportUtils.getPersonalDetailsForAccountID(policy?.ownerAccountID).login ?? ''; +function resetAccountingPreferredExporter(policy: OnyxEntry, loginList: string[]): OnyxDataReturnType { const optimisticData: OnyxUpdate[] = []; const successData: OnyxUpdate[] = []; const failureData: OnyxUpdate[] = []; - const policyKey = `${ONYXKEYS.COLLECTION.POLICY}${policyID}` as const; + if (!policy?.id) { + return {optimisticData, successData, failureData}; + } + const owner = policy?.owner ?? ReportUtils.getPersonalDetailsForAccountID(policy?.ownerAccountID).login ?? ''; + const policyKey = `${ONYXKEYS.COLLECTION.POLICY}${policy.id}` as const; const adminLoginList = loginList.filter((login) => isUserPolicyAdmin(policy, login)); const connections = [CONST.POLICY.CONNECTIONS.NAME.XERO, CONST.POLICY.CONNECTIONS.NAME.QBO, CONST.POLICY.CONNECTIONS.NAME.SAGE_INTACCT, CONST.POLICY.CONNECTIONS.NAME.QBD]; @@ -385,7 +385,7 @@ function removeMembers(policyID: string, selectedMemberEmails: string[], policyM }) .map((login) => policyMemberEmailsToAccountIDs[login]), ); - const preferredExporterOnyxData = resetAccountingPreferredExporter(policyID, selectedMemberEmails); + const preferredExporterOnyxData = resetAccountingPreferredExporter(policy, selectedMemberEmails); const optimisticMembersState: OnyxCollectionInputValue = {}; const successMembersState: OnyxCollectionInputValue = {}; @@ -576,7 +576,7 @@ function removeMembers(policyID: string, selectedMemberEmails: string[], policyM // If we delete all these logins then we should clear the informative messages since they are no longer relevant. if (!isEmptyObject(policy?.primaryLoginsInvited ?? {})) { // Take the current policy members and remove them optimistically - const employeeListEmails = Object.keys(allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.employeeList ?? {}); + const employeeListEmails = Object.keys(policy?.employeeList ?? {}); const remainingLogins = employeeListEmails.filter((email) => !selectedMemberEmails.includes(email)); const invitedPrimaryToSecondaryLogins: Record = {}; @@ -629,7 +629,8 @@ function removeMembers(policyID: string, selectedMemberEmails: string[], policyM } function buildUpdateWorkspaceMembersRoleOnyxData(policyID: string, selectedMemberEmails: string[], selectedMemberAccountIDs: number[], newRole: ValueOf) { - const previousEmployeeList = {...allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]?.employeeList}; + const policy = allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${policyID}`]; + const previousEmployeeList = {...policy?.employeeList}; const memberRoles: WorkspaceMembersRoleData[] = selectedMemberEmails.map((login: string) => { return { email: login, @@ -684,7 +685,7 @@ function buildUpdateWorkspaceMembersRoleOnyxData(policyID: string, selectedMembe if (newRole !== CONST.POLICY.ROLE.ADMIN) { const preferredExporterOnyxData = resetAccountingPreferredExporter( - policyID, + policy, memberRoles.map((member) => member.email), ); optimisticData.push(...preferredExporterOnyxData.optimisticData); diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index df3ba3aff114..c09f3f3809c7 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -494,6 +494,83 @@ describe('actions/PolicyMember', () => { expect(successAdminRoomMetadata?.pendingChatMembers).toBeUndefined(); }); + it('Change preferred accounting exporter to owner if the members include current preferred exporter', async () => { + // Given a policy + const policyID = '1'; + const defaultApprover = 'approver@gmail.com'; + const ownerAccountID = 1; + const ownerEmail = 'owner@gmail.com'; + const adminAccountID = 1234; + const adminEmail = 'admin@example.com'; + const auditorAccountID = 1235; + const auditorEmail = 'auditor@example.com'; + const userAccountID = 1236; + const userEmail = 'user@example.com'; + + await Onyx.set(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { + [adminAccountID]: {login: adminEmail}, + [auditorAccountID]: {login: auditorEmail}, + [userAccountID]: {login: userEmail}, + }); + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { + ...createRandomPolicy(Number(policyID)), + approver: defaultApprover, + owner: ownerEmail, + ownerAccountID, + employeeList: { + [ownerEmail]: {role: CONST.POLICY.ROLE.ADMIN}, + [adminEmail]: {role: CONST.POLICY.ROLE.ADMIN}, + [auditorEmail]: {role: CONST.POLICY.ROLE.AUDITOR}, + [userEmail]: {role: CONST.POLICY.ROLE.USER}, + }, + connections: { + [CONST.POLICY.CONNECTIONS.NAME.NETSUITE]: { + verified: true, + options: { + config: { + exporter: adminEmail, + }, + }, + lastSync: { + errorDate: '', + errorMessage: '', + isAuthenticationError: false, + isConnected: true, + isSuccessful: true, + source: 'NEWEXPENSIFY', + successfulDate: '', + }, + }, + }, + }); + + // When removing an admin, auditor, and user members + mockFetch?.pause?.(); + const memberEmailsToAccountIDs = { + [adminEmail]: adminAccountID, + [auditorEmail]: auditorAccountID, + [userEmail]: userAccountID, + }; + Member.removeMembers(policyID, [adminEmail, auditorEmail, userEmail], memberEmailsToAccountIDs); + + await waitForBatchedUpdates(); + + const policyConnectionPreferredExporter = await new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + callback: (policy) => { + Onyx.disconnect(connection); + resolve(policy?.connections?.[CONST.POLICY.CONNECTIONS.NAME.NETSUITE]?.options?.config?.exporter); + }, + }); + }); + + // Then the preferred exporter is the workspace owner + expect(policyConnectionPreferredExporter).toBe(ownerEmail); + + await mockFetch?.resume?.(); + }); + it('should archive the member expense chat and expense report', async () => { // Given a workspace expense chat and expense report const policyID = '1';