From 3e3bdcbd7315685fff03758cfb998655be515b98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Menou?= Date: Fri, 20 Dec 2024 18:00:21 +0100 Subject: [PATCH] Afficher la carte de la BNLC principale (#4403) * Afficher la carte de la BNLC principale Fixes #4401. * Renommage clef de tri * Renommage clef de tri --- .../lib/transport_web/views/dataset_view.ex | 12 +++- .../transport_web/views/dataset_view_test.exs | 62 +++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) diff --git a/apps/transport/lib/transport_web/views/dataset_view.ex b/apps/transport/lib/transport_web/views/dataset_view.ex index 4614bdd04c..54919eee7f 100644 --- a/apps/transport/lib/transport_web/views/dataset_view.ex +++ b/apps/transport/lib/transport_web/views/dataset_view.ex @@ -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}) @@ -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 diff --git a/apps/transport/test/transport_web/views/dataset_view_test.exs b/apps/transport/test/transport_web/views/dataset_view_test.exs index 4247f881bd..567ca5bf59 100644 --- a/apps/transport/test/transport_web/views/dataset_view_test.exs +++ b/apps/transport/test/transport_web/views/dataset_view_test.exs @@ -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