diff --git a/lib/microwaveprop/radio.ex b/lib/microwaveprop/radio.ex index 7bd72b62..604922a3 100644 --- a/lib/microwaveprop/radio.ex +++ b/lib/microwaveprop/radio.ex @@ -387,10 +387,13 @@ defmodule Microwaveprop.Radio do @dedup_window_seconds 3600 - @spec create_contact(map()) :: + @spec create_contact(map(), String.t() | nil) :: {:ok, Contact.t()} | {:error, Ecto.Changeset.t()} | {:error, :duplicate, Contact.t()} - def create_contact(attrs) do - changeset = Contact.submission_changeset(%Contact{}, attrs) + def create_contact(attrs, user_id \\ nil) do + changeset = + %Contact{} + |> Contact.submission_changeset(attrs) + |> maybe_put_user_id(user_id) if changeset.valid? do insert_validated_contact(changeset) @@ -399,6 +402,9 @@ defmodule Microwaveprop.Radio do end end + defp maybe_put_user_id(changeset, nil), do: changeset + defp maybe_put_user_id(changeset, user_id), do: Ecto.Changeset.put_change(changeset, :user_id, user_id) + defp insert_validated_contact(changeset) do if existing = find_duplicate_contact(changeset) do {:error, :duplicate, existing} @@ -632,12 +638,13 @@ defmodule Microwaveprop.Radio do end defp maybe_recompute_positions(changes, contact, proposed) do - grids_or_band_changed = + needs_recompute = Map.has_key?(proposed, "grid1") or Map.has_key?(proposed, "grid2") or - Map.has_key?(proposed, "band") + Map.has_key?(proposed, "band") or + Map.has_key?(proposed, "qso_timestamp") - if grids_or_band_changed do + if needs_recompute do recompute_positions(changes, contact, proposed) else changes diff --git a/lib/microwaveprop/radio/contact.ex b/lib/microwaveprop/radio/contact.ex index 801905e0..a6f72439 100644 --- a/lib/microwaveprop/radio/contact.ex +++ b/lib/microwaveprop/radio/contact.ex @@ -59,7 +59,7 @@ defmodule Microwaveprop.Radio.Contact do |> validate_required(@required_fields) end - @submission_fields ~w(station1 station2 qso_timestamp mode band grid1 grid2 submitter_email user_id)a + @submission_fields ~w(station1 station2 qso_timestamp mode band grid1 grid2 submitter_email)a @submission_required ~w(station1 station2 qso_timestamp mode band grid1 grid2)a @allowed_modes ~w(CW SSB FM FT8 FT4 Q65) @allowed_bands Enum.map(~w(1296 2304 3456 5760 10000 24000 47000 68000 75000 122000 134000 241000), &Decimal.new/1) diff --git a/lib/microwaveprop_web/live/contact_live/show.ex b/lib/microwaveprop_web/live/contact_live/show.ex index 3b9c2d49..eee57eec 100644 --- a/lib/microwaveprop_web/live/contact_live/show.ex +++ b/lib/microwaveprop_web/live/contact_live/show.ex @@ -122,8 +122,12 @@ defmodule MicrowavepropWeb.ContactLive.Show do end def handle_event("toggle_flag", _params, socket) do - contact = Radio.toggle_flagged_invalid!(socket.assigns.contact) - {:noreply, assign(socket, contact: contact)} + if admin?(socket.assigns.current_scope) do + contact = Radio.toggle_flagged_invalid!(socket.assigns.contact) + {:noreply, assign(socket, contact: contact)} + else + {:noreply, put_flash(socket, :error, "Admins only.")} + end end def handle_event("toggle_profile", %{"id" => id}, socket) do @@ -575,6 +579,7 @@ defmodule MicrowavepropWeb.ContactLive.Show do <% end %>