Files
econom/app/Models/TeamInvitation.php
brusnitsyn fb2e6c58e3
Some checks failed
linter / quality (push) Has been cancelled
tests / ci (8.3) (push) Has been cancelled
tests / ci (8.4) (push) Has been cancelled
tests / ci (8.5) (push) Has been cancelled
first commit
2026-04-06 00:06:00 +09:00

99 lines
2.2 KiB
PHP

<?php
namespace App\Models;
use App\Enums\TeamRole;
use Database\Factories\TeamInvitationFactory;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;
#[Fillable(['team_id', 'email', 'role', 'invited_by', 'expires_at', 'accepted_at'])]
class TeamInvitation extends Model
{
/** @use HasFactory<TeamInvitationFactory> */
use HasFactory;
/**
* Bootstrap the model and its traits.
*/
protected static function boot(): void
{
parent::boot();
static::creating(function (TeamInvitation $invitation) {
if (empty($invitation->code)) {
$invitation->code = Str::random(64);
}
});
}
/**
* Get the team that the invitation belongs to.
*
* @return BelongsTo<Team, $this>
*/
public function team(): BelongsTo
{
return $this->belongsTo(Team::class);
}
/**
* Get the user who sent the invitation.
*
* @return BelongsTo<Model, $this>
*/
public function inviter(): BelongsTo
{
return $this->belongsTo(User::class, 'invited_by');
}
/**
* Determine if the invitation has been accepted.
*/
public function isAccepted(): bool
{
return $this->accepted_at !== null;
}
/**
* Determine if the invitation is pending.
*/
public function isPending(): bool
{
return $this->accepted_at === null && ! $this->isExpired();
}
/**
* Determine if the invitation has expired.
*/
public function isExpired(): bool
{
return $this->expires_at !== null && $this->expires_at->isPast();
}
/**
* Get the attributes that should be cast.
*
* @return array<string, string>
*/
protected function casts(): array
{
return [
'role' => TeamRole::class,
'expires_at' => 'datetime',
'accepted_at' => 'datetime',
];
}
/**
* Get the route key for the model.
*/
public function getRouteKeyName(): string
{
return 'code';
}
}