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 ⭐️
PHP attendize-alternativedoor-managementevent-check-inevent-managementevent-registrationevent-ticketingeventbrite-alternativeeventslaravelopen-sourcephpreactself-hostedsell-ticketssell-tickets-onlineticket-sellingticketing-systemtickettailor-alternativetito-alternative
Add outgoing message tracking (#459)
Dave Earleyad50f8ce13c6
diff --git a/backend/app/DomainObjects/Generated/OutgoingMessageDomainObjectAbstract.php b/backend/app/DomainObjects/Generated/OutgoingMessageDomainObjectAbstract.phpnew file mode 100644index 000000000..8375d808f--- /dev/null+++ b/backend/app/DomainObjects/Generated/OutgoingMessageDomainObjectAbstract.php@@ -0,0 +1,146 @@+<?php++namespace HiEvents\DomainObjects\Generated;++/**+ * THIS FILE IS AUTOGENERATED - DO NOT EDIT IT DIRECTLY.+ * @package HiEvents\DomainObjects\Generated+ */+abstract class OutgoingMessageDomainObjectAbstract extends \HiEvents\DomainObjects\AbstractDomainObject+{+ final public const SINGULAR_NAME = 'outgoing_message';+ final public const PLURAL_NAME = 'outgoing_messages';+ final public const ID = 'id';+ final public const EVENT_ID = 'event_id';+ final public const MESSAGE_ID = 'message_id';+ final public const SUBJECT = 'subject';+ final public const RECIPIENT = 'recipient';+ final public const STATUS = 'status';+ final public const CREATED_AT = 'created_at';+ final public const UPDATED_AT = 'updated_at';+ final public const DELETED_AT = 'deleted_at';++ protected int $id;+ protected int $event_id;+ protected int $message_id;+ protected string $subject;+ protected string $recipient;+ protected string $status;+ protected ?string $created_at = null;+ protected ?string $updated_at = null;+ protected ?string $deleted_at = null;++ public function toArray(): array+ {+ return [+ 'id' => $this->id ?? null,+ 'event_id' => $this->event_id ?? null,+ 'message_id' => $this->message_id ?? null,+ 'subject' => $this->subject ?? null,+ 'recipient' => $this->recipient ?? null,+ 'status' => $this->status ?? null,+ 'created_at' => $this->created_at ?? null,+ 'updated_at' => $this->updated_at ?? null,+ 'deleted_at' => $this->deleted_at ?? null,+ ];+ }++ public function setId(int $id): self+ {+ $this->id = $id;+ return $this;+ }++ public function getId(): int+ {+ return $this->id;+ }++ public function setEventId(int $event_id): self+ {+ $this->event_id = $event_id;+ return $this;+ }++ public function getEventId(): int+ {+ return $this->event_id;+ }++ public function setMessageId(int $message_id): self+ {+ $this->message_id = $message_id;+ return $this;+ }++ public function getMessageId(): int+ {+ return $this->message_id;+ }++ public function setSubject(string $subject): self+ {+ $this->subject = $subject;+ return $this;+ }++ public function getSubject(): string+ {+ return $this->subject;+ }++ public function setRecipient(string $recipient): self+ {+ $this->recipient = $recipient;+ return $this;+ }++ public function getRecipient(): string+ {+ return $this->recipient;+ }++ public function setStatus(string $status): self+ {+ $this->status = $status;+ return $this;+ }++ public function getStatus(): string+ {+ return $this->status;+ }++ public function setCreatedAt(?string $created_at): self+ {+ $this->created_at = $created_at;+ return $this;+ }++ public function getCreatedAt(): ?string+ {+ return $this->created_at;+ }++ public function setUpdatedAt(?string $updated_at): self+ {+ $this->updated_at = $updated_at;+ return $this;+ }++ public function getUpdatedAt(): ?string+ {+ return $this->updated_at;+ }++ public function setDeletedAt(?string $deleted_at): self+ {+ $this->deleted_at = $deleted_at;+ return $this;+ }++ public function getDeletedAt(): ?string+ {+ return $this->deleted_at;+ }+}diff --git a/backend/app/DomainObjects/OutgoingMessageDomainObject.php b/backend/app/DomainObjects/OutgoingMessageDomainObject.phpnew file mode 100644index 000000000..25d613fd4--- /dev/null+++ b/backend/app/DomainObjects/OutgoingMessageDomainObject.php@@ -0,0 +1,7 @@+<?php++namespace HiEvents\DomainObjects;++class OutgoingMessageDomainObject extends Generated\OutgoingMessageDomainObjectAbstract+{+}diff --git a/backend/app/DomainObjects/Status/OutgoingMessageStatus.php b/backend/app/DomainObjects/Status/OutgoingMessageStatus.phpnew file mode 100644index 000000000..081c60f87--- /dev/null+++ b/backend/app/DomainObjects/Status/OutgoingMessageStatus.php@@ -0,0 +1,9 @@+<?php++namespace HiEvents\DomainObjects\Status;++enum OutgoingMessageStatus+{+ case SENT;+ case FAILED;+}diff --git a/backend/app/Jobs/Event/SendEventEmailJob.php b/backend/app/Jobs/Event/SendEventEmailJob.phpnew file mode 100644index 000000000..737c885d7--- /dev/null+++ b/backend/app/Jobs/Event/SendEventEmailJob.php@@ -0,0 +1,72 @@+<?php++namespace HiEvents\Jobs\Event;++use HiEvents\DomainObjects\Generated\OutgoingMessageDomainObjectAbstract;+use HiEvents\DomainObjects\Status\OutgoingMessageStatus;+use HiEvents\Mail\Event\EventMessage;+use HiEvents\Providers\AppServiceProvider;+use HiEvents\Repository\Interfaces\OutgoingMessageRepositoryInterface;+use HiEvents\Services\Application\Handlers\Message\DTO\SendMessageDTO;+use Illuminate\Bus\Queueable;+use Illuminate\Contracts\Queue\ShouldQueue;+use Illuminate\Foundation\Bus\Dispatchable;+use Illuminate\Mail\Mailer;+use Illuminate\Queue\InteractsWithQueue;+use Illuminate\Queue\Middleware\RateLimited;+use Illuminate\Queue\SerializesModels;+use Throwable;++class SendEventEmailJob implements ShouldQueue+{+ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;++ public function __construct(+ private readonly string $email,+ private readonly string $toName,+ private readonly EventMessage $eventMessage,+ private readonly SendMessageDTO $messageData,+ )+ {+ }++ public function middleware(): array+ {+ return [+ (new RateLimited(AppServiceProvider::MAIL_RATE_LIMIT_PER_SECOND)),+ ];+ }++ /**+ * @throws Throwable+ */+ public function handle(+ Mailer $mailer,+ OutgoingMessageRepositoryInterface $outgoingMessageRepository,+ ): void+ {+ try {+ $mailer+ ->to($this->email, $this->toName)+ ->send($this->eventMessage);+ } catch (Throwable $exception) {+ $outgoingMessageRepository->create([+ OutgoingMessageDomainObjectAbstract::MESSAGE_ID => $this->messageData->id,+ OutgoingMessageDomainObjectAbstract::EVENT_ID => $this->messageData->event_id,+ OutgoingMessageDomainObjectAbstract::STATUS => OutgoingMessageStatus::FAILED->name,+ OutgoingMessageDomainObjectAbstract::RECIPIENT => $this->email,+ OutgoingMessageDomainObjectAbstract::SUBJECT => $this->messageData->subject,+ ]);++ throw $exception;+ }++ $outgoingMessageRepository->create([+ OutgoingMessageDomainObjectAbstract::MESSAGE_ID => $this->messageData->id,+ OutgoingMessageDomainObjectAbstract::EVENT_ID => $this->messageData->event_id,+ OutgoingMessageDomainObjectAbstract::STATUS => OutgoingMessageStatus::SENT->name,+ OutgoingMessageDomainObjectAbstract::RECIPIENT => $this->email,+ OutgoingMessageDomainObjectAbstract::SUBJECT => $this->messageData->subject,+ ]);+ }+}diff --git a/backend/app/Models/Message.php b/backend/app/Models/Message.phpindex c9e1e058b..3680e2a19 100644--- a/backend/app/Models/Message.php+++ b/backend/app/Models/Message.php@@ -2,6 +2,7 @@namespace HiEvents\Models;+use Illuminate\Database\Eloquent\Relations\HasMany;use Illuminate\Database\Eloquent\Relations\HasOne;use Illuminate\Database\Eloquent\SoftDeletes;@@ -14,6 +15,11 @@ class Message extends BaseModelreturn $this->hasOne(User::class, 'id', 'sent_by_user_id');}+ public function outgoing_messages(): HasMany+ {+ return $this->hasMany(OutgoingMessage::class);+ }+protected function getCastMap(): array{return [@@ -22,5 +28,4 @@ class Message extends BaseModel'send_data' => 'array',];}-}diff --git a/backend/app/Models/OutgoingMessage.php b/backend/app/Models/OutgoingMessage.phpnew file mode 100644index 000000000..c857f7e96--- /dev/null+++ b/backend/app/Models/OutgoingMessage.php@@ -0,0 +1,10 @@+<?php++namespace HiEvents\Models;++use Illuminate\Database\Eloquent\SoftDeletes;++class OutgoingMessage extends BaseModel+{+ use SoftDeletes;+}diff --git a/backend/app/Providers/AppServiceProvider.php b/backend/app/Providers/AppServiceProvider.phpindex 008091b76..740c432a8 100644--- a/backend/app/Providers/AppServiceProvider.php+++ b/backend/app/Providers/AppServiceProvider.php@@ -9,16 +9,21 @@ use HiEvents\DomainObjects\EventDomainObject;use HiEvents\DomainObjects\OrganizerDomainObject;use HiEvents\Models\Event;use HiEvents\Models\Organizer;+use Illuminate\Cache\RateLimiting\Limit;+use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\Relations\Relation;use Illuminate\Support\Facades\DB;use Illuminate\Support\Facades\File;+use Illuminate\Support\Facades\RateLimiter;use Illuminate\Support\Facades\URL;use Illuminate\Support\ServiceProvider;use Stripe\StripeClient;class AppServiceProvider extends ServiceProvider{+ public const MAIL_RATE_LIMIT_PER_SECOND = 'mail-rate-limit-per-second';+public function register(): void{$this->bindDoctrineConnection();@@ -30,28 +35,25 @@ class AppServiceProvider extends ServiceProvider*/public function boot(): void{- if ($this->app->environment('local')) {- URL::forceScheme('https');- URL::forceRootUrl(config('app.url'));- }+ $this->handleHttpsEnforcing();- if (env('APP_DEBUG') === true && env('APP_LOG_QUERIES') === true && !app()->isProduction()) {- DB::listen(- static function ($query) {- File::append(- storage_path('/logs/query.log'),- $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL- );- }- );- }+ $this->handleQueryLogging();- Model::preventLazyLoading(!app()->isProduction());+ $this->disableLazyLoading();- Relation::enforceMorphMap([- EventDomainObject::class => Event::class,- OrganizerDomainObject::class => Organizer::class,- ]);+ $this->registerMorphMaps();++ $this->registerJobRateLimiters();+ }++ private function registerJobRateLimiters(): void+ {+ RateLimiter::for(+ name: self::MAIL_RATE_LIMIT_PER_SECOND,+ callback: static fn(ShouldQueue $job) => Limit::perMinute(+ maxAttempts: config('mail.rate_limit_per_second')+ )+ );}private function bindDoctrineConnection(): void@@ -90,4 +92,42 @@ class AppServiceProvider extends ServiceProviderfn() => new StripeClient(config('services.stripe.secret_key')));}++ /**+ * @return void+ */+ private function handleQueryLogging(): void+ {+ if (env('APP_DEBUG') === true && env('APP_LOG_QUERIES') === true && !app()->isProduction()) {+ DB::listen(+ static function ($query) {+ File::append(+ storage_path('/logs/query.log'),+ $query->sql . ' [' . implode(', ', $query->bindings) . ']' . PHP_EOL+ );+ }+ );+ }+ }++ private function handleHttpsEnforcing(): void+ {+ if ($this->app->environment('local')) {+ URL::forceScheme('https');+ URL::forceRootUrl(config('app.url'));+ }+ }++ private function registerMorphMaps(): void+ {+ Relation::enforceMorphMap([+ EventDomainObject::class => Event::class,+ OrganizerDomainObject::class => Organizer::class,+ ]);+ }++ private function disableLazyLoading(): void+ {+ Model::preventLazyLoading(!app()->isProduction());+ }}diff --git a/backend/app/Providers/RepositoryServiceProvider.php b/backend/app/Providers/RepositoryServiceProvider.phpindex 18cb630db..573d64c60 100644--- a/backend/app/Providers/RepositoryServiceProvider.php+++ b/backend/app/Providers/RepositoryServiceProvider.php@@ -23,6 +23,7 @@ use HiEvents\Repository\Eloquent\OrderItemRepository;use HiEvents\Repository\Eloquent\OrderRefundRepository;use HiEvents\Repository\Eloquent\OrderRepository;use HiEvents\Repository\Eloquent\OrganizerRepository;+use HiEvents\Repository\Eloquent\OutgoingMessageRepository;use HiEvents\Repository\Eloquent\PasswordResetRepository;use HiEvents\Repository\Eloquent\PasswordResetTokenRepository;use HiEvents\Repository\Eloquent\ProductCategoryRepository;@@ -57,6 +58,7 @@ use HiEvents\Repository\Interfaces\OrderItemRepositoryInterface;use HiEvents\Repository\Interfaces\OrderRefundRepositoryInterface;use HiEvents\Repository\Interfaces\OrderRepositoryInterface;use HiEvents\Repository\Interfaces\OrganizerRepositoryInterface;+use HiEvents\Repository\Interfaces\OutgoingMessageRepositoryInterface;use HiEvents\Repository\Interfaces\PasswordResetRepositoryInterface;use HiEvents\Repository\Interfaces\PasswordResetTokenRepositoryInterface;use HiEvents\Repository\Interfaces\ProductCategoryRepositoryInterface;@@ -114,6 +116,7 @@ class RepositoryServiceProvider extends ServiceProviderOrderApplicationFeeRepositoryInterface::class => OrderApplicationFeeRepository::class,AccountConfigurationRepositoryInterface::class => AccountConfigurationRepository::class,QuestionAndAnswerViewRepositoryInterface::class => QuestionAndAnswerViewRepository::class,+ OutgoingMessageRepositoryInterface::class => OutgoingMessageRepository::class,];public function register(): voiddiff --git a/backend/app/Repository/Eloquent/OutgoingMessageRepository.php b/backend/app/Repository/Eloquent/OutgoingMessageRepository.phpnew file mode 100644index 000000000..b977237a6--- /dev/null+++ b/backend/app/Repository/Eloquent/OutgoingMessageRepository.php@@ -0,0 +1,20 @@+<?php++namespace HiEvents\Repository\Eloquent;++use HiEvents\DomainObjects\OutgoingMessageDomainObject;+use HiEvents\Models\OutgoingMessage;+use HiEvents\Repository\Interfaces\OutgoingMessageRepositoryInterface;++class OutgoingMessageRepository extends BaseRepository implements OutgoingMessageRepositoryInterface+{+ protected function getModel(): string+ {+ return OutgoingMessage::class;+ }++ public function getDomainObject(): string+ {+ return OutgoingMessageDomainObject::class;+ }+}diff --git a/backend/app/Repository/Interfaces/OutgoingMessageRepositoryInterface.php b/backend/app/Repository/Interfaces/OutgoingMessageRepositoryInterface.phpnew file mode 100644index 000000000..af335677f--- /dev/null+++ b/backend/app/Repository/Interfaces/OutgoingMessageRepositoryInterface.php@@ -0,0 +1,14 @@+<?php++namespace HiEvents\Repository\Interfaces;++use HiEvents\DomainObjects\OutgoingMessageDomainObject;+use HiEvents\Repository\Eloquent\BaseRepository;++/**+ * @extends BaseRepository<OutgoingMessageDomainObject>+ */+interface OutgoingMessageRepositoryInterface extends RepositoryInterface+{++}diff --git a/backend/app/Services/Domain/Mail/SendEventEmailMessagesService.php b/backend/app/Services/Domain/Mail/SendEventEmailMessagesService.phpindex 03e997aac..f1937a52f 100644--- a/backend/app/Services/Domain/Mail/SendEventEmailMessagesService.php+++ b/backend/app/Services/Domain/Mail/SendEventEmailMessagesService.php@@ -11,6 +11,7 @@ use HiEvents\DomainObjects\OrganizerDomainObject;use HiEvents\DomainObjects\Status\AttendeeStatus;use HiEvents\DomainObjects\Status\MessageStatus;use HiEvents\Exceptions\UnableToSendMessageException;+use HiEvents\Jobs\Event\SendEventEmailJob;use HiEvents\Mail\Event\EventMessage;use HiEvents\Repository\Eloquent\Value\Relationship;use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;@@ -19,20 +20,22 @@ use HiEvents\Repository\Interfaces\MessageRepositoryInterface;use HiEvents\Repository\Interfaces\OrderRepositoryInterface;use HiEvents\Repository\Interfaces\UserRepositoryInterface;use HiEvents\Services\Application\Handlers\Message\DTO\SendMessageDTO;-use Illuminate\Mail\Mailer;+use Illuminate\Contracts\Bus\Dispatcher;use Illuminate\Support\Collection;use Symfony\Component\HttpKernel\Log\Logger;class SendEventEmailMessagesService{+ private array $sentEmails = [];+public function __construct(private readonly OrderRepositoryInterface $orderRepository,private readonly AttendeeRepositoryInterface $attendeeRepository,private readonly EventRepositoryInterface $eventRepository,private readonly MessageRepositoryInterface $messageRepository,private readonly UserRepositoryInterface $userRepository,- private readonly Mailer $mailer,- private readonly Logger $logger+ private readonly Logger $logger,+ private readonly Dispatcher $dispatcher,){}@@ -202,24 +205,6 @@ class SendEventEmailMessagesService);}- private function sendMessage(- string $emailAddress,- string $fullName,- SendMessageDTO $messageData,- EventDomainObject $event,- ): void- {- $this->mailer->to(- $emailAddress,- $fullName- )- ->queue(new EventMessage(- event: $event,- eventSettings: $event->getEventSettings(),- messageData: $messageData- ));- }-private function sendProductMessages(SendMessageDTO $messageData, EventDomainObject $event): void{$orders = $this->orderRepository->findOrdersAssociatedWithProducts(@@ -243,4 +228,31 @@ class SendEventEmailMessagesService);});}++ private function sendMessage(+ string $emailAddress,+ string $fullName,+ SendMessageDTO $messageData,+ EventDomainObject $event,+ ): void+ {+ if (in_array($emailAddress, $this->sentEmails, true)) {+ return;+ }++ $this->dispatcher->dispatch(+ new SendEventEmailJob(+ email: $emailAddress,+ toName: $fullName,+ eventMessage: new EventMessage(+ event: $event,+ eventSettings: $event->getEventSettings(),+ messageData: $messageData+ ),+ messageData: $messageData,+ )+ );++ $this->sentEmails[] = $emailAddress;+ }}diff --git a/backend/config/mail.php b/backend/config/mail.phpindex 3b6cca043..811c09f30 100644--- a/backend/config/mail.php+++ b/backend/config/mail.php@@ -1,6 +1,7 @@<?phpreturn [+ 'rate_limit_per_second' => env('MAIL_RATE_LIMIT_PER_SECOND', 14),/*|--------------------------------------------------------------------------diff --git a/backend/database/migrations/2025_03_24_052900_add_outgoing_messages_table.php b/backend/database/migrations/2025_03_24_052900_add_outgoing_messages_table.phpnew file mode 100644index 000000000..0a3afe568--- /dev/null+++ b/backend/database/migrations/2025_03_24_052900_add_outgoing_messages_table.php@@ -0,0 +1,37 @@+<?php++use Illuminate\Database\Migrations\Migration;+use Illuminate\Database\Schema\Blueprint;+use Illuminate\Support\Facades\Schema;++return new class extends Migration {+ /**+ * Run the migrations.+ */+ public function up(): void+ {+ Schema::create('outgoing_messages', static function (Blueprint $table) {+ $table->id();++ $table->foreignId('event_id')->constrained()->cascadeOnDelete();+ $table->foreignId('message_id')->constrained()->cascadeOnDelete();+ $table->string('subject');+ $table->string('recipient');+ $table->string('status');++ $table->index('event_id');+ $table->index('message_id');++ $table->timestamps();+ $table->softDeletes();+ });+ }++ /**+ * Reverse the migrations.+ */+ public function down(): void+ {+ Schema::dropIfExists('outgoing_messages');+ }+};