ldc-cocotte/app/Jobs/SendMissingOrderReminderJob.php
Jérémy Munsch 8c484ae053
fix(auth): correct Gate usage for customer permissions
Updated the Gate check to use forUser method for proper customer context.
2026-11-15 16:45:03 +01:00

63 lines
2 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);
$excludedMatricules = Collection::make($orders)
->map(fn ($order) => $order->employee->matricule)
->filter()
->unique();
return collect($employeeRepository->getWithCustomerWhereMatriculeNotIn($excludedMatricules))
->filter(fn ($employee) => $this->canNotifyEmployee($employee));
}
private function canNotifyEmployee(Employee $employee): bool
{
$customer = $employee->customer;
return $customer && Gate::forUser($customer)->check('customerPassOrders');
}
}