Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions backend/app/DomainObjects/OrderDomainObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public function getSeatLabels(): array
->all();
}

public function getSingleEventOccurrence(): ?EventOccurrenceDomainObject
{
$occurrences = ($this->getOrderItems() ?? collect())
->map(fn (OrderItemDomainObject $item) => $item->getEventOccurrence())
->filter()
->unique(fn (EventOccurrenceDomainObject $occurrence) => $occurrence->getId());

return $occurrences->count() === 1 ? $occurrences->first() : null;
}

public function isPaymentRequired(): bool
{
return (int) ceil($this->getTotalGross()) > 0;
Expand Down
99 changes: 71 additions & 28 deletions backend/app/Mail/Attendee/AttendeeTicketMail.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Mail\Mailables\Attachment;
use Illuminate\Mail\Mailables\Content;
use Illuminate\Mail\Mailables\Envelope;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Spatie\IcalendarGenerator\Components\Calendar;
use Spatie\IcalendarGenerator\Components\Event;
Expand All @@ -39,16 +40,18 @@ public function __construct(
private readonly OrganizerDomainObject $organizer,
?RenderedEmailTemplateDTO $renderedTemplate = null,
private readonly ?EventOccurrenceDomainObject $occurrence = null,
private readonly ?Collection $attendees = null,
) {
parent::__construct();
$this->renderedTemplate = $renderedTemplate;
}

public function envelope(): Envelope
{
$subject = $this->renderedTemplate?->subject ?? __('🎟️ Your Ticket for :event', [
'event' => Str::limit($this->event->getTitle(), 50),
]);
$replacements = ['event' => Str::limit($this->event->getTitle(), 50)];
$subject = $this->renderedTemplate?->subject ?? ($this->tickets()->count() > 1
? __('🎟️ Your Tickets for :event', $replacements)
: __('🎟️ Your Ticket for :event', $replacements));

return new Envelope(
replyTo: $this->eventSettings->getSupportEmail(),
Expand All @@ -69,30 +72,55 @@ public function content(): Content
);
}

$occurrence = $this->occurrence ?? $this->attendee->getEventOccurrence();
$eventLocation = $occurrence?->getEventLocation() ?? $this->event->getEventLocation();

return new Content(
markdown: 'emails.orders.attendee-ticket',
with: [
'event' => $this->event,
'attendee' => $this->attendee,
'eventSettings' => $this->eventSettings,
'organizer' => $this->organizer,
'order' => $this->order,
'occurrence' => $occurrence,
'eventLocation' => $eventLocation,
'effectiveVenueName' => $this->venueNameFor($eventLocation),
'effectiveAddressString' => $this->addressStringFor($eventLocation),
'ticketUrl' => sprintf(
Url::getFrontEndUrlFromConfig(Url::ATTENDEE_TICKET),
$this->event->getId(),
$this->attendee->getShortId(),
),
'tickets' => $this->tickets()
->map(fn (AttendeeDomainObject $attendee) => $this->ticketViewData($attendee))
->all(),
]
);
}

/**
* @return Collection<int, AttendeeDomainObject>
*/
private function tickets(): Collection
{
return ($this->attendees ?? null) ?? collect([$this->attendee]);
}

private function occurrenceFor(AttendeeDomainObject $attendee): ?EventOccurrenceDomainObject
{
if ($this->tickets()->count() > 1) {
return $attendee->getEventOccurrence();
}

return $this->occurrence ?? $attendee->getEventOccurrence();
}

private function ticketViewData(AttendeeDomainObject $attendee): array
{
$occurrence = $this->occurrenceFor($attendee);
$eventLocation = $occurrence?->getEventLocation() ?? $this->event->getEventLocation();

return [
'attendee' => $attendee,
'occurrence' => $occurrence,
'venueName' => $this->venueNameFor($eventLocation),
'addressString' => $this->addressStringFor($eventLocation),
'ticketUrl' => sprintf(
Url::getFrontEndUrlFromConfig(Url::ATTENDEE_TICKET),
$this->event->getId(),
$attendee->getShortId(),
),
];
}

