Skip to content

Commit

Permalink
fixup 2 credo
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Aug 25, 2024
1 parent 9fc55c4 commit 7f9f93e
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 25 deletions.
14 changes: 7 additions & 7 deletions lib/radiator/outline/dispatch.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
defmodule Radiator.Outline.Dispatch do
@moduledoc false

alias Radiator.Outline.Command
alias Radiator.Outline.Event
alias Radiator.Outline.EventProducer
alias Radiator.Outline.{Command, Event, EventProducer, Validations}

def insert_node(attributes, user_id, event_id) do
"insert_node"
Expand Down Expand Up @@ -34,11 +32,13 @@ defmodule Radiator.Outline.Dispatch do
end

def broadcast(event) do
if Mix.env == :dev || Mix.env == :test do
:ok = event
|> Event.episode_id()
|> Radiator.Outline.Validations.validate_tree_for_episode()
if Mix.env() == :dev || Mix.env() == :test do
:ok =
event
|> Event.episode_id()
|> Validations.validate_tree_for_episode()
end

Phoenix.PubSub.broadcast(Radiator.PubSub, "events", event)
end

Expand Down
1 change: 1 addition & 0 deletions lib/radiator/outline/event.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ defmodule Radiator.Outline.Event do
NodeInsertedEvent,
NodeMovedEvent
}

alias Radiator.Outline.NodeRepository

def payload(%NodeInsertedEvent{} = event) do
Expand Down
46 changes: 28 additions & 18 deletions lib/radiator/outline/validations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ defmodule Radiator.Outline.Validations do
end
end

@doc """
Validates a tree for an episode.
Returns :ok if the tree is valid
"""
def validate_tree_for_episode(episode_id) do
{:ok, tree_nodes} = Outline.get_node_tree(episode_id)
# tree_nodes
# every level has 1 node with prev_id nil
# all other nodes in level have prev_id set and are connected to the previous node

if Enum.count(tree_nodes) == NodeRepository.count_nodes_by_episode(episode_id) do
validate_tree_nodes(tree_nodes)
else
Expand All @@ -52,27 +54,35 @@ defmodule Radiator.Outline.Validations do
end

# iterate through the levels of the tree
# every level has 1 node with prev_id nil
# all other nodes in level have prev_id set and are connected to the previous node
# should be used in dev and test only
# might crash if the tree is not consistent
defp validate_tree_nodes(tree_nodes) do
tree_nodes
|> Enum.group_by(& &1.level)
|> Enum.map(fn {_level, nodes} ->
# get the node with prev_id nil
first_node = Enum.find(nodes, &(&1.prev_id == nil))
# get the rest of the nodes
rest_nodes = Enum.reject(nodes, &(&1.prev_id == nil))
# iterate through the rest of the nodes
Enum.reduce(rest_nodes, first_node, fn node, prev_node ->
check_node_prev_node_of(prev_node, node)
_all_level_nodes =
tree_nodes
|> Enum.group_by(& &1.level)
|> Enum.map(fn {_level, nodes} ->
# get the node with prev_id nil
first_node = Enum.find(nodes, &(&1.prev_id == nil))
# get the rest of the nodes
rest_nodes = Enum.reject(nodes, &(&1.prev_id == nil))
# iterate through the rest of the nodes
%Node{} =
_last_node =
Enum.reduce(rest_nodes, first_node, fn node, prev_node ->
check_node_prev_node_of(prev_node, node)
end)
end)
end)

:ok
end

defp check_node_prev_node_of(
%Node{uuid: id},
%Node{prev_id: id} = node
),
do: node
%Node{uuid: id},
%Node{prev_id: id} = node
),
do: node

defp check_node_prev_node_of(_, _), do: {:error, :prev_id_not_consistent}
end

0 comments on commit 7f9f93e

Please sign in to comment.