From e79c3f79a25b9c59db64c67933b310998f615e7e Mon Sep 17 00:00:00 2001 From: idevakk <219866223+idevakk@users.noreply.github.com> Date: Fri, 6 Mar 2026 01:33:15 +0530 Subject: [PATCH] feat(mailbox): Implement tier-based dynamic expiration with real-time Alpine.js countdown --- app/Livewire/Mailbox.php | 25 +++++- resources/views/livewire/mailbox.blade.php | 94 +++++++++++++++++++--- tests/Feature/UserTierTest.php | 35 ++++++++ 3 files changed, 142 insertions(+), 12 deletions(-) diff --git a/app/Livewire/Mailbox.php b/app/Livewire/Mailbox.php index 82fff52..212cd0d 100644 --- a/app/Livewire/Mailbox.php +++ b/app/Livewire/Mailbox.php @@ -223,7 +223,7 @@ class Mailbox extends Component 'created_ip' => request()->ip(), 'last_accessed_ip' => request()->ip(), 'last_accessed_at' => now(), - 'expires_at' => now()->addDays(7), // Default expiry + 'expires_at' => now()->addDays($this->getValidityDays()), ]); $this->currentMailboxId = $mailbox->id; @@ -262,7 +262,7 @@ class Mailbox extends Component 'created_ip' => request()->ip(), 'last_accessed_ip' => request()->ip(), 'last_accessed_at' => now(), - 'expires_at' => now()->addDays(7), + 'expires_at' => now()->addDays($this->getValidityDays()), ]); $this->currentMailboxId = $mailbox->id; @@ -270,6 +270,27 @@ class Mailbox extends Component Session::put('current_mailbox_id', $mailbox->id); } + /** + * Get mailbox lifespan in days based on user tier. + * Guest: 1 day, Free: 3 days, Pro: 7 days, Enterprise: 14 days. + */ + protected function getValidityDays(): int + { + $user = auth()->user(); + + if (! $user) { + return 1; // Guests get 24 hours + } + + return match (true) { + $user->isEnterprise() => 14, + $user->isAdmin() => 14, // Assuming admins get enterprise limits + $user->isPro() => 7, + $user->isFree() => 3, + default => 3, + }; + } + public function finishAutoCreation(): void { if ($this->getActiveMailboxesProperty()->isEmpty()) { diff --git a/resources/views/livewire/mailbox.blade.php b/resources/views/livewire/mailbox.blade.php index 0057257..2a06150 100644 --- a/resources/views/livewire/mailbox.blade.php +++ b/resources/views/livewire/mailbox.blade.php @@ -154,18 +154,58 @@
{{ $currentMailbox->address }}
-
+
Expires In - {{ $currentMailbox->expires_at?->diffForHumans(['parts' => 2, 'short' => true]) ?? 'Never' }} +
- @php - $percent = $currentMailbox->expires_at - ? max(0, min(100, (now()->diffInSeconds($currentMailbox->expires_at) / (86400 * 7)) * 100)) - : 100; - @endphp -
+
@@ -191,9 +231,43 @@ @if($mailbox->id !== $currentMailboxId) diff --git a/tests/Feature/UserTierTest.php b/tests/Feature/UserTierTest.php index de14418..2b3a1ac 100644 --- a/tests/Feature/UserTierTest.php +++ b/tests/Feature/UserTierTest.php @@ -86,4 +86,39 @@ class UserTierTest extends TestCase $this->assertTrue($accessibleDomains->contains($publicDomain)); $this->assertFalse($accessibleDomains->contains($premiumDomain)); } + + public function test_mailbox_expiration_is_based_on_user_tier() + { + $domain = Domain::factory()->create(['allowed_types' => ['public'], 'name' => 'public.com']); + + // Test Guest (1 day) + \Livewire\Livewire::test(\App\Livewire\Mailbox::class) + ->call('autoCreateRandomMailbox'); + $guestMailbox = \App\Models\Mailbox::latest()->first(); + $this->assertEquals(1, round(now()->diffInDays($guestMailbox->expires_at))); + + // Test Free (3 days) + $freeUser = User::factory()->free()->create(); + $this->actingAs($freeUser); + \Livewire\Livewire::test(\App\Livewire\Mailbox::class) + ->call('autoCreateRandomMailbox'); + $freeMailbox = \App\Models\Mailbox::where('user_id', $freeUser->id)->first(); + $this->assertEquals(3, round(now()->diffInDays($freeMailbox->expires_at))); + + // Test Pro (7 days) + $proUser = User::factory()->pro()->create(); + $this->actingAs($proUser); + \Livewire\Livewire::test(\App\Livewire\Mailbox::class) + ->call('autoCreateRandomMailbox'); + $proMailbox = \App\Models\Mailbox::where('user_id', $proUser->id)->first(); + $this->assertEquals(7, round(now()->diffInDays($proMailbox->expires_at))); + + // Test Enterprise (14 days) + $entUser = User::factory()->enterprise()->create(); + $this->actingAs($entUser); + \Livewire\Livewire::test(\App\Livewire\Mailbox::class) + ->call('autoCreateRandomMailbox'); + $entMailbox = \App\Models\Mailbox::where('user_id', $entUser->id)->first(); + $this->assertEquals(14, round(now()->diffInDays($entMailbox->expires_at))); + } }