create notification controller
This commit is contained in:
26
app/Exceptions/ProhibitedAction.php
Normal file
26
app/Exceptions/ProhibitedAction.php
Normal file
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use Exception;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Throwable;
|
||||
|
||||
class ProhibitedAction extends Exception
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
public function __construct(public $message = "", int $code = 422, ?Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the exception into an HTTP response.
|
||||
*/
|
||||
public function render(): JsonResponse
|
||||
{
|
||||
return $this->errorResponse($this->message);
|
||||
}
|
||||
}
|
||||
208
app/Http/Controllers/V3/NotificationController.php
Normal file
208
app/Http/Controllers/V3/NotificationController.php
Normal file
@@ -0,0 +1,208 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\V3;
|
||||
|
||||
use App\Exceptions\ProhibitedAction;
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Http\Traits\ApiResponse;
|
||||
use App\Models\EdarateOstani;
|
||||
use App\Models\RoadItemsProject;
|
||||
use App\Models\RoadObserved;
|
||||
use App\Models\SafetyAndPrivacy;
|
||||
use App\Models\User;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Throwable;
|
||||
|
||||
class NotificationController extends Controller
|
||||
{
|
||||
use ApiResponse;
|
||||
|
||||
/**
|
||||
* Handle the incoming request.
|
||||
* @throws Throwable
|
||||
*/
|
||||
public function __invoke(Request $request): JsonResponse
|
||||
{
|
||||
return $this->successResponse([
|
||||
'roadItemNotifications' => $this->roadItemNotifications(),
|
||||
'safetyAndPrivacyNotifications' => $this->safetyAndPrivacyNotifications(),
|
||||
'roadObservedNotifications' => $this->roadObservedNotifications(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
private function roadItemNotifications(): JsonResponse|array
|
||||
{
|
||||
$user = auth()->user();
|
||||
$road_items = [
|
||||
'total' => 0
|
||||
];
|
||||
|
||||
if ($user->hasPermissionTo('create-road-item')) {
|
||||
$road_items['operation_cnt'] = RoadItemsProject::query()
|
||||
->where('is_new', '=', 1)
|
||||
->where('user_id', '=', $user->id)
|
||||
->where('status', '=', 2)
|
||||
->count();
|
||||
|
||||
$road_items['total'] += $road_items['operation_cnt'];
|
||||
}
|
||||
if ($user->hasPermissionTo('supervise-road-item')) {
|
||||
$road_items['supervise_cnt'] = RoadItemsProject::query()
|
||||
->where('is_new', '=', 1)
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_items['total'] += $road_items['supervise_cnt'];
|
||||
}
|
||||
elseif ($user->hasPermissionTo('supervise-road-item-province')) {
|
||||
throw_if(is_null($user->province_id) || !$user->province_id, new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$road_items['supervise_cnt'] = RoadItemsProject::query()
|
||||
->where('is_new', '=', 1)
|
||||
->where('province_id', '=', $user->province_id)
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_items['total'] += $road_items['supervise_cnt'];
|
||||
}
|
||||
elseif ($user->hasPermissionTo('supervise-road-item-by-edareostani')) {
|
||||
throw_if(is_null($user->edarate_ostani_id) || !$user->edarate_ostani_id, new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$confirmableItems = EdarateOstani::query()->where('id', '=', $user->edarate_ostani_id)->value('items_for_confirm');
|
||||
$road_items['supervise_cnt'] = RoadItemsProject::query()
|
||||
->where('is_new', '=', 1)
|
||||
->where('province_id', '=', $user->province_id)
|
||||
->whereIn('item', explode(",", $confirmableItems))
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_items['total'] += $road_items['supervise_cnt'];
|
||||
}
|
||||
return $road_items;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
private function safetyAndPrivacyNotifications(): JsonResponse|array
|
||||
{
|
||||
$user = auth()->user();
|
||||
$safety_and_privacy = array();
|
||||
if ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable')) {
|
||||
$activity = SafetyAndPrivacy::query()
|
||||
->selectRaw('count(*) as cnt, step')
|
||||
->groupby('step')
|
||||
->orderBy('step')
|
||||
->get();
|
||||
|
||||
}
|
||||
elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-province')) {
|
||||
throw_if(is_null($user->province_id), new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$activity = SafetyAndPrivacy::query()
|
||||
->where('province_id', '=', $user->province_id)
|
||||
->selectRaw('count(*) as cnt, step')
|
||||
->groupby('step')
|
||||
->orderBy('step')
|
||||
->get();
|
||||
|
||||
}
|
||||
elseif ($user->hasPermissionTo('show-safety-and-privacy-operator-cartable-edarate-shahri')) {
|
||||
throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$activity = SafetyAndPrivacy::query()
|
||||
->where('edare_shahri_id', '=', $user->edarate_shahri_id)
|
||||
->selectRaw('count(*) as cnt, step')
|
||||
->groupby('step')
|
||||
->orderBy('step')
|
||||
->get();
|
||||
}
|
||||
|
||||
$safety_and_privacy['step_one'] = isset($activity[0]) ? $activity[0]->cnt : 0;
|
||||
$safety_and_privacy['step_two'] = isset($activity[1]) ? $activity[1]->cnt : 0;
|
||||
|
||||
return $safety_and_privacy;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws Throwable
|
||||
*/
|
||||
private function roadObservedNotifications(): JsonResponse|array
|
||||
{
|
||||
$user = auth()->user();
|
||||
$road_observations = [
|
||||
'total' => 0
|
||||
];
|
||||
|
||||
if ($user->hasPermissionTo('show-fast-react')) {
|
||||
$road_observations['operation_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 2)
|
||||
->count();
|
||||
|
||||
$road_observations['complaint_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['total'] += ($road_observations['operation_cnt'] + $road_observations['complaint_cnt']);
|
||||
}
|
||||
elseif ($user->hasPermissionTo('show-fast-react-province')) {
|
||||
throw_if(is_null($user->province_id) || !$user->province_id, new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$road_observations['operation_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 2)
|
||||
->where('rms_province_id', $user->province_id)
|
||||
->count();
|
||||
|
||||
$road_observations['complaint_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['total'] += ($road_observations['operation_cnt'] + $road_observations['complaint_cnt']);
|
||||
}
|
||||
elseif ($user->hasPermissionTo('show-fast-react-edarate-shahri')) {
|
||||
throw_if(is_null($user->edarate_shahri_id), new ProhibitedAction('اداره ای برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$road_observations['operation_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 2)
|
||||
->where('edarate_shahri_id', '=', $user->edarate_shahri_id)
|
||||
->count();
|
||||
|
||||
$road_observations['complaint_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['total'] += ($road_observations['operation_cnt'] + $road_observations['complaint_cnt']);
|
||||
}
|
||||
if ($user->hasPermissionTo('supervise-fast-react')) {
|
||||
$road_observations['supervise_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['complaint_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['total'] += ($road_observations['supervise_cnt'] + $road_observations['complaint_cnt']);
|
||||
}
|
||||
elseif ($user->hasPermissionTo('supervise-fast-react-province')) {
|
||||
throw_if(is_null($user->province_id) || !$user->province_id, new ProhibitedAction('استانی برای شما در سامانه ثبت نشده است!'));
|
||||
|
||||
$road_observations['supervise_cnt'] = RoadObserved::query()
|
||||
->where('province_id', '=', $user->province_id)
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['complaint_cnt'] = RoadObserved::query()
|
||||
->where('status', '=', 0)
|
||||
->count();
|
||||
|
||||
$road_observations['total'] += ($road_observations['supervise_cnt'] + $road_observations['complaint_cnt']);
|
||||
}
|
||||
|
||||
return $road_observations;
|
||||
}
|
||||
}
|
||||
@@ -21,6 +21,7 @@ use App\Http\Controllers\V3\Dashboard\SafetyAndPrivacyController;
|
||||
use App\Http\Controllers\V3\FMSVehicleManagementController;
|
||||
use App\Http\Controllers\V3\Harim\DivarkeshiController;
|
||||
use App\Http\Controllers\V3\LogListController;
|
||||
use App\Http\Controllers\V3\NotificationController;
|
||||
use App\Http\Controllers\V3\PermissionManagementController;
|
||||
use App\Http\Controllers\V3\ProfileController;
|
||||
use App\Http\Controllers\V3\RahdaranController;
|
||||
@@ -374,3 +375,7 @@ Route::prefix('permissions')
|
||||
Route::post('/{permission}', 'update')->name('update');
|
||||
Route::delete('/{permission}', 'destroy')->name('destroy');
|
||||
});
|
||||
|
||||
Route::prefix('notifications')
|
||||
->name('notifications')
|
||||
->get('/', NotificationController::class);
|
||||
|
||||
Reference in New Issue
Block a user