Phase 2: Implement Domain & Mailbox persistence, Analytics, and fix pagination issues

This commit is contained in:
idevakk
2026-03-05 20:19:30 +05:30
parent b981b6998f
commit 60b87a3609
23 changed files with 1037 additions and 256 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Models;
use MongoDB\Laravel\Eloquent\Model;
class AnalyticsEvent extends Model
{
protected $connection = 'mongodb';
protected $collection = 'analytics_events';
protected $fillable = [
'event_type',
'mailbox_hash',
'domain_hash',
'user_type',
'user_id',
'ip_address',
'user_agent',
'metadata',
];
protected function casts(): array
{
return [
'metadata' => 'array',
];
}
}

44
app/Models/Domain.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Domain extends Model
{
/** @use HasFactory<\Database\Factories\DomainFactory> */
use HasFactory, SoftDeletes;
protected $fillable = [
'domain_hash',
'name',
'allowed_types',
'is_active',
'is_archived',
];
protected function casts(): array
{
return [
'allowed_types' => 'array',
'is_active' => 'boolean',
'is_archived' => 'boolean',
];
}
protected static function booted(): void
{
static::creating(function (Domain $domain) {
if (empty($domain->domain_hash)) {
$domain->domain_hash = bin2hex(random_bytes(32));
}
});
}
public function mailboxes()
{
return $this->hasMany(Mailbox::class, 'domain_hash', 'domain_hash');
}
}

73
app/Models/Mailbox.php Normal file
View File

@@ -0,0 +1,73 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Mailbox extends Model
{
/** @use HasFactory<\Database\Factories\MailboxFactory> */
use HasFactory;
protected $fillable = [
'mailbox_hash',
'domain_hash',
'user_id',
'session_id',
'address',
'type',
'created_ip',
'last_accessed_ip',
'last_accessed_at',
'is_blocked',
'block_reason',
'blocked_at',
'expires_at',
];
protected function casts(): array
{
return [
'is_blocked' => 'boolean',
'last_accessed_at' => 'datetime',
'blocked_at' => 'datetime',
'expires_at' => 'datetime',
];
}
protected static function booted(): void
{
static::creating(function (Mailbox $mailbox) {
if (empty($mailbox->mailbox_hash)) {
$mailbox->mailbox_hash = bin2hex(random_bytes(32));
}
// Record creation analytics
\App\Jobs\TrackAnalytics::dispatch(
eventType: 'mailbox_created',
mailboxHash: $mailbox->mailbox_hash,
domainHash: $mailbox->domain_hash,
userId: $mailbox->user_id,
userType: $mailbox->user_id ? 'authenticated' : 'guest',
ipAddress: request()->ip(),
userAgent: request()->userAgent()
);
});
}
public function user()
{
return $this->belongsTo(User::class);
}
public function emails()
{
return $this->hasMany(Email::class, 'recipient_email', 'address');
}
public function domain()
{
return $this->belongsTo(Domain::class, 'domain_hash', 'domain_hash');
}
}