diff --git a/app/app/Console/Commands/ReportsCleanupExpired.php b/app/app/Console/Commands/ReportsCleanupExpired.php index dd1ff370..22378daa 100644 --- a/app/app/Console/Commands/ReportsCleanupExpired.php +++ b/app/app/Console/Commands/ReportsCleanupExpired.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace App\Console\Commands; use App\Models\ReportJob; +use App\Services\Pd\PdAuditLogger; use Illuminate\Console\Command; use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; @@ -69,6 +70,16 @@ class ReportsCleanupExpired extends Command if (! $dryRun) { Storage::disk('local')->delete($job->file_path); + app(PdAuditLogger::class)->record( + action: 'deleted', + subjectType: 'lead', + subjectId: null, + purpose: 'report_cleanup_expired_'.$job->id, + tenantId: $job->tenant_id, + actorTenantUserId: null, + actorAdminUserId: null, + ip: null, + ); $job->update(['file_path' => null]); } $count++; diff --git a/app/tests/Feature/Pd/ReportFileDeletePdLogTest.php b/app/tests/Feature/Pd/ReportFileDeletePdLogTest.php index 68409a27..0ec0c40c 100644 --- a/app/tests/Feature/Pd/ReportFileDeletePdLogTest.php +++ b/app/tests/Feature/Pd/ReportFileDeletePdLogTest.php @@ -40,3 +40,42 @@ it('writes pd deleted when a report file is destroyed', function () { ->and((int) $pd->actor_tenant_user_id)->toBe($this->user->id) ->and((int) $pd->tenant_id)->toBe((int) $this->tenant->id); }); + +it('writes pd deleted (system actor) when cron cleanup-expired runs', function () { + Storage::disk('local')->put('reports/'.(int) $this->tenant->id.'/cron1.csv', 'data'); + Storage::disk('local')->put('reports/'.(int) $this->tenant->id.'/cron2.csv', 'data'); + + ReportJob::create([ + 'tenant_id' => $this->tenant->id, + 'user_id' => $this->user->id, + 'type' => 'deals_export', + 'parameters' => ['format' => 'csv'], + 'status' => ReportJob::STATUS_DONE, + 'file_path' => 'reports/'.(int) $this->tenant->id.'/cron1.csv', + 'expires_at' => now()->subDay(), + ]); + ReportJob::create([ + 'tenant_id' => $this->tenant->id, + 'user_id' => $this->user->id, + 'type' => 'deals_export', + 'parameters' => ['format' => 'csv'], + 'status' => ReportJob::STATUS_DONE, + 'file_path' => 'reports/'.(int) $this->tenant->id.'/cron2.csv', + 'expires_at' => now()->subDay(), + ]); + + $this->artisan('reports:cleanup-expired')->assertExitCode(0); + + $rows = DB::table('pd_processing_log') + ->where('action', 'deleted') + ->where('purpose', 'like', 'report_cleanup_expired_%') + ->where('tenant_id', $this->tenant->id) + ->get(); + + expect($rows)->toHaveCount(2); + foreach ($rows as $r) { + expect($r->actor_tenant_user_id)->toBeNull() + ->and($r->actor_admin_user_id)->toBeNull() + ->and($r->subject_type)->toBe('lead'); + } +});