diff --git a/app/Http/Controllers/InspeksiController.php b/app/Http/Controllers/InspeksiController.php new file mode 100644 index 0000000..dffc6f6 --- /dev/null +++ b/app/Http/Controllers/InspeksiController.php @@ -0,0 +1,239 @@ +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); + } + } +} \ No newline at end of file