Tambah fitur riwayat permohonan

Menambahkan migrasi database, model, dan service untuk mencatat riwayat setiap permohonan. Migrasi menciptakan tabel `permohonan_histories` dengan menyimpan detail tentang status, keterangan, perubahan permohonan (sebelum dan sesudah), dan informasi file terkait. Model `PermohonanHistory` mengatur relasi dengan model `Permohonan` dan `User`. Service `PermohonanHistoryService` menangani pembuatan riwayat baru serta penanganan file terkait dan error handling yang memadai.
This commit is contained in:
Daeng Deni Mardaeni
2024-11-08 19:53:15 +07:00
parent 70344ff310
commit f3297988ff
3 changed files with 88 additions and 9 deletions

View File

@@ -0,0 +1,43 @@
<?php
namespace Modules\Lpj\Services;
use Modules\Lpj\Models\Permohonan;
use Modules\Lpj\Models\PermohonanHistory;
class PermohonanHistoryService
{
public function createHistory(Permohonan $permohonan, string $status, ?string $keterangan, array $beforeRequest, array $afterRequest, ?string $file = null)
{
$filePath = null;
if ($file) {
$filePath = $file->store('permohonan_history_files', 'public');
}
try {
$history = PermohonanHistory::create([
'permohonan_id' => $permohonan->id,
'status' => $status,
'keterangan' => $keterangan,
'before_request' => json_encode($beforeRequest),
'after_request' => json_encode($afterRequest),
'file_path' => $filePath,
'user_id' => auth()->id(),
]);
} catch (\Exception $e) {
// Log the error
\Log::error('Error creating PermohonanHistory: ' . $e->getMessage());
// You might want to delete the uploaded file if the database operation fails
if ($filePath) {
\Storage::disk('public')->delete($filePath);
}
// Rethrow the exception or handle it as per your application's error handling policy
throw new \Exception('Failed to create PermohonanHistory: ' . $e->getMessage());
}
}
}