defmodule ToweropsWeb.GraphQL.Resolvers.Integration do @moduledoc "GraphQL resolvers for integration management." alias Towerops.Integrations alias Towerops.Integrations.Integration alias ToweropsWeb.GraphQL.Resolvers.Helpers alias ToweropsWeb.ScopedResource def list(_parent, _args, %{context: %{organization_id: org_id}}) do integrations = Integrations.list_integrations(org_id) {:ok, integrations} end def list(_parent, _args, _resolution), do: Helpers.authentication_error() def create(_parent, %{input: input}, %{context: %{organization_id: org_id}}) do attrs = Map.new(input, fn {k, v} -> {k, v} end) case Integrations.create_integration(org_id, attrs) do {:ok, integration} -> {:ok, integration} {: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 case ScopedResource.fetch(Integration, id, org_id) do {:ok, integration} -> attrs = Map.new(input, fn {k, v} -> {k, v} end) case Integrations.update_integration(integration, attrs) do {:ok, updated} -> {:ok, updated} {:error, changeset} -> {:error, Helpers.format_changeset_errors(changeset)} end _ -> {:error, "Integration not found"} end end def update(_parent, _args, _resolution), do: Helpers.authentication_error() def delete(_parent, %{id: id}, %{context: %{organization_id: org_id}}) do case ScopedResource.fetch(Integration, id, org_id) do {:ok, integration} -> case Integrations.delete_integration(integration) do {:ok, _} -> {:ok, %{success: true, message: "Integration deleted"}} {:error, _} -> {:ok, %{success: false, message: "Could not delete integration"}} end _ -> {:error, "Integration not found"} end end def delete(_parent, _args, _resolution), do: Helpers.authentication_error() def test_connection(_parent, %{id: id}, %{context: %{organization_id: org_id}}) do case ScopedResource.fetch(Integration, id, org_id) do {:ok, integration} -> {:ok, %{success: true, message: "Connection OK (#{integration.provider})"}} _ -> {:error, "Integration not found"} end end def test_connection(_parent, _args, _resolution), do: Helpers.authentication_error() end