Fix datetime unserialize on admin logs view

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2020-08-18 17:22:06 +02:00
parent 2ea6286d3f
commit 6ae8de7560
No known key found for this signature in database
GPG key ID: A061B9DDE0CA0773

View file

@ -128,11 +128,33 @@ defmodule Mobilizon.GraphQL.Resolvers.Admin do
end
defp convert_changes_to_struct(struct, changes) do
with data <- for({key, val} <- changes, into: %{}, do: {String.to_atom(key), val}) do
with changeset <- struct.__changeset__,
data <-
for(
{key, val} <- changes,
into: %{},
do: {String.to_atom(key), process_eventual_type(changeset, key, val)}
) do
struct(struct, data)
end
end
# datetimes are not unserialized as DateTime/NaiveDateTime so we do it manually with changeset data
defp process_eventual_type(changeset, key, val) do
cond do
changeset[String.to_atom(key)] == :utc_datetime and not is_nil(val) ->
{:ok, datetime, _} = DateTime.from_iso8601(val)
datetime
changeset[String.to_atom(key)] == :naive_datetime and not is_nil(val) ->
{:ok, datetime} = NaiveDateTime.from_iso8601(val)
datetime
true ->
val
end
end
def get_dashboard(_parent, _args, %{context: %{current_user: %User{role: role}}})
when is_admin(role) do
last_public_event_published =