- Replace terse helper text with step-by-step setup guide in config form - Add what-gets-synced breakdown when PagerDuty is enabled - Clear visual hierarchy with numbered steps and inline callout
77 lines
2.2 KiB
Elixir
77 lines
2.2 KiB
Elixir
defmodule ToweropsWeb.GraphQL.Resolvers.Alert do
|
|
@moduledoc "GraphQL resolvers for alert queries and mutations."
|
|
|
|
alias Towerops.Alerts
|
|
alias Towerops.Repo
|
|
|
|
def list(_parent, args, %{context: %{organization_id: org_id}}) do
|
|
filters =
|
|
args
|
|
|> Map.take([:status, :device_id, :limit])
|
|
|> Map.new(fn {k, v} -> {to_string(k), v} end)
|
|
|
|
alerts = Alerts.list_organization_alerts(org_id, filters)
|
|
{:ok, alerts}
|
|
end
|
|
|
|
def list(_parent, _args, _resolution), do: {:error, "Authentication required"}
|
|
|
|
def get(_parent, %{id: id}, %{context: %{organization_id: org_id}}) do
|
|
alert = Alerts.get_alert!(id)
|
|
device = alert.device
|
|
|
|
if device && device.organization_id == org_id do
|
|
{:ok, alert}
|
|
else
|
|
{:error, "Alert not found"}
|
|
end
|
|
rescue
|
|
Ecto.NoResultsError -> {:error, "Alert not found"}
|
|
end
|
|
|
|
def get(_parent, _args, _resolution), do: {:error, "Authentication required"}
|
|
|
|
def acknowledge(_parent, %{id: id}, %{context: %{organization_id: org_id} = ctx}) do
|
|
alert = Alerts.get_alert!(id)
|
|
device = alert.device
|
|
|
|
if device && device.organization_id == org_id do
|
|
user_id = ctx[:user] && ctx[:user].id
|
|
|
|
case Alerts.acknowledge_alert(alert, user_id) do
|
|
{:ok, updated} ->
|
|
{:ok, Repo.preload(updated, [:device, :acknowledged_by], force: true)}
|
|
|
|
{:error, _changeset} ->
|
|
{:error, "Could not acknowledge alert"}
|
|
end
|
|
else
|
|
{:error, "Alert not found"}
|
|
end
|
|
rescue
|
|
Ecto.NoResultsError -> {:error, "Alert not found"}
|
|
end
|
|
|
|
def acknowledge(_parent, _args, _resolution), do: {:error, "Authentication required"}
|
|
|
|
def resolve_alert(_parent, %{id: id}, %{context: %{organization_id: org_id}}) do
|
|
alert = Alerts.get_alert!(id)
|
|
device = alert.device
|
|
|
|
if device && device.organization_id == org_id do
|
|
case Alerts.resolve_alert(alert) do
|
|
{:ok, updated} ->
|
|
{:ok, Repo.preload(updated, [:device, :acknowledged_by], force: true)}
|
|
|
|
{:error, _changeset} ->
|
|
{:error, "Could not resolve alert"}
|
|
end
|
|
else
|
|
{:error, "Alert not found"}
|
|
end
|
|
rescue
|
|
Ecto.NoResultsError -> {:error, "Alert not found"}
|
|
end
|
|
|
|
def resolve_alert(_parent, _args, _resolution), do: {:error, "Authentication required"}
|
|
end
|