From 39acad3cd8a884a9ca2bb54cbdc22eb5e4c42c82 Mon Sep 17 00:00:00 2001 From: saparatayev Date: Thu, 9 Dec 2021 17:11:13 +0500 Subject: [PATCH] api: get notifications --- .../api/NotificationsApiController.php | 44 +++++++++++++ plugins/ahmadfatoni/apigenerator/routes.php | 4 +- .../rainlab/notify/models/Notification.php | 62 +++++++++++++++++++ 3 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 plugins/ahmadfatoni/apigenerator/controllers/api/NotificationsApiController.php diff --git a/plugins/ahmadfatoni/apigenerator/controllers/api/NotificationsApiController.php b/plugins/ahmadfatoni/apigenerator/controllers/api/NotificationsApiController.php new file mode 100644 index 000000000..387b23daa --- /dev/null +++ b/plugins/ahmadfatoni/apigenerator/controllers/api/NotificationsApiController.php @@ -0,0 +1,44 @@ +Notification = $Notification; + // } + + public function index(Request $request) { + if (!$user = \JWTAuth::parseToken()->authenticate()) { + return response()->json('Unauthorized', 401); + } + + $validator = Validator::make($request->all(), [ + 'records_per_page' => 'numeric', + ]); + if($validator->fails()) { + return response()->json($validator->errors(), 400); + } + + $notifications = $user->notifications() + ->applyUnread() + ->paginate($request->records_per_page ? $request->records_per_page : 5) + ->makeHidden(['event_type', 'notifiable_type', 'notifiable_id', 'icon', 'type', 'body', 'data', 'parsed_body']) + ->transform(function ($notification) { + $notification->description_for_api = ""; + $notification->redirect_to_screen_for_api = ""; + return $notification; + }); + + + return response()->json(['data' => $notifications], 200); + } +} \ No newline at end of file diff --git a/plugins/ahmadfatoni/apigenerator/routes.php b/plugins/ahmadfatoni/apigenerator/routes.php index cd7f1129d..6b9d36b3e 100644 --- a/plugins/ahmadfatoni/apigenerator/routes.php +++ b/plugins/ahmadfatoni/apigenerator/routes.php @@ -34,4 +34,6 @@ Route::get('api/v1/messages/chatroom/{id}', 'AhmadFatoni\ApiGenerator\Controller Route::get('api/v1/messages/chatroom/{id}/load-more', 'AhmadFatoni\ApiGenerator\Controllers\API\MessagesapiController@loadMore')->where('id', '[0-9]+')->middleware('\Tymon\JWTAuth\Middleware\GetUserFromToken'); Route::post('api/v1/messages/{chatroom_id}', 'AhmadFatoni\ApiGenerator\Controllers\API\MessagesapiController@sendMessage')->where('chatroom_id', '[0-9]+')->middleware('\Tymon\JWTAuth\Middleware\GetUserFromToken'); Route::post('api/v1/messages/initialize-chatting/{seller_id}', 'AhmadFatoni\ApiGenerator\Controllers\API\MessagesapiController@initializeChatting')->where('seller_id', '[0-9]+')->middleware('\Tymon\JWTAuth\Middleware\GetUserFromToken'); -// Route::get('api/v1/messages/{id}/delete', ['as' => 'api/v1/messages.delete', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\API\MessagesapiController@destroy']); \ No newline at end of file +// Route::get('api/v1/messages/{id}/delete', ['as' => 'api/v1/messages.delete', 'uses' => 'AhmadFatoni\ApiGenerator\Controllers\API\MessagesapiController@destroy']); + +Route::resource('api/v1/notifications', 'AhmadFatoni\ApiGenerator\Controllers\API\NotificationsApiController', ['except' => ['destroy', 'create', 'edit']])->middleware('\Tymon\JWTAuth\Middleware\GetUserFromToken'); diff --git a/plugins/rainlab/notify/models/Notification.php b/plugins/rainlab/notify/models/Notification.php index 79a772d54..b8d37fdda 100644 --- a/plugins/rainlab/notify/models/Notification.php +++ b/plugins/rainlab/notify/models/Notification.php @@ -141,6 +141,68 @@ class Notification extends Model return 'Unknown type notification'; } + /** + * Get the localized description of the notification for api + * + * @return string + */ + public function getDescriptionForApiAttribute() + { + $e = new $this->event_type; + + if($e instanceof MessageReceivedEvent) { + + return [ + 'ru' => trans('validation.new_message', [], 'ru'), + 'en' => trans('validation.new_message', [], 'en'), + 'tm' => trans('validation.new_message', [], 'tm'), + ]; + + } elseif($e instanceof ProductReviewedEvent) { + + return [ + 'ru' => trans('validation.product_reviewed', [], 'ru'), + 'en' => trans('validation.product_reviewed', [], 'en'), + 'tm' => trans('validation.product_reviewed', [], 'tm'), + ]; + + } elseif($e instanceof PaymentReviewedEvent) { + + return [ + 'ru' => trans('validation.payment_reviewed', [], 'ru'), + 'en' => trans('validation.payment_reviewed', [], 'en'), + 'tm' => trans('validation.payment_reviewed', [], 'tm'), + ]; + } + + return 'Unknown type notification'; + } + + /** + * Get the screen where to redirect when clicking on the notification + * + * @return string + */ + public function getRedirectToScreenForApiAttribute() + { + $e = new $this->event_type; + + if($e instanceof MessageReceivedEvent) { + + return 'messages_screen'; + + } elseif($e instanceof ProductReviewedEvent) { + + return 'my_posts_screen'; + + } elseif($e instanceof PaymentReviewedEvent) { + + return 'balance_history_screen'; + } + + return 'main_screen'; + } + /** * Get the link where to redirect when the notification is clicked *