towerops/lib/towerops_web/scoped_resource.ex
Graham McIntire 532d88ffb9
perf: disable Req retry for faster error tests
- Add retry: false to VISP Client HTTP requests
- Add retry: false to ReleaseChecker GitHub API requests
- Remove unused Plug.Conn import from RemoteIpLogger
- Remove unused default parameter from req_get/2

Results:
- VISP sync test: 7007ms → 113ms (62x faster)
- ReleaseChecker test: 7004ms → 17ms (402x faster)
- UserResetPasswordLive test: 1495ms → 284ms (5x faster)

Req's default retry behavior (1s, 2s, 4s exponential backoff) was
causing 7-second delays for HTTP 500/503 error responses in tests.
For these clients, immediate failure is preferred over retries.
2026-03-10 16:19:31 -05:00

30 lines
893 B
Elixir

defmodule ToweropsWeb.ScopedResource do
@moduledoc """
Shared helpers for loading organization-scoped resources.
"""
alias Towerops.Repo
@spec fetch(module(), Ecto.UUID.t(), Ecto.UUID.t()) ::
{:ok, struct()} | {:error, :not_found | :forbidden}
def fetch(schema, id, organization_id) do
case Repo.get(schema, id) do
nil ->
{:error, :not_found}
%{organization_id: ^organization_id} = resource ->
{:ok, resource}
%{organization_id: _other_org_id} ->
{:error, :forbidden}
end
end
@spec fetch_preload(module(), Ecto.UUID.t(), Ecto.UUID.t(), atom() | [atom()]) ::
{:ok, struct()} | {:error, :not_found | :forbidden}
def fetch_preload(schema, id, organization_id, preloads) do
with {:ok, resource} <- fetch(schema, id, organization_id) do
{:ok, Repo.preload(resource, preloads)}
end
end
end