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

🤖 Fix webhook logs modal showing stale entries instead of the latest (#1371)

Michael Labuschkee4a80e5bebc7
diff --git a/backend/app/Http/Actions/Organizers/Webhooks/GetOrganizerWebhookLogsAction.php b/backend/app/Http/Actions/Organizers/Webhooks/GetOrganizerWebhookLogsAction.php
index 793edd31d..5a22c1419 100644
--- a/backend/app/Http/Actions/Organizers/Webhooks/GetOrganizerWebhookLogsAction.php
+++ b/backend/app/Http/Actions/Organizers/Webhooks/GetOrganizerWebhookLogsAction.php
@@ -3,7 +3,6 @@
namespace HiEvents\Http\Actions\Organizers\Webhooks;
use HiEvents\DomainObjects\OrganizerDomainObject;
-use HiEvents\DomainObjects\WebhookLogDomainObject;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Resources\Webhook\WebhookLogResource;
use HiEvents\Services\Application\Handlers\Webhook\GetWebhookLogsHandler;
@@ -26,10 +25,6 @@ class GetOrganizerWebhookLogsAction extends BaseAction
organizerId: $organizerId,
);
- $webhookLogs = $webhookLogs->sortBy(function (WebhookLogDomainObject $webhookLog) {
- return $webhookLog->getId();
- }, SORT_REGULAR, true);
-
return $this->resourceResponse(
resource: WebhookLogResource::class,
data: $webhookLogs
diff --git a/backend/app/Http/Actions/Webhooks/GetWebhookLogsAction.php b/backend/app/Http/Actions/Webhooks/GetWebhookLogsAction.php
index 44e8cf246..45178d3c3 100644
--- a/backend/app/Http/Actions/Webhooks/GetWebhookLogsAction.php
+++ b/backend/app/Http/Actions/Webhooks/GetWebhookLogsAction.php
@@ -3,7 +3,6 @@
namespace HiEvents\Http\Actions\Webhooks;
use HiEvents\DomainObjects\EventDomainObject;
-use HiEvents\DomainObjects\WebhookLogDomainObject;
use HiEvents\Http\Actions\BaseAction;
use HiEvents\Resources\Webhook\WebhookLogResource;
use HiEvents\Services\Application\Handlers\Webhook\GetWebhookLogsHandler;
@@ -25,10 +24,6 @@ class GetWebhookLogsAction extends BaseAction
eventId: $eventId,
);
- $webhookLogs = $webhookLogs->sortBy(function (WebhookLogDomainObject $webhookLog) {
- return $webhookLog->getId();
- }, SORT_REGULAR, true);
-
return $this->resourceResponse(
resource: WebhookLogResource::class,
data: $webhookLogs
diff --git a/backend/app/Services/Application/Handlers/Webhook/GetWebhookLogsHandler.php b/backend/app/Services/Application/Handlers/Webhook/GetWebhookLogsHandler.php
index 579bfddf5..ba1733054 100644
--- a/backend/app/Services/Application/Handlers/Webhook/GetWebhookLogsHandler.php
+++ b/backend/app/Services/Application/Handlers/Webhook/GetWebhookLogsHandler.php
@@ -2,9 +2,10 @@
namespace HiEvents\Services\Application\Handlers\Webhook;
+use HiEvents\Repository\Eloquent\Value\OrderAndDirection;
use HiEvents\Repository\Interfaces\WebhookLogRepositoryInterface;
use HiEvents\Repository\Interfaces\WebhookRepositoryInterface;
-use Illuminate\Pagination\LengthAwarePaginator;
+use Illuminate\Support\Collection;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class GetWebhookLogsHandler
@@ -14,7 +15,7 @@ class GetWebhookLogsHandler
private readonly WebhookRepositoryInterface $webhookRepository,
) {}
- public function handle(int $webhookId, int $accountId, ?int $eventId = null, ?int $organizerId = null): LengthAwarePaginator
+ public function handle(int $webhookId, int $accountId, ?int $eventId = null, ?int $organizerId = null): Collection
{
$where = ['id' => $webhookId, 'account_id' => $accountId];
if ($eventId !== null) {
@@ -33,10 +34,13 @@ class GetWebhookLogsHandler
}
return $this->webhookLogRepository
- ->paginateWhere(
+ ->findWhere(
where: [
'webhook_id' => $webhook->getId(),
],
+ orderAndDirections: [
+ new OrderAndDirection('id', OrderAndDirection::DIRECTION_DESC),
+ ],
limit: 10,
);
}
diff --git a/backend/tests/Unit/Services/Application/Handlers/Webhook/GetWebhookLogsHandlerTest.php b/backend/tests/Unit/Services/Application/Handlers/Webhook/GetWebhookLogsHandlerTest.php
new file mode 100644
index 000000000..61aa22bda
--- /dev/null
+++ b/backend/tests/Unit/Services/Application/Handlers/Webhook/GetWebhookLogsHandlerTest.php
@@ -0,0 +1,104 @@
+<?php
+
+namespace Tests\Unit\Services\Application\Handlers\Webhook;
+
+use HiEvents\DomainObjects\WebhookDomainObject;
+use HiEvents\DomainObjects\WebhookLogDomainObject;
+use HiEvents\Repository\Eloquent\Value\OrderAndDirection;
+use HiEvents\Repository\Interfaces\WebhookLogRepositoryInterface;
+use HiEvents\Repository\Interfaces\WebhookRepositoryInterface;
+use HiEvents\Services\Application\Handlers\Webhook\GetWebhookLogsHandler;
+use Mockery;
+use Mockery\MockInterface;
+use Symfony\Component\Routing\Exception\ResourceNotFoundException;
+use Tests\TestCase;
+
+class GetWebhookLogsHandlerTest extends TestCase
+{
+ private WebhookLogRepositoryInterface|MockInterface $webhookLogRepository;
+
+ private WebhookRepositoryInterface|MockInterface $webhookRepository;
+
+ private GetWebhookLogsHandler $handler;
+
+ protected function setUp(): void
+ {
+ parent::setUp();
+
+ $this->webhookLogRepository = Mockery::mock(WebhookLogRepositoryInterface::class);
+ $this->webhookRepository = Mockery::mock(WebhookRepositoryInterface::class);
+
+ $this->handler = new GetWebhookLogsHandler(
+ $this->webhookLogRepository,
+ $this->webhookRepository,
+ );
+ }
+
+ protected function tearDown(): void
+ {
+ Mockery::close();
+ parent::tearDown();
+ }
+
+ public function test_it_fetches_the_latest_logs_newest_first(): void
+ {
+ $this->webhookRepository
+ ->shouldReceive('findFirstWhere')
+ ->once()
+ ->with(['id' => 12, 'account_id' => 1, 'event_id' => 50])
+ ->andReturn((new WebhookDomainObject)->setId(12));
+
+ $logs = collect([
+ (new WebhookLogDomainObject)->setId(411),
+ (new WebhookLogDomainObject)->setId(410),
+ ]);
+
+ $this->webhookLogRepository
+ ->shouldReceive('findWhere')
+ ->once()
+ ->withArgs(function (array $where, array $columns, array $orderAndDirections, ?int $limit) {
+ return $where === ['webhook_id' => 12]
+ && count($orderAndDirections) === 1
+ && $orderAndDirections[0]->getOrder() === 'id'
+ && $orderAndDirections[0]->getDirection() === OrderAndDirection::DIRECTION_DESC
+ && $limit === 10;
+ })
+ ->andReturn($logs);
+
+ $result = $this->handler->handle(webhookId: 12, accountId: 1, eventId: 50);
+
+ $this->assertSame($logs, $result);
+ }
+
+ public function test_it_scopes_the_webhook_lookup_to_the_organizer(): void
+ {
+ $this->webhookRepository
+ ->shouldReceive('findFirstWhere')
+ ->once()
+ ->with(['id' => 12, 'account_id' => 1, 'organizer_id' => 7])
+ ->andReturn((new WebhookDomainObject)->setId(12));
+
+ $this->webhookLogRepository
+ ->shouldReceive('findWhere')
+ ->once()
+ ->andReturn(collect());
+
+ $result = $this->handler->handle(webhookId: 12, accountId: 1, organizerId: 7);
+
+ $this->assertTrue($result->isEmpty());
+ }
+
+ public function test_it_throws_when_the_webhook_does_not_exist(): void
+ {
+ $this->webhookRepository
+ ->shouldReceive('findFirstWhere')
+ ->once()
+ ->andReturnNull();
+
+ $this->webhookLogRepository->shouldNotReceive('findWhere');
+
+ $this->expectException(ResourceNotFoundException::class);
+
+ $this->handler->handle(webhookId: 12, accountId: 1, eventId: 50);
+ }
+}