| 1 |
|
defmodule WalletNotifications.Queries.ListUserNotifications do |
| 2 |
|
@moduledoc "Lists all notifications for a given user, optionally filtered by status or channel." |
| 3 |
|
alias WalletNotifications.NotificationStore |
| 4 |
|
|
| 5 |
|
@spec execute(user_id :: String.t(), opts :: keyword()) :: [WalletNotifications.Notification.t()] |
| 6 |
1 |
def execute(user_id, opts \\ []) do |
| 7 |
4 |
notifications = NotificationStore.list_by_user(user_id) |
| 8 |
|
|
| 9 |
|
notifications |
| 10 |
|
|> maybe_filter_status(Keyword.get(opts, :status)) |
| 11 |
4 |
|> maybe_filter_channel(Keyword.get(opts, :channel)) |
| 12 |
|
end |
| 13 |
|
|
| 14 |
2 |
defp maybe_filter_status(ns, nil), do: ns |
| 15 |
2 |
defp maybe_filter_status(ns, status), do: Enum.filter(ns, &(&1.status == status)) |
| 16 |
|
|
| 17 |
3 |
defp maybe_filter_channel(ns, nil), do: ns |
| 18 |
1 |
defp maybe_filter_channel(ns, channel), do: Enum.filter(ns, &(&1.channel == channel)) |
| 19 |
|
end |