feat: automate guest notification triggers

This commit is contained in:
Codex Agent
2025-11-12 18:46:00 +01:00
parent 4495ac1895
commit 642541c8fb
9 changed files with 416 additions and 0 deletions

View File

@@ -9,8 +9,10 @@ use App\Models\Event;
use App\Models\EventPackage;
use App\Models\MediaStorageTarget;
use App\Models\Package;
use App\Models\Photo;
use App\Models\Tenant;
use App\Services\EventJoinTokenService;
use App\Services\Packages\PackageLimitEvaluator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Bus;
@@ -82,6 +84,12 @@ class EventGuestUploadLimitTest extends TestCase
$response->assertJsonPath('error.code', 'photo_limit_exceeded');
Bus::assertNothingDispatched();
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => 'upload_alert',
'audience_scope' => 'all',
]);
}
public function test_guest_upload_increments_usage_and_succeeds(): void
@@ -167,4 +175,81 @@ class EventGuestUploadLimitTest extends TestCase
$response->assertJsonPath('limits.gallery.state', 'warning');
$response->assertJsonPath('limits.can_upload_photos', true);
}
public function test_device_limit_creates_targeted_notification(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create([
'status' => 'published',
]);
$mock = \Mockery::mock(PackageLimitEvaluator::class);
$mock->shouldReceive('assessPhotoUpload')->andReturn(null);
$mock->shouldReceive('resolveEventPackageForPhotoUpload')->andReturn(null);
$this->instance(PackageLimitEvaluator::class, $mock);
Photo::factory()->count(50)->for($event)->create([
'guest_name' => 'device-abc',
]);
$emotion = Emotion::factory()->create();
$emotion->eventTypes()->attach($event->event_type_id);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$response = $this->post("/api/v1/events/{$token}/upload", [
'photo' => UploadedFile::fake()->image('limit-device.jpg', 800, 600),
], [
'X-Device-Id' => 'device-abc',
]);
$response->assertStatus(429);
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => 'upload_alert',
'target_identifier' => 'device-abc',
'audience_scope' => 'guest',
]);
}
public function test_photo_limit_violation_creates_notification(): void
{
$tenant = Tenant::factory()->create();
$event = Event::factory()->for($tenant)->create([
'status' => 'published',
]);
$violation = [
'code' => 'photo_limit_exceeded',
'title' => 'Photo upload limit reached',
'message' => 'Limit reached',
'status' => 402,
'meta' => ['scope' => 'photos'],
];
$mock = \Mockery::mock(PackageLimitEvaluator::class);
$mock->shouldReceive('assessPhotoUpload')->andReturn($violation);
$mock->shouldReceive('resolveEventPackageForPhotoUpload')->andReturn(null);
$this->instance(PackageLimitEvaluator::class, $mock);
$emotion = Emotion::factory()->create();
$emotion->eventTypes()->attach($event->event_type_id);
$token = app(EventJoinTokenService::class)->createToken($event)->plain_token;
$response = $this->post("/api/v1/events/{$token}/upload", [
'photo' => UploadedFile::fake()->image('limit.jpg', 800, 600),
], [
'X-Device-Id' => 'device-xyz',
]);
$response->assertStatus(402);
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => 'upload_alert',
'audience_scope' => 'all',
]);
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Tests\Feature\Console;
use App\Models\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SendGuestFeedbackRemindersTest extends TestCase
{
use RefreshDatabase;
public function test_command_creates_feedback_notifications_for_past_events(): void
{
$event = Event::factory()->create([
'status' => 'published',
'is_active' => true,
'date' => now()->subDay(),
]);
$this->artisan('guest:feedback-reminders', ['--hours' => 0])
->assertExitCode(0);
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => 'feedback_request',
]);
// Running again should not duplicate notifications
$this->artisan('guest:feedback-reminders', ['--hours' => 0])
->assertExitCode(0);
$this->assertDatabaseCount('guest_notifications', 1);
}
}

View File

@@ -0,0 +1,42 @@
<?php
namespace Tests\Feature\Listeners;
use App\Enums\GuestNotificationType;
use App\Events\GuestPhotoUploaded;
use App\Models\Event;
use App\Models\Photo;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
class SendPhotoUploadedNotificationTest extends TestCase
{
use RefreshDatabase;
public function test_photo_upload_event_creates_notification(): void
{
$event = Event::factory()->create(['status' => 'published']);
$photo = Photo::factory()->for($event)->create(['guest_name' => 'Anne']);
event(new GuestPhotoUploaded($event->refresh(), $photo->id, 'Anne', 'Anne'));
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => GuestNotificationType::PHOTO_ACTIVITY->value,
]);
}
public function test_milestone_creates_achievement_notification(): void
{
$event = Event::factory()->create(['status' => 'published']);
Photo::factory()->count(4)->for($event)->create(['guest_name' => 'Mia']);
$photo = Photo::factory()->for($event)->create(['guest_name' => 'Mia']);
event(new GuestPhotoUploaded($event->refresh(), $photo->id, 'Mia', 'Mia'));
$this->assertDatabaseHas('guest_notifications', [
'event_id' => $event->id,
'type' => GuestNotificationType::ACHIEVEMENT_MAJOR->value,
]);
}
}