From 76545ee31c16914bb12a9bb0a5e8968b308e2eaa Mon Sep 17 00:00:00 2001 From: Graham McIntire Date: Sat, 7 Mar 2026 12:04:30 -0600 Subject: [PATCH] fix: handle alert_changed PubSub messages and primary owner ordering - Add handle_info for {:alert_changed, org_id} in AlertLive.Index and DashboardLive to properly handle organization-level alert change broadcasts - Fix get_primary_owner query to prioritize is_default memberships first, ensuring the organization creator is always returned when multiple owners exist Resolves 5 test failures related to PubSub message handling and deterministic primary owner selection. --- lib/towerops/organizations.ex | 2 +- lib/towerops_web/live/alert_live/index.ex | 5 +++++ lib/towerops_web/live/dashboard_live.ex | 5 +++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/towerops/organizations.ex b/lib/towerops/organizations.ex index 471ca2af..8b20b6f0 100644 --- a/lib/towerops/organizations.ex +++ b/lib/towerops/organizations.ex @@ -564,7 +564,7 @@ defmodule Towerops.Organizations do join: u in assoc(m, :user), where: m.organization_id == ^organization.id, where: m.role == :owner, - order_by: [asc: m.inserted_at], + order_by: [desc: m.is_default, asc: m.inserted_at, asc: m.id], limit: 1, select: u ) diff --git a/lib/towerops_web/live/alert_live/index.ex b/lib/towerops_web/live/alert_live/index.ex index 96dac9d9..3df8c9e5 100644 --- a/lib/towerops_web/live/alert_live/index.ex +++ b/lib/towerops_web/live/alert_live/index.ex @@ -186,6 +186,11 @@ defmodule ToweropsWeb.AlertLive.Index do {:noreply, load_alerts(socket, socket.assigns.current_scope.organization.id)} end + @impl true + def handle_info({:alert_changed, _org_id}, socket) do + {:noreply, load_alerts(socket, socket.assigns.current_scope.organization.id)} + end + defp load_alerts(socket, organization_id) do all_alerts = Alerts.list_organization_alerts(organization_id, %{"limit" => 500}) diff --git a/lib/towerops_web/live/dashboard_live.ex b/lib/towerops_web/live/dashboard_live.ex index da200b8e..dd6f5738 100644 --- a/lib/towerops_web/live/dashboard_live.ex +++ b/lib/towerops_web/live/dashboard_live.ex @@ -96,6 +96,11 @@ defmodule ToweropsWeb.DashboardLive do {:noreply, load_dashboard_data(socket, socket.assigns.current_scope.organization.id)} end + @impl true + def handle_info({:alert_changed, _org_id}, socket) do + {:noreply, load_dashboard_data(socket, socket.assigns.current_scope.organization.id)} + end + defp load_dashboard_data(socket, organization_id) do organization = socket.assigns.current_scope.organization summary = Dashboard.get_dashboard_summary(organization_id)