[WIP] Working on notification parser

This commit is contained in:
Kieran Eglin 2024-06-20 11:32:37 -07:00
parent 5069e01bd7
commit e8e85ff9c3
No known key found for this signature in database
GPG key ID: 193984967FCF432D
4 changed files with 66 additions and 1 deletions

View 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

View file

@ -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{}`
"""

View 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

View file

@ -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