ldc-cocotte/app/Notifications/MissingOrderReminderNotification.php
Jérémy Munsch 0ae05e031d
feat(jobs): enhance SendMissingOrderReminderJob with ICS content generation
- Added IcsGeneratorService for generating ICS content.
- Updated MissingOrderReminderNotification to include cutoff date and ICS content.
- Refactored SendMissingOrderReminderJob to utilize new ICS generation logic.
- Introduced IcsContent value object for better ICS content handling.
- Added tests for new functionality in SendMissingOrderReminderJob and IcsGeneratorService.
2026-11-15 19:49:48 +01:00

93 lines
3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Notifications;
use App\Jobs\SendMissingOrderReminderJob;
use App\Models\Employee;
use App\Models\Order;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Domain\Enums\OrderStatus;
use Domain\Models\ValueObjects\IcsContent;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
use Illuminate\Support\Facades\Config;
class MissingOrderReminderNotification extends Notification implements ShouldQueue
{
use Queueable;
private readonly string $appName;
private readonly string $appUrl;
private readonly string $appHost;
public function __construct(
private readonly CarbonInterface $pickupDay,
private readonly CarbonInterface $cutoffDate,
private readonly IcsContent $icsContent,
) {
$appUrl = Config::string('app.url');
$appHost = parse_url($appUrl, \PHP_URL_HOST);
if (empty($appHost)) {
throw new \UnexpectedValueException('Invalid app URL configuration');
}
$this->appUrl = $appUrl;
$this->appHost = $appHost;
$this->appName = Config::string('app.name', 'La Boutique de Cocotte');
}
/**
* Determine if the notification should be sent.
*/
public function shouldSend(Employee $notifiable, string $channel): bool
{
return ! Order::query()
->where('employee_matricule', $notifiable->matricule->value)
->whereDate('pickup_day', $this->pickupDay->format('Y-m-d'))
->where('status', OrderStatus::VALIDATED)
->exists();
}
/**
* @return array<int, string>
*/
public function via(mixed $notifiable): array
{
return ['mail'];
}
public function toMail(mixed $notifiable): MailMessage
{
/** @var Employee $notifiable */
$cutoffDate = CarbonImmutable::instance($this->cutoffDate)->setTimezone('Europe/Paris');
$closureTime = $cutoffDate->format('G') . 'h';
$reminderDay = $cutoffDate->translatedFormat('l');
$reminderTime = $cutoffDate->subHours(2)->format('G') . 'h';
return (new MailMessage())
->subject(trans(':time before order taking closes - :appname', [
'time' => SendMissingOrderReminderJob::HOURS_BEFORE_CUTOFF . 'h',
'appname' => $this->appName,
]))
->markdown('emails.missing-order-reminder', [
'employee' => $notifiable,
'deliveryDate' => $this->pickupDay->translatedFormat('d/m/Y'),
'closureTime' => $closureTime,
'reminderDay' => $reminderDay,
'reminderTime' => $reminderTime,
'appHost' => $this->appHost,
'appUrl' => $this->appUrl,
])
->attachData($this->icsContent->value, 'rappel-commande.ics', [
'mime' => 'text/calendar',
]);
}
}