From 6a7b2074b150dd94eda09f727364ab46cb860c8c Mon Sep 17 00:00:00 2001 From: abzokhattab Date: Mon, 2 Dec 2024 15:10:55 +0100 Subject: [PATCH 1/2] Fix categories approval field after removing workspace members --- src/libs/actions/Policy/Member.ts | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 8fb551cdec81..81dea1c828e7 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -26,7 +26,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import type {InvitedEmailsToAccountIDs, PersonalDetailsList, Policy, PolicyEmployee, PolicyOwnershipChangeChecks, Report, ReportAction} from '@src/types/onyx'; import type {PendingAction} from '@src/types/onyx/OnyxCommon'; import type {JoinWorkspaceResolution} from '@src/types/onyx/OriginalMessage'; -import type {Attributes, Rate} from '@src/types/onyx/Policy'; +import type {ApprovalRule, Attributes, Rate} from '@src/types/onyx/Policy'; import type {OnyxData} from '@src/types/onyx/Request'; import {isEmptyObject} from '@src/types/utils/EmptyObject'; import {createPolicyExpenseChats} from './Policy'; @@ -296,7 +296,17 @@ function removeMembers(accountIDs: number[], policyID: string) { failureMembersState[email] = {errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.people.error.genericRemove')}; }); + const approvalRules: ApprovalRule[] = policy?.rules?.approvalRules ?? []; + const optimisticApprovalRules: ApprovalRule[] = []; + Object.keys(policy?.employeeList ?? {}).forEach((employeeEmail) => { + approvalRules.forEach((rule) => { + if (employeeEmail === rule?.approver) { + return; + } + optimisticApprovalRules.push(rule); + }); + const employee = policy?.employeeList?.[employeeEmail]; optimisticMembersState[employeeEmail] = optimisticMembersState[employeeEmail] ?? {}; failureMembersState[employeeEmail] = failureMembersState[employeeEmail] ?? {}; @@ -336,7 +346,14 @@ function removeMembers(accountIDs: number[], policyID: string) { { onyxMethod: Onyx.METHOD.MERGE, key: policyKey, - value: {employeeList: optimisticMembersState, approver: emailList.includes(policy?.approver ?? '') ? policy?.owner : policy?.approver}, + value: { + employeeList: optimisticMembersState, + approver: emailList.includes(policy?.approver ?? '') ? policy?.owner : policy?.approver, + rules: { + ...(policy?.rules ?? {}), + approvalRules: optimisticApprovalRules, + }, + }, }, ]; optimisticData.push(...announceRoomMembers.onyxOptimisticData); @@ -354,7 +371,7 @@ function removeMembers(accountIDs: number[], policyID: string) { { onyxMethod: Onyx.METHOD.MERGE, key: policyKey, - value: {employeeList: failureMembersState, approver: policy?.approver}, + value: {employeeList: failureMembersState, approver: policy?.approver, rules: policy?.rules}, }, ]; failureData.push(...announceRoomMembers.onyxFailureData); From f8f5c02be1809073337d42638f622e3d162aea4a Mon Sep 17 00:00:00 2001 From: abzokhattab Date: Sat, 7 Dec 2024 05:11:09 +0100 Subject: [PATCH 2/2] Fixing the approval rules inside the optimistic rules --- src/libs/actions/Policy/Member.ts | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 81dea1c828e7..e6bf5bd434b5 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -296,17 +296,7 @@ function removeMembers(accountIDs: number[], policyID: string) { failureMembersState[email] = {errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.people.error.genericRemove')}; }); - const approvalRules: ApprovalRule[] = policy?.rules?.approvalRules ?? []; - const optimisticApprovalRules: ApprovalRule[] = []; - Object.keys(policy?.employeeList ?? {}).forEach((employeeEmail) => { - approvalRules.forEach((rule) => { - if (employeeEmail === rule?.approver) { - return; - } - optimisticApprovalRules.push(rule); - }); - const employee = policy?.employeeList?.[employeeEmail]; optimisticMembersState[employeeEmail] = optimisticMembersState[employeeEmail] ?? {}; failureMembersState[employeeEmail] = failureMembersState[employeeEmail] ?? {}; @@ -342,6 +332,9 @@ function removeMembers(accountIDs: number[], policyID: string) { } }); + const approvalRules: ApprovalRule[] = policy?.rules?.approvalRules ?? []; + const optimisticApprovalRules = approvalRules.filter((rule) => !emailList.includes(rule?.approver ?? '')); + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE,