34 lines
1.2 KiB
Elixir
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
|
|
Ecto.Changeset.traverse_errors(changeset, 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(fn {field, messages} -> "#{field}: #{Enum.join(messages, ", ")}" end)
|
|
|> Enum.join("; ")
|
|
end
|
|
end
|