Skip to content

Commit

Permalink
save user_id in events
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed May 11, 2024
1 parent 47577c8 commit f2b5470
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 33 deletions.
3 changes: 2 additions & 1 deletion lib/radiator/event_store.ex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ defmodule Radiator.EventStore do
create_event_data(%{
data: AbstractEvent.payload(event),
event_type: AbstractEvent.event_type(event),
uuid: event.event_id
uuid: event.event_id,
user_id: event.user_id
})

event
Expand Down
4 changes: 2 additions & 2 deletions lib/radiator/event_store/event_data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ defmodule Radiator.EventStore.EventData do
@doc false
def changeset(event, attrs) do
event
|> cast(attrs, [:uuid, :event_type, :data])
|> validate_required([:uuid, :event_type])
|> cast(attrs, [:uuid, :event_type, :data, :user_id])
|> validate_required([:uuid, :event_type, :user_id])
end
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_content_changed_event.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Radiator.Outline.Event.NodeContentChangedEvent do
@moduledoc false

defstruct [:event_id, :node_id, :content]
defstruct [:event_id, :node_id, :content, :user_id]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_deleted_event.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Radiator.Outline.Event.NodeDeletedEvent do
@moduledoc false
defstruct [:event_id, :node_id]
defstruct [:event_id, :node_id, :user_id]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_inserted_event.ex
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
defmodule Radiator.Outline.Event.NodeInsertedEvent do
@moduledoc false

defstruct [:event_id, :node]
defstruct [:event_id, :node, :user_id]
end
2 changes: 1 addition & 1 deletion lib/radiator/outline/event/node_moved_event.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule Radiator.Outline.Event.NodeMovedEvent do
@moduledoc false
defstruct [:event_id, :node_id, :parent_id, :prev_id]
defstruct [:event_id, :node_id, :parent_id, :prev_id, :user_id]
end
14 changes: 10 additions & 4 deletions lib/radiator/outline/event_consumer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ defmodule Radiator.Outline.EventConsumer do
{:noreply, [], state}
end

defp process_command(%InsertNodeCommand{payload: payload} = command) do
defp process_command(%InsertNodeCommand{payload: payload, user_id: user_id} = command) do
payload
|> Map.merge(%{"user_id" => user_id})
|> Outline.insert_node()
|> handle_insert_node_result(command)
end
Expand All @@ -37,7 +38,7 @@ defmodule Radiator.Outline.EventConsumer do
end

defp handle_insert_node_result({:ok, node}, command) do
%NodeInsertedEvent{node: node, event_id: command.event_id}
%NodeInsertedEvent{node: node, event_id: command.event_id, user_id: command.user_id}
|> EventStore.persist_event()
|> Dispatch.broadcast()

Expand All @@ -49,8 +50,13 @@ defmodule Radiator.Outline.EventConsumer do
:error
end

def handle_change_node_content_result({:ok, node}, command) do
%NodeContentChangedEvent{node_id: node.id, event_id: command.event_id}
def handle_change_node_content_result({:ok, node}, %ChangeNodeContentCommand{} = command) do
%NodeContentChangedEvent{
node_id: node.id,
content: node.content,
user_id: command.user_id,
event_id: command.event_id
}
|> EventStore.persist_event()
|> Dispatch.broadcast()

Expand Down
13 changes: 9 additions & 4 deletions test/radiator/event_store_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ defmodule Radiator.EventStoreTest do
describe "event_data" do
alias Radiator.EventStore.EventData

alias Radiator.AccountsFixtures
import Radiator.EventStoreFixtures

@invalid_attrs %{data: nil, uuid: nil, event_type: nil}
Expand All @@ -21,16 +22,20 @@ defmodule Radiator.EventStoreTest do
end

test "create_event/1 with valid data creates a event" do
user = AccountsFixtures.user_fixture()

valid_attrs = %{
data: %{},
uuid: "7488a646-e31f-11e4-aace-600308960662",
event_type: "some event_type"
uuid: Ecto.UUID.generate(),
event_type: "some event_type",
user_id: user.id
}

