diff --git a/changes/16504-policy-automation-bug b/changes/16504-policy-automation-bug new file mode 100644 index 0000000000..9c107d5679 --- /dev/null +++ b/changes/16504-policy-automation-bug @@ -0,0 +1 @@ +- Fix a bug where policy automations when saved were resetting automations on other pages diff --git a/frontend/pages/policies/ManagePoliciesPage/components/ManagePolicyAutomationsModal/ManagePolicyAutomationsModal.tsx b/frontend/pages/policies/ManagePoliciesPage/components/ManagePolicyAutomationsModal/ManagePolicyAutomationsModal.tsx index ae9670aeae..403702128a 100644 --- a/frontend/pages/policies/ManagePoliciesPage/components/ManagePolicyAutomationsModal/ManagePolicyAutomationsModal.tsx +++ b/frontend/pages/policies/ManagePoliciesPage/components/ManagePolicyAutomationsModal/ManagePolicyAutomationsModal.tsx @@ -225,11 +225,34 @@ const ManagePolicyAutomationsModal = ({ // newPolicyIds = []; // } + const updatedEnabledPoliciesAcrossPages = () => { + if (webhook.policy_ids) { + // Array of policy ids on the page + const availablePoliciesIds = availablePolicies.map( + (policy) => policy.id + ); + + // Array of policy ids enabled NOT on the page + const enabledPoliciesOnOtherPages = webhook.policy_ids.filter( + (policyId) => !availablePoliciesIds.includes(policyId) + ); + + // Concatenate with array of policies enabled on the page + const allEnabledPolicies = enabledPoliciesOnOtherPages.concat( + newPolicyIds + ); + + return allEnabledPolicies; + } + + return []; + }; + // NOTE: backend uses webhook_settings to store automated policy ids for both webhooks and integrations const newWebhook = { failing_policies_webhook: { destination_url: destinationUrl, - policy_ids: newPolicyIds, + policy_ids: updatedEnabledPoliciesAcrossPages(), enable_failing_policies_webhook: isPolicyAutomationsEnabled && isWebhookEnabled, },