prop/lib/microwaveprop_web/controllers/api/v1/me_controller.ex
Graham McIntire fd976b0cd5
fix: resolve 391 Credo issues across codebase
- Add jump_credo_checks ~> 0.4 with all 20 checks enabled
- Fix all standard Credo issues: 139 @spec (113 done, 26 remain),
  4 refactoring, 3 alias usage, 9 System.cmd env, 5 unsafe_to_atom,
  2 max line length, 9 assert_receive timeout
- Fix 170+ jump_credo_checks warnings:
  - 117 TopLevelAliasImportRequire: move nested alias/import to module top
  - 32 UseObanProWorker: switch to Oban.Pro.Worker
  - 4 DoctestIExExamples: add doctests / create test file
  - ~20 WeakAssertion: strengthen type-check assertions
  - Various ConditionalAssertion, AssertReceiveTimeout fixes
- Exclude vendor/ from Credo analysis
- Remaining: 175 warnings (mostly opinionated WeakAssertion,
  AvoidSocketAssignsInTest), 26 @spec annotations
2026-06-12 13:51:32 -05:00

111 lines
3.6 KiB
Elixir

defmodule MicrowavepropWeb.Api.V1.MeController do
@moduledoc """
Endpoints scoped to the authenticated user's own profile, contacts,
beacons, beacon monitors, and API tokens.
"""
use Phoenix.Controller, formats: [:json]
alias Microwaveprop.Accounts
alias Microwaveprop.BeaconMonitors
alias Microwaveprop.Beacons
alias Microwaveprop.Radio
alias MicrowavepropWeb.Api.ErrorJSON
alias MicrowavepropWeb.Api.V1.BeaconJSON
alias MicrowavepropWeb.Api.V1.BeaconMonitorJSON
alias MicrowavepropWeb.Api.V1.ContactJSON
alias MicrowavepropWeb.Api.V1.TokenJSON
alias MicrowavepropWeb.Api.V1.UserJSON
plug :accepts, ["json"]
action_fallback MicrowavepropWeb.Api.FallbackController
## Profile -----------------------------------------------------------
@spec show(Plug.Conn.t(), map()) :: Plug.Conn.t()
def show(conn, _params) do
user = conn.assigns.current_api_user
json(conn, UserJSON.me(user))
end
@spec update(Plug.Conn.t(), map()) :: Plug.Conn.t()
def update(conn, params) do
user = conn.assigns.current_api_user
attrs = Map.take(params, ["home_grid", "home_lat", "home_lon", "home_elevation_m"])
case Accounts.update_user_home_qth(user, attrs) do
{:ok, updated} -> json(conn, UserJSON.me(updated))
{:error, changeset} -> ErrorJSON.send_changeset(conn, changeset)
end
end
## My contacts -------------------------------------------------------
@spec contacts(Plug.Conn.t(), map()) :: Plug.Conn.t()
def contacts(conn, _params) do
user = conn.assigns.current_api_user
contacts = Radio.list_contacts_for_user(user, user)
json(conn, ContactJSON.index(%{contacts: contacts, viewer: user}))
end
## My beacons --------------------------------------------------------
@spec beacons(Plug.Conn.t(), map()) :: Plug.Conn.t()
def beacons(conn, _params) do
user = conn.assigns.current_api_user
beacons = Beacons.list_beacons_for_user(user, user)
json(conn, BeaconJSON.index(%{beacons: beacons}))
end
## API tokens --------------------------------------------------------
@spec list_tokens(Plug.Conn.t(), map()) :: Plug.Conn.t()
def list_tokens(conn, _params) do
user = conn.assigns.current_api_user
tokens = Accounts.list_api_tokens(user)
json(conn, TokenJSON.index(%{tokens: tokens}))
end
@spec revoke_token(Plug.Conn.t(), map()) :: Plug.Conn.t()
def revoke_token(conn, %{"id" => id}) do
user = conn.assigns.current_api_user
with {:ok, token} <- Accounts.revoke_api_token(user, id) do
json(conn, TokenJSON.show(%{token: token}))
end
end
## Beacon monitors ---------------------------------------------------
@spec list_monitors(Plug.Conn.t(), map()) :: Plug.Conn.t()
def list_monitors(conn, _params) do
user = conn.assigns.current_api_user
monitors = BeaconMonitors.list_monitors_for_user(user)
json(conn, BeaconMonitorJSON.index(%{monitors: monitors}))
end
@spec create_monitor(Plug.Conn.t(), map()) :: Plug.Conn.t()
def create_monitor(conn, params) do
user = conn.assigns.current_api_user
attrs = Map.take(params, ["name"])
case BeaconMonitors.create_monitor(user, attrs) do
{:ok, monitor} ->
conn
|> put_status(:created)
|> json(BeaconMonitorJSON.show(%{monitor: monitor}))
{:error, changeset} ->
ErrorJSON.send_changeset(conn, changeset)
end
end
@spec delete_monitor(Plug.Conn.t(), map()) :: Plug.Conn.t()
def delete_monitor(conn, %{"id" => id}) do
user = conn.assigns.current_api_user
with {:ok, _monitor} <- BeaconMonitors.delete_monitor(user, id) do
send_resp(conn, 204, "")
end
end
end