Bläddra i källkod

Sort results by 24-hour limit

theenglishway (time) 4 år sedan
förälder
incheckning
53908ed9cf
1 ändrade filer med 13 tillägg och 2 borttagningar
  1. 13 2
      lib/vaccins/location_store.ex

+ 13 - 2
lib/vaccins/location_store.ex

@@ -1,6 +1,7 @@
 defmodule Vaccins.LocationStore do
   defmodule Location do
     alias __MODULE__, as: Location
+    @limit 60 * 60 * 24
 
     defstruct [
       :id,
@@ -23,8 +24,18 @@ defmodule Vaccins.LocationStore do
       with url <- q |> provider.to_url() |> URI.to_string(),
            {:ok, result} <- url |> Vaccins.Scraper.get_json() do
         case result |> provider.analyze_result() do
-          {:ok, slots} -> {id, slots, bp}
-          {:error, reason} when reason in [:no_availability] -> {id, reason}
+          {:ok, slots} ->
+            case slots
+                 |> Enum.group_by(&(&1 |> DateTime.diff(DateTime.utc_now()) |> abs < @limit)) do
+              grouped = %{true: before_limit} ->
+                {:ok, before_limit, grouped |> Map.get(false, [])}
+
+              %{false: after_limit} ->
+                {:ok, after_limit}
+            end
+
+          error = {:error, reason} when reason in [:no_availability] ->
+            error
         end
       end
     end