Deduplicate select expressions in convert_to_notification

This commit is contained in:
Liam 2024-08-06 08:32:41 -04:00
parent c173162c1e
commit e704319dd9

View file

@ -47,34 +47,22 @@ defmodule Philomena.Notifications.Creator do
|> insert_notifications(notification_schema, unique_key) |> insert_notifications(notification_schema, unique_key)
end end
# TODO: the following cannot be accomplished with a single query expression defp convert_to_notification(subscription, extra) do
# due to this Ecto bug: https://github.com/elixir-ecto/ecto/issues/4430 now = dynamic([_], type(^DateTime.utc_now(:second), :utc_datetime))
defp convert_to_notification(subscription, [{name, object_id}]) do base = %{
now = DateTime.utc_now(:second) user_id: dynamic([s], s.user_id),
created_at: now,
updated_at: now,
read: false
}
from s in subscription, extra =
select: %{ Map.new(extra, fn {field, value} ->
^name => type(^object_id, :integer), {field, dynamic([_], type(^value, :integer))}
user_id: s.user_id, end)
created_at: ^now,
updated_at: ^now,
read: false
}
end
defp convert_to_notification(subscription, [{name1, object_id1}, {name2, object_id2}]) do from(subscription, select: ^Map.merge(base, extra))
now = DateTime.utc_now(:second)
from s in subscription,
select: %{
^name1 => type(^object_id1, :integer),
^name2 => type(^object_id2, :integer),
user_id: s.user_id,
created_at: ^now,
updated_at: ^now,
read: false
}
end end
defp subscription_query(subscription, notification_author) do defp subscription_query(subscription, notification_author) do