feat: add username management system
This commit is contained in:
135
database/factories/UsernameFactory.php
Normal file
135
database/factories/UsernameFactory.php
Normal file
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Factories;
|
||||
|
||||
use App\enum\ProviderType;
|
||||
use App\enum\UsernameType;
|
||||
use Illuminate\Database\Eloquent\Factories\Factory;
|
||||
|
||||
/**
|
||||
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Username>
|
||||
*/
|
||||
class UsernameFactory extends Factory
|
||||
{
|
||||
/**
|
||||
* Define the model's default state.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function definition(): array
|
||||
{
|
||||
return [
|
||||
'username' => $this->faker->unique()->userName(),
|
||||
'is_active' => $this->faker->boolean(80), // 80% chance of being active
|
||||
'daily_mailbox_limit' => $this->faker->numberBetween(50, 500),
|
||||
'username_type' => $this->faker->randomElement(UsernameType::class),
|
||||
'provider_type' => $this->faker->randomElement(ProviderType::class),
|
||||
'starts_at' => $this->faker->optional(0.3)->dateTimeBetween('-1 year', 'now'), // 30% chance of having start date
|
||||
'ends_at' => $this->faker->optional(0.2)->dateTimeBetween('now', '+2 years'), // 20% chance of having end date
|
||||
'last_used_at' => $this->faker->optional(0.7)->dateTimeBetween('-1 month', 'now'), // 70% chance of being used
|
||||
'checked_at' => $this->faker->optional(0.8)->dateTimeBetween('-1 week', 'now'), // 80% chance of being checked
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is active.
|
||||
*/
|
||||
public function active(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'is_active' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is inactive.
|
||||
*/
|
||||
public function inactive(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'is_active' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is public.
|
||||
*/
|
||||
public function public(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'username_type' => UsernameType::PUBLIC,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is premium.
|
||||
*/
|
||||
public function premium(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'username_type' => UsernameType::PREMIUM,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is for Gmail.
|
||||
*/
|
||||
public function gmail(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'provider_type' => ProviderType::GMAIL,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is for Yahoo.
|
||||
*/
|
||||
public function yahoo(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'provider_type' => ProviderType::YAHOO,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is for Outlook.
|
||||
*/
|
||||
public function outlook(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'provider_type' => ProviderType::OUTLOOK,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username is for custom provider.
|
||||
*/
|
||||
public function custom(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'provider_type' => ProviderType::CUSTOM,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username has expiration dates.
|
||||
*/
|
||||
public function withExpiration(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'starts_at' => $this->faker->dateTimeBetween('-1 month', 'now'),
|
||||
'ends_at' => $this->faker->dateTimeBetween('now', '+1 year'),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicate that the username has been recently used.
|
||||
*/
|
||||
public function recentlyUsed(): static
|
||||
{
|
||||
return $this->state(fn (array $attributes) => [
|
||||
'last_used_at' => $this->faker->dateTimeBetween('-1 day', 'now'),
|
||||
'checked_at' => $this->faker->dateTimeBetween('-1 day', '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('usernames', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('username')->unique();
|
||||
$table->boolean('is_active')->default(true);
|
||||
$table->integer('daily_mailbox_limit')->default(100);
|
||||
$table->string('username_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('usernames');
|
||||
}
|
||||
};
|
||||
90
database/seeders/UsernameSeeder.php
Normal file
90
database/seeders/UsernameSeeder.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Models\Username;
|
||||
use Illuminate\Database\Seeder;
|
||||
|
||||
class UsernameSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
// Create a variety of usernames for testing
|
||||
Username::factory()
|
||||
->count(15)
|
||||
->active()
|
||||
->public()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'public_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
Username::factory()
|
||||
->count(10)
|
||||
->active()
|
||||
->premium()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'premium_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
Username::factory()
|
||||
->count(8)
|
||||
->active()
|
||||
->gmail()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'gmail_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
Username::factory()
|
||||
->count(6)
|
||||
->active()
|
||||
->yahoo()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'yahoo_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
Username::factory()
|
||||
->count(6)
|
||||
->active()
|
||||
->outlook()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'outlook_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
Username::factory()
|
||||
->count(4)
|
||||
->active()
|
||||
->custom()
|
||||
->withExpiration()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'custom_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
// Create some inactive usernames
|
||||
Username::factory()
|
||||
->count(5)
|
||||
->inactive()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'inactive_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
|
||||
// Create some recently used usernames
|
||||
Username::factory()
|
||||
->count(8)
|
||||
->active()
|
||||
->recentlyUsed()
|
||||
->sequence(fn ($sequence) => [
|
||||
'username' => 'recent_user_'.($sequence->index + 1),
|
||||
])
|
||||
->create();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user