diff --git a/app/Http/Controllers/Bo/TicketController.php b/app/Http/Controllers/Bo/TicketController.php index 506918c..63412cc 100644 --- a/app/Http/Controllers/Bo/TicketController.php +++ b/app/Http/Controllers/Bo/TicketController.php @@ -541,7 +541,7 @@ class TicketController extends Controller Session::get('tickets.sort_way') ); } else { - $tickets->orderBy('id', 'DESC'); + $tickets->orderBy('last_activity', 'DESC'); } return $tickets; } diff --git a/app/Models/Message.php b/app/Models/Message.php index ae3887c..41daedd 100644 --- a/app/Models/Message.php +++ b/app/Models/Message.php @@ -51,7 +51,9 @@ class Message extends Model $q->withTrashed(); }]); /** @var \App\Models\Ticket $ticket */ - $ticket = $message->ticket; + $ticket = $message->ticket; + $ticket->last_activity = $message->updated_at; + $ticket->saveQuietly(); if ($message->user) { $ticket->load(['user' => function ($q) { $q->withTrashed(); diff --git a/app/Models/Ticket.php b/app/Models/Ticket.php index ba3b00a..899e558 100644 --- a/app/Models/Ticket.php +++ b/app/Models/Ticket.php @@ -33,7 +33,8 @@ class Ticket extends Model 'type', 'status', 'text', - 'is_internal' + 'is_internal', + 'last_activity', ]; /** @@ -44,7 +45,8 @@ class Ticket extends Model protected $casts = [ 'is_internal' => 'bool', 'type' => TicketTypeEnum::class, - 'status' => TicketStatusEnum::class + 'status' => TicketStatusEnum::class, + 'last_activity' => 'timestamp', ]; /** diff --git a/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php b/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php new file mode 100644 index 0000000..e4b1128 --- /dev/null +++ b/database/migrations/2023_08_14_142748_add_last_activity_tickets_table.php @@ -0,0 +1,43 @@ +timestamp('last_activity') + ->default(Carbon::now()) + ->comment('Last activity on the ticket.') + ->after('is_internal'); + }); + Ticket::get()->map(function (object $ticket) { + $lastMessage = $ticket->messages->whereNotNull('user_id')->sortByDesc('updated_at')->first(); + $ticket->last_activity = + $lastMessage->updated_at ?? $ticket->updated_at; + $ticket->saveQuietly(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down(): void + { + Schema::table('tickets', function (Blueprint $table) { + $table->dropColumn('last_activity'); + }); + } +}; diff --git a/resources/views/bo/modules/pagination.blade.php b/resources/views/bo/modules/pagination.blade.php index 9326c83..687ca1b 100644 --- a/resources/views/bo/modules/pagination.blade.php +++ b/resources/views/bo/modules/pagination.blade.php @@ -31,7 +31,7 @@ @foreach (\App\Enums\ItemsPerPaginationEnum::toArray() as $itemsPerPaginationEnum)