fix(trash): preserve photos during soft delete and only erase physical files on force delete
This commit is contained in:
@@ -780,13 +780,11 @@ class EmployeeController extends Controller
|
|||||||
$empNik = $employee->nik;
|
$empNik = $employee->nik;
|
||||||
$photoPath = $employee->photo_path;
|
$photoPath = $employee->photo_path;
|
||||||
|
|
||||||
DB::transaction(function () use ($employee, $photoPath) {
|
DB::transaction(function () use ($employee) {
|
||||||
$user = $employee->user ?: User::where('username', $employee->nik)->first();
|
$user = $employee->user ?: User::where('username', $employee->nik)->first();
|
||||||
|
|
||||||
// Hapus berkas foto profil jika tersimpan secara lokal di storage
|
// Berkas foto tidak dihapus saat soft delete agar foto tetap ada jika dipulihkan dari tong sampah.
|
||||||
if ($photoPath) {
|
// Berkas baru dihapus permanen ketika dilakukan 'Hapus Permanen' (force delete) di TrashService.
|
||||||
ImageCompressionService::deleteIfExists($photoPath);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Hapus data karyawan (relasi employee_simpers otomatis cascade terhapus)
|
// Hapus data karyawan (relasi employee_simpers otomatis cascade terhapus)
|
||||||
$employee->delete();
|
$employee->delete();
|
||||||
|
|||||||
@@ -1187,12 +1187,9 @@ class VehicleController extends Controller
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($vehicle->isSupportCategory()) {
|
// Berkas fisik foto tidak dihapus di sini karena data masuk ke Tong Sampah (Soft Delete).
|
||||||
foreach ($vehicle->units as $u) {
|
// Berkas foto akan tetap aman sehingga jika dipulihkan (restore), foto unit tetap ada.
|
||||||
ImageCompressionService::deleteIfExists($u->photo_path);
|
// Berkas fisik baru dihapus permanen ketika dieksekusi 'Hapus Permanen' di TrashService.
|
||||||
}
|
|
||||||
}
|
|
||||||
ImageCompressionService::deleteIfExists($vehicle->photo_path);
|
|
||||||
|
|
||||||
$code = $vehicle->code;
|
$code = $vehicle->code;
|
||||||
$vehicle->delete();
|
$vehicle->delete();
|
||||||
|
|||||||
@@ -17,6 +17,7 @@ use App\Models\UnitTransfer;
|
|||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Models\Vehicle;
|
use App\Models\Vehicle;
|
||||||
use App\Models\VehicleActivity;
|
use App\Models\VehicleActivity;
|
||||||
|
use App\Services\ImageCompressionService;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Illuminate\Pagination\LengthAwarePaginator;
|
use Illuminate\Pagination\LengthAwarePaginator;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -432,6 +433,24 @@ class TrashService
|
|||||||
->forceDelete();
|
->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();
|
$item->forceDelete();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -310,4 +310,36 @@ class TrashAndAutoDeleteTest extends TestCase
|
|||||||
'id' => $freshActivity->id,
|
'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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user