notificationService = $notificationService; $this->timesheets = $timesheets; $this->userService = $userService; $this->authService = $authService; $this->helperService = $helperService; $this->menuRepo = $menuRepo; $this->themeCore = $themeCore; } /** * @throws BindingResolutionException */ public function with(): array { // Fetch all notifications once, then filter for unread in PHP // instead of making two separate DB queries $notifications = []; if (session()->exists('userdata')) { $notifications = $this->notificationService->getAllNotifications(session('userdata.id')); } $nCount = is_array($notifications) ? count(array_filter($notifications, fn ($n) => ($n['read'] ?? 1) == 0)) : 0; $totalNotificationCount = $totalMentionCount = $totalNewMentions = $totalNewNotifications = 0; $menuType = $this->menuRepo->getSectionMenuType(FrontcontrollerCore::getCurrentRoute(), 'project'); foreach ($notifications as $notif) { if ($notif['type'] == 'mention') { $totalMentionCount++; if ($notif['read'] == 0) { $totalNewMentions++; } } else { $totalNotificationCount++; if ($notif['read'] == 0) { $totalNewNotifications++; } } } $user = false; if (session()->exists('userdata')) { $user = $this->userService->getUser(session('userdata.id')); } if (! $user) { $this->authService->logout(); FrontcontrollerCore::redirect(BASE_URL.'/auth/login'); } $modal = $this->helperService->getHelperModalByRoute(FrontcontrollerCore::getCurrentRoute()); if (! session()->exists('companysettings.logoPath')) { session(['companysettings.logoPath' => $this->themeCore->getLogoUrl()]); } return [ 'newNotificationCount' => $nCount, 'totalNotificationCount' => $totalNotificationCount, 'totalMentionCount' => $totalMentionCount, 'totalNewMentions' => $totalNewMentions, 'totalNewNotifications' => $totalNewNotifications, 'menuType' => $menuType, 'notifications' => $notifications, 'onTheClock' => session()->exists('userdata') ? $this->timesheets->isClocked(session('userdata.id')) : false, 'activePath' => FrontcontrollerCore::getCurrentRoute(), 'action' => FrontcontrollerCore::getActionName(), 'module' => FrontcontrollerCore::getModuleName(), 'user' => $user, 'modal' => $modal, ]; } }