private function venueNameFor(?EventLocationDomainObject $eventLocation): ?string
{
$venue = $this->venueLocation($eventLocation);
Expand Down Expand Up @@ -140,7 +168,30 @@ private function venueLocation(?EventLocationDomainObject $eventLocation): ?Loca

public function attachments(): array
{
$occurrence = $this->occurrence ?? null;
$calendarEvents = $this->tickets()
->groupBy(fn (AttendeeDomainObject $attendee) => $this->occurrenceFor($attendee)?->getId() ?? 0)
->map(fn (Collection $attendees) => $this->calendarEventFor($attendees->first()))
->filter()
->values()
->all();

if ($calendarEvents === []) {
return [];
}

$calendar = Calendar::create()
->event($calendarEvents)
->get();

return [
Attachment::fromData(static fn () => $calendar, 'event.ics')
->withMime('text/calendar'),
];
}

private function calendarEventFor(AttendeeDomainObject $attendee): ?Event
{
$occurrence = $this->occurrenceFor($attendee);

$startDateRaw = $occurrence?->getStartDate() ?? $this->event->getStartDate();
$endDateRaw = $occurrence?->getEndDate() ?? $this->event->getEndDate();
Expand All @@ -149,7 +200,7 @@ public function attachments(): array
$endDateTime = $endDateRaw ? Carbon::parse($endDateRaw, $this->event->getTimezone()) : null;

if ($startDateTime === null) {
return [];
return null;
}

$eventTitle = $this->event->getTitle();
Expand All @@ -159,7 +210,7 @@ public function attachments(): array

$event = Event::create()
->name($eventTitle)
->uniqueIdentifier('event-'.$this->attendee->getId())
->uniqueIdentifier('event-'.$attendee->getId())
->startsAt($startDateTime)
->url($this->event->getEventUrl())
->organizer($this->organizer->getEmail(), $this->organizer->getName());
Expand All @@ -168,7 +219,6 @@ public function attachments(): array
$event->description(StringHelper::previewFromHtml($this->event->getDescription()));
}

$occurrence = $this->occurrence ?? $this->attendee->getEventOccurrence();
$eventLocation = $occurrence?->getEventLocation() ?? $this->event->getEventLocation();
$address = $this->addressStringFor($eventLocation);
if ($address !== null) {
Expand All @@ -182,13 +232,6 @@ public function attachments(): array
$event->endsAt($endDateTime);
}

$calendar = Calendar::create()
->event($event)
->get();

return [
Attachment::fromData(static fn () => $calendar, 'event.ics')
->withMime('text/calendar'),
];
return $event;
}
}
5 changes: 1 addition & 4 deletions backend/app/Mail/Order/OrderSummary.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace HiEvents\Mail\Order;

use HiEvents\DomainObjects\EventDomainObject;
use HiEvents\DomainObjects\EventOccurrenceDomainObject;
use HiEvents\DomainObjects\EventSettingDomainObject;
use HiEvents\DomainObjects\InvoiceDomainObject;
use HiEvents\DomainObjects\OrderDomainObject;
Expand All @@ -29,7 +28,6 @@ public function __construct(
private readonly OrganizerDomainObject $organizer,
private readonly EventSettingDomainObject $eventSettings,
private readonly ?InvoiceDomainObject $invoice,
private readonly ?EventOccurrenceDomainObject $occurrence = null,
?RenderedEmailTemplateDTO $renderedTemplate = null,
) {
$this->renderedTemplate = $renderedTemplate;
Expand Down Expand Up @@ -60,15 +58,14 @@ public function content(): Content
);
}

// Fallback to original template
return new Content(
markdown: 'emails.orders.summary',
with: [
'eventSettings' => $this->eventSettings,
'event' => $this->event,
'order' => $this->order,
'organizer' => $this->organizer,
'occurrence' => $this->occurrence ?? null,
'occurrence' => $this->order->getSingleEventOccurrence(),
'orderUrl' => sprintf(
Url::getFrontEndUrlFromConfig(Url::ORDER_SUMMARY),
$this->event->getId(),
Expand Down
32 changes: 32 additions & 0 deletions backend/app/Services/Domain/Attendee/SendAttendeeTicketService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use HiEvents\DomainObjects\OrganizerDomainObject;
use HiEvents\Services\Domain\Email\MailBuilderService;
use Illuminate\Contracts\Mail\Mailer;
use Illuminate\Support\Collection;

class SendAttendeeTicketService
{
Expand Down Expand Up @@ -42,4 +43,35 @@ public function send(
->locale($attendee->getLocale())
->send($mail);
}

/**
* @param Collection<int, AttendeeDomainObject> $attendees
*/
public function sendTickets(
OrderDomainObject $order,
Collection $attendees,
EventDomainObject $event,
EventSettingDomainObject $eventSettings,
OrganizerDomainObject $organizer,
): void {
$recipient = $attendees->first();
if ($recipient?->getEmail() === null) {
return;
}

$mails = $this->mailBuilderService->buildAttendeeTicketMails(
$attendees,
$order,
$event,
$eventSettings,
$organizer,
);

foreach ($mails as $mail) {
$this->mailer
->to($recipient->getEmail())
->locale($recipient->getLocale())
->send($mail);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public function buildOrderConfirmationContext(
EventSettingDomainObject $eventSettings,
?EventOccurrenceDomainObject $occurrence = null,
): array {
$occurrence ??= $order->getSingleEventOccurrence();

$startDateRaw = $occurrence?->getStartDate() ?? $event->getStartDate();
$endDateRaw = $occurrence?->getEndDate() ?? $event->getEndDate();

Expand Down
Loading
Loading