defmodule ToweropsWeb.Api.V1.IntegrationsController do @moduledoc "API controller for third-party integrations." use ToweropsWeb, :controller alias Towerops.Integrations def index(conn, _params) do organization_id = conn.assigns.current_organization_id integrations = organization_id |> Integrations.list_integrations() |> Enum.map(&format_integration/1) json(conn, %{data: integrations}) end def create(conn, %{"integration" => attrs}) do organization_id = conn.assigns.current_organization_id case Integrations.create_integration(organization_id, to_atom_keys(attrs)) do {:ok, integration} -> conn |> put_status(:created) |> json(%{data: format_integration(integration)}) {:error, changeset} -> conn |> put_status(:unprocessable_entity) |> json(%{errors: translate_errors(changeset)}) end end def create(conn, _params) do conn |> put_status(:bad_request) |> json(%{error: "Missing 'integration' parameter"}) end def show(conn, %{"id" => id}) do organization_id = conn.assigns.current_organization_id case Integrations.get_integration_by_id(id) do {:ok, integration} when integration.organization_id == organization_id -> json(conn, %{data: format_integration(integration)}) _ -> conn |> put_status(:not_found) |> json(%{error: "Integration not found"}) end end def update(conn, %{"id" => id, "integration" => attrs}) do organization_id = conn.assigns.current_organization_id case Integrations.get_integration_by_id(id) do {:ok, integration} when integration.organization_id == organization_id -> case Integrations.update_integration(integration, to_atom_keys(attrs)) do {:ok, updated} -> json(conn, %{data: format_integration(updated)}) {:error, changeset} -> conn |> put_status(:unprocessable_entity) |> json(%{errors: translate_errors(changeset)}) end _ -> conn |> put_status(:not_found) |> json(%{error: "Integration not found"}) end end def update(conn, _params) do conn |> put_status(:bad_request) |> json(%{error: "Missing 'integration' parameter"}) end def delete(conn, %{"id" => id}) do organization_id = conn.assigns.current_organization_id case Integrations.get_integration_by_id(id) do {:ok, integration} when integration.organization_id == organization_id -> case Integrations.delete_integration(integration) do {:ok, _} -> conn |> put_status(:no_content) |> send_resp(204, "") {:error, _} -> conn |> put_status(:unprocessable_entity) |> json(%{error: "Could not delete integration"}) end _ -> conn |> put_status(:not_found) |> json(%{error: "Integration not found"}) end end def test_connection(conn, %{"id" => id}) do organization_id = conn.assigns.current_organization_id case Integrations.get_integration_by_id(id) do {:ok, integration} when integration.organization_id == organization_id -> # Simple connectivity test — just verify the integration exists and is configured json(conn, %{data: %{status: "ok", provider: integration.provider}}) _ -> conn |> put_status(:not_found) |> json(%{error: "Integration not found"}) end end defp format_integration(integration) do %{ id: integration.id, provider: integration.provider, enabled: integration.enabled, sync_interval_minutes: integration.sync_interval_minutes, last_synced_at: integration.last_synced_at, last_sync_status: integration.last_sync_status, inserted_at: integration.inserted_at, updated_at: integration.updated_at } end defp to_atom_keys(map) when is_map(map) do Map.new(map, fn {k, v} -> {String.to_existing_atom(k), v} end) rescue ArgumentError -> map end defp translate_errors(changeset) do Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} -> Regex.replace(~r"%{(\w+)}", msg, fn _, key -> if String.length(key) <= 50 and String.match?(key, ~r/^[a-z_]+$/) do opts |> Keyword.get(String.to_existing_atom(key), key) |> to_string() else key end end) end) end end