Membuat controller contoh yang menunjukkan best practices penggunaan InspeksiCleanupService dengan automatic cleanup trigger pada create dan update operations - Dependency injection InspeksiCleanupService melalui constructor - Method store() dengan automatic cleanup trigger saat dokument_id tersedia - Method update() dengan cleanup trigger hanya saat dokument_id berubah dari null - Method cleanup() untuk eksekusi manual cleanup dengan pilihan sync/async - Validasi request yang komprehensif untuk semua method - Database transaction handling dengan proper rollback pada error - Logging informatif untuk audit trail setiap operasi - Error handling dengan try-catch dan response JSON yang konsisten - Response format yang standar dengan status code HTTP yang tepat - PHPDoc dokumentasi untuk setiap method dengan parameter dan return type
239 lines
8.1 KiB
PHP
239 lines
8.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Lpj\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Modules\Lpj\Models\Inspeksi;
|
|
use Modules\Lpj\Services\InspeksiCleanupService;
|
|
|
|
/**
|
|
* Controller contoh untuk menunjukkan penggunaan InspeksiCleanupService
|
|
*
|
|
* Controller ini berisi contoh method untuk membuat dan update data inspeksi
|
|
* dengan otomatis menjalankan cleanup data lama yang tidak memiliki dokument_id
|
|
*/
|
|
class InspeksiController extends Controller
|
|
{
|
|
protected InspeksiCleanupService $cleanupService;
|
|
|
|
public function __construct(InspeksiCleanupService $cleanupService)
|
|
{
|
|
$this->cleanupService = $cleanupService;
|
|
}
|
|
|
|
/**
|
|
* Contoh method untuk membuat data inspeksi baru
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'permohonan_id' => 'required|integer|exists:permohonan,id',
|
|
'dokument_id' => 'nullable|integer|exists:dokumen_jaminan,id',
|
|
'data_form' => 'required|json',
|
|
'foto_form' => 'nullable|json',
|
|
'denah_form' => 'nullable|json',
|
|
'name' => 'required|string|max:255',
|
|
'status' => 'required|string|max:50',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
// Buat data inspeksi baru
|
|
$inspeksi = Inspeksi::create(array_merge($validated, [
|
|
'created_by' => Auth::id(),
|
|
'updated_by' => Auth::id(),
|
|
]));
|
|
|
|
Log::info('InspeksiController: Data inspeksi berhasil dibuat', [
|
|
'inspeksi_id' => $inspeksi->id,
|
|
'permohonan_id' => $inspeksi->permohonan_id,
|
|
'dokument_id' => $inspeksi->dokument_id,
|
|
'created_by' => $inspeksi->created_by
|
|
]);
|
|
|
|
// Commit transaksi utama
|
|
DB::commit();
|
|
|
|
// Jalankan cleanup secara async jika ada dokument_id
|
|
// Ini akan menghapus data lama yang tidak memiliki dokument_id
|
|
if ($inspeksi->dokument_id) {
|
|
Log::info('InspeksiController: Memulai cleanup data lama', [
|
|
'permohonan_id' => $inspeksi->permohonan_id,
|
|
'created_by' => $inspeksi->created_by,
|
|
'dokument_id' => $inspeksi->dokument_id
|
|
]);
|
|
|
|
// Dispatch job cleanup secara async
|
|
$this->cleanupService->cleanupAsync(
|
|
$inspeksi->permohonan_id,
|
|
$inspeksi->created_by,
|
|
$inspeksi->dokument_id
|
|
);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Data inspeksi berhasil dibuat',
|
|
'data' => $inspeksi->load(['permohonan', 'dokument'])
|
|
], 201);
|
|
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
Log::error('InspeksiController: Gagal membuat data inspeksi', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal membuat data inspeksi: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Contoh method untuk update data inspeksi
|
|
*
|
|
* @param Request $request
|
|
* @param int $id
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function update(Request $request, $id)
|
|
{
|
|
$validated = $request->validate([
|
|
'dokument_id' => 'nullable|integer|exists:dokumen_jaminan,id',
|
|
'data_form' => 'required|json',
|
|
'foto_form' => 'nullable|json',
|
|
'denah_form' => 'nullable|json',
|
|
'name' => 'required|string|max:255',
|
|
'status' => 'required|string|max:50',
|
|
]);
|
|
|
|
DB::beginTransaction();
|
|
|
|
try {
|
|
$inspeksi = Inspeksi::findOrFail($id);
|
|
|
|
// Simpan data lama untuk logging
|
|
$oldDokumentId = $inspeksi->dokument_id;
|
|
|
|
// Update data
|
|
$inspeksi->update(array_merge($validated, [
|
|
'updated_by' => Auth::id(),
|
|
]));
|
|
|
|
Log::info('InspeksiController: Data inspeksi berhasil diupdate', [
|
|
'inspeksi_id' => $inspeksi->id,
|
|
'permohonan_id' => $inspeksi->permohonan_id,
|
|
'old_dokument_id' => $oldDokumentId,
|
|
'new_dokument_id' => $inspeksi->dokument_id,
|
|
'updated_by' => $inspeksi->updated_by
|
|
]);
|
|
|
|
// Commit transaksi utama
|
|
DB::commit();
|
|
|
|
// Jalankan cleanup jika dokument_id berubah dari null ke ada nilai
|
|
if (!$oldDokumentId && $inspeksi->dokument_id) {
|
|
Log::info('InspeksiController: Dokument ID berubah dari null, memulai cleanup', [
|
|
'inspeksi_id' => $inspeksi->id,
|
|
'permohonan_id' => $inspeksi->permohonan_id,
|
|
'created_by' => $inspeksi->created_by,
|
|
'new_dokument_id' => $inspeksi->dokument_id
|
|
]);
|
|
|
|
// Dispatch job cleanup secara async
|
|
$this->cleanupService->cleanupAsync(
|
|
$inspeksi->permohonan_id,
|
|
$inspeksi->created_by,
|
|
$inspeksi->dokument_id
|
|
);
|
|
}
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Data inspeksi berhasil diupdate',
|
|
'data' => $inspeksi->load(['permohonan', 'dokument'])
|
|
]);
|
|
|
|
} catch (\Exception $e) {
|
|
DB::rollBack();
|
|
|
|
Log::error('InspeksiController: Gagal update data inspeksi', [
|
|
'inspeksi_id' => $id,
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal update data inspeksi: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Contoh method untuk menjalankan cleanup secara manual
|
|
*
|
|
* @param Request $request
|
|
* @return \Illuminate\Http\JsonResponse
|
|
*/
|
|
public function cleanup(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'permohonan_id' => 'required|integer|exists:permohonan,id',
|
|
'created_by' => 'required|integer|exists:users,id',
|
|
'sync' => 'boolean', // Opsional, default false (async)
|
|
]);
|
|
|
|
try {
|
|
$permohonanId = $validated['permohonan_id'];
|
|
$createdBy = $validated['created_by'];
|
|
$sync = $validated['sync'] ?? false;
|
|
|
|
Log::info('InspeksiController: Menjalankan cleanup manual', [
|
|
'permohonan_id' => $permohonanId,
|
|
'created_by' => $createdBy,
|
|
'sync' => $sync
|
|
]);
|
|
|
|
if ($sync) {
|
|
// Jalankan secara sync
|
|
$this->cleanupService->cleanupSync($permohonanId, $createdBy);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Cleanup selesai dijalankan secara synchronous'
|
|
]);
|
|
} else {
|
|
// Dispatch ke queue
|
|
$this->cleanupService->cleanupAsync($permohonanId, $createdBy);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => 'Cleanup job berhasil di-dispatch ke queue'
|
|
]);
|
|
}
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('InspeksiController: Gagal menjalankan cleanup manual', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString()
|
|
]);
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Gagal menjalankan cleanup: ' . $e->getMessage()
|
|
], 500);
|
|
}
|
|
}
|
|
} |