From 614cc1adc7a55db1615aa4d61060d6ac839ff4fe Mon Sep 17 00:00:00 2001 From: Thomas Citharel Date: Tue, 18 Aug 2020 17:22:06 +0200 Subject: [PATCH] Fix datetime unserialize on admin logs view Signed-off-by: Thomas Citharel --- lib/graphql/resolvers/admin.ex | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/lib/graphql/resolvers/admin.ex b/lib/graphql/resolvers/admin.ex index bef94265a..90ef50f0b 100644 --- a/lib/graphql/resolvers/admin.ex +++ b/lib/graphql/resolvers/admin.ex @@ -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 =