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
21 changes: 11 additions & 10 deletions src/libs/actions/Policy/Member.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Policy>, loginList: string[]): OnyxDataReturnType {
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
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];

Expand Down Expand Up @@ -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<PolicyEmployee> = {};
const successMembersState: OnyxCollectionInputValue<PolicyEmployee> = {};
Expand Down Expand Up @@ -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<string, string> = {};

Expand Down Expand Up @@ -629,7 +629,8 @@ function removeMembers(policyID: string, selectedMemberEmails: string[], policyM
}

function buildUpdateWorkspaceMembersRoleOnyxData(policyID: string, selectedMemberEmails: string[], selectedMemberAccountIDs: number[], newRole: ValueOf<typeof CONST.POLICY.ROLE>) {
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,
Expand Down Expand Up @@ -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);
Expand Down
77 changes: 77 additions & 0 deletions tests/actions/PolicyMemberTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string | undefined>((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);
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved
Comment thread
shubham1206agra marked this conversation as resolved.
Resolved

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';
Expand Down