Skip to content

Commit

Permalink
fix error at insert_node when parent_id has been ommitted
Browse files Browse the repository at this point in the history
  • Loading branch information
electronicbites committed Sep 11, 2024
1 parent 7413292 commit 609f73c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
12 changes: 7 additions & 5 deletions lib/radiator/outline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,22 @@ defmodule Radiator.Outline do
prev_id = attrs["prev_id"]
parent_id = attrs["parent_id"]
episode_id = attrs["episode_id"]

prev_node = NodeRepository.get_node_if(prev_id)
parent_node = find_parent_node(prev_node, parent_id)

# find Node which has been previously connected to prev_node
next_node =
Node
|> where(episode_id: ^episode_id)
|> where_prev_node_equals(prev_id)
|> where_parent_node_equals(parent_id)
|> where_parent_node_equals(get_node_id(parent_node))
|> Repo.one()

with prev_node <- NodeRepository.get_node_if(prev_id),
parent_node <- find_parent_node(prev_node, parent_id),
true <- parent_and_prev_consistent?(parent_node, prev_node),
with true <- parent_and_prev_consistent?(parent_node, prev_node),
true <- episode_valid?(episode_id, parent_node, prev_node),
{:ok, node} <- NodeRepository.create_node(set_parent_id_if(attrs, parent_node)),
{:ok, _node_to_move} <- move_node_if(next_node, parent_id, node.uuid) do
{:ok, _node_to_move} <- move_node_if(next_node, get_node_id(parent_node), node.uuid) do
%NodeRepoResult{node: node, next_id: get_node_id(next_node)}
else
false ->
Expand Down
15 changes: 15 additions & 0 deletions test/radiator/outline_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,21 @@ defmodule Radiator.OutlineTest do
assert new_node.prev_id == nested_node_1.uuid
end

test "the next node - if existing - changes its prev_id and gets returned", %{
node_2: node_2,
node_3: node_3
} do
node_attrs = %{
"content" => "new node",
"episode_id" => node_2.episode_id,
"prev_id" => node_2.uuid
}

{:ok, %{next_id: next_id, node: new_node}} = Outline.insert_node(node_attrs)
assert node_3.uuid == next_id
assert NodeRepository.get_node!(node_3.uuid).prev_id == new_node.uuid
end

test "if prev_id has been given the parent_id can be omitted", %{
node_3: node_3,
nested_node_1: nested_node_1
Expand Down

0 comments on commit 609f73c

Please sign in to comment.