From 358694e7e3339f610703bcf1e34eceae0e46f79e Mon Sep 17 00:00:00 2001 From: Yisroel Baum Date: Thu, 23 Apr 2026 20:54:08 +0300 Subject: [PATCH] nodes are scheduled on different days according to dates provided --- app/Plan/UseCases/CreatePlan.php | 15 ++++++-- app/ScheduledNode/ScheduledNode.php | 5 +++ tests/Unit/Plan/UseCases/CreatePlanTest.php | 40 +++++++++++++++++++++ 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/app/Plan/UseCases/CreatePlan.php b/app/Plan/UseCases/CreatePlan.php index 0be80cb..64c7bda 100644 --- a/app/Plan/UseCases/CreatePlan.php +++ b/app/Plan/UseCases/CreatePlan.php @@ -74,10 +74,21 @@ class CreatePlan name: $request->name, user: $user, )); - foreach ($nodesOfText as $node) { + + $dates = []; + $currentDate = $startDate; + while ($currentDate <= $endDate) { + $dates[] = $currentDate; + $currentDate = $currentDate->modify('+1 day'); + } + + foreach ($nodesOfText as $index => $node) { + $dateIndex = $index % count($dates); + $scheduledDate = $dates[$dateIndex]; + $this->createScheduledNode->execute( new CreateScheduledNodeRequest( - date: '2025-01-01', // TODO: this should be cycling over some list of dates + date: $scheduledDate->format('Y-m-d'), planId: $plan->getId(), ) ); diff --git a/app/ScheduledNode/ScheduledNode.php b/app/ScheduledNode/ScheduledNode.php index ce6ed05..7d479db 100644 --- a/app/ScheduledNode/ScheduledNode.php +++ b/app/ScheduledNode/ScheduledNode.php @@ -22,4 +22,9 @@ class ScheduledNode { return $this->plan; } + + public function getDate(): DateTimeImmutable + { + return $this->date; + } } diff --git a/tests/Unit/Plan/UseCases/CreatePlanTest.php b/tests/Unit/Plan/UseCases/CreatePlanTest.php index 89ba509..d6f686d 100644 --- a/tests/Unit/Plan/UseCases/CreatePlanTest.php +++ b/tests/Unit/Plan/UseCases/CreatePlanTest.php @@ -11,6 +11,7 @@ use App\Text\CreateTextDto; use App\User\UseCases\CreateUserDto; use App\User\User; use App\ValueObjects\EmailAddress; +use DateTimeImmutable; use DomainException; use Tests\Fakes\FakeNodeRepository; use Tests\Fakes\FakePlanRepository; @@ -230,4 +231,43 @@ class CreatePlanTest extends TestCase dateEnd: '2025-01-01', )); } + + public function test_scheduled_nodes_are_scheduled_on_different_days(): void + { + $text = $this->textRepo->find(0); + $rootNode = $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'root node', + parentNode: null, + )); + $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'child 1', + parentNode: $rootNode, + )); + $this->nodeRepo->create(new CreateNodeDto( + text: $text, + title: 'child 2', + parentNode: $rootNode, + )); + $plan = $this->useCase->execute(new CreatePlanRequest( + userId: 0, + name: 'testPlan', + textId: 0, + dateStart: '2025-01-01', + dateEnd: '2025-01-02', + )); + $childOne = $this->scheduledNodeRepo->find(0); + $childTwo = $this->scheduledNodeRepo->find(1); + $this->assertNotNull($childOne); + $this->assertNotNull($childTwo); + $this->assertEquals( + new DateTimeImmutable('2025-01-01'), + $childOne->getDate() + ); + $this->assertEquals( + new DateTimeImmutable('2025-01-02'), + $childTwo->getDate() + ); + } }