fix(surveyor): optimasi save data inpeksi
This commit is contained in:
@@ -167,6 +167,23 @@ class SurveyorController extends Controller
|
||||
public function store(Request $request)
|
||||
{
|
||||
$validatedData = $request->all();
|
||||
|
||||
$fotoTypes = [
|
||||
'foto_gistaru',
|
||||
'foto_bhumi',
|
||||
'foto_argis_region',
|
||||
'foto_tempat',
|
||||
'foto_sentuh_tanahku',
|
||||
'upload_gs'
|
||||
];
|
||||
|
||||
// Hapus data foto dari $validatedData
|
||||
foreach ($fotoTypes as $fotoType) {
|
||||
if (isset($validatedData[$fotoType])) {
|
||||
unset($validatedData[$fotoType]);
|
||||
}
|
||||
}
|
||||
|
||||
$result = $this->inspeksiService->storeInspeksi($validatedData, $request->input('type'), $request);
|
||||
|
||||
if ($result['success']) {
|
||||
@@ -2028,9 +2045,9 @@ class SurveyorController extends Controller
|
||||
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
||||
// abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
// if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
||||
// abort(403, 'Sorry! You are not allowed to view users.');
|
||||
// }
|
||||
|
||||
$query = Permohonan::query();
|
||||
$query = $query->orderBy('nomor_registrasi', 'desc');
|
||||
@@ -2099,7 +2116,7 @@ class SurveyorController extends Controller
|
||||
|
||||
public function dataForDatatablesData(Request $request, $type)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('jenis_aset.view')) {
|
||||
if (is_null(auth()->user()) || !$this->user->can('jenis_aset.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
@@ -2818,4 +2835,73 @@ class SurveyorController extends Controller
|
||||
], 500);
|
||||
}
|
||||
}
|
||||
|
||||
public function uploadFileFoto(Request $request, $url)
|
||||
{
|
||||
|
||||
// dd($request->all());
|
||||
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))
|
||||
->where('dokument_id', $request->input('dokument_id'))
|
||||
->first();
|
||||
|
||||
$fotoTypes = [
|
||||
'foto_gistaru',
|
||||
'foto_bhumi',
|
||||
'foto_argis_region',
|
||||
'foto_tempat',
|
||||
'foto_sentuh_tanahku',
|
||||
'upload_gs'
|
||||
];
|
||||
|
||||
if (!in_array($url, $fotoTypes)) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'Invalid key for upload'
|
||||
], 400);
|
||||
}
|
||||
|
||||
|
||||
$existingData = $inspeksi ? json_decode($inspeksi->data_form, true) : [];
|
||||
$existingData = $existingData ?? [];
|
||||
|
||||
|
||||
$factData = $existingData;
|
||||
if ($request->hasFile('file')) {
|
||||
$file = $request->file('file');
|
||||
$filePath = $file->store('uploads', 'public');
|
||||
$uploadedPath = str_replace('public/', '', $filePath);
|
||||
|
||||
|
||||
$factData[$url] = $uploadedPath;
|
||||
|
||||
|
||||
$existingData = $factData;
|
||||
if ($inspeksi) {
|
||||
$inspeksi->data_form = json_encode($existingData);
|
||||
$inspeksi->save();
|
||||
} else {
|
||||
Inspeksi::create([
|
||||
'permohonan_id' => $request->input('permohonan_id'),
|
||||
'dokument_id' => $request->input('document_id'),
|
||||
'data_form' => json_encode($existingData),
|
||||
]);
|
||||
}
|
||||
} else {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => 'No file uploaded for the given key'
|
||||
], 400);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'message' => 'Berhasil upload file foto',
|
||||
'data' => [
|
||||
'key' => $url,
|
||||
'path' => $factData[$url] ?? null
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -10,44 +10,50 @@ class SaveFormInspesksiService
|
||||
public function storeInspeksi(array $validatedData, string $type, Request $request)
|
||||
{
|
||||
try {
|
||||
$processedData = $this->getActionSpecificRules($validatedData, $type, $request);
|
||||
|
||||
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))
|
||||
->where('dokument_id', $request->input('dokument_id'))
|
||||
->first();
|
||||
|
||||
if ($inspeksi) {
|
||||
// Jika data sudah ada, merge dengan data yang baru
|
||||
$existingData = json_decode($inspeksi->data_form, true) ?: [];
|
||||
|
||||
if (isset($existingData['signature']) && !isset($processedData['signature'])) {
|
||||
$processedData['signature'] = $existingData['signature'];
|
||||
}
|
||||
|
||||
$mergedData = $this->arrayMergeRecursive($existingData, $processedData);
|
||||
|
||||
// Update record
|
||||
$inspeksi->update([
|
||||
'data_form' => json_encode($mergedData),
|
||||
'name' => $request->input('type')
|
||||
]);
|
||||
|
||||
$responseData = $mergedData;
|
||||
} else {
|
||||
// Jika belum ada data, buat record baru
|
||||
$inspeksi = Inspeksi::create([
|
||||
$inspeksi = Inspeksi::firstOrNew(
|
||||
[
|
||||
'permohonan_id' => $request->input('permohonan_id'),
|
||||
'dokument_id' => $request->input('dokument_id'),
|
||||
'data_form' => json_encode($processedData),
|
||||
'name' => $request->input('type')
|
||||
]);
|
||||
'dokument_id' => $request->input('dokument_id')
|
||||
]
|
||||
);
|
||||
|
||||
$responseData = $processedData;
|
||||
$inspeksi->name = $request->input('type');
|
||||
|
||||
$processedData = $this->getActionSpecificRules($validatedData, $type, $request, $inspeksi);
|
||||
|
||||
// Merge data lama dengan data baru
|
||||
$existingData = json_decode($inspeksi->data_form, true) ?: [];
|
||||
|
||||
$fotoTypes = [
|
||||
'foto_gistaru',
|
||||
'foto_bhumi',
|
||||
'foto_argis_region',
|
||||
'foto_tempat',
|
||||
'foto_sentuh_tanahku',
|
||||
'upload_gs'
|
||||
];
|
||||
|
||||
foreach ($fotoTypes as $fotoType) {
|
||||
if (isset($existingData[$fotoType])) {
|
||||
$processedData[$fotoType] = $existingData[$fotoType];
|
||||
}
|
||||
}
|
||||
|
||||
$mergedData = $this->arrayMergeRecursive($existingData, $processedData);
|
||||
|
||||
if (isset($existingData['signature']) && !isset($processedData['signature'])) {
|
||||
$mergedData['signature'] = $existingData['signature'];
|
||||
}
|
||||
|
||||
$inspeksi->data_form = json_encode($mergedData);
|
||||
$inspeksi->save();
|
||||
|
||||
return [
|
||||
'success' => true,
|
||||
'message' => 'Data berhasil disimpan',
|
||||
];
|
||||
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return [
|
||||
'success' => false,
|
||||
@@ -58,7 +64,7 @@ class SaveFormInspesksiService
|
||||
}
|
||||
|
||||
|
||||
private function getActionSpecificRules($data, $action, $request): array
|
||||
private function getActionSpecificRules($data, $action, $request, $inspeksi): array
|
||||
{
|
||||
$allowedActions = [
|
||||
'apartemen-kantor' => 'getUnitData',
|
||||
@@ -95,7 +101,7 @@ class SaveFormInspesksiService
|
||||
if (isset($allowedActions[$act])) {
|
||||
$method = $allowedActions[$act];
|
||||
|
||||
$actionRules = $this->$method($data, $request);
|
||||
$actionRules = $this->$method($data, $request, $inspeksi);
|
||||
$rules = array_merge($rules, $actionRules);
|
||||
|
||||
// Cek apakah act memerlukan asset description rules
|
||||
@@ -374,7 +380,7 @@ class SaveFormInspesksiService
|
||||
}
|
||||
|
||||
|
||||
private function getFactData($data, $request): array
|
||||
private function getFactData($data, $request, $inspeksi): array
|
||||
{
|
||||
$factData = [
|
||||
'fakta' => [
|
||||
@@ -400,44 +406,16 @@ class SaveFormInspesksiService
|
||||
];
|
||||
|
||||
|
||||
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))->where('dokument_id', $request->input('dokument_id'))->first();
|
||||
|
||||
|
||||
$fotoTypes = [
|
||||
'foto_gistaru',
|
||||
'foto_bhumi',
|
||||
'foto_argis_region',
|
||||
'foto_tempat',
|
||||
'foto_sentuh_tanahku',
|
||||
'upload_gs'
|
||||
];
|
||||
|
||||
if ($inspeksi) {
|
||||
$dataForm = json_decode($inspeksi->data_form, true);
|
||||
foreach ($fotoTypes as $fotoType) {
|
||||
// Jika ada file baru diupload
|
||||
if ($request->hasFile($fotoType)) {
|
||||
$factData[$fotoType] = $this->updateOrDeleteFile($dataForm, $request, $fotoType) ?: null;
|
||||
} else {
|
||||
$factData[$fotoType] = $dataForm[$fotoType] ?? null;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
foreach ($fotoTypes as $fotoType) {
|
||||
$factData[$fotoType] = $this->updateOrDeleteFile($data, $request, $fotoType) ?: null;
|
||||
}
|
||||
}
|
||||
|
||||
return $factData;
|
||||
}
|
||||
|
||||
private function getRapData($data, $request): array
|
||||
private function getRapData($data, $request, $inspeksi): array
|
||||
{
|
||||
|
||||
$inspeksi = Inspeksi::where('permohonan_id', $request->input('permohonan_id'))
|
||||
->where('dokument_id', $request->input('dokument_id'))
|
||||
->first();
|
||||
|
||||
$dataForm = json_decode($inspeksi->data_form, true);
|
||||
|
||||
$perizinanData = isset($dataForm['perizinan']) ? $dataForm['perizinan'] : [];
|
||||
@@ -966,14 +944,14 @@ class SaveFormInspesksiService
|
||||
];
|
||||
}
|
||||
|
||||
private function arrayMergeRecursive($arr1, $arr2)
|
||||
private function arrayMergeRecursive(array $arr1, array $arr2): array
|
||||
{
|
||||
foreach ($arr2 as $key => $value) {
|
||||
if ($key === 'signature' && isset($arr1['signature'])) {
|
||||
// Jika key adalah signature, gabungkan secara spesifik
|
||||
$arr1['signature'] = array_merge($arr1['signature'], $value);
|
||||
// Gabungkan 'signature' secara spesifik
|
||||
$arr1['signature'] = array_merge_recursive((array) $arr1['signature'], (array) $value);
|
||||
} elseif (is_array($value) && isset($arr1[$key]) && is_array($arr1[$key])) {
|
||||
// Rekursif untuk key lainnya
|
||||
// Rekursif untuk elemen array
|
||||
$arr1[$key] = $this->arrayMergeRecursive($arr1[$key], $value);
|
||||
} else {
|
||||
// Ganti nilai lama dengan nilai baru
|
||||
@@ -981,12 +959,8 @@ class SaveFormInspesksiService
|
||||
}
|
||||
}
|
||||
|
||||
// Bersihkan key lama yang tidak ada di array baru
|
||||
foreach ($arr1 as $key => $value) {
|
||||
if (!array_key_exists($key, $arr2) && $key !== 'signature') {
|
||||
unset($arr1[$key]);
|
||||
}
|
||||
}
|
||||
// Hapus key lama yang tidak ada di array baru kecuali 'signature'
|
||||
$arr1 = array_intersect_key($arr1, $arr2 + ['signature' => true]);
|
||||
|
||||
return $arr1;
|
||||
}
|
||||
@@ -1010,7 +984,7 @@ class SaveFormInspesksiService
|
||||
throw new Exception("Invalid file upload for {$fileKey}");
|
||||
}
|
||||
} elseif (isset($data[$fileKey]) && $data[$fileKey]) {
|
||||
return $data[$fileKey];
|
||||
return $data[$fileKey] ?? null;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user