prop/lib/microwaveprop/radio/edit_notifier.ex
Graham McIntire 316fb2fbc7
Fix low-severity bugs and re-enable Credo checks
- Bug #12: Lower rate limit to 15/min
- Bug #13: Wrap model loading in Task.start
- Bug #14: Fix classify_time_period guard gap at -3.0
- Bug #15: Not applicable (Elixir has no ?? operator)
- Bug #16: Remove fallback Repo.get for station preload
- Bug #17: Extract cache_key() in ContactMapController
- Bug #18: Add has_many :contacts and :beacons to User schema
- A&D #4: Move serve_markdown_if_requested after secure headers
- Config #1: Move signing_salt to runtime.exs env var
- Config #2: Use --check-unused instead of --unused
- Config #3: Re-enable UnsafeToAtom Credo check
- Config #5: Re-enable LeakyEnvironment Credo check
- Add @spec annotations to fix re-enabled Specs violations
- Replace String.to_atom with to_existing_atom where guarded
2026-05-29 17:29:22 -05:00

91 lines
2.4 KiB
Elixir

defmodule Microwaveprop.Radio.EditNotifier do
@moduledoc false
import Swoosh.Email
alias Microwaveprop.Mailer
alias Microwaveprop.Radio.ContactEdit
@spec deliver_edit_approved(ContactEdit.t()) :: {:ok, term()} | {:error, term()}
def deliver_edit_approved(edit) do
edit = Microwaveprop.Repo.preload(edit, [:user, :contact])
contact = edit.contact
body = """
==============================
Hi #{edit.user.callsign},
Your suggested edit to the contact #{contact.station1} <-> #{contact.station2}
(#{format_band(contact.band)}, #{format_ts(contact.qso_timestamp)}) has been APPROVED
and applied.
Changes applied:
#{format_changes(edit.proposed_changes)}
#{format_note(edit.admin_note)}
View the contact: #{url()}/contacts/#{contact.id}
==============================
"""
deliver(edit.user.email, "Your contact edit was approved", body)
end
@spec deliver_edit_rejected(ContactEdit.t()) :: {:ok, term()} | {:error, term()}
def deliver_edit_rejected(edit) do
edit = Microwaveprop.Repo.preload(edit, [:user, :contact])
contact = edit.contact
body = """
==============================
Hi #{edit.user.callsign},
Your suggested edit to the contact #{contact.station1} <-> #{contact.station2}
(#{format_band(contact.band)}, #{format_ts(contact.qso_timestamp)}) has been REJECTED.
Proposed changes:
#{format_changes(edit.proposed_changes)}
#{format_note(edit.admin_note)}
View the contact: #{url()}/contacts/#{contact.id}
==============================
"""
deliver(edit.user.email, "Your contact edit was rejected", body)
end
defp deliver(recipient, subject, body) do
email =
new()
|> to(recipient)
|> Mailer.apply_defaults()
|> subject(subject)
|> text_body(body)
with {:ok, _metadata} <- Mailer.deliver(email) do
{:ok, email}
end
end
defp url do
MicrowavepropWeb.Endpoint.url()
end
defp format_band(nil), do: "?"
defp format_band(band) do
mhz = Decimal.to_integer(band)
if mhz >= 1000, do: "#{div(mhz, 1000)} GHz", else: "#{mhz} MHz"
end
defp format_ts(nil), do: "?"
defp format_ts(ts), do: Calendar.strftime(ts, "%Y-%m-%d %H:%M UTC")
defp format_changes(changes) do
Enum.map_join(changes, "\n", fn {field, value} -> " - #{field}: #{value}" end)
end
defp format_note(nil), do: ""
defp format_note(""), do: ""
defp format_note(note), do: "\nAdmin note: #{note}\n"
end