ldc-cocotte/app/Jobs/SendMissingOrderReminderJob.php
Jérémy Munsch efbd507ac9
feat(jobs): implement unique job handling for reminders
Added ShouldBeUnique interface to SendMissingOrderReminderJob and SendReminderMail for unique job processing.
2026-11-15 20:39:57 +01:00

114 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Jobs;
use App\Dto\IcsContentDto;
use App\Models\Employee;
use App\Models\Order;
use App\Notifications\MissingOrderReminderNotification;
use App\Services\IcsGeneratorService;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\IcsContent;
use Domain\Models\ValueObjects\Matricule;
use Domain\Services\PickupSchedulingService;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Date;
use Illuminate\Support\Facades\Gate;
class SendMissingOrderReminderJob implements ShouldBeUnique, ShouldQueue
{
use Queueable;
public const int DAYS_BEFORE_CUTOFF = 0;
public const int HOURS_BEFORE_CUTOFF = 6;
public int $uniqueFor = 3600;
public function uniqueId(): string
{
return Date::now()->format('o-\WW');
}
public function handle(PickupSchedulingService $schedulingService, IcsGeneratorService $icsGenerator): void
{
$pickupDay = Carbon::now()->next($schedulingService->getPickupDay()->value);
$employees = $this->getEmployeesToNotify($pickupDay);
if ($employees->isEmpty()) {
return;
}
$cutoffDate = CarbonImmutable::instance(
$schedulingService->getPeriodForPickupDate($pickupDay)->getStartDate()
)->setTimezone('Europe/Paris');
$icsContent = $this->generateIcsContent($icsGenerator, $cutoffDate);
foreach ($employees as $employee) {
$employee->notify(new MissingOrderReminderNotification($pickupDay, $cutoffDate, $icsContent));
}
}
private function generateIcsContent(IcsGeneratorService $icsGenerator, CarbonImmutable $cutoffDate): IcsContent
{
$appUrl = Config::string('app.url');
$appHost = parse_url($appUrl, \PHP_URL_HOST);
if (empty($appHost)) {
throw new \UnexpectedValueException('Invalid app URL configuration');
}
$appName = Config::string('app.name', 'La Boutique de Cocotte');
$closureTime = $cutoffDate->format('G') . 'h';
$eventStart = $cutoffDate->subHours(2);
$eventEnd = $eventStart->addMinutes(15);
return $icsGenerator->generate(new IcsContentDto(
uid: 'reminder-' . $eventStart->format('Y-m-d') . '@' . $appHost,
summary: trans('Place my order :appname', [
'appname' => $appName,
]),
description: trans('Remember to validate your order before :closureTime on :url', [
'closureTime' => $closureTime,
'url' => $appHost,
]),
startsAt: $eventStart,
endsAt: $eventEnd,
url: $appUrl,
));
}
/** @return Collection<int,\App\Models\Employee> */
private function getEmployeesToNotify(CarbonInterface $pickupDay): Collection
{
$excludedMatricules = Order::query()
->whereDate('pickup_day', $pickupDay->format('Y-m-d'))
->where('status', OrderStatus::VALIDATED)
->get()
->map(fn (Order $order): Matricule => $order->employee->matricule)
->unique();
return Employee::query()
->with('customer')
->whereNotIn('matricule', $excludedMatricules)
->get()
->filter(fn (Employee $employee): bool => $this->canNotifyEmployee($employee));
}
private function canNotifyEmployee(Employee $employee): bool
{
$customer = $employee->customer;
return $customer && Gate::forUser($customer)->check('customerPassOrders');
}
}