Skip to content

Commit

Permalink
Répare discussions repliées (#3439)
Browse files Browse the repository at this point in the history
* compare datetime to get the max one instead of relying on order

* more elixirish syntax

* use pattern matching

* mix credo
  • Loading branch information
vdegove authored Sep 5, 2023
1 parent 44cae7c commit af989b9
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions apps/transport/lib/transport_web/live/discussions_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,16 @@ defmodule TransportWeb.DiscussionsLive do
def discussion_should_be_closed?(%{"closed" => closed}) when not is_nil(closed), do: true

def discussion_should_be_closed?(%{"discussion" => comment_list}) do
{:ok, latest_comment_datetime, 0} = List.first(comment_list)["posted_on"] |> DateTime.from_iso8601()
DateTime.utc_now() |> Timex.shift(months: -2) |> DateTime.compare(latest_comment_datetime) == :gt
latest_comment_datetime =
comment_list
|> Enum.map(fn comment ->
{:ok, comment_datetime, 0} = DateTime.from_iso8601(comment["posted_on"])
comment_datetime
end)
|> Enum.max(DateTime)

two_months_ago = DateTime.utc_now() |> Timex.shift(months: -2)
DateTime.compare(two_months_ago, latest_comment_datetime) == :gt
end
end

Expand Down

0 comments on commit af989b9

Please sign in to comment.