prop/lib/microwaveprop_web/controllers/api/v1/me_controller.ex
Graham McIntire fb49eb016d
feat(monitors): schema migration + remove user self-service creation
- Add hardware/config fields migration to beacon_monitors table
- Update BeaconMonitor schema with provision/config changesets
- Add context functions: create_hardware, update_config, list_all_monitors
- Remove user-facing monitor creation (browser POST + API POST)
- Update settings page: show assigned monitors table with hardware info
- Update profile page: show assigned monitors, remove register links
- Fix all tests to match new API
2026-07-21 18:28:51 -05:00

95 lines
3.1 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 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