make sure notification always returns array

This commit is contained in:
Denis Duliçi 2021-08-08 23:44:59 +03:00
parent 1921c3465d
commit d3233740b3
5 changed files with 57 additions and 47 deletions

View File

@ -2,15 +2,15 @@
namespace App\Http\Controllers\Common; namespace App\Http\Controllers\Common;
use Date;
use App\Abstracts\Http\Controller; use App\Abstracts\Http\Controller;
use App\Traits\Modules as RemoteModules;
use App\Http\Requests\Common\Notification as Request; use App\Http\Requests\Common\Notification as Request;
use App\Traits\Modules;
use App\Utilities\Date;
use Illuminate\Support\Str; use Illuminate\Support\Str;
class Notifications extends Controller class Notifications extends Controller
{ {
use RemoteModules; use Modules;
/** /**
* Display a listing of the resource. * Display a listing of the resource.
@ -39,10 +39,14 @@ class Notifications extends Controller
$module_notifications = $this->getNotifications('new-apps'); $module_notifications = $this->getNotifications('new-apps');
foreach ($module_notifications as $module_notification) { foreach ($module_notifications as $module_notification) {
setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.name', $module_notification->name); $prefix = 'notifications.' . user()->id . '.' . $module_notification->alias;
setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.message', $module_notification->alias);
setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.date', Date::now()); setting()->set([
setting()->set('notifications.'. user()->id . '.' . $module_notification->alias . '.status', '0'); $prefix . '.name' => $module_notification->name,
$prefix . '.message' => $module_notification->alias,
$prefix . '.date' => Date::now(),
$prefix . '.status' => '0',
]);
} }
setting()->save(); setting()->save();
@ -68,19 +72,22 @@ class Notifications extends Controller
$notifications = $this->getNotifications($path); $notifications = $this->getNotifications($path);
if ($notifications) {
foreach ($notifications as $notification) { foreach ($notifications as $notification) {
if ($notification->id == $id) { if ($notification->id == $id) {
setting()->set('notifications.'. $path . '.' . $id . '.name', $notification->name); $prefix = 'notifications.' . $path . '.' . $id;
setting()->set('notifications.'. $path . '.' . $id . '.message', $notification->message);
setting()->set('notifications.'. $path . '.' . $id . '.date', Date::now()); setting()->set([
setting()->set('notifications.'. $path . '.' . $id . '.status', '0'); $prefix . '.name' => $notification->name,
$prefix . '.message' => $notification->message,
$prefix . '.date' => Date::now(),
$prefix . '.status' => '0',
]);
setting()->save(); setting()->save();
break; break;
} }
} }
}
return response()->json([ return response()->json([
'message' => trans('messages.success.disabled', [ 'message' => trans('messages.success.disabled', [

View File

@ -2,8 +2,8 @@
namespace App\Http\Livewire\Common\Notifications; namespace App\Http\Livewire\Common\Notifications;
use Date;
use App\Traits\Modules; use App\Traits\Modules;
use App\Utilities\Date;
use Livewire\Component; use Livewire\Component;
class NewApps extends Component class NewApps extends Component
@ -19,19 +19,23 @@ class NewApps extends Component
continue; continue;
} }
$readed = $notification; $read = $notification;
} }
setting()->set('notifications.'. user()->id . '.' . $alias . '.name', $readed->name); $prefix = 'notifications.' . user()->id . '.' . $alias;
setting()->set('notifications.'. user()->id . '.' . $alias . '.message', $readed->alias);
setting()->set('notifications.'. user()->id . '.' . $alias . '.date', Date::now()); setting()->set([
setting()->set('notifications.'. user()->id . '.' . $alias . '.status', '0'); $prefix . '.name' => $read->name,
$prefix . '.message' => $read->alias,
$prefix . '.date' => Date::now(),
$prefix . '.status' => '0',
]);
setting()->save(); setting()->save();
$this->dispatchBrowserEvent('mark-read', [ $this->dispatchBrowserEvent('mark-read', [
'type' => 'new-apps', 'type' => 'new-apps',
'message' => trans('notifications.messages.mark_read', ['type' => $notification->name]), 'message' => trans('notifications.messages.mark_read', ['type' => $read->name]),
]); ]);
} }
@ -40,11 +44,14 @@ class NewApps extends Component
$notifications = $this->getNotifications('new-apps'); $notifications = $this->getNotifications('new-apps');
foreach ($notifications as $notification) { foreach ($notifications as $notification) {
setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.name', $notification->name); $prefix = 'notifications.' . user()->id . '.' . $notification->alias;
setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.message', $notification->alias);
setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.date', Date::now());
setting()->set('notifications.'. user()->id . '.' . $notification->alias . '.status', '0');
setting()->set([
$prefix . '.name' => $notification->name,
$prefix . '.message' => $notification->alias,
$prefix . '.date' => Date::now(),
$prefix . '.status' => '0',
]);
} }
setting()->save(); setting()->save();

View File

@ -2,8 +2,8 @@
namespace App\Http\ViewComposers; namespace App\Http\ViewComposers;
use App\Utilities\Versions;
use App\Traits\Modules; use App\Traits\Modules;
use App\Utilities\Versions;
use Illuminate\View\View; use Illuminate\View\View;
class Header class Header
@ -71,7 +71,6 @@ class Header
$new_apps = $this->getNotifications('new-apps'); $new_apps = $this->getNotifications('new-apps');
if ($new_apps) {
foreach ($new_apps as $key => $new_app) { foreach ($new_apps as $key => $new_app) {
if (setting('notifications.' . user()->id . '.' . $new_app->alias)) { if (setting('notifications.' . user()->id . '.' . $new_app->alias)) {
unset($new_apps[$key]); unset($new_apps[$key]);
@ -82,7 +81,6 @@ class Header
$notifications++; $notifications++;
} }
} }
}
if ($user->can('read-install-updates')) { if ($user->can('read-install-updates')) {
$updates = count(Versions::getUpdates()); $updates = count(Versions::getUpdates());

View File

@ -3,7 +3,7 @@
namespace App\Http\ViewComposers; namespace App\Http\ViewComposers;
use App\Traits\Modules; use App\Traits\Modules;
use Route; use Illuminate\Support\Facades\Route;
use Illuminate\View\View; use Illuminate\View\View;
class Notifications class Notifications
@ -29,9 +29,7 @@ class Notifications
$path = str_replace('{company_id}/', '', $path); $path = str_replace('{company_id}/', '', $path);
if (!$notifications = $this->getNotifications($path)) { $notifications = $this->getNotifications($path);
return;
}
// Push to a stack // Push to a stack
foreach ($notifications as $notification) { foreach ($notifications as $notification) {

View File

@ -396,7 +396,7 @@ trait Modules
return false; return false;
} }
public function getNotifications($path) public function getNotifications($path): array
{ {
$key = 'apps.notifications'; $key = 'apps.notifications';
@ -407,10 +407,10 @@ trait Modules
} }
if (!empty($data) && array_key_exists($path, $data)) { if (!empty($data) && array_key_exists($path, $data)) {
return $data[$path]; return (array) $data[$path];
} }
return false; return [];
} }
public function getPageNumberOfModules($data = []) public function getPageNumberOfModules($data = [])