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

7.1 KB
Raw Blame
<?php

namespace HiEvents\Services\Domain\SelfService;

use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Enums\OrderAuditAction;
use HiEvents\DomainObjects\EventLocationDomainObject;
use HiEvents\DomainObjects\EventOccurrenceDomainObject;
use HiEvents\DomainObjects\EventSettingDomainObject;
use HiEvents\DomainObjects\InvoiceDomainObject;
use HiEvents\DomainObjects\LocationDomainObject;
use HiEvents\DomainObjects\OrderDomainObject;
use HiEvents\DomainObjects\OrderItemDomainObject;
use HiEvents\DomainObjects\OrganizerDomainObject;
use HiEvents\DomainObjects\ProductDomainObject;
use HiEvents\Exceptions\ResourceConflictException;
use HiEvents\Repository\Eloquent\Value\Relationship;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
use HiEvents\Repository\Interfaces\EventRepositoryInterface;
use HiEvents\Repository\Interfaces\OrderRepositoryInterface;
use HiEvents\Services\Domain\Attendee\SendAttendeeTicketService;
use HiEvents\Services\Domain\Mail\SendOrderDetailsService;

class SelfServiceResendEmailService
{
    public function __construct(
        private readonly SendAttendeeTicketService $sendAttendeeTicketService,
        private readonly SendOrderDetailsService $sendOrderDetailsService,
        private readonly AttendeeRepositoryInterface $attendeeRepository,
        private readonly OrderRepositoryInterface $orderRepository,
        private readonly EventRepositoryInterface $eventRepository,
        private readonly OrderAuditLogService $orderAuditLogService,
    ) {}

    public function resendAttendeeTicket(
        int $attendeeId,
        int $orderId,
        int $eventId,
        string $ipAddress,
        ?string $userAgent
    ): void {
        $attendee = $this->attendeeRepository
            ->loadRelation(new Relationship(OrderDomainObject::class, nested: [
                new Relationship(OrderItemDomainObject::class),
            ], name: 'order'))
            ->loadRelation(new Relationship(
                domainObject: EventOccurrenceDomainObject::class,
                nested: [
                    new Relationship(domainObject: EventLocationDomainObject::class, name: 'event_location', nested: [
                        new Relationship(domainObject: LocationDomainObject::class, name: 'location'),
                    ]),
                ],
                name: 'event_occurrence',
            ))
            ->loadRelation(new Relationship(
                domainObject: ProductDomainObject::class,
                name: 'product',
            ))
            ->findFirstWhere([
                'id' => $attendeeId,
                'order_id' => $orderId,
                'event_id' => $eventId,
            ]);

        if ($attendee->getEmail() === null) {
            throw new ResourceConflictException(
                __('This ticket has no email address associated with it.')
            );
        }

        $event = $this->eventRepository
            ->loadRelation(new Relationship(OrganizerDomainObject::class, name: 'organizer'))
            ->loadRelation(EventSettingDomainObject::class)
            ->loadRelation(new Relationship(domainObject: EventLocationDomainObject::class, name: 'event_location', nested: [
                new Relationship(domainObject: LocationDomainObject::class, name: 'location'),
            ]))
            ->findById($eventId);

        $this->sendAttendeeTicketService->send(
            order: $attendee->getOrder(),
            attendee: $attendee,
            event: $event,
            eventSettings: $event->getEventSettings(),
            organizer: $event->getOrganizer(),
        );

        $this->orderAuditLogService->logEmailResent(
            action: OrderAuditAction::ATTENDEE_EMAIL_RESENT->value,
            eventId: $eventId,
            orderId: $orderId,
            attendeeId: $attendeeId,
            ipAddress: $ipAddress,
            userAgent: $userAgent
        );
    }

    public function resendOrderConfirmation(
        int $orderId,
        int $eventId,
        string $ipAddress,
        ?string $userAgent
    ): void {
        $order = $this->orderRepository
            ->loadRelation(new Relationship(
                domainObject: OrderItemDomainObject::class,
                nested: [
                    new Relationship(
                        domainObject: EventOccurrenceDomainObject::class,
                        nested: [
                            new Relationship(domainObject: EventLocationDomainObject::class, name: 'event_location', nested: [
                                new Relationship(domainObject: LocationDomainObject::class, name: 'location'),
                            ]),
                        ],
                        name: 'event_occurrence',
                    ),
                ],
            ))
            ->loadRelation(new Relationship(
                domainObject: AttendeeDomainObject::class,
                nested: [
                    new Relationship(
                        domainObject: EventOccurrenceDomainObject::class,
                        nested: [
                            new Relationship(domainObject: EventLocationDomainObject::class, name: 'event_location', nested: [
                                new Relationship(domainObject: LocationDomainObject::class, name: 'location'),
                            ]),
                        ],
                        name: 'event_occurrence',
                    ),
                ],
            ))
            ->loadRelation(InvoiceDomainObject::class)
            ->findFirstWhere([
                'id' => $orderId,
                'event_id' => $eventId,
            ]);

        if ($order->getEmail() === null) {
            throw new ResourceConflictException(
                __('This order has no email address associated with it.')
            );
        }

        $event = $this->eventRepository
            ->loadRelation(new Relationship(OrganizerDomainObject::class, name: 'organizer'))
            ->loadRelation(new Relationship(EventSettingDomainObject::class))
            ->loadRelation(new Relationship(domainObject: EventOccurrenceDomainObject::class, nested: [
                new Relationship(domainObject: EventLocationDomainObject::class, name: 'event_location', nested: [
                    new Relationship(domainObject: LocationDomainObject::class, name: 'location'),
                ]),
            ]))
            ->loadRelation(new Relationship(domainObject: EventLocationDomainObject::class, name: 'event_location', nested: [
                new Relationship(domainObject: LocationDomainObject::class, name: 'location'),
            ]))
            ->findById($eventId);

        $this->sendOrderDetailsService->sendCustomerOrderSummary(
            order: $order,
            event: $event,
            organizer: $event->getOrganizer(),
            eventSettings: $event->getEventSettings(),
            invoice: $order->getLatestInvoice()
        );

        $this->orderAuditLogService->logEmailResent(
            action: OrderAuditAction::ORDER_EMAIL_RESENT->value,
            eventId: $eventId,
            orderId: $orderId,
            attendeeId: null,
            ipAddress: $ipAddress,
            userAgent: $userAgent
        );
    }
}