distribute scheduled nodes evenly by filling days sequentially

This commit is contained in:
Yisroel Baum 2026-04-23 21:06:22 +03:00
parent 358694e7e3
commit 49663d70d9
Signed by: yisroelbaum
GPG key ID: 0FA60884F75520A9
2 changed files with 52 additions and 1 deletions

View file

@ -82,8 +82,9 @@ class CreatePlan
$currentDate = $currentDate->modify('+1 day');
}
$nodesPerDay = (int) ceil(count($nodesOfText) / count($dates));
foreach ($nodesOfText as $index => $node) {
$dateIndex = $index % count($dates);
$dateIndex = (int) floor($index / $nodesPerDay);
$scheduledDate = $dates[$dateIndex];
$this->createScheduledNode->execute(

View file

@ -270,4 +270,54 @@ class CreatePlanTest extends TestCase
$childTwo->getDate()
);
}
public function test_more_scheduled_nodes_than_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,
));
$this->nodeRepo->create(new CreateNodeDto(
text: $text,
title: 'child 3',
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);
$childThree = $this->scheduledNodeRepo->find(2);
$this->assertNotNull($childOne);
$this->assertNotNull($childTwo);
$this->assertNotNull($childThree);
$this->assertEquals(
new DateTimeImmutable('2025-01-01'),
$childOne->getDate()
);
$this->assertEquals(
new DateTimeImmutable('2025-01-01'),
$childTwo->getDate()
);
$this->assertEquals(
new DateTimeImmutable('2025-01-02'),
$childThree->getDate()
);
}
}