assert {:ok, %EventData{} = event} = EventStore.create_event_data(valid_attrs)
assert event.data == %{}
assert event.uuid == "7488a646-e31f-11e4-aace-600308960662"
assert event.event_type == "some event_type"
assert event.uuid == valid_attrs.uuid
assert event.event_type == valid_attrs.event_type
assert event.user_id == valid_attrs.user_id
end

test "create_event/1 with invalid data returns error changeset" do
Expand Down
56 changes: 39 additions & 17 deletions test/radiator/outline/event_consumer_test.exs
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
defmodule Radiator.Outline.EventConsumerTest do
alias Radiator.Outline.NodeRepository
use Radiator.DataCase

alias Radiator.AccountsFixtures
alias Radiator.Outline.Command
alias Radiator.EventStore
alias Radiator.Outline.{Command, Dispatch, EventConsumer, EventProducer, NodeRepository}
alias Radiator.Outline.Command.InsertNodeCommand
alias Radiator.Outline.Dispatch
alias Radiator.Outline.Event.NodeInsertedEvent
alias Radiator.Outline.EventConsumer
alias Radiator.Outline.EventProducer
alias Radiator.PodcastFixtures

describe "handle_events/2" do
test "insert_node" do
episode = PodcastFixtures.episode_fixture()
setup :prepare_outline

test "insert_node stores a node", %{episode: episode, user: user, event_id: event_id} do
attributes = %{
"title" => "Node Title",
"content" => "Node Content",
"episode_id" => episode.id
}

user_id = "user_id"
event_id = Ecto.UUID.generate()

command = Command.build("insert_node", attributes, user_id, event_id)
num_nodes = NodeRepository.count_nodes_by_episode(episode.id)
command = Command.build("insert_node", attributes, user.id, event_id)
EventConsumer.handle_events([command], 0, nil)

# assert a node has been created
# assert an event has been created (and be stored)
assert num_nodes + 1 == NodeRepository.count_nodes_by_episode(episode.id)
end

test "handles previously enqueued events" do
producer = start_supervised!({EventProducer, name: TestEventProducer})
test "insert_node creates and stores an event", %{
episode: episode,
user: user,
event_id: event_id
} do
new_content = "Node Content"

attributes = %{
"title" => "Node Title",
"content" => new_content,
"episode_id" => episode.id
}

episode = PodcastFixtures.episode_fixture()
user = AccountsFixtures.user_fixture()
event_id = Ecto.UUID.generate()
command = Command.build("insert_node", attributes, user.id, event_id)
EventConsumer.handle_events([command], 0, nil)
event = EventStore.list_event_data() |> hd()

assert event.event_type == "NodeInsertedEvent"
assert event.user_id == user.id
assert event.data["content"] == new_content
end

test "handles previously enqueued events", %{episode: episode, user: user, event_id: event_id} do
producer = start_supervised!({EventProducer, name: TestEventProducer})

command = %InsertNodeCommand{
event_id: event_id,
Expand All @@ -47,7 +63,6 @@ defmodule Radiator.Outline.EventConsumerTest do
}

Dispatch.subscribe(episode.id)

EventProducer.enqueue(producer, command)

start_supervised!(
Expand All @@ -57,4 +72,11 @@ defmodule Radiator.Outline.EventConsumerTest do
assert_receive(%NodeInsertedEvent{}, 1000)
end
end

def prepare_outline(_) do
episode = PodcastFixtures.episode_fixture()
user = AccountsFixtures.user_fixture()
event_id = Ecto.UUID.generate()
%{episode: episode, user: user, event_id: event_id}
end
end
6 changes: 5 additions & 1 deletion test/support/fixtures/event_store_fixtures.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ defmodule Radiator.EventStoreFixtures do
This module defines test helpers for creating
entities via the `Radiator.EventStore` context.
"""
alias Radiator.AccountsFixtures

@doc """
Generate a event data.
"""
def event_data_fixture(attrs \\ %{}) do
user = AccountsFixtures.user_fixture()

{:ok, event} =
attrs
|> Enum.into(%{
data: %{},
event_type: "some event_type",
uuid: "7488a646-e31f-11e4-aace-600308960662"
uuid: Ecto.UUID.generate(),
user_id: user.id
})
|> Radiator.EventStore.create_event_data()

Expand Down

0 comments on commit f2b5470

Please sign in to comment.