fix: clear sync status when integration credentials change

When saving new or updated API credentials, reset last_sync_status to
"never" and clear last_sync_message and last_synced_at so stale error
messages from previous syncs don't persist in the UI.
This commit is contained in:
Graham McIntire 2026-03-12 14:58:36 -05:00
parent bbd74e895f
commit da5cd4054b
No known key found for this signature in database
2 changed files with 43 additions and 0 deletions

View file

@ -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", ""),

View file

@ -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