prop/lib/microwaveprop_web/controllers/beacon_monitor_controller.ex
Graham McIntire 355bed178d Add beacon monitor registration and fix prod SMTP
Users can now register beacon monitors on the settings page. Each
monitor gets a unique random token the remote program will use to
authenticate its reports. Name is the only user-supplied field for
now; more will be added as the monitor protocol is defined.

Also adds :gen_smtp to deps. Swoosh's SMTP adapter depends on
:mimemail which lives in that package, and production was 500'ing
when trying to send confirmation emails without it.
2026-04-08 11:30:28 -05:00

38 lines
1.1 KiB
Elixir

defmodule MicrowavepropWeb.BeaconMonitorController do
use MicrowavepropWeb, :controller
alias Microwaveprop.BeaconMonitors
def create(conn, %{"beacon_monitor" => params}) do
user = conn.assigns.current_scope.user
case BeaconMonitors.create_monitor(user, params) do
{:ok, monitor} ->
conn
|> put_flash(:info, "Monitor '#{monitor.name}' created. Copy its token below.")
|> redirect(to: ~p"/users/settings")
{:error, changeset} ->
message =
changeset
|> Ecto.Changeset.traverse_errors(fn {msg, _} -> msg end)
|> Enum.map_join("; ", fn {k, v} -> "#{k}: #{Enum.join(v, ", ")}" end)
conn
|> put_flash(:error, "Could not create monitor (#{message}).")
|> redirect(to: ~p"/users/settings")
end
end
def delete(conn, %{"id" => id}) do
user = conn.assigns.current_scope.user
conn =
case BeaconMonitors.delete_monitor(user, id) do
{:ok, _monitor} -> put_flash(conn, :info, "Monitor deleted.")
{:error, :not_found} -> put_flash(conn, :error, "Monitor not found.")
end
redirect(conn, to: ~p"/users/settings")
end
end