feat: add guest notification center

This commit is contained in:
Codex Agent
2025-11-12 16:56:50 +01:00
parent 062932ce38
commit 4495ac1895
27 changed files with 2042 additions and 64 deletions

View File

@@ -0,0 +1,92 @@
<?php
namespace Tests\Feature\Api\Event;
use App\Enums\GuestNotificationAudience;
use App\Models\Event;
use App\Models\GuestNotification;
use App\Models\Tenant;
use App\Services\EventJoinTokenService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class GuestNotificationCenterTest extends TestCase
{
use RefreshDatabase;
public function test_guest_can_fetch_notifications(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
GuestNotification::factory()->create([
'tenant_id' => $tenant->id,
'event_id' => $event->id,
'title' => 'Broadcast',
]);
GuestNotification::factory()->create([
'tenant_id' => $tenant->id,
'event_id' => $event->id,
'title' => 'Target',
'audience_scope' => GuestNotificationAudience::GUEST,
'target_identifier' => 'device-123',
]);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$response = $this->withHeaders([
'X-Device-Id' => 'device-123',
])->getJson("/api/v1/events/{$token}/notifications");
$response->assertOk();
$response->assertJsonPath('meta.unread_count', 2);
$response->assertJsonCount(2, 'data');
$response->assertJsonFragment(['title' => 'Target']);
}
public function test_guest_can_mark_notification_read(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$notification = GuestNotification::factory()->create([
'tenant_id' => $tenant->id,
'event_id' => $event->id,
]);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$response = $this->withHeaders([
'X-Device-Id' => 'device-abc',
])->postJson("/api/v1/events/{$token}/notifications/{$notification->id}/read");
$response->assertOk();
$response->assertJsonPath('status', 'read');
$this->assertDatabaseHas('guest_notification_receipts', [
'guest_notification_id' => $notification->id,
'guest_identifier' => 'device-abc',
'status' => 'read',
]);
}
public function test_guest_cannot_access_notification_from_other_event(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create(['status' => 'published']);
$otherEvent = Event::factory()->for($tenant)->create(['status' => 'published']);
$notification = GuestNotification::factory()->create([
'tenant_id' => $tenant->id,
'event_id' => $otherEvent->id,
]);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$response = $this->withHeaders([
'X-Device-Id' => 'device-999',
])->postJson("/api/v1/events/{$token}/notifications/{$notification->id}/read");
$response->assertStatus(404);
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace Tests\Feature\Tenant;
use App\Models\Event;
use App\Models\GuestNotification;
use Illuminate\Support\Str;
class GuestNotificationBroadcastTest extends TenantTestCase
{
public function test_admin_can_list_guest_notifications(): void
{
$event = Event::factory()->for($this->tenant)->create();
GuestNotification::factory()->count(2)->create([
'tenant_id' => $this->tenant->id,
'event_id' => $event->id,
]);
$response = $this->authenticatedRequest('GET', "/api/v1/tenant/events/{$event->slug}/guest-notifications");
$response->assertOk();
$response->assertJsonCount(2, 'data');
}
public function test_admin_can_create_broadcast_notification(): void
{
$event = Event::factory()->for($this->tenant)->create();
$payload = [
'title' => 'Gruppenfoto gleich',
'message' => 'Kommt bitte zur Bühne, wir starten in 5 Minuten.',
'type' => 'broadcast',
'cta' => [
'label' => 'Route öffnen',
'url' => 'https://example.com/map',
],
'expires_in_minutes' => 120,
];
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$event->slug}/guest-notifications", $payload);
$response->assertCreated();
$response->assertJsonPath('data.title', 'Gruppenfoto gleich');
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'title' => 'Gruppenfoto gleich',
]);
}
public function test_admin_cannot_access_foreign_event(): void
{
$otherTenantEvent = Event::factory()->create([
'slug' => 'foreign-'.Str::random(6),
]);
$response = $this->authenticatedRequest('POST', "/api/v1/tenant/events/{$otherTenantEvent->slug}/guest-notifications", [
'title' => 'Hi',
'message' => 'Nope',
]);
$response->assertStatus(403);
}
}