85 lines
2.3 KiB
PHP
85 lines
2.3 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace Database\Factories;
|
||
|
|
|
||
|
|
use App\FolderRole;
|
||
|
|
use App\Models\Folder;
|
||
|
|
use App\Models\Invitation;
|
||
|
|
use App\Models\Organization;
|
||
|
|
use App\Models\User;
|
||
|
|
use Illuminate\Database\Eloquent\Factories\Factory;
|
||
|
|
use Illuminate\Support\Carbon;
|
||
|
|
use Illuminate\Support\Str;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @extends Factory<Invitation>
|
||
|
|
*/
|
||
|
|
class InvitationFactory extends Factory
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Define the model's default state.
|
||
|
|
*
|
||
|
|
* @return array<string, mixed>
|
||
|
|
*/
|
||
|
|
public function definition(): array
|
||
|
|
{
|
||
|
|
return [
|
||
|
|
'inviter_id' => User::factory(),
|
||
|
|
'email' => fake()->unique()->safeEmail(),
|
||
|
|
'role' => FolderRole::Viewer->value,
|
||
|
|
'inviteable_type' => Folder::class,
|
||
|
|
'inviteable_id' => Folder::factory(),
|
||
|
|
'token' => Str::random(64),
|
||
|
|
'expires_at' => now()->addDays(7),
|
||
|
|
'accepted_at' => null,
|
||
|
|
'accepted_by_id' => null,
|
||
|
|
];
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicate that the invitation invites someone into a folder.
|
||
|
|
*/
|
||
|
|
public function forFolder(Folder $folder, FolderRole $role = FolderRole::Viewer): static
|
||
|
|
{
|
||
|
|
return $this->state(fn (array $attributes) => [
|
||
|
|
'inviteable_type' => Folder::class,
|
||
|
|
'inviteable_id' => $folder->id,
|
||
|
|
'role' => $role->value,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicate that the invitation invites someone into a workspace.
|
||
|
|
*/
|
||
|
|
public function forOrganization(Organization $organization, string $role = 'member'): static
|
||
|
|
{
|
||
|
|
return $this->state(fn (array $attributes) => [
|
||
|
|
'inviteable_type' => Organization::class,
|
||
|
|
'inviteable_id' => $organization->id,
|
||
|
|
'role' => $role,
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicate that the invitation has been redeemed.
|
||
|
|
*/
|
||
|
|
public function accepted(): static
|
||
|
|
{
|
||
|
|
return $this->afterCreating(function (Invitation $invitation): void {
|
||
|
|
$invitation->accepted_at = Carbon::now();
|
||
|
|
$invitation->accepted_by_id = $invitation->accepted_by_id ?? $invitation->inviter_id;
|
||
|
|
$invitation->save();
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Indicate that the invitation has lapsed.
|
||
|
|
*/
|
||
|
|
public function expired(): static
|
||
|
|
{
|
||
|
|
return $this->state(fn (array $attributes) => [
|
||
|
|
'expires_at' => now()->subDay(),
|
||
|
|
]);
|
||
|
|
}
|
||
|
|
}
|