2026-03-11 23:33:10 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Support\Str;
|
|
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
class DeclarationInvitation extends Model
|
2026-03-11 23:33:10 +00:00
|
|
|
{
|
2026-03-12 18:25:32 +00:00
|
|
|
protected $table = 'declaration_invitations';
|
|
|
|
|
|
2026-03-11 23:33:10 +00:00
|
|
|
/**
|
|
|
|
|
* The attributes that are mass assignable.
|
|
|
|
|
*
|
|
|
|
|
* @var list<string>
|
|
|
|
|
*/
|
|
|
|
|
protected $fillable = [
|
2026-03-12 18:25:32 +00:00
|
|
|
'declaration_id',
|
2026-03-11 23:33:10 +00:00
|
|
|
'token',
|
|
|
|
|
'email',
|
|
|
|
|
'expires_at',
|
|
|
|
|
'used_at',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the attributes that should be cast.
|
|
|
|
|
*
|
|
|
|
|
* @return array<string, string>
|
|
|
|
|
*/
|
|
|
|
|
protected function casts(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
'expires_at' => 'datetime',
|
|
|
|
|
'used_at' => 'datetime',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Boot the model.
|
|
|
|
|
*/
|
|
|
|
|
protected static function boot(): void
|
|
|
|
|
{
|
|
|
|
|
parent::boot();
|
|
|
|
|
|
2026-03-12 18:25:32 +00:00
|
|
|
static::creating(function (DeclarationInvitation $invitation) {
|
2026-03-11 23:33:10 +00:00
|
|
|
if (empty($invitation->token)) {
|
|
|
|
|
$invitation->token = Str::uuid()->toString();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-12 18:25:32 +00:00
|
|
|
* Get the declaration that owns the invitation.
|
2026-03-11 23:33:10 +00:00
|
|
|
*
|
2026-03-12 18:25:32 +00:00
|
|
|
* @return BelongsTo<Declaration, $this>
|
2026-03-11 23:33:10 +00:00
|
|
|
*/
|
2026-03-12 18:25:32 +00:00
|
|
|
public function declaration(): BelongsTo
|
2026-03-11 23:33:10 +00:00
|
|
|
{
|
2026-03-12 18:25:32 +00:00
|
|
|
return $this->belongsTo(Declaration::class);
|
2026-03-11 23:33:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if the invitation is valid (not expired, not used).
|
|
|
|
|
*/
|
|
|
|
|
public function isValid(): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->used_at !== null) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->expires_at->isFuture();
|
|
|
|
|
}
|
|
|
|
|
}
|