- 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.
105 lines
3.5 KiB
PHP
105 lines
3.5 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\ShouldQueue;
|
|
use Illuminate\Foundation\Queue\Queueable;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
use Illuminate\Support\Facades\Config;
|
|
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, 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');
|
|
}
|
|
}
|