2026-01-26 21:08:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
2026-01-27 16:30:46 +00:00
|
|
|
namespace Core\Tenant\Database\Factories;
|
2026-01-26 21:08:59 +00:00
|
|
|
|
2026-01-27 16:30:46 +00:00
|
|
|
use Core\Tenant\Models\WorkspaceInvitation;
|
2026-01-26 21:08:59 +00:00
|
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
2026-01-29 12:20:53 +00:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
2026-01-26 21:08:59 +00:00
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
|
|
|
|
/**
|
2026-01-27 16:30:46 +00:00
|
|
|
* @extends \Illuminate\Database\Eloquent\Factories\Factory<\Core\Tenant\Models\WorkspaceInvitation>
|
2026-01-26 21:08:59 +00:00
|
|
|
*/
|
|
|
|
|
class WorkspaceInvitationFactory extends Factory
|
|
|
|
|
{
|
|
|
|
|
protected $model = WorkspaceInvitation::class;
|
|
|
|
|
|
2026-01-29 12:20:53 +00:00
|
|
|
/**
|
|
|
|
|
* The plaintext token for the last created invitation.
|
|
|
|
|
*
|
|
|
|
|
* Since tokens are hashed, tests may need access to the original plaintext.
|
|
|
|
|
*/
|
|
|
|
|
public static ?string $lastPlaintextToken = null;
|
|
|
|
|
|
2026-01-26 21:08:59 +00:00
|
|
|
/**
|
|
|
|
|
* Define the model's default state.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, mixed>
|
|
|
|
|
*/
|
|
|
|
|
public function definition(): array
|
|
|
|
|
{
|
2026-01-29 12:20:53 +00:00
|
|
|
// Store the plaintext token so tests can access it if needed
|
|
|
|
|
static::$lastPlaintextToken = Str::random(64);
|
|
|
|
|
|
2026-01-26 21:08:59 +00:00
|
|
|
return [
|
|
|
|
|
'email' => fake()->unique()->safeEmail(),
|
2026-01-29 12:20:53 +00:00
|
|
|
// Token will be hashed by the model's creating event
|
|
|
|
|
'token' => static::$lastPlaintextToken,
|
2026-01-26 21:08:59 +00:00
|
|
|
'role' => 'member',
|
|
|
|
|
'invited_by' => null,
|
|
|
|
|
'expires_at' => now()->addDays(7),
|
|
|
|
|
'accepted_at' => null,
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-29 12:20:53 +00:00
|
|
|
/**
|
|
|
|
|
* Create a factory with a specific plaintext token.
|
|
|
|
|
*
|
|
|
|
|
* Useful for tests that need to know the token before creation.
|
|
|
|
|
*/
|
|
|
|
|
public function withPlaintextToken(string $token): static
|
|
|
|
|
{
|
|
|
|
|
static::$lastPlaintextToken = $token;
|
|
|
|
|
|
|
|
|
|
return $this->state(fn (array $attributes) => [
|
|
|
|
|
'token' => $token,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-26 21:08:59 +00:00
|
|
|
/**
|
|
|
|
|
* Indicate the invitation has been accepted.
|
|
|
|
|
*/
|
|
|
|
|
public function accepted(): static
|
|
|
|
|
{
|
|
|
|
|
return $this->state(fn (array $attributes) => [
|
|
|
|
|
'accepted_at' => fake()->dateTimeBetween('-7 days', 'now'),
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Indicate the invitation has expired.
|
|
|
|
|
*/
|
|
|
|
|
public function expired(): static
|
|
|
|
|
{
|
|
|
|
|
return $this->state(fn (array $attributes) => [
|
|
|
|
|
'expires_at' => fake()->dateTimeBetween('-30 days', '-1 day'),
|
|
|
|
|
'accepted_at' => null,
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the role to admin.
|
|
|
|
|
*/
|
|
|
|
|
public function asAdmin(): static
|
|
|
|
|
{
|
|
|
|
|
return $this->state(fn (array $attributes) => [
|
|
|
|
|
'role' => 'admin',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the role to owner.
|
|
|
|
|
*/
|
|
|
|
|
public function asOwner(): static
|
|
|
|
|
{
|
|
|
|
|
return $this->state(fn (array $attributes) => [
|
|
|
|
|
'role' => 'owner',
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
}
|