update task and schema module

This commit is contained in:
Marek Lenczewski
2026-04-14 18:15:05 +02:00
parent d576747155
commit 0d028beda8
23 changed files with 227 additions and 59 deletions

View File

@@ -2,7 +2,3 @@ framework:
messenger:
transports:
sync: 'sync://'
scheduler_default: 'scheduler://default'
routing:
App\Message\GenerateTasksMessage: scheduler_default

Binary file not shown.

View File

@@ -1,4 +1,4 @@
{
"versionCode": 4,
"versionCode": 5,
"apkFile": "haushalt.apk"
}

View File

@@ -24,7 +24,7 @@ class TaskSchemaRequest
#[Context([DateTimeNormalizer::FORMAT_KEY => '!Y-m-d'])]
public readonly ?\DateTimeImmutable $date = null,
public readonly ?array $repeat = null,
public ?array $repeat = null,
#[Context([DateTimeNormalizer::FORMAT_KEY => '!Y-m-d'])]
public readonly ?\DateTimeImmutable $start = null,
@@ -32,5 +32,8 @@ class TaskSchemaRequest
#[Context([DateTimeNormalizer::FORMAT_KEY => '!Y-m-d'])]
public readonly ?\DateTimeImmutable $end = null,
) {
if (isset($this->repeat['days']) && is_array($this->repeat['days'])) {
$this->repeat['days'] = array_values(array_unique($this->repeat['days']));
}
}
}

View File

@@ -32,4 +32,15 @@ class TaskSchemaRepository extends ServiceEntityRepository
->getQuery()
->getResult();
}
/** @return list<TaskSchema> */
public function findExpired(): array
{
return $this->createQueryBuilder('s')
->andWhere('s.end IS NOT NULL')
->andWhere('s.end < :today')
->setParameter('today', new \DateTimeImmutable('today'))
->getQuery()
->getResult();
}
}

View File

@@ -22,6 +22,6 @@ class Schedule implements ScheduleProviderInterface
return (new SymfonySchedule())
->stateful($this->cache)
->processOnlyLastMissedRun(true)
->add(RecurringMessage::cron('0 3 * * *', new GenerateTasksMessage()));
->add(RecurringMessage::every('1 day', new GenerateTasksMessage(), from: new \DateTimeImmutable('03:00')));
}
}

View File

@@ -19,11 +19,20 @@ class TaskGenerator
public function generateNewTasks(): void
{
$this->deleteExpiredSchemas();
$today = new \DateTimeImmutable('today');
$schemas = $this->schemaRepo->findActive();
foreach ($schemas as $schema) {
$this->removeTasks($schema);
$this->generateTasks($schema);
if ($schema->getRepeatType() === null) {
$date = $schema->getDate();
if ($date !== null && $date->format('Y-m-d') === $today->format('Y-m-d')) {
$this->createTask($schema, $today);
}
} elseif ($this->matchesDate($schema, $today)) {
$this->createTask($schema, $today);
}
}
$this->em->flush();
@@ -42,12 +51,26 @@ class TaskGenerator
$dates = $this->getDates($schema);
foreach ($dates as $date) {
$task = new Task();
$task->setName($schema->getName());
$task->setDate($date);
$task->setStatus($schema->getTaskStatus());
$task->setSchema($schema);
$this->em->persist($task);
$this->createTask($schema, $date);
}
}
private function createTask(TaskSchema $schema, \DateTimeImmutable $date): void
{
$task = new Task();
$task->setName($schema->getName());
$task->setDate($date);
$task->setStatus($schema->getTaskStatus());
$task->setSchema($schema);
$this->em->persist($task);
}
private function deleteExpiredSchemas(): void
{
$expired = $this->schemaRepo->findExpired();
foreach ($expired as $schema) {
$this->removeTasks($schema);
$this->em->remove($schema);
}
}
@@ -94,6 +117,10 @@ class TaskGenerator
return $repeat['monthly'][$monthday];
}
if ($type === 'days') {
return in_array($date->format('Y-m-d'), $repeat['days'], true);
}
return false;
}
}