ldc-boutique-cnc-sable/app/Jobs/SendMissingOrderReminderJob.php
2026-10-27 09:58:12 +01:00

64 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Contracts\Repositories\EmployeeRepository;
use App\Contracts\Repositories\OrderRepository;
use App\Models\Employee;
use App\Notifications\MissingOrderReminderNotification;
use Carbon\CarbonInterface;
use Domain\Services\PickupSchedulingService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
class SendMissingOrderReminderJob implements ShouldQueue
{
use Queueable;
public const int DAYS_BEFORE_CUTOFF = 0;
public const int HOURS_BEFORE_CUTOFF = 6;
public function handle(
PickupSchedulingService $schedulingService,
OrderRepository $orderRepository,
EmployeeRepository $employeeRepository
): void {
$pickupDay = Carbon::now()->next($schedulingService->getPickupDay()->value);
$employees = $this->getEmployeesToNotify($pickupDay, $orderRepository, $employeeRepository);
foreach ($employees as $employee) {
$employee->notify(new MissingOrderReminderNotification($pickupDay));
}
}
/** @return Collection<int,\App\Models\Employee> */
private function getEmployeesToNotify(
CarbonInterface $pickupDay,
OrderRepository $orderRepository,
EmployeeRepository $employeeRepository
): Collection {
$orders = $orderRepository->getValidatedOrdersForPickupDay($pickupDay);
/** @var array<int, \Domain\Models\ValueObjects\Matricule> $excludedMatricules */
$excludedMatricules = collect($orders)
->map(fn ($order) => $order->employee->matricule)
->uniqueValues()
->all();
return collect($employeeRepository->getWithCustomerWhereMatriculeNotIn($excludedMatricules))
->filter(fn ($employee) => $this->canNotifyEmployee($employee));
}
private function canNotifyEmployee(Employee $employee): bool
{
$customer = $employee->customer;
return $customer && Gate::check('customerPassOrders', $customer);
}
}