diff --git a/lib/pinchflat/platform/app_notification_parser.ex b/lib/pinchflat/platform/app_notification_parser.ex new file mode 100644 index 0000000..e8ed507 --- /dev/null +++ b/lib/pinchflat/platform/app_notification_parser.ex @@ -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 diff --git a/lib/pinchflat/platform/platform.ex b/lib/pinchflat/platform/platform.ex index f50fb29..9c1c242 100644 --- a/lib/pinchflat/platform/platform.ex +++ b/lib/pinchflat/platform/platform.ex @@ -25,9 +25,14 @@ defmodule Pinchflat.Platform do def create_app_notification(attrs \\ %{}) do %AppNotification{} |> AppNotification.changeset(attrs) + |> IO.inspect() |> Repo.insert() end + # def mark_all_as_read do + # Repo.update_all(AppNotification, set: [read_at: DateTime.utc_now()]) + # end + @doc """ Returns `%Ecto.Changeset{}` """ diff --git a/test/pinchflat/platform/app_notification_parser_test.exs b/test/pinchflat/platform/app_notification_parser_test.exs new file mode 100644 index 0000000..8ee7f0c --- /dev/null +++ b/test/pinchflat/platform/app_notification_parser_test.exs @@ -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 diff --git a/test/support/fixtures/platform_fixtures.ex b/test/support/fixtures/platform_fixtures.ex index f049d8c..ba22157 100644 --- a/test/support/fixtures/platform_fixtures.ex +++ b/test/support/fixtures/platform_fixtures.ex @@ -5,7 +5,7 @@ defmodule Pinchflat.PlatformFixtures do """ @doc """ - Generate a app_notification. + Generate an app_notification. """ def app_notification_fixture(attrs \\ %{}) do {:ok, app_notification} = @@ -22,4 +22,20 @@ defmodule Pinchflat.PlatformFixtures do app_notification 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