Skip to content

Add domain events (Preparation for integrations) #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 24, 2025
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
4 changes: 2 additions & 2 deletions backend/app/Http/Request/Webhook/UpsertWebhookRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

namespace HiEvents\Http\Request\Webhook;

use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\DomainObjects\Status\WebhookStatus;
use HiEvents\Http\Request\BaseRequest;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use Illuminate\Validation\Rule;

class UpsertWebhookRequest extends BaseRequest
Expand All @@ -13,7 +13,7 @@ public function rules(): array
{
return [
'url' => 'required|url',
'event_types.*' => ['required', Rule::in(WebhookEventType::valuesArray())],
'event_types.*' => ['required', Rule::in(DomainEventType::valuesArray())],
'status' => ['nullable', Rule::in(WebhookStatus::valuesArray())],
];
}
Expand Down
6 changes: 3 additions & 3 deletions backend/app/Jobs/Order/Webhook/DispatchAttendeeWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HiEvents\Jobs\Order\Webhook;

use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -14,8 +14,8 @@ class DispatchAttendeeWebhookJob
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public int $attendeeId,
public WebhookEventType $eventType,
public int $attendeeId,
public DomainEventType $eventType,
)
{
}
Expand Down
6 changes: 3 additions & 3 deletions backend/app/Jobs/Order/Webhook/DispatchCheckInWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HiEvents\Jobs\Order\Webhook;

use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -14,8 +14,8 @@ class DispatchCheckInWebhookJob
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public int $attendeeCheckInId,
public WebhookEventType $eventType,
public int $attendeeCheckInId,
public DomainEventType $eventType,
)
{
}
Expand Down
6 changes: 3 additions & 3 deletions backend/app/Jobs/Order/Webhook/DispatchOrderWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HiEvents\Jobs\Order\Webhook;

use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -14,8 +14,8 @@ class DispatchOrderWebhookJob
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public int $orderId,
public WebhookEventType $eventType,
public int $orderId,
public DomainEventType $eventType,
)
{
}
Expand Down
6 changes: 3 additions & 3 deletions backend/app/Jobs/Order/Webhook/DispatchProductWebhookJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace HiEvents\Jobs\Order\Webhook;

use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use Illuminate\Bus\Queueable;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -14,8 +14,8 @@ class DispatchProductWebhookJob
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public function __construct(
public int $productId,
public WebhookEventType $eventType,
public int $productId,
public DomainEventType $eventType,
)
{
}
Expand Down
55 changes: 55 additions & 0 deletions backend/app/Listeners/Webhook/WebhookEventListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace HiEvents\Listeners\Webhook;

use HiEvents\Jobs\Order\Webhook\DispatchAttendeeWebhookJob;
use HiEvents\Jobs\Order\Webhook\DispatchCheckInWebhookJob;
use HiEvents\Jobs\Order\Webhook\DispatchOrderWebhookJob;
use HiEvents\Jobs\Order\Webhook\DispatchProductWebhookJob;
use HiEvents\Services\Infrastructure\DomainEvents\Events\AttendeeEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\BaseDomainEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\CheckinEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\OrderEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\ProductEvent;
use Illuminate\Config\Repository;

class WebhookEventListener
{
public function __construct(
private readonly Repository $config,
)
{
}

public function handle(BaseDomainEvent $event): void
{
$queueName = $this->config->get('queue.webhook_queue_name');

switch (get_class($event)) {
case AttendeeEvent::class:
DispatchAttendeeWebhookJob::dispatch(
attendeeId: $event->attendeeId,
eventType: $event->type,
)->onQueue($queueName);
break;
case OrderEvent::class:
DispatchOrderWebhookJob::dispatch(
orderId: $event->orderId,
eventType: $event->type,
)->onQueue($queueName);
break;
case ProductEvent::class:
DispatchProductWebhookJob::dispatch(
productId: $event->productId,
eventType: $event->type,
)->onQueue($queueName);
break;
case CheckinEvent::class:
DispatchCheckInWebhookJob::dispatch(
attendeeCheckInId: $event->attendeeCheckinId,
eventType: $event->type,
)->onQueue($queueName);
break;
}
}
}
35 changes: 27 additions & 8 deletions backend/app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,27 @@

