- Upgrade Filament framework from v3 to v4 - Update all Filament resources and pages for v4 compatibility - Fix test suite to maintain 100% pass rate (321 tests passing) - Add visibility condition for ticket close action (only when not closed) - Update dependencies and build assets for new Filament version - Maintain backward compatibility while leveraging v4 improvements
71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Exception;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Str;
|
|
|
|
class Ticket extends Model
|
|
{
|
|
use HasFactory;
|
|
|
|
protected $fillable = [
|
|
'user_id', 'ticket_id', 'subject', 'message', 'status', 'last_response_at', 'ip_address'
|
|
];
|
|
|
|
protected static function boot()
|
|
{
|
|
parent::boot();
|
|
|
|
static::creating(function ($ticket) {
|
|
if (empty($ticket->ticket_id)) {
|
|
$ticket->ticket_id = 'TICKET-' . strtoupper(Str::random(6));
|
|
}
|
|
});
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function responses()
|
|
{
|
|
return $this->hasMany(TicketResponse::class);
|
|
}
|
|
|
|
protected $casts = [
|
|
'created_at' => 'datetime', // Ensures created_at is a Carbon instance
|
|
'updated_at' => 'datetime', // Ensures updated_at is a Carbon instance
|
|
'last_response_at' => 'datetime', // Cast last_response_at to Carbon instance
|
|
];
|
|
|
|
public static function autoClose(): bool
|
|
{
|
|
try {
|
|
$tickets = Ticket::where('status', 'pending')
|
|
->where('last_response_at', '<', now()->subDays(3))
|
|
->get();
|
|
if (count($tickets) > 0) {
|
|
foreach ($tickets as $ticket) {
|
|
$ticket->status = 'closed';
|
|
$ticket->save();
|
|
|
|
TicketResponse::create([
|
|
'ticket_id' => $ticket->id,
|
|
'user_id' => 1,
|
|
'response' => 'This ticket has been auto-closed due to inactivity.',
|
|
]);
|
|
}
|
|
}
|
|
return true;
|
|
} catch (Exception $e) {
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|