Current section
Files
Jump to
Current section
Files
lib/contexts/notifications_context.ex
defmodule NotificationDispatcher.Context.NotificationsContext do
@moduledoc """
The Notifications context.
"""
import Ecto.Query, warn: false
def get_repo(), do: Application.fetch_env!(:notification_dispatcher, :repo)
alias NotificationDispatcher.Schema.{Device, NotificationMessage}
def get_notification_message!(id), do: get_repo().get!(NotificationMessage, id)
def get_one_notification_message!(channel) do
NotificationMessage.query_main()
|> NotificationMessage.where_channel(channel)
|> NotificationMessage.limit(1)
|> get_repo().one()
end
def get_by_type_and_language(type, language) do
NotificationMessage.query_main()
|> NotificationMessage.where_type(type)
|> NotificationMessage.where_locale(language)
|> NotificationMessage.limit(1)
|> get_repo().one()
end
def get_by_type(type) do
NotificationMessage.query_main()
|> NotificationMessage.where_type(type)
|> NotificationMessage.limit(1)
|> get_repo().one()
end
def create_notification_message(attrs \\ %{}) do
%NotificationMessage{}
|> NotificationMessage.changeset(attrs)
|> get_repo().insert()
end
def delete_notification_message(%NotificationMessage{} = notification_message) do
get_repo().delete(notification_message)
end
def get_devices(user_ids) do
Device.query_main()
|> Device.where_user_id_in(user_ids)
|> get_repo().all()
end
def get_device!(id) do
Device.query_main
|> Device.where(id)
|> Repo.one
end
def get_device(id) do
device = get_device!(id)
if is_nil(device), do: :not_found, else: {:ok, device}
end
def update_device(%Device{} = device, attrs) do
device
|> Device.changeset_update(attrs)
|> Repo.update
end
end