fix(user,trash): fix 500 server error when deleting user with operational history and protect audit integrity

This commit is contained in:
Ray1111-aa
2026-11-03 09:14:53 +07:00
parent 5a4969b467
commit ff3ab616e0
9 changed files with 236 additions and 52 deletions
+20 -16
View File
@@ -73,24 +73,28 @@ class TrashController extends Controller
*/
public function forceDelete(string $category, int $id): RedirectResponse
{
$success = $this->trashService->forceDeleteItem($category, $id);
try {
$success = $this->trashService->forceDeleteItem($category, $id);
if (!$success) {
return back()->with('error', 'Data tidak ditemukan di tong sampah atau gagal dihapus permanen.');
if (!$success) {
return back()->with('error', 'Data tidak ditemukan di tong sampah atau gagal dihapus permanen.');
}
$catDefinitions = TrashService::getCategoryDefinitions();
$label = $catDefinitions[$category]['label'] ?? $category;
AdminActivityLog::record(
actionType: 'delete',
module: 'Tong Sampah',
description: "Menghapus permanen data pada kategori '{$label}' (ID: {$id}).",
targetType: $category,
targetId: $id
);
return back()->with('success', "Data '{$label}' berhasil dihapus permanen dari sistem.");
} catch (\Throwable $e) {
return back()->with('error', 'Aksi Ditolak: ' . $e->getMessage());
}
$catDefinitions = TrashService::getCategoryDefinitions();
$label = $catDefinitions[$category]['label'] ?? $category;
AdminActivityLog::record(
actionType: 'delete',
module: 'Tong Sampah',
description: "Menghapus permanen data pada kategori '{$label}' (ID: {$id}).",
targetType: $category,
targetId: $id
);
return back()->with('success', "Data '{$label}' berhasil dihapus permanen dari sistem.");
}
/**
+22 -15
View File
@@ -11,6 +11,7 @@ use App\Services\ImageCompressionService;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Validation\Rule;
use Illuminate\View\View;
@@ -419,23 +420,29 @@ class UserController extends Controller
$userRole = $user->role ?? '';
$userId = $user->id;
$user->syncRoles([]);
$user->delete();
try {
DB::transaction(function () use ($user) {
$user->delete();
});
// Rekam ke Log Riwayat Aktivitas Super Admin
$roleTitle = $this->getFriendlyRoleName($userRole);
AdminActivityLog::record(
actionType: 'delete',
module: 'Manajemen Pengguna',
description: "Menghapus akun pengguna: {$name} (Username: {$username}) dengan hak akses '{$roleTitle}'.",
targetType: 'User',
targetId: $userId,
targetName: $name,
oldValues: $deletedSnapshot
);
// Rekam ke Log Riwayat Aktivitas Super Admin
$roleTitle = $this->getFriendlyRoleName($userRole);
AdminActivityLog::record(
actionType: 'delete',
module: 'Manajemen Pengguna',
description: "Menghapus akun pengguna: {$name} (Username: {$username}) dengan hak akses '{$roleTitle}'.",
targetType: 'User',
targetId: $userId,
targetName: $name,
oldValues: $deletedSnapshot
);
return redirect()->route('admin.users.index')
->with('success', "Akun pengguna [{$name}] berhasil dihapus dari sistem.");
return redirect()->route('admin.users.index')
->with('success', "Akun pengguna [{$name}] berhasil dihapus dan dipindahkan ke Tong Sampah.");
} catch (\Throwable $e) {
return redirect()->route('admin.users.index')
->with('error', 'Gagal menghapus pengguna: ' . $e->getMessage());
}
}
/**
@@ -1446,12 +1446,12 @@ class VehicleController extends Controller
if ($vehicle->isSupportCategory()) {
// Otomatis buatkan Master Template P2H baru jika belum ada
$templateCode = $vehicle->unit_type ?: $vehicle->code;
$templateCode = $vehicle->code;
$template = P2hTemplate::firstOrCreate(
['code' => $templateCode],
[
'name' => $vehicle->unit_type ?: $vehicle->brand_type,
'description' => "Template form inspeksi P2H resmi untuk {$vehicle->unit_type} (Kode: {$templateCode}).",
'name' => $vehicle->unit_category ?: ($vehicle->unit_type ?: $vehicle->brand_type),
'description' => "Template form inspeksi P2H resmi untuk " . ($vehicle->unit_category ?: $vehicle->brand_type) . " (Kode: {$templateCode}).",
'is_active' => true,
]
);