From 6a7b2074b150dd94eda09f727364ab46cb860c8c Mon Sep 17 00:00:00 2001 From: abzokhattab Date: Mon, 2 Dec 2024 15:10:55 +0100 Subject: [PATCH] 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);