namespace HiEvents\Providers;

use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use HiEvents\Listeners\Webhook\WebhookEventListener;
use HiEvents\Services\Infrastructure\DomainEvents\Events\AttendeeEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\CheckinEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\OrderEvent;
use HiEvents\Services\Infrastructure\DomainEvents\Events\ProductEvent;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;

class EventServiceProvider extends ServiceProvider
{
/**
* The event to listener mappings for the application.
* Map of listeners to the events they should handle.
*
* @var array<class-string, array<int, class-string>>
* @var array<class-string, array<class-string>>
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
private static array $domainEventMap = [
WebhookEventListener::class => [
ProductEvent::class,
OrderEvent::class,
AttendeeEvent::class,
CheckinEvent::class,
],
];

Expand All @@ -24,7 +31,19 @@ class EventServiceProvider extends ServiceProvider
*/
public function boot(): void
{
//
$this->registerDomainEventListeners();
}

/**
* Dynamically register all domain event listeners.
*/
private function registerDomainEventListeners(): void
{
foreach (self::$domainEventMap as $listener => $events) {
foreach ($events as $event) {
Event::listen($event, [$listener, 'handle']);
}
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions backend/app/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function paginateWhere(

public function simplePaginateWhere(
array $where,
int $limit = null,
?int $limit = null,
array $columns = self::DEFAULT_COLUMNS,
): Paginator
{
Expand Down Expand Up @@ -126,7 +126,7 @@ public function findById(int $id, array $columns = self::DEFAULT_COLUMNS): Domai

public function findFirstByField(
string $field,
string $value = null,
?string $value = null,
array $columns = ['*']
): ?DomainObjectInterface
{
Expand Down
4 changes: 2 additions & 2 deletions backend/app/Repository/Interfaces/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function paginateWhere(
*/
public function simplePaginateWhere(
array $where,
int $limit = null,
?int $limit = null,
array $columns = self::DEFAULT_COLUMNS,
): Paginator;

Expand Down Expand Up @@ -129,7 +129,7 @@ public function findFirstWhere(array $where, array $columns = self::DEFAULT_COLU
*/
public function findFirstByField(
string $field,
string $value = null,
?string $value = null,
array $columns = ['*']
): ?DomainObjectInterface;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use Brick\Money\Money;
use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Enums\ProductType;
use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\DomainObjects\Generated\AttendeeDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\OrderDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\OrderItemDomainObjectAbstract;
Expand All @@ -31,7 +30,9 @@
use HiEvents\Services\Domain\Order\OrderManagementService;
use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Domain\Tax\TaxAndFeeRollupService;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\OrderEvent;
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Collection;
use RuntimeException;
Expand All @@ -49,7 +50,7 @@ public function __construct(
private readonly TaxAndFeeRepositoryInterface $taxAndFeeRepository,
private readonly TaxAndFeeRollupService $taxAndFeeRollupService,
private readonly OrderManagementService $orderManagementService,
private readonly WebhookDispatchService $webhookDispatchService,
private readonly DomainEventDispatcherService $domainEventDispatcherService,
)
{
}
Expand Down Expand Up @@ -103,7 +104,7 @@ public function handle(CreateAttendeeDTO $attendeeDTO): AttendeeDomainObject

$this->fireEventsAndUpdateQuantities($attendeeDTO, $order);

$this->queueWebhooks($attendee, $order);
$this->queueWebhooks($order);

return $attendee;
});
Expand Down Expand Up @@ -247,11 +248,10 @@ private function fireEventsAndUpdateQuantities(CreateAttendeeDTO $attendeeDTO, O
));
}

private function queueWebhooks(AttendeeDomainObject $attendee, OrderDomainObject $order): void
private function queueWebhooks(OrderDomainObject $order): void
{
$this->webhookDispatchService->queueOrderWebhook(
eventType: WebhookEventType::ORDER_CREATED,
orderId: $order->getId(),
$this->domainEventDispatcherService->dispatch(
new OrderEvent(DomainEventType::ORDER_CREATED, $order->getId())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Enums\ProductPriceType;
use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\DomainObjects\Generated\AttendeeDomainObjectAbstract;
use HiEvents\DomainObjects\Generated\ProductDomainObjectAbstract;
use HiEvents\DomainObjects\ProductDomainObject;
Expand All @@ -14,7 +13,9 @@
use HiEvents\Repository\Interfaces\ProductRepositoryInterface;
use HiEvents\Services\Application\Handlers\Attendee\DTO\EditAttendeeDTO;
use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\AttendeeEvent;
use Illuminate\Database\DatabaseManager;
use Illuminate\Validation\ValidationException;
use Throwable;
Expand All @@ -26,7 +27,7 @@ public function __construct(
private readonly ProductRepositoryInterface $productRepository,
private readonly ProductQuantityUpdateService $productQuantityService,
private readonly DatabaseManager $databaseManager,
private readonly WebhookDispatchService $webhookDispatchService,
private readonly DomainEventDispatcherService $domainEventDispatcherService,
)
{
}
Expand All @@ -46,9 +47,11 @@ public function handle(EditAttendeeDTO $editAttendeeDTO): AttendeeDomainObject

$updatedAttendee = $this->updateAttendee($editAttendeeDTO);

$this->webhookDispatchService->queueAttendeeWebhook(
eventType: WebhookEventType::ATTENDEE_UPDATED,
attendeeId: $updatedAttendee->getId(),
$this->domainEventDispatcherService->dispatch(
new AttendeeEvent(
type: DomainEventType::ATTENDEE_UPDATED,
attendeeId: $updatedAttendee->getId(),
)
);

return $updatedAttendee;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
namespace HiEvents\Services\Application\Handlers\Attendee;

use HiEvents\DomainObjects\AttendeeDomainObject;
use HiEvents\DomainObjects\Enums\WebhookEventType;
use HiEvents\DomainObjects\Status\AttendeeStatus;
use HiEvents\Repository\Interfaces\AttendeeRepositoryInterface;
use HiEvents\Services\Application\Handlers\Attendee\DTO\PartialEditAttendeeDTO;
use HiEvents\Services\Domain\Product\ProductQuantityUpdateService;
use HiEvents\Services\Infrastructure\Webhook\WebhookDispatchService;
use HiEvents\Services\Infrastructure\DomainEvents\DomainEventDispatcherService;
use HiEvents\Services\Infrastructure\DomainEvents\Enums\DomainEventType;
use HiEvents\Services\Infrastructure\DomainEvents\Events\AttendeeEvent;
use Illuminate\Database\DatabaseManager;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Throwable;
Expand All @@ -19,7 +20,7 @@ public function __construct(
private readonly AttendeeRepositoryInterface $attendeeRepository,
private readonly ProductQuantityUpdateService $productQuantityService,
private readonly DatabaseManager $databaseManager,
private readonly WebhookDispatchService $webhookDispatchService,
private readonly DomainEventDispatcherService $domainEventDispatcherService,
)
{
}
Expand Down Expand Up @@ -52,9 +53,11 @@ private function updateAttendee(PartialEditAttendeeDTO $data): AttendeeDomainObj
}

if ($statusIsUpdated && $data->status === AttendeeStatus::CANCELLED->name) {
$this->webhookDispatchService->queueAttendeeWebhook(
eventType: WebhookEventType::ATTENDEE_CANCELLED,
attendeeId: $attendee->getId(),
$this->domainEventDispatcherService->dispatch(
new AttendeeEvent(
type: DomainEventType::ATTENDEE_CANCELLED,
attendeeId: $attendee->getId(),
)
);
}

Expand Down
Loading
Loading