mitgliederverwaltung/lib/mv_web/live/join_request_live/helpers.ex
Simon 086ecdcb1b
All checks were successful
continuous-integration/drone/push Build is passing
feat: prevent join requests with equal mail
2026-03-13 11:18:34 +01:00

60 lines
1.9 KiB
Elixir

defmodule MvWeb.JoinRequestLive.Helpers do
@moduledoc """
Shared helpers for JoinRequest LiveViews (Index, Show): status display,
badge variants, and reviewer display.
"""
use Gettext, backend: MvWeb.Gettext
@doc "Human-readable label for a join request status atom."
def format_status(:pending_confirmation), do: gettext("Pending confirmation")
def format_status(:submitted), do: gettext("Submitted")
def format_status(:approved), do: gettext("Approved")
def format_status(:rejected), do: gettext("Rejected")
def format_status(other), do: to_string(other)
@doc "Badge variant for the status (used with CoreComponents.badge)."
def status_badge_variant(:submitted), do: :info
def status_badge_variant(:approved), do: :success
def status_badge_variant(:rejected), do: :error
def status_badge_variant(_), do: :neutral
@doc """
Returns the reviewer display string (e.g. email) for a join request, or nil if none.
Prefers the denormalized :reviewed_by_display (set on approve/reject) so the UI
works for all roles without loading the User resource. Falls back to
:reviewed_by_user when loaded (e.g. admin or legacy data before backfill).
"""
def reviewer_display(req) when is_map(req) do
case Map.get(req, :reviewed_by_display) do
s when is_binary(s) ->
trimmed = String.trim(s)
if trimmed == "", do: reviewer_display_from_user(req), else: trimmed
_ ->
reviewer_display_from_user(req)
end
end
def reviewer_display(_), do: nil
defp reviewer_display_from_user(req) do
user = Map.get(req, :reviewed_by_user)
case user do
nil ->
nil
%{email: email} when is_binary(email) ->
s = String.trim(email)
if s == "", do: nil, else: s
%{"email" => email} when is_binary(email) ->
s = String.trim(email)
if s == "", do: nil, else: s
_ ->
nil
end
end
end