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 ⭐️

15.1 KB
Raw Blame
<?php

namespace HiEvents\Services\Domain\Waitlist;

use HiEvents\Constants;
use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\EventSettingDomainObject;
use HiEvents\DomainObjects\Generated\EventOccurrenceDomainObjectAbstract;
use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\DomainObjects\ProductPriceDomainObject;
use HiEvents\DomainObjects\Status\WaitlistEntryStatus;
use HiEvents\DomainObjects\TaxAndFeesDomainObject;
use HiEvents\DomainObjects\WaitlistEntryDomainObject;
use HiEvents\Enterprise\Seating\Services\Domain\SeatedProductLookupService;
use HiEvents\Exceptions\NoCapacityAvailableException;
use HiEvents\Exceptions\ResourceConflictException;
use HiEvents\Exceptions\ResourceNotFoundException;
use HiEvents\Jobs\Waitlist\SendWaitlistOfferEmailJob;
use HiEvents\Repository\Eloquent\Value\OrderAndDirection;
use HiEvents\Repository\Interfaces\EventOccurrenceRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductPriceRepositoryInterface;
use HiEvents\Repository\Interfaces\ProductRepositoryInterface;
use HiEvents\Repository\Interfaces\WaitlistEntryRepositoryInterface;
use HiEvents\Services\Application\Handlers\Order\DTO\ProductOrderDetailsDTO;
use HiEvents\Services\Domain\EventOccurrence\OccurrencePurchaseEligibilityService;
use HiEvents\Services\Domain\Order\DTO\ProcessedOrderItemDTO;
use HiEvents\Services\Domain\Order\OrderItemProcessingService;
use HiEvents\Services\Domain\Order\OrderManagementService;
use HiEvents\Services\Domain\Product\AvailableProductQuantitiesFetchService;
use HiEvents\Services\Domain\Product\DTO\OrderProductPriceDTO;
use HiEvents\Services\Infrastructure\Lock\TransactionLockService;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class ProcessWaitlistService
{
    private const DEFAULT_OFFER_TIMEOUT_MINUTES = 60 * 12;

    public function __construct(
        private readonly WaitlistEntryRepositoryInterface $waitlistEntryRepository,
        private readonly DatabaseManager $databaseManager,
        private readonly OrderManagementService $orderManagementService,
        private readonly OrderItemProcessingService $orderItemProcessingService,
        private readonly ProductRepositoryInterface $productRepository,
        private readonly AvailableProductQuantitiesFetchService $availableQuantitiesService,
        private readonly ProductPriceRepositoryInterface $productPriceRepository,
        private readonly EventOccurrenceRepositoryInterface $eventOccurrenceRepository,
        private readonly OccurrencePurchaseEligibilityService $eligibilityService,
        private readonly SeatedProductLookupService $seatedProductLookup,
        private readonly TransactionLockService $transactionLockService,
    ) {}

    /**
     * @return Collection<int, WaitlistEntryDomainObject>
     */
    public function offerToNext(
        int $productPriceId,
        int $quantity,
        EventDomainObject $event,
        EventSettingDomainObject $eventSettings,
        ?int $eventOccurrenceId = null,
    ): Collection {
        if ($quantity <= 0) {
            throw new NoCapacityAvailableException(
                __('No capacity available for the selected waitlist entries.')
            );
        }

        return $this->databaseManager->transaction(function () use ($productPriceId, $quantity, $event, $eventSettings, $eventOccurrenceId) {
            $this->transactionLockService->lockEvent($event->getId());
            $this->waitlistEntryRepository->lockForProductPrice($productPriceId, $eventOccurrenceId);

            $entries = $eventOccurrenceId !== null
                ? $this->waitlistEntryRepository->getNextWaitingEntries($productPriceId, eventOccurrenceId: $eventOccurrenceId)
                : $this->waitlistEntryRepository->getNextWaitingEntries($productPriceId);

            if ($entries->isEmpty()) {
                throw new NoCapacityAvailableException(
                    __('There are no waiting entries for this product')
                );
            }

            if ($this->isSeatedPrice($productPriceId)) {
                throw new NoCapacityAvailableException(
                    __('The waitlist is not available for reserved seating tickets')
                );
            }

            $offeredEntries = collect();
            $remainingByOccurrenceAndPrice = [];

            foreach ($entries as $entry) {
                try {
                    $occurrenceId = $this->resolveEventOccurrenceId($event, $entry);
                } catch (ResourceConflictException|ResourceNotFoundException) {
                    continue;
                }

                if (! $this->isOccurrenceStillEligibleForEntry($event, $occurrenceId, $entry)) {
                    continue;
                }

                $priceId = $entry->getProductPriceId();

                if (! isset($remainingByOccurrenceAndPrice[$occurrenceId][$priceId])) {
                    $quantities = $this->availableQuantitiesService->getAvailableProductQuantities(
                        $event->getId(),
                        ignoreCache: true,
                        eventOccurrenceId: $occurrenceId,
                    );
                    $remainingByOccurrenceAndPrice[$occurrenceId][$priceId] = $this->getAvailableCountForPrice($quantities, $priceId);
                }

                if ($remainingByOccurrenceAndPrice[$occurrenceId][$priceId] <= 0) {
                    continue;
                }

                $updatedEntry = $this->offerEntry($entry, $event, $eventSettings);
                $offeredEntries->push($updatedEntry);

                if ($remainingByOccurrenceAndPrice[$occurrenceId][$priceId] !== Constants::INFINITE) {
                    $remainingByOccurrenceAndPrice[$occurrenceId][$priceId]--;
                }

                if ($offeredEntries->count() >= $quantity) {
                    break;
                }
            }

            if ($offeredEntries->isEmpty()) {
                throw new NoCapacityAvailableException(
                    __('No capacity available for the selected waitlist entries.')
                );
            }

            return $offeredEntries;
        });
    }

    /**
     * @return Collection<int, WaitlistEntryDomainObject>
     */
    public function offerSpecificEntry(
        int $entryId,
        int $eventId,
        EventDomainObject $event,
        EventSettingDomainObject $eventSettings,
    ): Collection {
        return $this->databaseManager->transaction(function () use ($entryId, $eventId, $event, $eventSettings) {
            $this->transactionLockService->lockEvent($event->getId());

            /** @var WaitlistEntryDomainObject|null $entry */
            $entry = $this->waitlistEntryRepository->findFirstWhere([
                'id' => $entryId,
                'event_id' => $eventId,
            ]);

            if ($entry === null) {
                throw new ResourceNotFoundException(__('Waitlist entry not found'));
            }

            $validStatuses = [WaitlistEntryStatus::WAITING->name, WaitlistEntryStatus::OFFER_EXPIRED->name];
            if (! in_array($entry->getStatus(), $validStatuses, true)) {
                throw new ResourceConflictException(
                    __('This waitlist entry cannot be offered in its current status')
                );
            }

            if ($this->isSeatedPrice($entry->getProductPriceId())) {
                throw new ResourceConflictException(
                    __('The waitlist is not available for reserved seating tickets')
                );
            }

            $this->waitlistEntryRepository->lockForProductPrice(
                $entry->getProductPriceId(),
                $entry->getEventOccurrenceId(),
            );

            try {
                $occurrenceId = $this->resolveEventOccurrenceId($event, $entry);
            } catch (ResourceConflictException|ResourceNotFoundException $e) {
                throw new ResourceConflictException(
                    __('This waitlist entry is no longer linked to a valid event date.'),
                    previous: $e,
                );
            }

            if (! $this->isOccurrenceStillEligibleForEntry($event, $occurrenceId, $entry)) {
                throw new ResourceConflictException(
                    __('This event date is no longer available for this product.')
                );
            }

            if (! $this->hasCapacityForEntry($entry, $event)) {
                throw new NoCapacityAvailableException(
                    __('No capacity available to offer this waitlist entry. You will need to increase the available quantity for the product or date.')
                );
            }

            $updatedEntry = $this->offerEntry($entry, $event, $eventSettings);

            return collect([$updatedEntry]);
        });
    }

    private function isOccurrenceStillEligibleForEntry(
        EventDomainObject $event,
        int $occurrenceId,
        WaitlistEntryDomainObject $entry,
    ): bool {
        try {
            $this->eligibilityService->assertOccurrencePurchasable(
                eventId: $event->getId(),
                occurrenceId: $occurrenceId,
                additionalQuantity: 1,
                overrideCapacity: true,
            );

            $productPrice = $this->productPriceRepository->findById($entry->getProductPriceId());
            if ($productPrice === null) {
                return false;
            }

            $this->eligibilityService->assertProductsVisibleOnOccurrence(
                $occurrenceId,
                [$productPrice->getProductId()],
            );
        } catch (ValidationException) {
            return false;
        }

        return true;
    }

    private function offerEntry(
        WaitlistEntryDomainObject $entry,
        EventDomainObject $event,
        EventSettingDomainObject $eventSettings,
    ): WaitlistEntryDomainObject {
        $offerExpiresAt = $this->calculateOfferExpiry($eventSettings);
        $sessionIdentifier = sha1(Str::uuid().Str::random(40));
        $order = $this->createReservedOrder($entry, $event, $eventSettings, $sessionIdentifier);

        $this->waitlistEntryRepository->updateWhere(
            attributes: [
                'status' => WaitlistEntryStatus::OFFERED->name,
                'offer_token' => Str::random(64),
                'offered_at' => now(),
                'offer_expires_at' => $offerExpiresAt,
                'order_id' => $order->getId(),
            ],
            where: ['id' => $entry->getId()],
        );

        /** @var WaitlistEntryDomainObject $updatedEntry */
        $updatedEntry = $this->waitlistEntryRepository->findById($entry->getId());

        SendWaitlistOfferEmailJob::dispatch($updatedEntry, $order->getShortId(), $sessionIdentifier);

        return $updatedEntry;
    }

    private function createReservedOrder(
        WaitlistEntryDomainObject $entry,
        EventDomainObject $event,
        EventSettingDomainObject $eventSettings,
        string $sessionIdentifier,
    ): OrderDomainObject {
        $timeoutMinutes = $eventSettings->getWaitlistOfferTimeoutMinutes() ?? self::DEFAULT_OFFER_TIMEOUT_MINUTES;

        $order = $this->orderManagementService->createNewOrder(
            eventId: $event->getId(),
            event: $event,
            timeOutMinutes: $timeoutMinutes,
            locale: $entry->getLocale(),
            promoCode: null,
            sessionId: $sessionIdentifier,
        );

        $productPrice = $this->productPriceRepository->findById($entry->getProductPriceId());

        $product = $this->productRepository
            ->loadRelation(TaxAndFeesDomainObject::class)
            ->loadRelation(ProductPriceDomainObject::class)
            ->findById($productPrice->getProductId());

        $eventOccurrenceId = $this->resolveEventOccurrenceId($event, $entry);

        $orderDetails = collect([
            new ProductOrderDetailsDTO(
                product_id: $product->getId(),
                quantities: collect([
                    new OrderProductPriceDTO(
                        quantity: 1,
                        price_id: $productPrice->getId(),
                    ),
                ]),
                event_occurrence_id: $eventOccurrenceId,
            ),
        ]);

        $orderItems = $this->orderItemProcessingService->process(
            order: $order,
            productsOrderDetails: $orderDetails,
            event: $event,
            promoCode: null,
        );

        return $this->orderManagementService->updateOrderTotals(
            $order,
            $orderItems->map(fn (ProcessedOrderItemDTO $item) => $item->order_item),
        );
    }

    private function resolveEventOccurrenceId(EventDomainObject $event, WaitlistEntryDomainObject $entry): ?int
    {
        if ($entry->getEventOccurrenceId() !== null) {
            return $entry->getEventOccurrenceId();
        }

        if ($event->isRecurring()) {
            throw new ResourceConflictException(__('Waitlist entry is missing an event date.'));
        }

        $occurrence = $this->eventOccurrenceRepository
            ->findWhere(
                where: [
                    EventOccurrenceDomainObjectAbstract::EVENT_ID => $event->getId(),
                ],
                orderAndDirections: [
                    new OrderAndDirection(EventOccurrenceDomainObjectAbstract::START_DATE, 'asc'),
                ],
            )
            ->first();

        if ($occurrence === null) {
            throw new ResourceNotFoundException(__('No occurrence found for this event.'));
        }

        return $occurrence->getId();
    }

    private function getAvailableCountForPrice(object $quantities, int $priceId): int
    {
        foreach ($quantities->productQuantities as $productQuantity) {
            if ($productQuantity->price_id === $priceId) {
                $available = max(0, $productQuantity->quantity_available);
                if ($available === Constants::INFINITE) {
                    return Constants::INFINITE;
                }

                return $available;
            }
        }

        return 0;
    }

    private function isSeatedPrice(int $productPriceId): bool
    {
        return $this->seatedProductLookup->isSeated($this->productPriceRepository->findById($productPriceId)->getProductId());
    }

    private function hasCapacityForEntry(WaitlistEntryDomainObject $entry, EventDomainObject $event): bool
    {
        try {
            $eventOccurrenceId = $this->resolveEventOccurrenceId($event, $entry);
        } catch (ResourceConflictException|ResourceNotFoundException) {
            return false;
        }

        $quantities = $this->availableQuantitiesService->getAvailableProductQuantities(
            $event->getId(),
            ignoreCache: true,
            eventOccurrenceId: $eventOccurrenceId,
        );

        return $this->getAvailableCountForPrice($quantities, $entry->getProductPriceId()) > 0;
    }

    private function calculateOfferExpiry(EventSettingDomainObject $eventSettings): string
    {
        $timeoutMinutes = $eventSettings->getWaitlistOfferTimeoutMinutes() ?? self::DEFAULT_OFFER_TIMEOUT_MINUTES;

        return now()->addMinutes($timeoutMinutes)->toDateTimeString();
    }
}