diff --git a/app/Http/Controllers/Api/NotificationController.php b/app/Http/Controllers/Api/NotificationController.php index eaf180442..f2ec45f7a 100644 --- a/app/Http/Controllers/Api/NotificationController.php +++ b/app/Http/Controllers/Api/NotificationController.php @@ -28,6 +28,21 @@ public function update(Request $request, $id) $notification->markAsRead(); } + /** + * Mark all notification as read. + * + * @api {put} notifications/all Mark all notifications as read + * @apiGroup Notifications + * @apiName UpdateReadNotifications + * @apiPermission user + * + * @param Request $request + */ + public function updateAll(Request $request) + { + $request->user()->unreadNotifications()->eachById(fn ($n) => $n->markAsRead()); + } + /** * Delete a read notification. * diff --git a/resources/assets/js/core/api/notifications.js b/resources/assets/js/core/api/notifications.js index ed94f8038..f4a89c12f 100644 --- a/resources/assets/js/core/api/notifications.js +++ b/resources/assets/js/core/api/notifications.js @@ -14,5 +14,9 @@ * @type {Vue.resource} */ export default Vue.resource('api/v1/notifications{/id}', {}, { - markRead: {method: 'PUT'} + markRead: {method: 'PUT'}, + markReadAll: { + method: 'PUT', + url: 'api/v1/notifications/all'} }); + diff --git a/resources/assets/js/core/notifications/list.vue b/resources/assets/js/core/notifications/list.vue index bb019462d..51b749b44 100644 --- a/resources/assets/js/core/notifications/list.vue +++ b/resources/assets/js/core/notifications/list.vue @@ -63,6 +63,7 @@ export default { data() { return { notifications: [], + isLoading: false, }; }, computed: { @@ -73,6 +74,22 @@ export default { return Store.countUnread > 0; }, }, + methods:{ + markAllAsRead() { + this.isLoading = true; + return NotificationsApi.markReadAll({}, {}) + .then(() => { + this.notifications.map(item => { + item.read_at = new Date(); + Store.remove(item.id); + }); + }) + .catch(Messages.handleErrorResponse) + .finally(() => { + this.isLoading = false; + }); + } + }, created() { Store.initialize(); this.notifications = Store.all; diff --git a/resources/views/notifications/index.blade.php b/resources/views/notifications/index.blade.php index 25430efe7..cea6cfacd 100644 --- a/resources/views/notifications/index.blade.php +++ b/resources/views/notifications/index.blade.php @@ -19,6 +19,11 @@
+ @if (!$all) +

+ +

+ @endif
diff --git a/routes/api.php b/routes/api.php index c24cb026c..1c4997bc0 100644 --- a/routes/api.php +++ b/routes/api.php @@ -143,6 +143,8 @@ 'parameters' => ['media-types' => 'id'], ]); +$router->put('notifications/all', 'NotificationController@updateAll'); + $router->resource('notifications', 'NotificationController', [ 'only' => ['update', 'destroy'], ]); diff --git a/tests/php/Http/Controllers/Api/NotificationControllerTest.php b/tests/php/Http/Controllers/Api/NotificationControllerTest.php index babedae0d..e8310f3c0 100644 --- a/tests/php/Http/Controllers/Api/NotificationControllerTest.php +++ b/tests/php/Http/Controllers/Api/NotificationControllerTest.php @@ -47,4 +47,19 @@ public function testDestroy() ->assertStatus(200); $this->assertEquals(0, $user->notifications()->count()); } + + public function testUpdateAll() + { + $user = UserTest::create(); + $user->notify(new InAppNotification('test', 'test')); + $user->notify(new InAppNotification('test', 'test')); + $user->notify(new InAppNotification('test', 'test')); + + $this->doTestApiRoute('PUT', '/api/v1/notifications/all'); + + $this->be($user); + $this->assertEquals(3, $user->unreadNotifications()->count()); + $this->put('/api/v1/notifications/all')->assertSuccessful(); + $this->assertEquals(0, $user->unreadNotifications()->count()); + } }