Skip to content

Open-source event management and ticket selling platform — perfect for concerts, conferences, and everything in between 🎟️ If you find this project helpful, please consider giving us a star ⭐️

Fix/update attendee statistic updates (#1071)

Dave Earleyf6858f0e43f8
diff --git a/backend/app/Jobs/Event/UpdateEventStatisticsJob.php b/backend/app/Jobs/Event/UpdateEventStatisticsJob.php
index 3101501a3..e71bbf544 100644
--- a/backend/app/Jobs/Event/UpdateEventStatisticsJob.php
+++ b/backend/app/Jobs/Event/UpdateEventStatisticsJob.php
@@ -6,13 +6,14 @@ use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\Exceptions\EventStatisticsVersionMismatchException;
use HiEvents\Services\Domain\EventStatistics\EventStatisticsIncrementService;
use Illuminate\Bus\Queueable;
+use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Throwable;
-class UpdateEventStatisticsJob implements ShouldQueue
+class UpdateEventStatisticsJob implements ShouldQueue, ShouldBeUnique
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
@@ -22,11 +23,18 @@ class UpdateEventStatisticsJob implements ShouldQueue
public int $backoff = 10; // seconds
+ public int $uniqueFor = 60; // seconds
+
public function __construct(OrderDomainObject $order)
{
$this->order = $order;
}
+ public function uniqueId(): string
+ {
+ return (string) $this->order->getId();
+ }
+
/**
* @throws EventStatisticsVersionMismatchException|Throwable
*/
diff --git a/backend/app/Services/Application/Handlers/Attendee/PartialEditAttendeeHandler.php b/backend/app/Services/Application/Handlers/Attendee/PartialEditAttendeeHandler.php
index bfd59608d..36c5bc22a 100644
--- a/backend/app/Services/Application/Handlers/Attendee/PartialEditAttendeeHandler.php
+++ b/backend/app/Services/Application/Handlers/Attendee/PartialEditAttendeeHandler.php
@@ -10,23 +10,27 @@ use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Services\Application\Handlers\Attendee\DTO\PartialEditAttendeeDTO;
use HiEvents\Services\Domain\EventStatistics\EventStatisticsCancellationService;
+use HiEvents\Services\Domain\EventStatistics\EventStatisticsReactivationService;
use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\AttendeeEvent;
use Illuminate\Database\DatabaseManager;
+use Psr\Log\LoggerInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Throwable;
class PartialEditAttendeeHandler
{
public function __construct(
- private readonly AttendeeRepositoryInterface $attendeeRepository,
- private readonly OrderRepositoryInterface $orderRepository,
- private readonly ProductQuantityUpdateService $productQuantityService,
- private readonly DatabaseManager $databaseManager,
- private readonly DomainEventDispatcherService $domainEventDispatcherService,
+ private readonly AttendeeRepositoryInterface $attendeeRepository,
+ private readonly OrderRepositoryInterface $orderRepository,
+ private readonly ProductQuantityUpdateService $productQuantityService,
+ private readonly DatabaseManager $databaseManager,
+ private readonly DomainEventDispatcherService $domainEventDispatcherService,
private readonly EventStatisticsCancellationService $eventStatisticsCancellationService,
+ private readonly EventStatisticsReactivationService $eventStatisticsReactivationService,
+ private readonly LoggerInterface $logger,
)
{
}
@@ -90,6 +94,13 @@ class PartialEditAttendeeHandler
{
if ($data->status === AttendeeStatus::ACTIVE->name) {
$this->productQuantityService->increaseQuantitySold($attendee->getProductPriceId());
+
+ event(new CapacityChangedEvent(
+ eventId: $attendee->getEventId(),
+ direction: CapacityChangeDirection::DECREASED,
+ productId: $attendee->getProductId(),
+ productPriceId: $attendee->getProductPriceId(),
+ ));
} elseif ($data->status === AttendeeStatus::CANCELLED->name) {
$this->productQuantityService->decreaseQuantitySold($attendee->getProductPriceId());
@@ -109,21 +120,30 @@ class PartialEditAttendeeHandler
*/
private function adjustEventStatistics(PartialEditAttendeeDTO $data, AttendeeDomainObject $attendee): void
{
- if ($data->status === AttendeeStatus::CANCELLED->name) {
- // Get the order to access the creation date for daily statistics
- $order = $this->orderRepository->findFirstWhere([
- 'id' => $attendee->getOrderId(),
+ $order = $this->orderRepository->findFirstWhere([
+ 'id' => $attendee->getOrderId(),
+ 'event_id' => $attendee->getEventId(),
+ ]);
+
+ if ($order === null) {
+ $this->logger->error('Order not found when adjusting event statistics for attendee', [
+ 'attendee_id' => $attendee->getId(),
+ 'order_id' => $attendee->getOrderId(),
'event_id' => $attendee->getEventId(),
]);
+ return;
+ }
- if ($order === null) {
- return;
- }
-
+ if ($data->status === AttendeeStatus::CANCELLED->name) {
$this->eventStatisticsCancellationService->decrementForCancelledAttendee(
eventId: $attendee->getEventId(),
orderDate: $order->getCreatedAt()
);
+ } elseif ($data->status === AttendeeStatus::ACTIVE->name) {
+ $this->eventStatisticsReactivationService->incrementForReactivatedAttendee(
+ eventId: $attendee->getEventId(),
+ orderDate: $order->getCreatedAt()
+ );
}
}
}
diff --git a/backend/app/Services/Domain/EventStatistics/EventStatisticsReactivationService.php b/backend/app/Services/Domain/EventStatistics/EventStatisticsReactivationService.php
new file mode 100644
index 000000000..61428ec45
--- /dev/null
+++ b/backend/app/Services/Domain/EventStatistics/EventStatisticsReactivationService.php
@@ -0,0 +1,156 @@
+<?php
+
+declare(strict_types=1);
+
+namespace HiEvents\Services\Domain\EventStatistics;
+
+use HiEvents\Exceptions\EventStatisticsVersionMismatchException;
+use HiEvents\Repository\Interfaces\EventDailyStatisticRepositoryInterface;
+use HiEvents\Repository\Interfaces\EventStatisticRepositoryInterface;
+use HiEvents\Services\Infrastructure\Utlitiy\Retry\Retrier;
+use Illuminate\Database\DatabaseManager;
+use Illuminate\Support\Carbon;
+use Psr\Log\LoggerInterface;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
+use Throwable;
+
+class EventStatisticsReactivationService
+{
+ public function __construct(
+ private readonly EventStatisticRepositoryInterface $eventStatisticsRepository,
+ private readonly EventDailyStatisticRepositoryInterface $eventDailyStatisticRepository,
+ private readonly LoggerInterface $logger,
+ private readonly DatabaseManager $databaseManager,
+ private readonly Retrier $retrier,
+ )
+ {
+ }
+
+ /**
+ * @throws EventStatisticsVersionMismatchException
+ * @throws Throwable
+ */
+ public function incrementForReactivatedAttendee(int $eventId, string $orderDate, int $attendeeCount = 1): void
+ {
+ $this->retrier->retry(
+ callableAction: function () use ($eventId, $orderDate, $attendeeCount): void {
+ $this->databaseManager->transaction(function () use ($eventId, $orderDate, $attendeeCount): void {
+ $this->incrementAggregateAttendeeStatistics($eventId, $attendeeCount);
+ $this->incrementDailyAttendeeStatistics($eventId, $orderDate, $attendeeCount);
+ });
+ },
+ onFailure: function (int $attempt, Throwable $e) use ($eventId, $orderDate, $attendeeCount): void {
+ $this->logger->error(
+ 'Failed to increment event statistics for reactivated attendee after multiple attempts',
+ [
+ 'event_id' => $eventId,
+ 'order_date' => $orderDate,
+ 'attendee_count' => $attendeeCount,
+ 'attempts' => $attempt,
+ 'exception' => $e::class,
+ 'message' => $e->getMessage(),
+ ]
+ );
+ },
+ retryOn: [EventStatisticsVersionMismatchException::class]
+ );
+ }
+
+ /**
+ * @throws EventStatisticsVersionMismatchException
+ */
+ private function incrementAggregateAttendeeStatistics(int $eventId, int $attendeeCount): void
+ {
+ $eventStatistics = $this->eventStatisticsRepository->findFirstWhere([
+ 'event_id' => $eventId,
+ ]);
+
+ if (!$eventStatistics) {
+ throw new ResourceNotFoundException('Event statistics not found for event ' . $eventId);
+ }
+
+ $updates = [
+ 'attendees_registered' => $eventStatistics->getAttendeesRegistered() + $attendeeCount,
+ 'version' => $eventStatistics->getVersion() + 1,
+ ];
+
+ $updated = $this->eventStatisticsRepository->updateWhere(
+ attributes: $updates,
+ where: [
+ 'id' => $eventStatistics->getId(),
+ 'version' => $eventStatistics->getVersion(),
+ ]
+ );
+
+ if ($updated === 0) {
+ throw new EventStatisticsVersionMismatchException(
+ 'Event statistics version mismatch. Expected version '
+ . $eventStatistics->getVersion() . ' but it was already updated.'
+ );
+ }
+
+ $this->logger->info(
+ 'Event aggregate statistics incremented for reactivated attendee',
+ [
+ 'event_id' => $eventId,
+ 'attendees_incremented' => $attendeeCount,
+ 'new_version' => $eventStatistics->getVersion() + 1,
+ ]
+ );
+ }
+
+ /**
+ * @throws EventStatisticsVersionMismatchException
+ */
+ private function incrementDailyAttendeeStatistics(int $eventId, string $orderDate, int $attendeeCount): void
+ {
+ $formattedDate = (new Carbon($orderDate))->format('Y-m-d');
+
+ $eventDailyStatistic = $this->eventDailyStatisticRepository->findFirstWhere([
+ 'event_id' => $eventId,
+ 'date' => $formattedDate,
+ ]);
+
+ if (!$eventDailyStatistic) {
+ $this->logger->warning(
+ 'Event daily statistics not found for event, skipping daily increment for reactivated attendee',
+ [
+ 'event_id' => $eventId,
+ 'date' => $formattedDate,
+ ]
+ );
+ return;
+ }
+
+ $updates = [
+ 'attendees_registered' => $eventDailyStatistic->getAttendeesRegistered() + $attendeeCount,
+ 'version' => $eventDailyStatistic->getVersion() + 1,
+ ];
+
+ $updated = $this->eventDailyStatisticRepository->updateWhere(
+ attributes: $updates,
+ where: [
+ 'event_id' => $eventId,
+ 'date' => $formattedDate,
+ 'version' => $eventDailyStatistic->getVersion(),
+ ]
+ );
+
+ if ($updated === 0) {
+ throw new EventStatisticsVersionMismatchException(
+ 'Event daily statistics version mismatch. Expected version '
+ . $eventDailyStatistic->getVersion() . ' but it was already updated.'
+ );
+ }
+
+ $this->logger->info(
+ 'Event daily statistics incremented for reactivated attendee',
+ [
+ 'event_id' => $eventId,
+ 'date' => $formattedDate,
+ 'attendees_incremented' => $attendeeCount,
+ 'new_version' => $eventDailyStatistic->getVersion() + 1,
+ ]
+ );
+ }
+}