After code:
defp where_age_groups_in(queryable, %{selected_age_groups: age_groups}) do
conditions =
Enum.map(age_groups, fn age_group ->
case age_group do
"0-30" ->
dynamic([vi], fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate)
dynamic(
[vi],
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate) >= 30 and
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate)
dynamic(
[vi],
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate) >= 60 and
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate)
dynamic([vi], fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate) > 90)
_ ->
true
end
end)
combined_conditions =
Enum.reduce(conditions, fn condition, acc ->
dynamic([vi], ^acc or ^condition)
end)
from(vi in queryable, where: ^combined_conditions)
end
defp where_age_groups_in(queryable, %{selected_age_groups: age_groups}) do
conditions =
Enum.map(age_groups, fn age_group ->
case age_group do
"0-30" ->
dynamic([vi], fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate)
dynamic(
[vi],
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate) >= 30 and
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate)
dynamic(
[vi],
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate) >= 60 and
fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate)
dynamic([vi], fragment("date_part('day', now() - ?::timestamp)::int", vi.entrydate) > 90)
_ ->
true
end
end)
combined_conditions =
Enum.reduce(conditions, fn condition, acc ->
dynamic([vi], ^acc or ^condition)
end)
from(vi in queryable, where: ^combined_conditions)
end