From 6b359f642156d9441e2854e1a4ffeb8b68ad052c Mon Sep 17 00:00:00 2001 From: Watiah11 Date: Mon, 6 May 2024 08:36:36 +0700 Subject: [PATCH] fix(activity): fixed activity delete --- app/Http/Controllers/ActivityController.php | 50 ++++++++++++++------- 1 file changed, 33 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/ActivityController.php b/app/Http/Controllers/ActivityController.php index a804f8a..2571438 100755 --- a/app/Http/Controllers/ActivityController.php +++ b/app/Http/Controllers/ActivityController.php @@ -629,32 +629,48 @@ class ActivityController extends Controller return response()->json(['status' => 'success', 'message' => 'Activity Updated!', 'code' => 200], 200); } - public function delete($id, $company_id) + public function delete($id) { - if (!$data = Activity::find($id)) + DB::beginTransaction(); + $activity_id = (int)$id; + $data = Activity::query() + ->from('m_activity as ma') + ->select('mp.company_id') + ->join('m_proyek as mp','ma.proyek_id','mp.id') + ->where('ma.id', $activity_id); + $cloneQuery = clone $data; + if (!$data->exists()) { + DB::rollBack(); return response()->json(['status' => 'failed', 'action' => 'error', 'message' => 'Data not found!', 'code' => 404], 404); - $this->deleteRelative($id, $company_id); - if (!$data->delete()) - return response()->json(['status' => 'failed', 'action' => 'error', 'message' => 'data activity failed deleted!', 'code' => 500], 500); + } + + $this->deleteRelative($activity_id, $cloneQuery->first()->company_id); - return response()->json(['status' => 'success', "action" => "deleted", 'message' => 'data activity successfully deleted!', 'code' => 200], 200); + if (!$data->delete()) { + DB::rollBack(); + return response()->json(['status' => 'failed', 'action' => 'error', 'message' => 'Data activity failed deleted!', 'code' => 500], 500); + } + DB::commit(); + return response()->json(['status' => 'success', "action" => "deleted", 'message' => 'Data activity successfully deleted!', 'code' => 200], 200); } private function deleteRelative($activity_id, $company_id) { - UserToActivity::where('activity_id', $activity_id)->delete(); - AssignMaterial::where('activity_id', $activity_id)->delete(); - $dataAd = ActivityDokumen::where("activity_id", $activity_id)->get(); - $company = Company::find($company_id); - if($company) { - $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); - foreach ($dataAd as $ad) { - if (file_exists($destinationPath['pathActivityDocument'] . $ad->file)) { - unlink($destinationPath['pathActivityDocument'] . $ad->file); + DB::transaction(function() use($activity_id, $company_id) { + UserToActivity::where('activity_id', $activity_id)->delete(); + AssignMaterial::where('activity_id', $activity_id)->delete(); + $dataAd = ActivityDokumen::where("activity_id", $activity_id)->get(); + $company = Company::find($company_id); + if($company) { + $destinationPath = $this->setCustomeDirectoryUpload($company['company_name']); + foreach ($dataAd as $ad) { + if (file_exists($destinationPath['pathActivityDocument'] . $ad->file)) { + unlink($destinationPath['pathActivityDocument'] . $ad->file); + } } + ActivityDokumen::where("activity_id", $activity_id)->delete(); } - ActivityDokumen::where("activity_id", $activity_id)->delete(); - } + }); } public function getUpdate($id)