prop/lib/microwaveprop_web/controllers/api/v1/contact_json.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

64 lines
2 KiB
Elixir

defmodule MicrowavepropWeb.Api.V1.ContactJSON do
@moduledoc "Renders contact (QSO) representations."
alias Microwaveprop.Accounts.User
alias Microwaveprop.Radio.Contact
@spec index(map()) :: map()
def index(%{contacts: contacts} = assigns) do
viewer = Map.get(assigns, :viewer)
%{data: Enum.map(contacts, &data(&1, viewer))}
end
@spec index_paginated(map()) :: map()
def index_paginated(%{contacts: contacts} = assigns) do
viewer = Map.get(assigns, :viewer)
%{
data: Enum.map(contacts, &data(&1, viewer)),
meta: %{
page: assigns.page,
per_page: assigns.per_page,
total_entries: assigns.total_entries,
total_pages: assigns.total_pages
}
}
end
@spec show(map()) :: map()
def show(%{contact: contact} = assigns) do
viewer = Map.get(assigns, :viewer)
%{data: data(contact, viewer)}
end
defp data(%Contact{} = c, viewer) do
%{
id: c.id,
station1: c.station1,
station2: c.station2,
qso_timestamp: c.qso_timestamp,
grid1: c.grid1,
grid2: c.grid2,
pos1: c.pos1,
pos2: c.pos2,
mode: c.mode,
band_mhz: c.band && Decimal.to_string(c.band),
distance_km: c.distance_km && Decimal.to_string(c.distance_km),
private: c.private,
user_declared_prop_mode: c.user_declared_prop_mode,
propagation_mechanism: c.propagation_mechanism,
propagation_mechanism_confidence: c.propagation_mechanism_confidence,
notes: maybe_notes(c, viewer),
mine?: mine?(c, viewer)
}
end
# Private notes are only visible to the submitter; everyone else sees nil.
defp maybe_notes(%Contact{user_id: nil}, _viewer), do: nil
defp maybe_notes(%Contact{notes: nil}, _viewer), do: nil
defp maybe_notes(%Contact{user_id: uid, notes: notes}, %User{id: uid}), do: notes
defp maybe_notes(_contact, _viewer), do: nil
defp mine?(%Contact{user_id: uid}, %User{id: uid}) when not is_nil(uid), do: true
defp mine?(_contact, _viewer), do: false
end