api: get notifications

This commit is contained in:
saparatayev 2021-12-09 17:11:13 +05:00
parent 3c3b505ab9
commit 39acad3cd8
3 changed files with 109 additions and 1 deletions

View File

@ -0,0 +1,44 @@
<?php namespace AhmadFatoni\ApiGenerator\Controllers\API;
use Illuminate\Http\Request;
use Cms\Classes\Controller;
use Illuminate\Support\Facades\Validator;
// use RainLab\Notify\Models\Notification;
class NotificationsApiController extends Controller
{
// protected $Notification;
// public function __construct(Notification $Notification)
// {
// parent::__construct();
// // $this->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);
}
}

View File

@ -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']);
// 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');

View File

@ -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
*