|
5 | 5 | use HiEvents\DomainObjects\AttendeeDomainObject; |
6 | 6 | use HiEvents\DomainObjects\Status\AttendeeStatus; |
7 | 7 | use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface; |
| 8 | +use HiEvents\Repository\Interfaces\OrderRepositoryInterface; |
8 | 9 | use HiEvents\Services\Application\Handlers\Attendee\DTO\PartialEditAttendeeDTO; |
| 10 | +use HiEvents\Services\Domain\EventStatistics\EventStatisticsCancellationService; |
9 | 11 | use HiEvents\Services\Domain\Product\ProductQuantityUpdateService; |
10 | 12 | use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService; |
11 | 13 | use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType; |
|
17 | 19 | class PartialEditAttendeeHandler |
18 | 20 | { |
19 | 21 | public function __construct( |
20 | | - private readonly AttendeeRepositoryInterface $attendeeRepository, |
21 | | - private readonly ProductQuantityUpdateService $productQuantityService, |
22 | | - private readonly DatabaseManager $databaseManager, |
23 | | - private readonly DomainEventDispatcherService $domainEventDispatcherService, |
| 22 | + private readonly AttendeeRepositoryInterface $attendeeRepository, |
| 23 | + private readonly OrderRepositoryInterface $orderRepository, |
| 24 | + private readonly ProductQuantityUpdateService $productQuantityService, |
| 25 | + private readonly DatabaseManager $databaseManager, |
| 26 | + private readonly DomainEventDispatcherService $domainEventDispatcherService, |
| 27 | + private readonly EventStatisticsCancellationService $eventStatisticsCancellationService, |
24 | 28 | ) |
25 | 29 | { |
26 | 30 | } |
@@ -50,6 +54,7 @@ private function updateAttendee(PartialEditAttendeeDTO $data): AttendeeDomainObj |
50 | 54 |
|
51 | 55 | if ($statusIsUpdated) { |
52 | 56 | $this->adjustProductQuantity($data, $attendee); |
| 57 | + $this->adjustEventStatistics($data, $attendee); |
53 | 58 | } |
54 | 59 |
|
55 | 60 | if ($statusIsUpdated && $data->status === AttendeeStatus::CANCELLED->name) { |
@@ -87,4 +92,22 @@ private function adjustProductQuantity(PartialEditAttendeeDTO $data, AttendeeDom |
87 | 92 | $this->productQuantityService->decreaseQuantitySold($attendee->getProductPriceId()); |
88 | 93 | } |
89 | 94 | } |
| 95 | + |
| 96 | + /** |
| 97 | + * Adjust event statistics when attendee status changes |
| 98 | + * |
| 99 | + * @throws Throwable |
| 100 | + */ |
| 101 | + private function adjustEventStatistics(PartialEditAttendeeDTO $data, AttendeeDomainObject $attendee): void |
| 102 | + { |
| 103 | + if ($data->status === AttendeeStatus::CANCELLED->name) { |
| 104 | + // Get the order to access the creation date for daily statistics |
| 105 | + $order = $this->orderRepository->findById($attendee->getOrderId()); |
| 106 | + |
| 107 | + $this->eventStatisticsCancellationService->decrementForCancelledAttendee( |
| 108 | + eventId: $attendee->getEventId(), |
| 109 | + orderDate: $order->getCreatedAt() |
| 110 | + ); |
| 111 | + } |
| 112 | + } |
90 | 113 | } |
0 commit comments