towerops/lib/towerops_web/graphql/resolvers/maintenance_window.ex
Graham McIntire 17fa005781
add maintenance windows REST API and GraphQL support
REST endpoints for CRUD, scoped filtering (active/upcoming/past).
GraphQL queries and mutations for maintenance windows.
Fix agent edit test assertion message.
2026-03-11 14:33:25 -05:00

79 lines
2.7 KiB
Elixir

defmodule ToweropsWeb.GraphQL.Resolvers.MaintenanceWindow do
@moduledoc "GraphQL resolvers for maintenance window queries and mutations."
alias Towerops.Maintenance
alias Towerops.Maintenance.MaintenanceWindow
alias ToweropsWeb.GraphQL.Resolvers.Helpers
alias ToweropsWeb.ScopedResource
def list(_parent, args, %{context: %{organization_id: org_id}}) do
opts =
case Map.get(args, :filter) do
"active" -> [filter: :active]
"upcoming" -> [filter: :upcoming]
"past" -> [filter: :past]
_ -> []
end
{:ok, Maintenance.list_windows(org_id, opts)}
end
def list(_parent, _args, _resolution), do: Helpers.authentication_error()
def get(_parent, %{id: id}, %{context: %{organization_id: org_id}}) do
case ScopedResource.fetch(MaintenanceWindow, id, org_id) do
{:ok, window} -> {:ok, window}
{:error, _} -> {:error, "Maintenance window not found"}
end
end
def get(_parent, _args, _resolution), do: Helpers.authentication_error()
def create(_parent, %{input: input}, %{context: %{organization_id: org_id, user: user}}) do
user_id = user.id
attrs =
input
|> Map.new(fn {k, v} -> {to_string(k), v} end)
|> Map.put("organization_id", org_id)
|> Map.put("created_by_id", user_id)
case Maintenance.create_window(attrs) do
{:ok, window} -> {:ok, window}
{:error, changeset} -> {:error, Helpers.format_changeset_errors(changeset)}
end
end
def create(_parent, _args, _resolution), do: Helpers.authentication_error()
def update(_parent, %{id: id, input: input}, %{context: %{organization_id: org_id}}) do
with {:ok, window} <- fetch_org_window(id, org_id) do
attrs = Map.new(input, fn {k, v} -> {to_string(k), v} end)
case Maintenance.update_window(window, attrs) do
{:ok, updated} -> {:ok, updated}
{:error, changeset} -> {:error, Helpers.format_changeset_errors(changeset)}
end
end
end
def update(_parent, _args, _resolution), do: Helpers.authentication_error()
def delete(_parent, %{id: id}, %{context: %{organization_id: org_id}}) do
with {:ok, window} <- fetch_org_window(id, org_id) do
case Maintenance.delete_window(window) do
{:ok, _} -> {:ok, %{success: true, message: "Maintenance window deleted"}}
{:error, _} -> {:ok, %{success: false, message: "Could not delete maintenance window"}}
end
end
end
def delete(_parent, _args, _resolution), do: Helpers.authentication_error()
defp fetch_org_window(id, org_id) do
case ScopedResource.fetch(MaintenanceWindow, id, org_id) do
{:ok, window} -> {:ok, window}
{:error, _} -> {:error, "Maintenance window not found"}
end
end
end