From 9ec406a94240cc63273b579a17c6efee81bee04b Mon Sep 17 00:00:00 2001 From: wahyuun Date: Mon, 18 Sep 2023 09:42:00 +0700 Subject: [PATCH] fix search and add broadcast --- app/Http/Controllers/BroadcastController.php | 348 ++++++++++--------- 1 file changed, 177 insertions(+), 171 deletions(-) diff --git a/app/Http/Controllers/BroadcastController.php b/app/Http/Controllers/BroadcastController.php index 87eb02d..66ca433 100644 --- a/app/Http/Controllers/BroadcastController.php +++ b/app/Http/Controllers/BroadcastController.php @@ -1,171 +1,177 @@ -all(); - $data['status_send'] = true; - $data['created_by'] = $this->currentName; - if (isset($data['send_to_id'])) { - $idArray = $data['send_to_id']; - if (is_array($data['send_to_id'])) { - $data['send_to_id'] = implode(", ", $idArray); - } - } - // dd($data); - $result = Broadcast::create($data); - if($result){ - $this->sendNotification($data); - return response()->json(['status'=>'success','message'=>'add broadcast successfully!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'add broadcast failed!','code'=>400], 400); - } - } - - public function edit($id){ - if(!$id || (int) $id < 0 || $id==""){ - return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); - die(); - } - - $result = Broadcast::find($id); - - if($result){ - return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'failed get data broadcast, please try again later!','code'=>400], 400); - } - } - - public function update(Request $request, $id) - { - if(!$id || (int) $id < 0 || $id==""){ - return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); - } - - $data = Broadcast::find($id); - - if($data){ - $result = $data->update($request->all()); - }else{ - return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); - die(); - } - - - if($result){ - return response()->json(['status'=>'success','message'=>'data broadcast successfully updated!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data broadcast failed updated!','code'=>400], 400); - } - } - - public function delete($id) - { - $data = Broadcast::find($id); - - if($data){ - $delete = $data->delete(); - }else{ - return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); - die(); - } - - - if($delete){ - return response()->json(['status'=>'success','message'=>'data broadcast successfully deleted!','code'=>200], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'data broadcast failed deleted!','code'=>400], 400); - } - } - - public function search(Request $request) - { - $payload = $request->all(); - $dataBuilder = $this->setUpPayload($payload, 'm_broadcast'); - $builder = $dataBuilder['builder']; - $countBuilder = $dataBuilder['count']; - $dataGet = $builder->get(); - $totalRecord = $countBuilder->count(); - return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); - } - - public function list() - { - $data = Broadcast::all(); - $countData = $data->count(); - - if($data){ - return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); - }else{ - return response()->json(['status'=>'failed','message'=>'failed get list broadcast, please try again later!','code'=>400], 400); - } - } - - public function sendNotification($data) - { - // send_to_type (all, roles, user) - if (isset($data['send_to_type'])) { - switch ($data['send_to_type']) { - case 'all': - $users = User::whereNotNull('fcm_token')->get(); - if (isset($users)) { - foreach ($users as $user) { - FCMService::send( - $user->fcm_token, - [ - 'title' => $data['title_notif'], - 'body' => $data['message_notif'], - ] - ); - } - } - break; - - case 'roles': - $users = User::where("role_id", $data['send_to_id'])->whereNotNull('fcm_token')->get(); - if (isset($users)) { - foreach ($users as $user) { - FCMService::send( - $user->fcm_token, - [ - 'title' => $data['title_notif'], - 'body' => $data['message_notif'], - ] - ); - } - } - break; - - case 'users': - $idArray = explode(", ", $data['send_to_id']); - foreach ($idArray as $key => $value) { - $user = User::where("id", $value)->whereNotNull('fcm_token')->first(); - if (isset($user)) { - FCMService::send( - $user->fcm_token, - [ - 'title' => $data['title_notif'], - 'body' => $data['message_notif'], - ] - ); - } - } - break; - - default: - # code... - break; - } - } - } -} +all(); + $data['created_by'] = $this->currentName; + if (isset($data['send_to_id'])) { + $idArray = $data['send_to_id']; + if (is_array($data['send_to_id'])) { + $data['send_to_id'] = implode(", ", $idArray); + } + } + $result = Broadcast::create($data); + if($result){ + $data['status_send'] !== "registered" ? $this->sendNotification($data) : ''; + return response()->json(['status'=>'success','message'=>'add broadcast successfully!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'add broadcast failed!','code'=>400], 400); + } + } + + public function edit($id){ + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + die(); + } + + $result = Broadcast::find($id); + + if($result){ + return response()->json(['status'=>'success','code'=>200,'data'=>$result], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'failed get data broadcast, please try again later!','code'=>400], 400); + } + } + + public function update(Request $request, $id) + { + if(!$id || (int) $id < 0 || $id==""){ + return response()->json(['status'=>'failed','message'=>'id is required!','code'=>400], 400); + } + + $data = Broadcast::find($id); + + $status_send = $request->status_send; + + if($status_send === 'resend' || $status_send === 'send' && !empty($data)) + { + $data['status_send'] = $status_send; + $this->sendNotification($data); + } + + if($data){ + $result = $data->update($request->all()); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); + die(); + } + + + if($result){ + return response()->json(['status'=>'success','message'=>'data broadcast successfully updated!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast failed updated!','code'=>400], 400); + } + } + + public function delete($id) + { + $data = Broadcast::find($id); + + if($data){ + $delete = $data->delete(); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast not found!','code'=>400], 400); + die(); + } + + + if($delete){ + return response()->json(['status'=>'success','message'=>'data broadcast successfully deleted!','code'=>200], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'data broadcast failed deleted!','code'=>400], 400); + } + } + + public function search(Request $request) + { + $payload = $request->all(); + $dataBuilder = $this->setUpPayload($payload, 'm_broadcast'); + $builder = $dataBuilder['builder']; + $countBuilder = $dataBuilder['count']; + $dataGet = $builder->get(); + $totalRecord = $countBuilder->count(); + return response()->json(['status'=>'success','code'=>200,'data'=>$dataGet, 'totalRecord'=>$totalRecord], 200); + } + + public function list() + { + $data = Broadcast::all(); + $countData = $data->count(); + + if($data){ + return response()->json(['status'=>'success','code'=>200,'data'=>$data, 'totalRecord'=>$countData], 200); + }else{ + return response()->json(['status'=>'failed','message'=>'failed get list broadcast, please try again later!','code'=>400], 400); + } + } + + public function sendNotification($data) + { + // send_to_type (all, roles, user) + if (isset($data['send_to_type'])) { + switch ($data['send_to_type']) { + case 'all': + $users = User::whereNotNull('fcm_token')->get(); + if (isset($users)) { + foreach ($users as $user) { + FCMService::send( + $user->fcm_token, + [ + 'title' => $data['title_notif'], + 'body' => $data['message_notif'], + ] + ); + } + } + break; + + case 'roles': + $users = User::where("role_id", $data['send_to_id'])->whereNotNull('fcm_token')->get(); + if (isset($users)) { + foreach ($users as $user) { + FCMService::send( + $user->fcm_token, + [ + 'title' => $data['title_notif'], + 'body' => $data['message_notif'], + ] + ); + } + } + break; + + case 'users': + $idArray = explode(", ", $data['send_to_id']); + foreach ($idArray as $key => $value) { + $user = User::where("id", $value)->whereNotNull('fcm_token')->first(); + if (isset($user)) { + FCMService::send( + $user->fcm_token, + [ + 'title' => $data['title_notif'], + 'body' => $data['message_notif'], + ] + ); + } + } + break; + + default: + # code... + break; + } + } + } +}