feat(ticket): implement ticket generation service, model, and exception handling with tests

This commit is contained in:
2026-07-21 11:20:52 -03:00
parent c3f54c79cb
commit a928a2e848
6 changed files with 526 additions and 0 deletions

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Domains\Ticket\Models;
use App\Domains\Auth\Models\User;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable([
'tenant_code',
'ticket',
'name',
'description',
'starts_at',
'expires_at',
'used_at',
'user_id',
])]
class Ticket extends Model
{
use HasFactory;
public $timestamps = false;
protected $appends = [
'is_valid',
'is_expired',
'is_used',
];
protected function casts(): array
{
return [
'starts_at' => 'datetime',
'expires_at' => 'datetime',
'used_at' => 'datetime',
'user_id' => 'integer',
];
}
/** @return BelongsTo<Tenant, $this> */
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
}
/** @return BelongsTo<User, $this> */
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function isValid(): bool
{
$now = now();
return $this->used_at === null
&& ($this->starts_at === null || $this->starts_at->lessThanOrEqualTo($now))
&& ($this->expires_at === null || $this->expires_at->greaterThan($now));
}
public function getIsValidAttribute(): bool
{
return $this->isValid();
}
public function getIsExpiredAttribute(): bool
{
return $this->used_at === null
&& $this->expires_at !== null
&& $this->expires_at->lessThanOrEqualTo(now());
}
public function getIsUsedAttribute(): bool
{
return $this->used_at !== null;
}
}