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',
]);
}
}