diff --git a/src/libs/actions/Policy.ts b/src/libs/actions/Policy.ts index 2adcfd29e00d..d69594548218 100644 --- a/src/libs/actions/Policy.ts +++ b/src/libs/actions/Policy.ts @@ -436,6 +436,7 @@ function buildAnnounceRoomMembersOnyxData(policyID: string, accountIDs: number[] } function setWorkspaceAutoReporting(policyID: string, enabled: boolean, frequency: ValueOf) { + const policy = ReportUtils.getPolicy(policyID); const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -443,7 +444,7 @@ function setWorkspaceAutoReporting(policyID: string, enabled: boolean, frequency value: { autoReporting: enabled, harvesting: { - enabled: true, + enabled, }, autoReportingFrequency: frequency, pendingFields: {isAutoApprovalEnabled: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}, @@ -456,7 +457,11 @@ function setWorkspaceAutoReporting(policyID: string, enabled: boolean, frequency onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - autoReporting: !enabled, + autoReporting: policy.autoReporting ?? null, + harvesting: { + enabled: policy.harvesting?.enabled ?? null, + }, + autoReportingFrequency: policy.autoReportingFrequency ?? null, pendingFields: {isAutoApprovalEnabled: null, harvesting: null}, }, }, @@ -478,6 +483,8 @@ function setWorkspaceAutoReporting(policyID: string, enabled: boolean, frequency } function setWorkspaceAutoReportingFrequency(policyID: string, frequency: ValueOf) { + const policy = ReportUtils.getPolicy(policyID); + const optimisticData: OnyxUpdate[] = [ { onyxMethod: Onyx.METHOD.MERGE, @@ -494,6 +501,7 @@ function setWorkspaceAutoReportingFrequency(policyID: string, frequency: ValueOf onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { + autoReportingFrequency: policy.autoReportingFrequency ?? null, pendingFields: {autoReportingFrequency: null}, }, }, @@ -515,6 +523,7 @@ function setWorkspaceAutoReportingFrequency(policyID: string, frequency: ValueOf function setWorkspaceAutoReportingMonthlyOffset(policyID: string, autoReportingOffset: number | ValueOf) { const value = JSON.stringify({autoReportingOffset: autoReportingOffset.toString()}); + const policy = ReportUtils.getPolicy(policyID); const optimisticData: OnyxUpdate[] = [ { @@ -532,6 +541,7 @@ function setWorkspaceAutoReportingMonthlyOffset(policyID: string, autoReportingO onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { + autoReportingOffset: policy.autoReportingOffset ?? null, pendingFields: {autoReportingOffset: null}, }, }, @@ -553,6 +563,7 @@ function setWorkspaceAutoReportingMonthlyOffset(policyID: string, autoReportingO function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMode: ValueOf) { const isAutoApprovalEnabled = approvalMode === CONST.POLICY.APPROVAL_MODE.BASIC; + const policy = ReportUtils.getPolicy(policyID); const value = { approver, @@ -576,6 +587,9 @@ function setWorkspaceApprovalMode(policyID: string, approver: string, approvalMo onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { + approver: policy.approver ?? null, + approvalMode: policy.approvalMode ?? null, + isAutoApprovalEnabled: policy.isAutoApprovalEnabled ?? null, pendingFields: {approvalMode: null}, }, }, @@ -627,8 +641,8 @@ function setWorkspacePayer(policyID: string, reimburserEmail: string, reimburser onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - reimburserEmail: policy.reimburserEmail, - reimburserAccountID: policy.reimburserAccountID, + reimburserEmail: policy.reimburserEmail ?? null, + reimburserAccountID: policy.reimburserAccountID ?? null, errorFields: {reimburserEmail: ErrorUtils.getMicroSecondOnyxError('workflowsPayerPage.genericErrorMessage')}, pendingFields: {reimburserEmail: null}, }, @@ -677,9 +691,9 @@ function setWorkspaceReimbursement(policyID: string, reimbursementChoice: ValueO onyxMethod: Onyx.METHOD.MERGE, key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, value: { - reimbursementChoice: policy.reimbursementChoice, - reimburserAccountID: policy.reimburserAccountID, - reimburserEmail: policy.reimburserEmail, + reimbursementChoice: policy.reimbursementChoice ?? null, + reimburserAccountID: policy.reimburserAccountID ?? null, + reimburserEmail: policy.reimburserEmail ?? null, errorFields: {reimbursementChoice: ErrorUtils.getMicroSecondOnyxError('common.genericErrorMessage')}, pendingFields: {reimbursementChoice: null}, },