feat: add Vereinfacht connection test button to settings
This commit is contained in:
parent
fca0194a7d
commit
f29bbb02a2
6 changed files with 389 additions and 9 deletions
|
|
@ -10,6 +10,54 @@ defmodule Mv.Vereinfacht.Client do
|
|||
|
||||
@content_type "application/vnd.api+json"
|
||||
|
||||
@doc """
|
||||
Tests the connection to the Vereinfacht API with the given credentials.
|
||||
|
||||
Makes a lightweight `GET /finance-contacts?page[size]=1` request to verify
|
||||
that the API URL, API key, and club ID are valid and reachable.
|
||||
|
||||
## Returns
|
||||
- `{:ok, :connected}` – credentials are valid (HTTP 200)
|
||||
- `{:error, :not_configured}` – any parameter is nil or blank
|
||||
- `{:error, {:http, status, message}}` – API returned an error (e.g. 401, 403)
|
||||
- `{:error, {:request_failed, reason}}` – network/transport error
|
||||
|
||||
## Examples
|
||||
|
||||
iex> test_connection("https://api.example.com/api/v1", "token", "2")
|
||||
{:ok, :connected}
|
||||
|
||||
iex> test_connection(nil, "token", "2")
|
||||
{:error, :not_configured}
|
||||
"""
|
||||
@spec test_connection(String.t() | nil, String.t() | nil, String.t() | nil) ::
|
||||
{:ok, :connected} | {:error, term()}
|
||||
def test_connection(api_url, api_key, club_id) do
|
||||
if blank?(api_url) or blank?(api_key) or blank?(club_id) do
|
||||
{:error, :not_configured}
|
||||
else
|
||||
url =
|
||||
api_url
|
||||
|> String.trim_trailing("/")
|
||||
|> then(&"#{&1}/finance-contacts?page[size]=1")
|
||||
|
||||
case Req.get(url, [headers: headers(api_key)] ++ req_http_options()) do
|
||||
{:ok, %{status: 200}} ->
|
||||
{:ok, :connected}
|
||||
|
||||
{:ok, %{status: status, body: body}} ->
|
||||
{:error, {:http, status, extract_error_message(body)}}
|
||||
|
||||
{:error, reason} ->
|
||||
{:error, {:request_failed, reason}}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
defp blank?(nil), do: true
|
||||
defp blank?(s) when is_binary(s), do: String.trim(s) == ""
|
||||
defp blank?(_), do: true
|
||||
|
||||
@doc """
|
||||
Creates a finance contact in Vereinfacht for the given member.
|
||||
|
||||
|
|
@ -360,5 +408,16 @@ defmodule Mv.Vereinfacht.Client do
|
|||
defp extract_error_message(%{"errors" => [%{"detail" => d} | _]}) when is_binary(d), do: d
|
||||
defp extract_error_message(%{"errors" => [%{"title" => t} | _]}) when is_binary(t), do: t
|
||||
defp extract_error_message(body) when is_map(body), do: inspect(body)
|
||||
|
||||
defp extract_error_message(body) when is_binary(body) do
|
||||
trimmed = String.trim(body)
|
||||
|
||||
if String.starts_with?(trimmed, "<") do
|
||||
:html_response
|
||||
else
|
||||
trimmed
|
||||
end
|
||||
end
|
||||
|
||||
defp extract_error_message(other), do: inspect(other)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -14,6 +14,27 @@ defmodule Mv.Vereinfacht do
|
|||
alias Mv.Helpers.SystemActor
|
||||
alias Mv.Helpers
|
||||
|
||||
@doc """
|
||||
Tests the connection to the Vereinfacht API using the current configuration.
|
||||
|
||||
Delegates to `Mv.Vereinfacht.Client.test_connection/3` with the values from
|
||||
`Mv.Config` (ENV variables take priority over database settings).
|
||||
|
||||
## Returns
|
||||
- `{:ok, :connected}` – credentials are valid and API is reachable
|
||||
- `{:error, :not_configured}` – URL, API key or club ID is missing
|
||||
- `{:error, {:http, status, message}}` – API returned an error (e.g. 401, 403)
|
||||
- `{:error, {:request_failed, reason}}` – network/transport error
|
||||
"""
|
||||
@spec test_connection() :: {:ok, :connected} | {:error, term()}
|
||||
def test_connection do
|
||||
Client.test_connection(
|
||||
Mv.Config.vereinfacht_api_url(),
|
||||
Mv.Config.vereinfacht_api_key(),
|
||||
Mv.Config.vereinfacht_club_id()
|
||||
)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Syncs a single member to Vereinfacht (create or update finance contact).
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue