towerops/lib/towerops_web/graphql/resolvers/organization.ex
graham c5481c2cd3 add mobile token auth to GraphQL endpoint (#48)
Extend the GraphQL API and socket to accept mobile session tokens
alongside existing API tokens. Add OrganizationScope middleware
that extracts organization_id from query args for mobile users.
Add my_organizations query for mobile app org listing.

Reviewed-on: graham/towerops-web#48
2026-03-16 15:23:07 -05:00

45 lines
1.4 KiB
Elixir

defmodule ToweropsWeb.GraphQL.Resolvers.Organization do
@moduledoc "GraphQL resolvers for organization queries and mutations."
alias Towerops.Alerts
alias Towerops.Devices
alias Towerops.Organizations
alias Towerops.Sites
alias ToweropsWeb.GraphQL.Resolvers.Helpers
def list_mine(_parent, _args, %{context: %{user: user}}) do
orgs =
user.id
|> Organizations.list_user_organizations()
|> Enum.map(fn org ->
Map.merge(org, %{
sites_count: Sites.count_organization_sites(org.id),
device_count: Devices.count_organization_devices(org.id),
active_alerts_count: Alerts.count_active_alerts(org.id)
})
end)
{:ok, orgs}
end
def list_mine(_parent, _args, _resolution), do: Helpers.authentication_error()
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: Helpers.authentication_error()
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, Helpers.format_changeset_errors(changeset)}
end
end
def update(_parent, _args, _resolution), do: Helpers.authentication_error()
end