71 lines
1.4 KiB
PHP
71 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
class ExportToken extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id',
|
|
'token',
|
|
'is_premium',
|
|
'credits_reserved',
|
|
'metadata',
|
|
'expires_at',
|
|
'used_at',
|
|
];
|
|
|
|
protected $casts = [
|
|
'is_premium' => 'boolean',
|
|
'metadata' => 'array',
|
|
'expires_at' => 'datetime',
|
|
'used_at' => 'datetime',
|
|
];
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function isAnonymous(): bool
|
|
{
|
|
return is_null($this->user_id);
|
|
}
|
|
|
|
public function isExpired(): bool
|
|
{
|
|
return $this->expires_at->isPast();
|
|
}
|
|
|
|
public function isUsed(): bool
|
|
{
|
|
return ! is_null($this->used_at);
|
|
}
|
|
|
|
public function isValid(): bool
|
|
{
|
|
return ! $this->isExpired() && ! $this->isUsed();
|
|
}
|
|
|
|
public function markAsUsed(): void
|
|
{
|
|
$this->update(['used_at' => now()]);
|
|
}
|
|
|
|
public function scopeExpiredAndUnused($query)
|
|
{
|
|
return $query->where('expires_at', '<', now())
|
|
->whereNull('used_at');
|
|
}
|
|
|
|
public function scopeForUser($query, $userId)
|
|
{
|
|
return $query->where('user_id', $userId);
|
|
}
|
|
}
|