Skip to content

Commit

Permalink
Afficher la carte de la BNLC principale (#4403)
Browse files Browse the repository at this point in the history
* Afficher la carte de la BNLC principale

Fixes #4401.

* Renommage clef de tri

* Renommage clef de tri
  • Loading branch information
ptitfred authored Dec 20, 2024
1 parent 1a28b21 commit 3e3bdcb
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
12 changes: 11 additions & 1 deletion apps/transport/lib/transport_web/views/dataset_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ defmodule TransportWeb.DatasetView do
resources
|> Enum.filter(fn r -> r.format == "csv" end)
|> Enum.reject(fn r -> Resource.community_resource?(r) or Resource.documentation?(r) end)
|> Enum.max_by(& &1.last_update, DateTime, fn -> nil end)
|> Enum.max_by(&{&1.type, &1.last_update}, TransportWeb.DatasetView.ResourceTypeSortKey, fn -> nil end)
end

def get_resource_to_display(%Dataset{type: type, resources: resources})
Expand Down Expand Up @@ -579,3 +579,13 @@ defmodule TransportWeb.DatasetView do
"fa fa-heart #{value}" |> String.trim()
end
end

defmodule TransportWeb.DatasetView.ResourceTypeSortKey do
def compare({left_type, left_last_update}, {right_type, right_last_update}) do
cond do
left_type == right_type -> DateTime.compare(left_last_update, right_last_update)
left_type == "main" -> :gt
true -> :lt
end
end
end
62 changes: 62 additions & 0 deletions apps/transport/test/transport_web/views/dataset_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,68 @@ defmodule TransportWeb.DatasetViewTest do
assert get_resource_to_display(dataset_only_roads) == nil
end

test "resource to display for BNLC" do
dataset_two_bnlcs = %DB.Dataset{
type: "carpooling-areas",
resources: [
%DB.Resource{
id: 1,
url: "https://example.com/bnlc.csv",
format: "csv",
schema_name: "etalab/schema-lieux-covoiturage",
last_update: ~U[2016-05-24 13:26:08Z],
type: "other"
},
%DB.Resource{
id: 2,
url: "https://example.com/bnlc-consolidated.csv",
format: "csv",
schema_name: "etalab/schema-lieux-covoiturage",
last_update: ~U[2016-05-24 13:25:08Z],
type: "other"
}
]
}

dataset_two_bnlcs_with_the_main = %DB.Dataset{
type: "carpooling-areas",
resources: [
%DB.Resource{
id: 1,
url: "https://example.com/bnlc.csv",
format: "csv",
schema_name: "etalab/schema-lieux-covoiturage",
last_update: ~U[2016-05-24 13:26:08Z],
type: "other"
},
%DB.Resource{
id: 2,
url: "https://example.com/bnlc-consolidated-1.csv",
format: "csv",
schema_name: "etalab/schema-lieux-covoiturage",
# not the latest, but we prefer the main one
last_update: ~U[2016-05-24 13:24:08Z],
type: "main"
},
%DB.Resource{
id: 3,
url: "https://example.com/bnlc-consolidated-2.csv",
format: "csv",
schema_name: "etalab/schema-lieux-covoiturage",
# not the latest, but we prefer the main one
last_update: ~U[2016-05-24 13:25:08Z],
type: "main"
}
]
}

# Display the latest
assert get_resource_to_display(dataset_two_bnlcs).id == 1

# Display the latest main one
assert get_resource_to_display(dataset_two_bnlcs_with_the_main).id == 3
end

test "test data is up to date" do
assert "tipi.bison-fute.gouv.fr" == Application.fetch_env!(:transport, :bison_fute_host)
end
Expand Down

0 comments on commit 3e3bdcb

Please sign in to comment.