fix(trash): preserve photos during soft delete and only erase physical files on force delete

This commit is contained in:
Ray1111-aa
2026-10-28 13:47:44 +07:00
parent af61766546
commit 852404be32
4 changed files with 57 additions and 11 deletions
@@ -780,13 +780,11 @@ class EmployeeController extends Controller
$empNik = $employee->nik;
$photoPath = $employee->photo_path;
DB::transaction(function () use ($employee, $photoPath) {
DB::transaction(function () use ($employee) {
$user = $employee->user ?: User::where('username', $employee->nik)->first();
// Hapus berkas foto profil jika tersimpan secara lokal di storage
if ($photoPath) {
ImageCompressionService::deleteIfExists($photoPath);
}
// Berkas foto tidak dihapus saat soft delete agar foto tetap ada jika dipulihkan dari tong sampah.
// Berkas baru dihapus permanen ketika dilakukan 'Hapus Permanen' (force delete) di TrashService.
// Hapus data karyawan (relasi employee_simpers otomatis cascade terhapus)
$employee->delete();
@@ -1187,12 +1187,9 @@ class VehicleController extends Controller
}
}
if ($vehicle->isSupportCategory()) {
foreach ($vehicle->units as $u) {
ImageCompressionService::deleteIfExists($u->photo_path);
}
}
ImageCompressionService::deleteIfExists($vehicle->photo_path);
// Berkas fisik foto tidak dihapus di sini karena data masuk ke Tong Sampah (Soft Delete).
// Berkas foto akan tetap aman sehingga jika dipulihkan (restore), foto unit tetap ada.
// Berkas fisik baru dihapus permanen ketika dieksekusi 'Hapus Permanen' di TrashService.
$code = $vehicle->code;
$vehicle->delete();
+19
View File
@@ -17,6 +17,7 @@ use App\Models\UnitTransfer;
use App\Models\User;
use App\Models\Vehicle;
use App\Models\VehicleActivity;
use App\Services\ImageCompressionService;
use Carbon\Carbon;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
@@ -432,6 +433,24 @@ class TrashService
->forceDelete();
}
// Hapus berkas fisik saat benar-benar dihapus permanen dari tong sampah
if ($item instanceof Vehicle) {
ImageCompressionService::deleteIfExists($item->photo_path);
if (method_exists($item, 'isSupportCategory') && $item->isSupportCategory()) {
foreach ($item->units as $u) {
ImageCompressionService::deleteIfExists($u->photo_path);
}
}
}
if ($item instanceof Employee) {
ImageCompressionService::deleteIfExists($item->photo_path);
}
if ($item instanceof User) {
ImageCompressionService::deleteIfExists($item->photo_path ?? null);
}
$item->forceDelete();
});
+32
View File
@@ -310,4 +310,36 @@ class TrashAndAutoDeleteTest extends TestCase
'id' => $freshActivity->id,
]);
}
public function test_vehicle_photo_preserved_on_soft_delete_and_restored(): void
{
$vehicle = Vehicle::create([
'code' => 'LV-PHOTO-01',
'category' => 'light_vehicle',
'brand_type' => 'Hilux',
'license_plate' => 'BH 9999 AA',
'status' => 'Operation',
'current_km' => 500,
'photo_path' => 'vehicles/dummy_photo.jpg',
]);
// Simulasikan penghapusan kendaraan
$this->actingAs($this->superAdmin)
->delete(route('qma.vehicles.destroy', $vehicle->id));
$this->assertSoftDeleted('vehicles', ['id' => $vehicle->id]);
// Verifikasi photo_path masih tersimpan di record
$trashed = Vehicle::onlyTrashed()->find($vehicle->id);
$this->assertNotNull($trashed);
$this->assertEquals('vehicles/dummy_photo.jpg', $trashed->photo_path);
// Pulihkan dari tong sampah
$this->actingAs($this->superAdmin)
->post(route('admin.trash.restore', ['category' => 'vehicles', 'id' => $vehicle->id]));
$restored = Vehicle::find($vehicle->id);
$this->assertNotNull($restored);
$this->assertEquals('vehicles/dummy_photo.jpg', $restored->photo_path);
}
}