[WIP] Working on notification parser
This commit is contained in:
parent
5069e01bd7
commit
e8e85ff9c3
4 changed files with 66 additions and 1 deletions
28
lib/pinchflat/platform/app_notification_parser.ex
Normal file
28
lib/pinchflat/platform/app_notification_parser.ex
Normal file
|
|
@ -0,0 +1,28 @@
|
||||||
|
defmodule Pinchflat.Platform.AppNotificationParser do
|
||||||
|
alias Pinchflat.Platform
|
||||||
|
|
||||||
|
def parse_and_store_notifications(notification_json) do
|
||||||
|
{:ok, notifications} =
|
||||||
|
notification_json
|
||||||
|
|> parse_notification_json!()
|
||||||
|
|> store_notifications()
|
||||||
|
|
||||||
|
{:ok, notifications}
|
||||||
|
end
|
||||||
|
|
||||||
|
defp parse_notification_json!(json) do
|
||||||
|
Phoenix.json_library().decode!(json)
|
||||||
|
end
|
||||||
|
|
||||||
|
defp store_notifications(notification_json) do
|
||||||
|
Enum.map(notification_json, fn notification ->
|
||||||
|
{:ok, notification} = Platform.create_app_notification(notification)
|
||||||
|
|
||||||
|
notification
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
|
||||||
|
# defp prior_notifications_exist? do
|
||||||
|
# # TODO
|
||||||
|
# end
|
||||||
|
end
|
||||||
|
|
@ -25,9 +25,14 @@ defmodule Pinchflat.Platform do
|
||||||
def create_app_notification(attrs \\ %{}) do
|
def create_app_notification(attrs \\ %{}) do
|
||||||
%AppNotification{}
|
%AppNotification{}
|
||||||
|> AppNotification.changeset(attrs)
|
|> AppNotification.changeset(attrs)
|
||||||
|
|> IO.inspect()
|
||||||
|> Repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# def mark_all_as_read do
|
||||||
|
# Repo.update_all(AppNotification, set: [read_at: DateTime.utc_now()])
|
||||||
|
# end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Returns `%Ecto.Changeset{}`
|
Returns `%Ecto.Changeset{}`
|
||||||
"""
|
"""
|
||||||
|
|
|
||||||
16
test/pinchflat/platform/app_notification_parser_test.exs
Normal file
16
test/pinchflat/platform/app_notification_parser_test.exs
Normal file
|
|
@ -0,0 +1,16 @@
|
||||||
|
defmodule Pinchflat.Platform.AppNotificationParserTest do
|
||||||
|
use Pinchflat.DataCase
|
||||||
|
|
||||||
|
import Pinchflat.PlatformFixtures
|
||||||
|
|
||||||
|
alias Pinchflat.Platform.AppNotificationParser
|
||||||
|
|
||||||
|
describe "parse_and_store_notifications/1" do
|
||||||
|
test "parses and stores notifications" do
|
||||||
|
notification_json = app_notification_json_fixture()
|
||||||
|
|
||||||
|
assert {:ok, notifications} = AppNotificationParser.parse_and_store_notifications(notification_json)
|
||||||
|
# assert Enum.count(notifications) == 2
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
@ -5,7 +5,7 @@ defmodule Pinchflat.PlatformFixtures do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Generate a app_notification.
|
Generate an app_notification.
|
||||||
"""
|
"""
|
||||||
def app_notification_fixture(attrs \\ %{}) do
|
def app_notification_fixture(attrs \\ %{}) do
|
||||||
{:ok, app_notification} =
|
{:ok, app_notification} =
|
||||||
|
|
@ -22,4 +22,20 @@ defmodule Pinchflat.PlatformFixtures do
|
||||||
|
|
||||||
app_notification
|
app_notification
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@doc """
|
||||||
|
Generate an app_notification JSON fixture.
|
||||||
|
"""
|
||||||
|
def app_notification_json_fixture(attrs \\ %{}) do
|
||||||
|
attrs
|
||||||
|
|> Enum.into(%{
|
||||||
|
title: "a cool title",
|
||||||
|
body: "a cool notification",
|
||||||
|
notification_date: ~D[2020-01-01],
|
||||||
|
read_at: nil,
|
||||||
|
severity: :alert,
|
||||||
|
uuid: Ecto.UUID.generate()
|
||||||
|
})
|
||||||
|
|> Phoenix.json_library().encode!()
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue