Track machine counters and maintenance with counter and time intervals

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:52:49 +02:00
co-authored by Claude Sonnet 5.5
parent aac579257a
commit 3e1c671949
53 changed files with 2636 additions and 6 deletions
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Modules\Notifications\Application\UseCases;
use App\Modules\Machines\Application\Contracts\MaintenanceQueries;
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
use App\Modules\Notifications\Domain\ValueObjects\NewNotification;
use App\Modules\Shared\Application\Contracts\TransactionManager;
use App\Modules\Users\Application\Contracts\Clock;
/**
* The daily run of maintenance requests (spec 19.5): tells each machine's
* responsible person about maintenance coming up and late. The same need
* (item, last maintenance and level) is told once, so daily maintenance
* is told again only after it has been done.
*/
final class NotifyMaintenanceDue
{
public function __construct(
private MaintenanceQueries $maintenance,
private NotificationRepository $notifications,
private TransactionManager $transactions,
private Clock $clock,
) {}
/**
* @return int How many notifications were added.
*/
public function execute(): int
{
$now = $this->clock->now();
return $this->transactions->run(function () use ($now): int {
$added = 0;
foreach ($this->maintenance->due() as $item) {
if ($item->responsibleUserId === null) {
continue;
}
$notification = NewNotification::maintenanceDue($item->responsibleUserId, $item->id, $item->lastRecordId, $item->level->value, [
'machine_id' => $item->machineId,
'machine' => $item->machineName,
'item' => $item->name,
'level' => $item->level->value,
], $now);
if ($this->notifications->addIfNew($notification)) {
$added++;
}
}
return $added;
});
}
}