current init
This commit is contained in:
92
backend/src/Service/TaskSynchronizer.php
Normal file
92
backend/src/Service/TaskSynchronizer.php
Normal file
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Service;
|
||||
|
||||
use App\Entity\Task;
|
||||
use App\Entity\TaskSchema;
|
||||
use App\Enum\TaskSchemaType;
|
||||
use App\Repository\TaskRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
|
||||
class TaskSynchronizer
|
||||
{
|
||||
public function __construct(
|
||||
private EntityManagerInterface $em,
|
||||
private TaskRepository $taskRepository,
|
||||
private DeadlineCalculator $deadlineCalculator,
|
||||
) {}
|
||||
|
||||
public function syncForSchema(TaskSchema $schema): void
|
||||
{
|
||||
$today = new \DateTimeImmutable('today');
|
||||
|
||||
// Range: bis endDate oder mindestens +6 Tage
|
||||
$minEnd = $today->modify('+6 days');
|
||||
$end = $schema->getEndDate()
|
||||
? new \DateTimeImmutable($schema->getEndDate()->format('Y-m-d'))
|
||||
: $minEnd;
|
||||
if ($end < $minEnd) {
|
||||
$end = $minEnd;
|
||||
}
|
||||
|
||||
// Soll-Termine berechnen
|
||||
$deadlines = $this->deadlineCalculator->getDeadlinesForRange($schema, $today, $end);
|
||||
$shouldExist = [];
|
||||
foreach ($deadlines as $dl) {
|
||||
$shouldExist[$dl->format('Y-m-d')] = true;
|
||||
}
|
||||
|
||||
// Alle zukünftigen Tasks laden (mit Datum)
|
||||
$futureTasks = $this->taskRepository->findByTaskFromDate($schema, $today);
|
||||
$existingByDate = [];
|
||||
foreach ($futureTasks as $occ) {
|
||||
$existingByDate[$occ->getDate()->format('Y-m-d')] = $occ;
|
||||
}
|
||||
|
||||
// Null-Datum Tasks laden
|
||||
$nullDateTasks = $this->taskRepository->findBy(['schema' => $schema, 'date' => null]);
|
||||
|
||||
// Nicht mehr im Schema -> entfernen
|
||||
foreach ($existingByDate as $dateKey => $occ) {
|
||||
if (!isset($shouldExist[$dateKey])) {
|
||||
$this->em->remove($occ);
|
||||
unset($existingByDate[$dateKey]);
|
||||
}
|
||||
}
|
||||
|
||||
// Einzel ohne Deadline: null-date Task behalten
|
||||
if ($schema->getTaskType() === TaskSchemaType::Single && $schema->getDeadline() === null) {
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
}
|
||||
} else {
|
||||
// Sonst null-date Tasks entfernen
|
||||
foreach ($nullDateTasks as $occ) {
|
||||
$this->em->remove($occ);
|
||||
}
|
||||
}
|
||||
|
||||
// Bestehende zukünftige Overrides zurücksetzen
|
||||
foreach ($existingByDate as $occ) {
|
||||
$occ->setName(null);
|
||||
$occ->setCategory(null);
|
||||
$occ->setCategoryOverridden(false);
|
||||
}
|
||||
|
||||
// Fehlende Tasks erstellen
|
||||
foreach ($deadlines as $dl) {
|
||||
$dateKey = $dl->format('Y-m-d');
|
||||
if (!isset($existingByDate[$dateKey])) {
|
||||
$occ = new Task();
|
||||
$occ->setSchema($schema);
|
||||
$occ->setDate(new \DateTime($dateKey));
|
||||
|
||||
$this->em->persist($occ);
|
||||
}
|
||||
}
|
||||
|
||||
$this->em->flush();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user