Skip to content

Commit

Permalink
Recherche de contacts : ignorer valeurs nulles (#3352)
Browse files Browse the repository at this point in the history
  • Loading branch information
AntoineAugusti authored Jul 26, 2023
1 parent 0920dd2 commit ab2f338
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ defmodule TransportWeb.Backoffice.ContactController do
|> render("index.html")
end

defp search_datalist do
def search_datalist do
:organization
|> contact_values_for_field()
|> Enum.concat(contact_values_for_field(:last_name))
Expand All @@ -87,6 +87,7 @@ defmodule TransportWeb.Backoffice.ContactController do
defp contact_values_for_field(field) when is_atom(field) do
DB.Contact.base_query()
|> select([contact: c], field(c, ^field))
|> where([contact: c], not is_nil(field(c, ^field)))
|> order_by([contact: c], asc: ^field)
|> distinct(true)
|> DB.Repo.all()
Expand Down
10 changes: 7 additions & 3 deletions apps/transport/lib/transport_web/views/backoffice/page_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,17 @@ defmodule TransportWeb.Backoffice.PageView do

@doc """
Replaces accented letters by their regular versions.
From https://stackoverflow.com/a/68724296
Taken from https://stackoverflow.com/a/68724296
iex> unaccent("Et Ça sera sa moitié.")
"Et Ca sera sa moitie."
iex> unaccent(nil)
""
"""
def unaccent(value) do
@spec unaccent(nil | binary()) :: binary()
def unaccent(nil), do: ""

def unaccent(value) when is_binary(value) do
~R<\p{Mn}>u
|> Regex.replace(value |> :unicode.characters_to_nfd_binary(), "")
|> :unicode.characters_to_nfc_binary()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule TransportWeb.Backoffice.ContactControllerTest do
use TransportWeb.ConnCase, async: true
import DB.Factory
alias TransportWeb.Backoffice.ContactController

setup do
Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo)
Expand Down Expand Up @@ -199,6 +200,17 @@ defmodule TransportWeb.Backoffice.ContactControllerTest do
assert is_nil(DB.Repo.reload(contact))
end

test "search_datalist" do
DB.Contact.insert!(sample_contact_args(%{last_name: "Doe", organization: "FooBar"}))
DB.Contact.insert!(sample_contact_args(%{last_name: "Oppenheimer", organization: "FooBar"}))

DB.Contact.insert!(
sample_contact_args(%{first_name: nil, last_name: nil, organization: "Disney", mailing_list_title: "Data"})
)

assert ["Disney", "Doe", "FooBar", "Oppenheimer"] == ContactController.search_datalist()
end

defp sample_contact_args(%{} = args \\ %{}) do
Map.merge(
%{
Expand Down

0 comments on commit ab2f338

Please sign in to comment.