Track machine counters and maintenance with counter and time intervals
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
@@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -24,4 +24,9 @@ enum NotificationType: string
|
||||
* Someone else gave the user a role on a customer job.
|
||||
*/
|
||||
case JobAssigned = 'job_assigned';
|
||||
|
||||
/**
|
||||
* Maintenance of a machine the user is responsible for is coming up or late.
|
||||
*/
|
||||
case MaintenanceDue = 'maintenance_due';
|
||||
}
|
||||
|
||||
@@ -31,6 +31,17 @@ final readonly class NewNotification
|
||||
return new self($userId, NotificationType::TaskStartsSoon, $params, "task_starts:{$taskId}:{$startsOn}:{$userId}", $at);
|
||||
}
|
||||
|
||||
/**
|
||||
* One notification per maintenance need: the item, its last maintenance
|
||||
* and the level (coming up or late).
|
||||
*
|
||||
* @param array<string, int|string> $params
|
||||
*/
|
||||
public static function maintenanceDue(int $userId, int $itemId, ?int $lastRecordId, string $level, array $params, DateTimeImmutable $at): self
|
||||
{
|
||||
return new self($userId, NotificationType::MaintenanceDue, $params, 'maintenance:'.$itemId.':'.($lastRecordId ?? 'start').':'.$level.':'.$userId, $at);
|
||||
}
|
||||
|
||||
/**
|
||||
* Each assignment is its own notification: one removed and added again
|
||||
* is told again.
|
||||
|
||||
@@ -9,6 +9,7 @@ use App\Modules\Notifications\Application\UseCases\NotifyAddedPeople;
|
||||
use App\Modules\Notifications\Domain\Contracts\NotificationRepository;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Repositories\EloquentNotificationQueries;
|
||||
use App\Modules\Notifications\Infrastructure\Persistence\Repositories\EloquentNotificationRepository;
|
||||
use App\Modules\Notifications\Presentation\Console\NotifyMaintenanceDueCommand;
|
||||
use App\Modules\Notifications\Presentation\Console\NotifyTasksStartingSoonCommand;
|
||||
use Illuminate\Support\Facades\Event;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
@@ -39,7 +40,7 @@ class NotificationsServiceProvider extends ServiceProvider
|
||||
Event::listen(PeopleAddedToJob::class, fn (PeopleAddedToJob $event) => app(NotifyAddedPeople::class)->forJob($event));
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([NotifyTasksStartingSoonCommand::class]);
|
||||
$this->commands([NotifyTasksStartingSoonCommand::class, NotifyMaintenanceDueCommand::class]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Notifications\Presentation\Console;
|
||||
|
||||
use App\Modules\Notifications\Application\UseCases\NotifyMaintenanceDue;
|
||||
use Illuminate\Console\Attributes\Description;
|
||||
use Illuminate\Console\Attributes\Signature;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
#[Signature('notifications:maintenance-due')]
|
||||
#[Description('Notify the responsible persons of machines whose maintenance is coming up or late (run daily at 7.00)')]
|
||||
class NotifyMaintenanceDueCommand extends Command
|
||||
{
|
||||
public function handle(NotifyMaintenanceDue $notify): int
|
||||
{
|
||||
$added = $notify->execute();
|
||||
|
||||
$this->components->info("{$added} notifications added.");
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
@@ -76,6 +76,10 @@ new class extends Component
|
||||
'job' => (string) $notification->params['job_name'],
|
||||
'roles' => $this->roleList((string) $notification->params['roles']),
|
||||
]),
|
||||
NotificationType::MaintenanceDue => __('notifications.types.maintenance_due.'.$notification->params['level'], [
|
||||
'machine' => (string) $notification->params['machine'],
|
||||
'item' => (string) $notification->params['item'],
|
||||
]),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -84,6 +88,7 @@ new class extends Component
|
||||
return match ($notification->type) {
|
||||
NotificationType::TaskStartsSoon, NotificationType::TaskAssigned => route('jobs.show', (int) $notification->params['job_id']).'#task-'.$notification->params['task_id'],
|
||||
NotificationType::JobAssigned => route('jobs.show', (int) $notification->params['job_id']),
|
||||
NotificationType::MaintenanceDue => route('machines.show', (int) $notification->params['machine_id']).'#maintenance-heading',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user