Commit 5aabfc1d authored by Miguel Basticioto's avatar Miguel Basticioto
Browse files

Merge branch 'dev' of /Users/Miguel/Desktop/Elixir/Ubi/notification_dispatcher with conflicts.

parent 61626598
......@@ -6,7 +6,7 @@ defmodule NotificationDispatcher.Context.NotificationsContext do
import Ecto.Query, warn: false
def get_repo(), do: Application.fetch_env!(:notification_dispatcher, :repo)
alias NotificationDispatcher.Schema.NotificationMessage
alias NotificationDispatcher.Schema.{Device, NotificationMessage}
def get_notification_message!(id), do: get_repo().get!(NotificationMessage, id)
......@@ -41,4 +41,52 @@ defmodule NotificationDispatcher.Context.NotificationsContext do
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
def create_device(attrs \\ %{}, user_id) do
attrs = Map.put(attrs, "user_id", user_id)
os = case attrs["os"] do
"android" -> 0
"ios" -> 1
_ -> 2
end
attrs = Map.put(attrs, "os", os)
%Device{}
|> Device.changeset(attrs)
|> Repo.insert(
on_conflict: [
set: [
device_token: attrs["device_token"],
os: attrs["os"]
]
],
conflict_target: [:device_token],
returning: [:id]
)
end
end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment