diff --git a/app/Http/Controllers/Admin/APIs/Customer_API/NotificationController.php b/app/Http/Controllers/Admin/APIs/Customer_API/NotificationController.php index e8a5c26..714f42d 100644 --- a/app/Http/Controllers/Admin/APIs/Customer_API/NotificationController.php +++ b/app/Http/Controllers/Admin/APIs/Customer_API/NotificationController.php @@ -2,107 +2,107 @@ namespace App\Http\Controllers\Admin\APIs\Customer_API; use Illuminate\Support\Facades\Log; +use App\Helpers\onesignalhelper; use Illuminate\Support\Facades\Validator; use Exception; use App\Http\Controllers\Controller; use App\Models\IamPrincipal; +use App\Models\NotificationDetails; use Illuminate\Http\Request; class NotificationController extends Controller { - // public function getNotificationApi(Request $request) - // { - // try { + public function getNotificationApi(Request $request) + { + try { - // $token = readHeaderToken(); + $token = readHeaderToken(); - // if ($token) { - // $customerIamId = $token['sub']; - // $perPage = $request->query('per_page', 10); // Default to 10 items per page - // $page = $request->query('page', 1); // Default to page 1 + if ($token) { + $customerIamId = $token['sub']; + $perPage = $request->query('per_page', 10); + $page = $request->query('page', 1); - // if ($customerIamId) { - // $notificationsData = NotificationDetails::where('principal_xid', $customerIamId)->orderBy('id', 'Desc')->get(); + if ($customerIamId) { + $notificationsData = NotificationDetails::where('principal_xid', $customerIamId)->orderBy('id', 'Desc')->get(); - // // Calculate the total number of items and total pages - // $totalItems = $notificationsData->count(); - // $totalPages = ceil($totalItems / $perPage); + $totalItems = $notificationsData->count(); + $totalPages = ceil($totalItems / $perPage); - // // Slice the collection based on the current page and items per page - // $offset = ($page - 1) * $perPage; - // $currentPageItems = $notificationsData->slice($offset, $perPage); + // Slice the collection based on the current page and items per page + $offset = ($page - 1) * $perPage; + $currentPageItems = $notificationsData->slice($offset, $perPage); - // if (!empty($notificationsData)) { - // return jsonResponseWithSuccessMessage(__('success.data_fetched_successfully'), $notificationsData, 200); - // } - // } - // } else { - // return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); - // } - // return jsonResponseWithSuccessMessage(__('success.data_fetched_successfully'), $notificationsData, 200); - // } catch (\Exception $e) { - // Log::error(' get Notification Controller function failed: ' . $e->getMessage()); - // return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); - // } - // } + if (!empty($notificationsData)) { + return jsonResponseWithSuccessMessage(__('success.data_fetched_successfully'), $notificationsData, 200); + } + } + } else { + return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); + } + return jsonResponseWithSuccessMessage(__('success.data_fetched_successfully'), $notificationsData, 200); + } catch (\Exception $e) { + Log::error(' get Notification Controller function failed: ' . $e->getMessage()); + return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500); + } + } - // public function sendNotificationApi(Request $request) - // { - // try { - // $playerId = $request->one_signal_player_id; - // $message = $request->message; - // $title = $request->title; - // $content_type = $request->content_type; - // $imageUrl = $request->image ? $request->image : null; + public function sendNotificationApi(Request $request) + { + try { + $playerId = $request->one_signal_player_id; + $message = $request->message; + $title = $request->title; + $content_type = $request->content_type; + $imageUrl = $request->image ? $request->image : null; - // $result = onesignalhelper::sendNotificationApi($playerId, $title, $message, $content_type, $imageUrl, $id = null); - // // $res = onesignalhelper::StoreNotificationDetails($customerId, 1, $description); + $result = onesignalhelper::sendNotificationApi($playerId, $title, $message, $content_type, $imageUrl, $id = null); - // return response()->json([ - // 'message' => 'Notification Send Successfully!', - // 'error_code' => 0, - // 'status' => 200, - // 'result' => null, - // ]); - // } catch (\Exception $e) { - // return response()->json([ - // 'message' => "Network Error! Please try again after sometime.", - // 'error_code' => 1, - // 'status' => 500, - // 'result' => null - // ]); - // } - // } + return response()->json([ + 'message' => 'Notification Send Successfully!', + 'error_code' => 0, + 'status' => 200, + 'result' => null, + ]); + } catch (\Exception $e) { + return response()->json([ + 'message' => "Network Error! Please try again after sometime.", + 'error_code' => 1, + 'status' => 500, + 'result' => null + ]); + } + } - // public function AlertNotificationApi(Request $request) - // { - // try { - // $token = readHeaderToken(); - // if ($token) { - // $iam_principal_id = $token['sub']; - // $validator = Validator::make($request->all(), [ - // 'status' => 'required|in:0,1', - // ]); - // if ($validator->fails()) { - // return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400); - // } + public function AlertNotificationApi(Request $request) + { + try { + $token = readHeaderToken(); + if ($token) { + $iam_principal_id = $token['sub']; + $validator = Validator::make($request->all(), [ + 'status' => 'required|in:0,1', + ]); + if ($validator->fails()) { + return jsonResponseWithErrorMessageApi($validator->errors()->first(), 400); + } - // $imPrincipal = IamPrincipal::where('id', $iam_principal_id)->first(); - // if (!$imPrincipal) { - // return jsonResponseWithErrorMessageApi(__('auth.user_not_found'), 404); - // } + $imPrincipal = IamPrincipal::where('id', $iam_principal_id)->first(); + if (!$imPrincipal) { + return jsonResponseWithErrorMessageApi(__('auth.user_not_found'), 404); + } - // $imPrincipal->notification_status = $request->status; + $imPrincipal->notification_status = $request->status; - // $imPrincipal->save(); + $imPrincipal->save(); - // return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), 200); - // } else { - // return jsonResponseWithErrorMessageApi(__('auth.user_not_found'), 409); - // } - // } catch (Exception $e) { - // Log::error('Contact form controller function failed: ' . $e); - // return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); - // } - // } + return jsonResponseWithSuccessMessage(__('auth.data_updated_successfully'), 200); + } else { + return jsonResponseWithErrorMessageApi(__('auth.user_not_found'), 409); + } + } catch (Exception $e) { + Log::error('Contact form controller function failed: ' . $e); + return jsonResponseWithErrorMessageApi(__('auth.something_went_wrong'), 500); + } + } } diff --git a/composer.json b/composer.json index 3cf6b28..cedad74 100644 --- a/composer.json +++ b/composer.json @@ -36,7 +36,8 @@ }, "files": [ "app/Http/Helpers/Webhelper.php", - "app/Http/Helpers/Imagehelper.php" + "app/Http/Helpers/Imagehelper.php", + "app/Http/Helpers/onesignalhelper.php" ] }, "autoload-dev": { diff --git a/routes/customer_api.php b/routes/customer_api.php index 70e643c..1ba2a98 100644 --- a/routes/customer_api.php +++ b/routes/customer_api.php @@ -55,6 +55,8 @@ Route::middleware(['customerApiBasicAuth'])->group(function () { //*******************************************************notification******************************************************** Route::get('/v1/get-notification', [NotificationController::class, 'getNotificationApi']); Route::post('/v1/send-notification', [NotificationController::class, 'sendNotificationApi']); + Route::post('/v1/customer-alert-notification', [NotificationController::class, 'AlertNotificationApi']); +