Tambah log historis pada Permohonan

Integrasi `PermohonanHistoryService` untuk mencatat log historis setiap pembuatan dan pembaruan permohonan. Penambahan ini mencakup pemrosesan unggahan file lampiran, pengambilan keterangan, dan pencatatan status permohonan sebelum dan sesudah perubahan.
This commit is contained in:
Daeng Deni Mardaeni
2024-11-08 19:54:02 +07:00
parent f3297988ff
commit ac80537ab5

View File

@@ -21,10 +21,17 @@
use Modules\Lpj\Models\Permohonan; use Modules\Lpj\Models\Permohonan;
use Modules\Lpj\Models\StatusPermohonan; use Modules\Lpj\Models\StatusPermohonan;
use Modules\Lpj\Models\TujuanPenilaian; use Modules\Lpj\Models\TujuanPenilaian;
use Modules\Lpj\Services\PermohonanHistoryService;
class PermohonanController extends Controller class PermohonanController extends Controller
{ {
public $user; public $user;
protected $historyService;
public function __construct(PermohonanHistoryService $historyService)
{
$this->historyService = $historyService;
}
public function index() public function index()
{ {
@@ -36,8 +43,30 @@
$validate = $request->validated(); $validate = $request->validated();
if ($validate) { if ($validate) {
try { try {
// Process file upload
$filePath = null;
if ($request->hasFile('attachment')) {
$file = $request->file('attachment');
$fileName = time() . '_' . $file->getClientOriginalName();
$filePath = $file->storeAs('permohonan_attachments', $fileName, 'public');
}
// Get keterangan if provided
$keterangan = $request->input('keterangan') ?? null;
// Save to database // Save to database
Permohonan::create($validate); $permohonan = Permohonan::create($validate);
// Create history
$this->historyService->createHistory(
$permohonan,
$validate['status'],
$keterangan,
[], // beforeRequest is empty for new permohonan
$permohonan->toArray(),
$filePath
);
return redirect() return redirect()
->route('permohonan.index')->with('success', 'Permohonan created successfully'); ->route('permohonan.index')->with('success', 'Permohonan created successfully');
} catch (Exception $e) { } catch (Exception $e) {
@@ -107,16 +136,41 @@
public function update(PermohonanRequest $request, $id) public function update(PermohonanRequest $request, $id)
{ {
$permohonan = Permohonan::findOrFail($id);
$beforeRequest = $permohonan->toArray();
$validate = $request->validated(); $validate = $request->validated();
if ($validate) { if ($validate) {
try { try {
// Update in database // Update in database
$permohonan = Permohonan::find($id);
if ($permohonan->status == 'revisi') { if ($permohonan->status == 'revisi') {
$validate['status'] = 'order'; $validate['status'] = 'order';
} }
$permohonan->update($validate); $permohonan->update($validate);
$afterRequest = $permohonan->fresh()->toArray();
// Process file upload
$file = null;
if ($request->hasFile('attachment')) {
$file = $request->file('attachment');
}
// Get keterangan if provided
$keterangan = $request->input('keterangan') ?? null;
$status =$validate['status'] ?? $permohonan->status;
$this->historyService->createHistory(
$permohonan,
$status,
$keterangan,
$beforeRequest,
$afterRequest,
$file
);
return redirect() return redirect()
->route('permohonan.index')->with('success', 'Permohonan updated successfully'); ->route('permohonan.index')->with('success', 'Permohonan updated successfully');
} catch (Exception $e) { } catch (Exception $e) {