diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 2ce21c4..cf9a3d2 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -35,7 +35,9 @@ class CreatePlan } $textId = $request->textId; $text = $this->textRepo->find($textId); - $nodesOfText = $this->nodeRepo->findByTextId($textId); + $nodesOfText = $this->filterForNonParentNodes( + $this->nodeRepo->findByTextId($textId) + ); $plan = $this->planRepo->create(new CreatePlanDto( name: $request->name, user: $user, @@ -51,4 +53,22 @@ class CreatePlan return $plan; } + + /** + * @param Node[] $nodes + * @return Node[] + */ + private function filterForNonParentNodes(array $nodes): array + { + $result = []; + foreach ($nodes as $node) { + $result[$node->getId()] = $node; + + $parentNode = $node->getParentNode(); + if ($parentNode !== null) { + unset($result[$parentNode->getId()]); + } + } + return array_values($result); + } }