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.
This commit is contained in:
Graham McIntire 2026-03-07 12:04:30 -06:00
parent 3fa0ea383a
commit 76545ee31c
No known key found for this signature in database
3 changed files with 11 additions and 1 deletions

View file

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

View file

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

View file

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