feat(pd): pd_processing_log 'deleted' on report file destroy (152-ФЗ)

This commit is contained in:
Дмитрий
2026-05-22 15:57:48 +03:00
parent d3b3a4f436
commit 5d7d7af00c
2 changed files with 55 additions and 2 deletions
@@ -8,6 +8,7 @@ use App\Http\Controllers\Controller;
use App\Jobs\GenerateReportJob;
use App\Models\ReportJob;
use App\Models\User;
use App\Services\Pd\PdAuditLogger;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Carbon;
@@ -305,12 +306,12 @@ class ReportJobController extends Controller
/**
* DELETE /api/reports/jobs/{id} удалить terminal job + файл.
*/
public function destroy(Request $request, int $id): JsonResponse
public function destroy(Request $request, int $id, PdAuditLogger $pdLog): JsonResponse
{
/** @var User $user */
$user = $request->user();
return DB::transaction(function () use ($user, $id): JsonResponse {
return DB::transaction(function () use ($user, $id, $request, $pdLog): JsonResponse {
DB::statement('SET LOCAL app.current_tenant_id = '.(int) $user->tenant_id);
$job = ReportJob::query()
@@ -335,6 +336,16 @@ class ReportJobController extends Controller
if ($job->file_path !== null) {
Storage::disk('local')->delete($job->file_path);
$pdLog->record(
action: 'deleted',
subjectType: 'lead',
subjectId: null,
purpose: 'report_file_'.$job->id,
tenantId: (int) $job->tenant_id,
actorTenantUserId: (int) $user->id,
actorAdminUserId: null,
ip: $request->ip(),
);
}
$job->delete();
@@ -0,0 +1,42 @@
<?php declare(strict_types=1);
use App\Models\ReportJob;
use App\Models\Tenant;
use App\Models\User;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
uses(DatabaseTransactions::class);
beforeEach(function () {
Storage::fake('local');
$this->tenant = Tenant::factory()->create();
$this->user = User::factory()->create(['tenant_id' => $this->tenant->id]);
$this->actingAs($this->user);
DB::statement('SET app.current_tenant_id = '.(int) $this->tenant->id);
});
it('writes pd deleted when a report file is destroyed', function () {
$job = ReportJob::create([
'tenant_id' => $this->tenant->id,
'user_id' => $this->user->id,
'type' => 'deals_export',
'parameters' => ['format' => 'csv', 'date_from' => '2026-04-01', 'date_to' => '2026-04-30'],
'status' => ReportJob::STATUS_DONE,
'file_path' => 'reports/'.(int) $this->tenant->id.'/test.csv',
]);
$this->deleteJson("/api/reports/jobs/{$job->id}")->assertOk();
$pd = DB::table('pd_processing_log')
->where('action', 'deleted')
->orderByDesc('id')
->first();
expect($pd)->not->toBeNull()
->and($pd->subject_type)->toBe('lead')
->and($pd->purpose)->toBe('report_file_'.$job->id)
->and((int) $pd->actor_tenant_user_id)->toBe($this->user->id)
->and((int) $pd->tenant_id)->toBe((int) $this->tenant->id);
});