Skip to content

Commit e753e74

Browse files
committed
perf: add artisan command to archive old tickets
1 parent 6568aa4 commit e753e74

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use App\Model\helpdesk\Ticket\Ticket_Thread;
6+
use App\Model\helpdesk\Ticket\Tickets;
7+
use Carbon\Carbon;
8+
use Illuminate\Console\Command;
9+
10+
class ArchiveOldTickets extends Command
11+
{
12+
/**
13+
* The name and signature of the console command.
14+
*
15+
* @var string
16+
*/
17+
protected $signature = 'tickets:archive {--days=365}';
18+
19+
/**
20+
* The console command description.
21+
*
22+
* @var string
23+
*/
24+
protected $description = 'Archive tickets older than a specified number of days';
25+
26+
/**
27+
* Execute the console command.
28+
*
29+
* @return mixed
30+
*/
31+
public function handle()
32+
{
33+
$days = $this->option('days');
34+
$this->info("Archiving tickets older than $days days...");
35+
$date = Carbon::now()->subDays($days);
36+
$tickets = Tickets::where('updated_at', '<', $date)->where('status', '!=', 5)->get();
37+
38+
if ($tickets->isEmpty()) {
39+
$this->info('No tickets to archive.');
40+
return;
41+
}
42+
43+
foreach ($tickets as $ticket) {
44+
$ticket->status = 5; // Assuming 5 is the status for "Archived"
45+
$ticket->save();
46+
47+
$thread = new Ticket_Thread();
48+
$thread->ticket_id = $ticket->id;
49+
$thread->user_id = 1; // System user
50+
$thread->is_internal = 1;
51+
$thread->body = 'Ticket has been archived by the system.';
52+
$thread->save();
53+
54+
$this->info("Archived ticket #{$ticket->id}");
55+
}
56+
$this->info('Done.');
57+
}
58+
}

app/Console/Kernel.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Kernel extends ConsoleKernel
2525
\App\Console\Commands\InstallDB::class,
2626
\App\Console\Commands\SetupTestEnv::class,
2727
\App\Console\Commands\SecureFaveoAPPKey::class,
28+
\App\Console\Commands\ArchiveOldTickets::class,
2829
SyncFaveoToLatestVersion::class,
2930
];
3031

0 commit comments

Comments
 (0)