feat: add domain management system
This commit is contained in:
61
app/Filament/Resources/Domains/DomainResource.php
Normal file
61
app/Filament/Resources/Domains/DomainResource.php
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Domains;
|
||||||
|
|
||||||
|
use App\Filament\Resources\Domains\Pages\CreateDomain;
|
||||||
|
use App\Filament\Resources\Domains\Pages\EditDomain;
|
||||||
|
use App\Filament\Resources\Domains\Pages\ListDomains;
|
||||||
|
use App\Filament\Resources\Domains\Schemas\DomainForm;
|
||||||
|
use App\Filament\Resources\Domains\Tables\DomainsTable;
|
||||||
|
use App\Models\Domain;
|
||||||
|
use BackedEnum;
|
||||||
|
use Filament\Resources\Resource;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
use Filament\Support\Icons\Heroicon;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
|
use UnitEnum;
|
||||||
|
|
||||||
|
class DomainResource extends Resource
|
||||||
|
{
|
||||||
|
protected static ?string $model = Domain::class;
|
||||||
|
|
||||||
|
protected static string|BackedEnum|null $navigationIcon = Heroicon::GlobeAlt;
|
||||||
|
|
||||||
|
protected static string|UnitEnum|null $navigationGroup = 'Email';
|
||||||
|
|
||||||
|
public static function form(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return DomainForm::configure($schema);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function table(Table $table): Table
|
||||||
|
{
|
||||||
|
return DomainsTable::configure($table);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRelations(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
//
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getPages(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'index' => ListDomains::route('/'),
|
||||||
|
'create' => CreateDomain::route('/create'),
|
||||||
|
'edit' => EditDomain::route('/{record}/edit'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getRecordRouteBindingEloquentQuery(): Builder
|
||||||
|
{
|
||||||
|
return parent::getRecordRouteBindingEloquentQuery()
|
||||||
|
->withoutGlobalScopes([
|
||||||
|
SoftDeletingScope::class,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
11
app/Filament/Resources/Domains/Pages/CreateDomain.php
Normal file
11
app/Filament/Resources/Domains/Pages/CreateDomain.php
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Domains\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\Domains\DomainResource;
|
||||||
|
use Filament\Resources\Pages\CreateRecord;
|
||||||
|
|
||||||
|
class CreateDomain extends CreateRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = DomainResource::class;
|
||||||
|
}
|
||||||
23
app/Filament/Resources/Domains/Pages/EditDomain.php
Normal file
23
app/Filament/Resources/Domains/Pages/EditDomain.php
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Domains\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\Domains\DomainResource;
|
||||||
|
use Filament\Actions\DeleteAction;
|
||||||
|
use Filament\Actions\ForceDeleteAction;
|
||||||
|
use Filament\Actions\RestoreAction;
|
||||||
|
use Filament\Resources\Pages\EditRecord;
|
||||||
|
|
||||||
|
class EditDomain extends EditRecord
|
||||||
|
{
|
||||||
|
protected static string $resource = DomainResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
DeleteAction::make(),
|
||||||
|
ForceDeleteAction::make(),
|
||||||
|
RestoreAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
19
app/Filament/Resources/Domains/Pages/ListDomains.php
Normal file
19
app/Filament/Resources/Domains/Pages/ListDomains.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Domains\Pages;
|
||||||
|
|
||||||
|
use App\Filament\Resources\Domains\DomainResource;
|
||||||
|
use Filament\Actions\CreateAction;
|
||||||
|
use Filament\Resources\Pages\ListRecords;
|
||||||
|
|
||||||
|
class ListDomains extends ListRecords
|
||||||
|
{
|
||||||
|
protected static string $resource = DomainResource::class;
|
||||||
|
|
||||||
|
protected function getHeaderActions(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
CreateAction::make(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
67
app/Filament/Resources/Domains/Schemas/DomainForm.php
Normal file
67
app/Filament/Resources/Domains/Schemas/DomainForm.php
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Domains\Schemas;
|
||||||
|
|
||||||
|
use App\enum\DomainType;
|
||||||
|
use App\enum\ProviderType;
|
||||||
|
use Filament\Forms\Components\DateTimePicker;
|
||||||
|
use Filament\Forms\Components\Select;
|
||||||
|
use Filament\Forms\Components\TextInput;
|
||||||
|
use Filament\Forms\Components\ToggleButtons;
|
||||||
|
use Filament\Infolists\Components\TextEntry;
|
||||||
|
use Filament\Schemas\Schema;
|
||||||
|
|
||||||
|
class DomainForm
|
||||||
|
{
|
||||||
|
public static function configure(Schema $schema): Schema
|
||||||
|
{
|
||||||
|
return $schema
|
||||||
|
->components([
|
||||||
|
TextInput::make('name')
|
||||||
|
->columnSpan(1)
|
||||||
|
->helperText('Domain name: example.com')
|
||||||
|
->required(),
|
||||||
|
TextInput::make('daily_mailbox_limit')
|
||||||
|
->integer()
|
||||||
|
->minValue(1)
|
||||||
|
->default(100)
|
||||||
|
->helperText('How many mailboxes can be created with this domain daily')
|
||||||
|
->columnSpan(1)
|
||||||
|
->required(),
|
||||||
|
ToggleButtons::make('is_active')
|
||||||
|
->options([
|
||||||
|
true => 'Active',
|
||||||
|
false => 'Disabled',
|
||||||
|
])
|
||||||
|
->inline()
|
||||||
|
->default(true)
|
||||||
|
->columnSpanFull()
|
||||||
|
->required(),
|
||||||
|
Select::make('domain_type')
|
||||||
|
->options(DomainType::class)
|
||||||
|
->enum(DomainType::class)
|
||||||
|
->required(),
|
||||||
|
Select::make('provider_type')
|
||||||
|
->options(ProviderType::class)
|
||||||
|
->enum(ProviderType::class)
|
||||||
|
->required(),
|
||||||
|
DateTimePicker::make('starts_at'),
|
||||||
|
DateTimePicker::make('ends_at'),
|
||||||
|
TextEntry::make('last_used_at')
|
||||||
|
->label('Last Used At')
|
||||||
|
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : 'Never')
|
||||||
|
->visible(fn ($context) => $context === 'edit'),
|
||||||
|
TextEntry::make('checked_at')
|
||||||
|
->label('Last Checked At')
|
||||||
|
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : 'Never')
|
||||||
|
->visible(fn ($context) => $context === 'edit'),
|
||||||
|
TextEntry::make('deleted_at')
|
||||||
|
->label('Deleted At')
|
||||||
|
->formatStateUsing(fn ($state) => $state ? $state->diffForHumans() : null)
|
||||||
|
->color('danger')
|
||||||
|
->icon('heroicon-o-trash')
|
||||||
|
->visible(fn ($record) => $record?->deleted_at !== null),
|
||||||
|
|
||||||
|
])->columns(2);
|
||||||
|
}
|
||||||
|
}
|
||||||
107
app/Filament/Resources/Domains/Tables/DomainsTable.php
Normal file
107
app/Filament/Resources/Domains/Tables/DomainsTable.php
Normal file
@@ -0,0 +1,107 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Filament\Resources\Domains\Tables;
|
||||||
|
|
||||||
|
use App\enum\DomainType;
|
||||||
|
use App\enum\ProviderType;
|
||||||
|
use Filament\Actions\BulkActionGroup;
|
||||||
|
use Filament\Actions\DeleteBulkAction;
|
||||||
|
use Filament\Actions\EditAction;
|
||||||
|
use Filament\Actions\ForceDeleteBulkAction;
|
||||||
|
use Filament\Actions\RestoreBulkAction;
|
||||||
|
use Filament\Tables\Columns\TextColumn;
|
||||||
|
use Filament\Tables\Columns\ToggleColumn;
|
||||||
|
use Filament\Tables\Filters\SelectFilter;
|
||||||
|
use Filament\Tables\Filters\TrashedFilter;
|
||||||
|
use Filament\Tables\Table;
|
||||||
|
|
||||||
|
class DomainsTable
|
||||||
|
{
|
||||||
|
public static function configure(Table $table): Table
|
||||||
|
{
|
||||||
|
return $table
|
||||||
|
->columns([
|
||||||
|
TextColumn::make('name')
|
||||||
|
->label('Domain')
|
||||||
|
->searchable()
|
||||||
|
->weight('medium')
|
||||||
|
->icon('heroicon-o-globe-alt')
|
||||||
|
->copyable()
|
||||||
|
->copyMessage('Domain copied!')
|
||||||
|
->copyMessageDuration(1500),
|
||||||
|
ToggleColumn::make('is_active')
|
||||||
|
->label('Active')
|
||||||
|
->alignCenter(),
|
||||||
|
TextColumn::make('domain_type')
|
||||||
|
->label('Type')
|
||||||
|
->formatStateUsing(fn ($state) => $state ? DomainType::tryFrom($state)?->getLabel() : '-')
|
||||||
|
->badge()
|
||||||
|
->color(fn ($state) => $state ? DomainType::tryFrom($state)?->getColor() : 'gray')
|
||||||
|
->alignCenter(),
|
||||||
|
TextColumn::make('provider_type')
|
||||||
|
->label('Provider')
|
||||||
|
->formatStateUsing(fn ($state) => $state ? ProviderType::tryFrom($state)?->getLabel() : '-')
|
||||||
|
->badge()
|
||||||
|
->color(fn ($state) => $state ? ProviderType::tryFrom($state)?->getColor() : 'gray')
|
||||||
|
->alignCenter(),
|
||||||
|
TextColumn::make('daily_mailbox_limit')
|
||||||
|
->label('Daily Limit')
|
||||||
|
->numeric()
|
||||||
|
->formatStateUsing(fn ($state) => number_format($state))
|
||||||
|
->alignCenter()
|
||||||
|
->icon('heroicon-o-inbox'),
|
||||||
|
TextColumn::make('last_used_at')
|
||||||
|
->label('Last Used')
|
||||||
|
->dateTime('M j, Y g:i A')
|
||||||
|
->placeholder('Never')
|
||||||
|
->sortable()
|
||||||
|
->since()
|
||||||
|
->alignCenter(),
|
||||||
|
TextColumn::make('checked_at')
|
||||||
|
->label('Checked')
|
||||||
|
->dateTime('M j, Y')
|
||||||
|
->placeholder('Never')
|
||||||
|
->sortable()
|
||||||
|
->since()
|
||||||
|
->alignCenter()
|
||||||
|
->toggleable(isToggledHiddenByDefault: true),
|
||||||
|
|
||||||
|
])
|
||||||
|
->filters([
|
||||||
|
SelectFilter::make('domain_type')
|
||||||
|
->label('Domain Type')
|
||||||
|
->options(DomainType::class),
|
||||||
|
SelectFilter::make('provider_type')
|
||||||
|
->label('Provider Type')
|
||||||
|
->options(ProviderType::class),
|
||||||
|
SelectFilter::make('is_active')
|
||||||
|
->label('Status')
|
||||||
|
->options([
|
||||||
|
'1' => 'Active',
|
||||||
|
'0' => 'Inactive',
|
||||||
|
]),
|
||||||
|
TrashedFilter::make(),
|
||||||
|
])
|
||||||
|
->recordActions([
|
||||||
|
EditAction::make(),
|
||||||
|
])
|
||||||
|
->toolbarActions([
|
||||||
|
BulkActionGroup::make([
|
||||||
|
DeleteBulkAction::make(),
|
||||||
|
ForceDeleteBulkAction::make(),
|
||||||
|
RestoreBulkAction::make(),
|
||||||
|
]),
|
||||||
|
])
|
||||||
|
->emptyStateHeading('No domains found')
|
||||||
|
->emptyStateDescription('Get started by creating your first domain.')
|
||||||
|
->emptyStateActions([
|
||||||
|
// Add create action if needed
|
||||||
|
])
|
||||||
|
->poll('60s')
|
||||||
|
->striped()
|
||||||
|
->defaultPaginationPageOption(10)
|
||||||
|
->paginated([10, 25, 50, 100])
|
||||||
|
->reorderable('sort_order')
|
||||||
|
->defaultSort('name');
|
||||||
|
}
|
||||||
|
}
|
||||||
71
app/Models/Domain.php
Normal file
71
app/Models/Domain.php
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Models;
|
||||||
|
|
||||||
|
use App\enum\DomainType;
|
||||||
|
use App\enum\ProviderType;
|
||||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||||
|
use Illuminate\Database\Eloquent\Model;
|
||||||
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
|
|
||||||
|
class Domain extends Model
|
||||||
|
{
|
||||||
|
use HasFactory, SoftDeletes;
|
||||||
|
|
||||||
|
protected $fillable = [
|
||||||
|
'name',
|
||||||
|
'is_active',
|
||||||
|
'daily_mailbox_limit',
|
||||||
|
'domain_type',
|
||||||
|
'provider_type',
|
||||||
|
'starts_at',
|
||||||
|
'ends_at',
|
||||||
|
'last_used_at',
|
||||||
|
'checked_at',
|
||||||
|
];
|
||||||
|
|
||||||
|
protected function casts(): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'is_active' => 'boolean',
|
||||||
|
'daily_mailbox_limit' => 'integer',
|
||||||
|
'starts_at' => 'datetime',
|
||||||
|
'ends_at' => 'datetime',
|
||||||
|
'last_used_at' => 'datetime',
|
||||||
|
'checked_at' => 'datetime',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve active domains by type and provider.
|
||||||
|
*
|
||||||
|
* @param DomainType|null $domainType Filter by domain type
|
||||||
|
* @param ProviderType|null $providerType Filter by provider type
|
||||||
|
* @return array Array of domain names
|
||||||
|
*/
|
||||||
|
public static function getActiveDomainsByType(
|
||||||
|
?DomainType $domainType = null,
|
||||||
|
?ProviderType $providerType = null
|
||||||
|
): array {
|
||||||
|
$query = static::query()
|
||||||
|
->where('is_active', true)
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->whereNull('starts_at')
|
||||||
|
->orWhere('starts_at', '<=', now());
|
||||||
|
})
|
||||||
|
->where(function ($query) {
|
||||||
|
$query->whereNull('ends_at')
|
||||||
|
->orWhere('ends_at', '>=', now());
|
||||||
|
});
|
||||||
|
|
||||||
|
if ($domainType) {
|
||||||
|
$query->where('domain_type', $domainType->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($providerType) {
|
||||||
|
$query->where('provider_type', $providerType->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query->pluck('name')->toArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
25
app/enum/DomainType.php
Normal file
25
app/enum/DomainType.php
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\enum;
|
||||||
|
|
||||||
|
enum DomainType: string
|
||||||
|
{
|
||||||
|
case PUBLIC = 'public';
|
||||||
|
case PREMIUM = 'premium';
|
||||||
|
|
||||||
|
public function getColor(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::PUBLIC => 'warning',
|
||||||
|
self::PREMIUM => 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLabel(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::PUBLIC => 'Public',
|
||||||
|
self::PREMIUM => 'Premium',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
31
app/enum/ProviderType.php
Normal file
31
app/enum/ProviderType.php
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\enum;
|
||||||
|
|
||||||
|
enum ProviderType: string
|
||||||
|
{
|
||||||
|
case GMAIL = 'gmail.com';
|
||||||
|
case YAHOO = 'yahoo.com';
|
||||||
|
case OUTLOOK = 'outlook.com';
|
||||||
|
case CUSTOM = 'custom';
|
||||||
|
|
||||||
|
public function getColor(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::GMAIL => 'danger',
|
||||||
|
self::YAHOO => 'primary',
|
||||||
|
self::OUTLOOK => 'info',
|
||||||
|
self::CUSTOM => 'success',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getLabel(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::GMAIL => 'Gmail',
|
||||||
|
self::YAHOO => 'Yahoo',
|
||||||
|
self::OUTLOOK => 'Outlook',
|
||||||
|
self::CUSTOM => 'Custom',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
37
database/factories/DomainFactory.php
Normal file
37
database/factories/DomainFactory.php
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Factories;
|
||||||
|
|
||||||
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Domain>
|
||||||
|
*/
|
||||||
|
class DomainFactory extends Factory
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Define the model's default state.
|
||||||
|
*
|
||||||
|
* @return array<string, mixed>
|
||||||
|
*/
|
||||||
|
public function definition(): array
|
||||||
|
{
|
||||||
|
$domain = $this->faker->unique()->domainName();
|
||||||
|
$startDate = $this->faker->optional()->dateTimeBetween('-6 months', 'now');
|
||||||
|
$endDate = $startDate ?
|
||||||
|
$this->faker->optional()->dateTimeBetween($startDate, '+1 year') :
|
||||||
|
$this->faker->optional()->dateTimeBetween('now', '+1 year');
|
||||||
|
|
||||||
|
return [
|
||||||
|
'name' => $domain,
|
||||||
|
'is_active' => true,
|
||||||
|
'daily_mailbox_limit' => $this->faker->numberBetween(50, 500),
|
||||||
|
'domain_type' => $this->faker->randomElement(['disposable', 'temporary', 'custom']),
|
||||||
|
'provider_type' => $this->faker->randomElement(['internal', 'external', 'partner']),
|
||||||
|
'starts_at' => $startDate,
|
||||||
|
'ends_at' => $endDate,
|
||||||
|
'last_used_at' => $this->faker->optional()->dateTimeBetween('-1 month', 'now'),
|
||||||
|
'checked_at' => $this->faker->optional()->dateTimeBetween('-1 week', 'now'),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
use Illuminate\Database\Schema\Blueprint;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
Schema::create('domains', function (Blueprint $table) {
|
||||||
|
$table->id();
|
||||||
|
$table->string('name')->unique();
|
||||||
|
$table->boolean('is_active')->default(true);
|
||||||
|
$table->integer('daily_mailbox_limit')->default(100);
|
||||||
|
$table->string('domain_type')->nullable();
|
||||||
|
$table->string('provider_type')->nullable();
|
||||||
|
$table->timestamp('starts_at')->nullable();
|
||||||
|
$table->timestamp('ends_at')->nullable();
|
||||||
|
$table->timestamp('last_used_at')->nullable();
|
||||||
|
$table->timestamp('checked_at')->nullable();
|
||||||
|
$table->softDeletes();
|
||||||
|
$table->timestamps();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
Schema::dropIfExists('domains');
|
||||||
|
}
|
||||||
|
};
|
||||||
19
database/seeders/DomainSeeder.php
Normal file
19
database/seeders/DomainSeeder.php
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Database\Seeders;
|
||||||
|
|
||||||
|
use App\Models\Domain;
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
|
||||||
|
class DomainSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
Domain::factory()
|
||||||
|
->count(20)
|
||||||
|
->create();
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user