fix(surveyor): optimasi save data inpeksi
This commit is contained in:
@@ -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