towerops/lib/towerops_web/graphql/resolvers/organization.ex
Graham McIntire 146f5745cf
fix: resolve compilation errors, test failures, and credo issues
- Escape HEEx template braces in GraphQL/API docs with raw(~S[...])
- Fix test assertions for updated marketing copy and UI text
- Extract helper functions in GraphQL resolvers to reduce nesting depth
- Create shared ErrorHelpers module for API controllers
- Fix ETS race condition in brute force whitelist cache for async tests
- Fix property test generators to use ASCII instead of printable unicode
- Add alert_severity helper to site_live/show
- Update accounts fixtures for explicit user confirmation
2026-02-14 12:23:10 -06:00

34 lines
1.2 KiB
Elixir

defmodule ToweropsWeb.GraphQL.Resolvers.Organization do
@moduledoc "GraphQL resolvers for organization queries and mutations."
alias Towerops.Organizations
def get(_parent, _args, %{context: %{organization_id: org_id}}) do
org = Organizations.get_organization!(org_id)
{:ok, org}
end
def get(_parent, _args, _resolution), do: {:error, "Authentication required"}
def update(_parent, %{input: input}, %{context: %{organization_id: org_id}}) do
org = Organizations.get_organization!(org_id)
attrs = Map.new(input, fn {k, v} -> {to_string(k), v} end)
case Organizations.update_organization(org, attrs) do
{:ok, updated} -> {:ok, updated}
{:error, changeset} -> {:error, format_errors(changeset)}
end
end
def update(_parent, _args, _resolution), do: {:error, "Authentication required"}
defp format_errors(changeset) do
changeset
|> Ecto.Changeset.traverse_errors(fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string()
end)
end)
|> Enum.map_join("; ", fn {field, messages} -> "#{field}: #{Enum.join(messages, ", ")}" end)
end
end