File: /mnt/data/ghayatcom/ghayatcom-api/app/Http/Controllers/Api/NotificationController.php
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Library\PusherLibrary;
use App\Http\Requests\NotificationIdRequest;
use App\Http\Resources\NotificationCollection;
use App\Http\Resources\NotificationResource;
use App\User;
use Illuminate\Support\Carbon;
use DB;
use Auth;
use Exception;
use Throwable;
use Illuminate\Database\QueryException;
class NotificationController extends Controller
{
public function index(PusherLibrary $PusherLibrary)
{
$result = $PusherLibrary->notificationRetrieve();
return new NotificationCollection($result, 200);
}
public function unreadNotifications(Request $request, PusherLibrary $PusherLibrary)
{
try {
/** @var \App\User $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
$paginate = $request->count_per_page ? $request->count_per_page : 10;
$pageNumber = $request->page ? $request->page : 1;
$search_key = $request->search ? $request->search : '';
$sort_by_col = (isset($request->sort_by)) ? $request->sort_by : 'id';
/**
* @var int|null $paginate
* @var int|null $pageNumber
* @var string|null $search_key
* @var string|null $sort_by_col
*/
$data_order_by = $request->order_by;
/** @var string $data_order_by */
$orderBy = $data_order_by ? strtoupper($data_order_by) : strtoupper('desc');
$user_role = 'user';
// if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
// $user_role = 'admin';
// $data = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at');
// } else {
// $user_role = 'user';
// $data = auth()->user()->unreadNotifications()->whereNull('deleted_at');
// }
if ($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
$user_role = 'admin';
$data = DB::table('notifications')
->whereNull('admin_read_at')
->whereNull('deleted_at')
->whereRaw("JSON_LENGTH(data) > 0"); // Exclude empty data
} else {
$user_role = 'user';
$data = auth()->user()->unreadNotifications()
->whereNull('deleted_at')
->whereRaw("JSON_LENGTH(data) > 0"); // Exclude empty data
}
if(!empty($search_key))
{
$data = $data->where(function($q) use($search_key){
$q->where('data', 'LIKE', ["%{$search_key}%"]);
});
}
// $data = $data->get();
if($sort_by_col) {
/**
* @var string $sort_by_col
* @var string $orderBy
*/
$data = $data->orderBy($sort_by_col, $orderBy);
}
$paginateData = $data->paginate($paginate, ['*'], 'page', $pageNumber);
$unread_count = auth()->user()->unreadNotifications()->whereNull('deleted_at')->count();
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin'))
$unread_count = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->count();
// $result_array = ['unread_count' => $unread_count, 'notify' => $data];
// $PusherLibrary->notificationRetrieveEvent($result_array);
return new NotificationCollection($paginateData, 200, $unread_count, $user_role);
} catch(Exception | Throwable | QueryException $e){
return self::sentResponse(500, [], $e->getMessage());
}
}
public function notifications(Request $request, PusherLibrary $PusherLibrary)
{
try {
/** @var \App\User $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
$paginate = $request->count_per_page ? $request->count_per_page : 10;
$pageNumber = $request->page ? $request->page : 1;
$search_key = $request->search ? $request->search : '';
$sort_by_col = (isset($request->sort_by)) ? $request->sort_by : 'id';
/**
* @var int|null $paginate
* @var int|null $pageNumber
* @var string|null $search_key
* @var string|null $sort_by_col
*/
$data_order_by = $request->order_by;
/** @var string $data_order_by */
$orderBy = $data_order_by ? strtoupper($data_order_by) : strtoupper('desc');
$user_role = 'user';
// if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
// $user_role = 'admin';
// $data = DB::table('notifications')->whereNull('deleted_at');
// } else {
// $user_role = 'user';
// $data = auth()->user()->notifications()->whereNull('deleted_at');
// }
if ($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
$user_role = 'admin';
$data = DB::table('notifications')
// ->whereNull('admin_read_at')
->whereNull('deleted_at')
->whereRaw("JSON_LENGTH(data) > 0"); // Exclude empty data
} else {
$user_role = 'user';
$data = auth()->user()->notifications()
->whereNull('deleted_at')
->whereRaw("JSON_LENGTH(data) > 0"); // Exclude empty data
}
if(!empty($search_key))
{
$data = $data->where(function($q) use($search_key){
$q->where('data', 'LIKE', ["%{$search_key}%"]);
});
}
// $data = $data->get();
if($sort_by_col) {
/**
* @var string $sort_by_col
* @var string $orderBy
*/
$data = $data->orderBy($sort_by_col, $orderBy);
}
$paginateData = $data->paginate($paginate, ['*'], 'page', $pageNumber);
$unread_count = auth()->user()->unreadNotifications()->whereNull('deleted_at')->count();
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin'))
$unread_count = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->count();
// $result_array = ['unread_count' => $unread_count, 'notify' => $data];
// $PusherLibrary->notificationRetrieveEvent($result_array);
return new NotificationCollection($paginateData, 200, $unread_count, $user_role);
} catch(Exception | Throwable | QueryException $e){
return self::sentResponse(500, [], $e->getMessage());
}
}
public function markAsReadAll()
{
try {
/** @var \App\User $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->update(['admin_read_at' => Carbon::now()]);
$data = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->get();
} else {
auth()->user()->unreadNotifications->markAsRead();
$data = auth()->user()->unreadNotifications()->whereNull('deleted_at')->get();
}
$result_array = ['notify' => $data, 'unread_count' => 0];
// $PusherLibrary->notificationRetrieveEvent();
return self::sentResponse(200,$result_array,__('digimed_validation.success_response.notification_update_success'));
} catch(Exception | Throwable | QueryException $e){
return self::sentResponse(500, [], $e->getMessage());
}
}
public function markAsRead(NotificationIdRequest $request)
{
try {
/** @var \App\User $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->update(['admin_read_at' => Carbon::now()]);
$data = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->get();
} else {
auth()->user()->unreadNotifications->where('id', $request->notification_id)->whereNull('deleted_at')->markAsRead();
$data = auth()->user()->unreadNotifications()->whereNull('deleted_at')->get();
}
$unread_count = User::find(auth()->user()->id)->unreadNotifications()->whereNull('deleted_at')->count();
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin'))
$unread_count = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->count();
$result_array = ['notify' => $data, 'unread_count' => $unread_count];
// $PusherLibrary->notificationRetrieveEvent($result_array);
return self::sentResponse(200,$result_array,__('digimed_validation.success_response.notification_update_success'));
} catch(Exception | Throwable | QueryException $e){
return self::sentResponse(500, [], $e->getMessage());
}
}
public function markAsDelete(Request $request)
{
try {
/** @var \App\User $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
$notifications_id = explode(',',$request->notification_id);
if ($authRole->hasRole(['super_admin', 'clinic_admin'])) {
DB::table('notifications')
->whereIn('id', $notifications_id)
->update(['deleted_at' => Carbon::now()]);
$data = DB::table('notifications')->whereIn('id', $notifications_id)->get();
} else {
auth()->user()->notifications()->whereIN('id', $notifications_id)->update(['deleted_at' => Carbon::now()]);
$data = auth()->user()->unreadNotifications()->whereNull('deleted_at')->get();
}
$unread_count = User::find(auth()->user()->id)->unreadNotifications()->whereNull('deleted_at')->count();
if ($authRole->hasRole(['super_admin', 'clinic_admin'])) {
$unread_count = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->count();
}
$result_array = ['notify' => $data, 'unread_count' => $unread_count];
// $PusherLibrary->notificationRetrieveEvent($result_array);
return self::sentResponse(200,$result_array,__('digimed_validation.success_response.notification_remove_success'));
} catch(Exception | Throwable | QueryException $e){
return self::sentResponse(500, [], $e->getMessage());
}
}
public function markAsDeleteAll(Request $request)
{
try {
/** @var \App\User $authRole */
$authRole = Auth::user();
$auth_id = $authRole->id;
$notifications_id = explode(',',$request->notification_id);
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin')) {
DB::table('notifications')->update(['deleted_at' => Carbon::now()]);
$data = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->get();
} else {
auth()->user()->notifications()->delete();
$data = auth()->user()->unreadNotifications()->whereNull('deleted_at')->get();
}
$unread_count = User::find(auth()->user()->id)->unreadNotifications()->whereNull('deleted_at')->count();
if($authRole->hasRole('super_admin') || $authRole->hasRole('admin') || $authRole->hasRole('clinic_admin'))
$unread_count = DB::table('notifications')->whereNull('admin_read_at')->whereNull('deleted_at')->count();
$result_array = ['notify' => $data, 'unread_count' => $unread_count];
// $PusherLibrary->notificationRetrieveEvent($result_array);
return self::sentResponse(200,$result_array,__('digimed_validation.success_response.notification_remove_success'));
} catch(Exception | Throwable | QueryException $e){
return self::sentResponse(500, [], $e->getMessage());
}
}
}