329 lines
11 KiB
PHP
329 lines
11 KiB
PHP
<?php
|
|
namespace App\Repositories\Api\Access\Notification;
|
|
|
|
use App\Models\User;
|
|
use App\Constant\Constant;
|
|
use App\Models\Notifications;
|
|
use App\Models\UserDeviceToken;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Session;
|
|
use App\Repositories\Api\Access\Notification\NotificationsInterface;
|
|
|
|
class NotificationsRepository implements NotificationsInterface
|
|
{
|
|
/**
|
|
* @var Notifications
|
|
*/
|
|
protected $notification;
|
|
|
|
/**
|
|
* @var UserDeviceToken
|
|
*/
|
|
protected $userDeviceToken;
|
|
|
|
/**
|
|
* @param Notifications $notification
|
|
* @param UserDeviceToken $userDeviceToken
|
|
* NotificationsRepository constructor.
|
|
*
|
|
*/
|
|
public function __construct(Notifications $notification, UserDeviceToken $userDeviceToken)
|
|
{
|
|
$this->notification = $notification;
|
|
$this->userDeviceToken = $userDeviceToken;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function notificationListing($data)
|
|
{
|
|
$response = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
|
|
if ($user) {
|
|
$perPage = request('per_page') ?? Constant::PAGINATE_LIMIT;
|
|
$data = $this->notification->where('user_id', $user->id)->with([
|
|
'user:id,name,avatar',
|
|
'sant:id,name,avatar'
|
|
]);
|
|
$notifications = $data
|
|
->orderBy('status', 'ASC')
|
|
->orderBy('created_at', 'DESC')
|
|
->paginate($perPage);
|
|
|
|
// $responseData = [];
|
|
// $responseData['current_page'] = $notifications->currentPage();
|
|
// $responseData['last_page'] = $notifications->lastPage();
|
|
// $responseData['per_page'] = $notifications->perPage();
|
|
// $responseData['next_page_url'] = $notifications->nextPageUrl();
|
|
// $responseData['last_page_url'] = request()->url().'?page='.$notifications->lastPage();
|
|
// $responseData['total'] = $notifications->total();
|
|
// $responseData['count'] = $notifications->count();
|
|
|
|
// if (($notifications->currentPage() <= $notifications->lastPage()) && request('page') > Constant::STATUS_ONE) {
|
|
// $responseData['prev_page_url'] = request()->url().'?page='.(request('page') - Constant::STATUS_ONE);
|
|
|
|
// } else {
|
|
// $responseData['prev_page_url'] = null;
|
|
// }
|
|
// $responseData['hasMorePages'] = $notifications->hasMorePages();
|
|
|
|
//Update status to read
|
|
$data->update(['status' => '1']);
|
|
|
|
//Make unread notification count to 0
|
|
$unreadNotificationCount = User::where('id', $user->id)->first();
|
|
|
|
if($unreadNotificationCount->unread_notification_count != 0){
|
|
$unreadNotificationCount->unread_notification_count = 0;
|
|
$unreadNotificationCount->save();
|
|
}
|
|
|
|
// $response = $responseData;
|
|
$response['data'] = $notifications;
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
} else {
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param $id
|
|
* @return array
|
|
*/
|
|
public function deleteNotification($id)
|
|
{
|
|
$response = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
|
|
if ($user) {
|
|
$notification = $this->notification->where('id', $id)->delete();
|
|
|
|
if ($notification) {
|
|
$response['message'] = trans('auth.notification.delete');
|
|
$response['status'] = Constant::CODE_200;
|
|
} else {
|
|
$response['message'] = trans('auth.no_record_found');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function deleteAllNotifications()
|
|
{
|
|
$response = [];
|
|
|
|
try {
|
|
$user = loggedInUser();
|
|
|
|
if ($user) {
|
|
$notification = $this->notification->where('user_id', $user->id)->delete();
|
|
|
|
if ($notification) {
|
|
$response['message'] = trans('auth.notification.delete');
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
} else {
|
|
$response['message'] = trans('auth.no_record_found');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function pushNotificationRegister($request)
|
|
{
|
|
$response = [];
|
|
|
|
try{
|
|
$user = loggedInUser();
|
|
$deviceID = $this->userDeviceToken->where('device_id', $request->device_id)->first();
|
|
|
|
if ($user && empty($deviceID)) {
|
|
$deviceToken = new UserDeviceToken();
|
|
$deviceToken->user_id = $user->id;
|
|
$deviceToken->os = getPlatFormKey($request->os);
|
|
$deviceToken->device_id = $request->device_id;
|
|
$deviceToken->token = $request->fcm_token;
|
|
|
|
if ($deviceToken->save()) {
|
|
$response['message'] = trans('auth.device_token_saved');
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
} else {
|
|
$response['error'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
} else {
|
|
|
|
if (!empty($deviceID)) {
|
|
$checkCurrentToken = $this->userDeviceToken
|
|
->where('device_id', $request->device_id)
|
|
->where('token', $request->fcm_token)
|
|
->first();
|
|
|
|
if ($checkCurrentToken) {
|
|
} else {
|
|
$deviceID->delete();
|
|
$deviceToken = new UserDeviceToken();
|
|
$deviceToken->user_id = $user->id;
|
|
$deviceToken->os = getPlatFormKey($request->os);
|
|
$deviceToken->device_id = $request->device_id;
|
|
$deviceToken->token = $request->fcm_token;
|
|
$deviceToken->save();
|
|
}
|
|
}
|
|
|
|
$response['message'] = '';
|
|
$response['status'] = Constant::CODE_200;
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_401;
|
|
}
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function deleteUserToken($request)
|
|
{
|
|
$response = [];
|
|
|
|
try{
|
|
$user = loggedInUser();
|
|
|
|
if($user){
|
|
$userDeviceToken = $this->userDeviceToken->where([
|
|
'os' => $request->os,
|
|
'device_id' => $request->device_id,
|
|
'user_id' => $user->id
|
|
])->first();
|
|
|
|
if ($userDeviceToken) {
|
|
$userDeviceToken->delete();
|
|
$user->currentAccessToken()->delete();
|
|
$response['message'] = trans('auth.device_token_delete');
|
|
$response['status'] = Constant::CODE_200;
|
|
} else {
|
|
$response['error'] = trans('auth.token_not_found');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_401;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @param $request
|
|
* @return array
|
|
*/
|
|
public function deleteExistingDeviceToken($request)
|
|
{
|
|
$response = [];
|
|
|
|
try{
|
|
$userDeviceToken = $this->userDeviceToken->where([
|
|
'os' => $request->os,
|
|
'device_id' => $request->device_id
|
|
])->first();
|
|
|
|
if ($userDeviceToken) {
|
|
$userDeviceToken->delete();
|
|
$response['message'] = trans('auth.device_token_delete');
|
|
$response['status'] = Constant::CODE_200;
|
|
} else {
|
|
$response['message'] = "";
|
|
$response['status'] = Constant::CODE_200;
|
|
}
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_401;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function getUnreadCounts()
|
|
{
|
|
$response = [];
|
|
|
|
try {
|
|
$counts = User::where('id', loggedInUser()->id)->first();
|
|
|
|
//For total unread notification counts
|
|
if ($counts) {
|
|
$response['data']['unread_message_count'] = $counts->unread_message_count ?? Constant::STATUS_ZERO;
|
|
$response['data']['unread_notification_count'] = $counts->unread_notification_count ?? Constant::STATUS_ZERO;
|
|
$response['data']['sangh_menu'] = $counts->sangh_menu ?? Constant::STATUS_ZERO;
|
|
$response['data']['pending_sangh_invitation'] = $counts->pending_sangh_invitation ?? Constant::STATUS_ZERO;
|
|
$response['data']['sant_menu'] = $counts->sant_menu ?? Constant::STATUS_ZERO;
|
|
$response['data']['pending_sant_action'] = $counts->pending_sant_action ?? Constant::STATUS_ZERO;
|
|
$response['data']['shravak_menu'] = $counts->shravak_menu ?? Constant::STATUS_ZERO;
|
|
$response['data']['pending_friend_request'] = $counts->pending_friend_request ?? Constant::STATUS_ZERO;
|
|
$response['data']['profile_completion_percentage'] = $counts->profile_statistics['profile_completion_percentage'] ?? Constant::STATUS_ZERO;
|
|
}
|
|
$response['status'] = Constant::CODE_200;
|
|
|
|
} catch (\Exception $ex) {
|
|
Log::error($ex);
|
|
$response['message'] = trans('auth.something_went_wrong');
|
|
$response['status'] = Constant::CODE_403;
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|