Files
myep/src/MessageHandler/CreateClickUpBookingTaskHandler.php
T

86 lines
3.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\MessageHandler;
use App\ClickUp\ApiClient;
use App\ClickUp\Exception\ClickUpException;
use App\Message\CreateClickUpBookingTaskMessage;
use App\Repository\Groups\AccommodationBookingRepository;
use Psr\Log\LoggerInterface;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;
#[AsMessageHandler]
final class CreateClickUpBookingTaskHandler
{
/** The status every freshly created group-operations task starts in. */
public const string TASK_STATUS = 'GRO';
public function __construct(
private readonly AccommodationBookingRepository $bookingRepository,
private readonly ApiClient $clickUpClient,
private readonly LoggerInterface $logger,
) {
}
public function __invoke(CreateClickUpBookingTaskMessage $message): void
{
if (!$this->clickUpClient->isConfigured()) {
$this->logger->warning('ClickUp is not configured, skipping task creation', [
'bookingId' => $message->bookingId,
]);
return;
}
$booking = $this->bookingRepository->find($message->bookingId);
if (null === $booking) {
// The booking is gone — a retry cannot bring it back.
$this->logger->warning('Accommodation booking not found, skipping ClickUp task creation', [
'bookingId' => $message->bookingId,
]);
return;
}
$hotelCode = $booking->getAccommodation()?->getCalendarCode();
$dateFrom = $booking->getDateFrom();
$contactName = trim(trim((string) $booking->getFirstName()).' '.trim((string) $booking->getLastName()));
if (null === $hotelCode || '' === $hotelCode || null === $dateFrom || '' === $contactName) {
// A task named " 2026-08-20 " helps nobody, and no amount of retrying fills the gaps.
$this->logger->warning('Accommodation booking lacks the data for a ClickUp task name, skipping', [
'bookingId' => $message->bookingId,
]);
return;
}
$name = sprintf('%s %s %s', $hotelCode, $dateFrom->format('Y-m-d'), $contactName);
$taskId = $this->clickUpClient->createTaskFromTemplate($name);
try {
$this->clickUpClient->updateTaskStatus($taskId, self::TASK_STATUS);
} catch (ClickUpException $e) {
// Deliberately swallowed: letting this bubble would retry the whole handler and create
// a second task in ClickUp. A task with the wrong status can be fixed by hand, a
// duplicate task cannot be un-created.
$this->logger->error('ClickUp task created but status update failed', [
'bookingId' => $message->bookingId,
'taskId' => $taskId,
'status' => self::TASK_STATUS,
'error' => $e->getMessage(),
]);
return;
}
$this->logger->info('Created ClickUp task for accommodation booking', [
'bookingId' => $message->bookingId,
'taskId' => $taskId,
]);
}
}