first commit
This commit is contained in:
98
app/Models/TeamInvitation.php
Normal file
98
app/Models/TeamInvitation.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user