diff --git a/lib/towerops_web/live/org/integrations_live.ex b/lib/towerops_web/live/org/integrations_live.ex index 0ba6bc26..71b754e3 100644 --- a/lib/towerops_web/live/org/integrations_live.ex +++ b/lib/towerops_web/live/org/integrations_live.ex @@ -371,6 +371,13 @@ defmodule ToweropsWeb.Org.IntegrationsLive do attrs = %{credentials: credentials} + attrs = + if credentials_changed?(existing_integration, credentials) do + Map.merge(attrs, %{last_sync_status: "never", last_sync_message: nil, last_synced_at: nil}) + else + attrs + end + if sync_interval && sync_interval != "" do Map.put(attrs, :sync_interval_minutes, sync_interval) else @@ -378,6 +385,14 @@ defmodule ToweropsWeb.Org.IntegrationsLive do end end + defp credentials_changed?(nil, _new_credentials), do: false + + defp credentials_changed?(%Integration{credentials: old}, new) when is_map(old) and is_map(new) do + old != new + end + + defp credentials_changed?(_existing, _new), do: true + defp build_credentials("splynx", params, _existing) do %{ "instance_url" => Map.get(params, "instance_url", ""), diff --git a/test/towerops_web/live/org/integrations_live_test.exs b/test/towerops_web/live/org/integrations_live_test.exs index 64680153..77ee8a9a 100644 --- a/test/towerops_web/live/org/integrations_live_test.exs +++ b/test/towerops_web/live/org/integrations_live_test.exs @@ -226,5 +226,33 @@ defmodule ToweropsWeb.Org.IntegrationsLiveTest do html = view |> element("#test-connection") |> render_click() assert html =~ "Connection successful" end + + test "clears sync status when credentials change", %{conn: conn, user: user, organization: org} do + {:ok, _} = + Towerops.Integrations.create_integration(org.id, %{ + provider: "preseem", + enabled: true, + credentials: %{"api_key" => "old-key"}, + last_sync_status: "failed", + last_sync_message: "some raw error from previous sync" + }) + + {:ok, view, _html} = + conn + |> log_in_user(user) + |> live(~p"/orgs/#{org.slug}/settings/integrations") + + view |> element("#configure-preseem") |> render_click() + + view + |> form("#preseem-form", %{integration: %{api_key: "new-key-456"}}) + |> render_submit() + + assert {:ok, integration} = Towerops.Integrations.get_integration(org.id, "preseem") + assert integration.credentials["api_key"] == "new-key-456" + assert integration.last_sync_status == "never" + assert is_nil(integration.last_sync_message) + assert is_nil(integration.last_synced_at) + end end end