Skip to content

Commit

Permalink
created notification registry
Browse files Browse the repository at this point in the history
  • Loading branch information
TemuulenBM committed Oct 26, 2023
1 parent 56daee0 commit 7684050
Show file tree
Hide file tree
Showing 6 changed files with 207 additions and 2 deletions.
104 changes: 103 additions & 1 deletion src/Http/Controllers/Internal/v1/NotificationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
namespace Fleetbase\Http\Controllers\Internal\v1;

use Fleetbase\Http\Controllers\FleetbaseController;
use Fleetbase\Models\Notification;
use Illuminate\Http\Request;

/**
* Controller for managing notifications.
*/
class NotificationController extends FleetbaseController
{
/**
Expand All @@ -12,4 +17,101 @@ class NotificationController extends FleetbaseController
* @var string
*/
public $resource = 'notification';
}

/**
* Receives an array of ID's for notifications which should be marked as read.
*
* @param \Illuminate\Http\Request $request The HTTP request object.
* @return \Illuminate\Http\Response The HTTP response.
*/
public function markAsRead(Request $request)
{
$notifications = $request->input('notifications');
$total = count($notifications);
$read = [];

foreach ($notifications as $id) {
$notification = Notification::where('id', $id)->first();

if ($notification) {
$read[] = $notification->markAsRead();
}
}

return response()->json([
'status' => 'ok',
'message' => 'Notifications marked as read',
'marked_as_read' => count($read),
'total' => $total
]);
}

/**
* Receives an array of ID's for notifications which should be marked as read.
*
* @return \Illuminate\Http\Response The HTTP response.
*/
public function markAllAsRead()
{
$notifications = Notification::where('notifiable_id', session('user'))->get();

foreach ($notifications as $notification) {
$notification->markAsRead();
}

return response()->json([
'status' => 'ok',
'message' => 'All notifications marked as read',
]);
}

/**
* Deletes a single notification.
*
* @param int $notificationId The ID of the notification to delete.
* @return \Illuminate\Http\Response The HTTP response.
*/
public function deleteNotification($notificationId)
{
$notification = Notification::find($notificationId);

if ($notification) {
$notification->deleteNotification();
return response()->json(['message' => 'Notification deleted successfully'], 200);
} else {
return response()->json(['error' => 'Notification not found'], 404);
}
}

/**
* Deletes all notifications for the authenticated user.
*
* @param \Illuminate\Http\Request $request The HTTP request object.
* @return \Illuminate\Http\Response The HTTP response.
*/
public function bulkDelete(Request $request)
{
$notifications = $request->input('notifications');

if (empty($notifications)) {
Notification::where('notifiable_id', session('user'))->delete();
} else {
Notification::whereIn('id', $notifications)->delete();
}

return response()->json([
'status' => 'ok',
'message' => 'Selected notifications deleted successfully',
]);
}

/**
* Get the list of registered notifications from the NotificationRegistry.
*
* @return \Illuminate\Http\JsonResponse The JSON response containing registered notifications.
*/
public function registry()
{
return response()->json(\Fleetbase\Notifications\NotificationRegistry::$notifications);
}
}
27 changes: 27 additions & 0 deletions src/Models/Notification.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,31 @@ class Notification extends DatabaseNotification
* @var array
*/
protected $searchableColumns = ['data->message'];

/**
* Marks the notification as read.
*
* @param boolean $save
* @return \Fleetbase\Models\Notification
*/
public function markAsRead($save = true): Notification
{
$this->read_at = now();

if ($save) {
$this->save();
}

return $this;
}
/**
* Delete the notification.
*
* @return void
*/
public function deleteNotification()
{
$this->delete();
}

}
61 changes: 61 additions & 0 deletions src/Notifications/NotificationRegistry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Fleetbase\Notifications;

/**
* Notification Registry for managing registered notifications.
*/
class NotificationRegistry
{
/**
* Array to store registered notifications.
*
* @var array
*/
public static $notifications = [];

/**
* Register a notification.
*
* @param string|array $notificationClass The class or an array of classes to register.
* @param string $name The name of the notification.
* @param string $description The description of the notification.
* @param string $package The package to which the notification belongs.
* @param array $options Additional options for the notification.
*/
public static function register($notificationClass): void
{
if (is_array($notificationClass)) {
foreach ($notificationClass as $notificationClassElement) {
static::register($notificationClassElement);
}

return;
}

static::$notifications[] = [
'definition' => $notificationClass,
'name' => static::getNotificationClassProperty($notificationClass, 'name'),
'description' => static::getNotificationClassProperty($notificationClass, 'description'),
'package' => static::getNotificationClassProperty($notificationClass, 'package'),
'options' => static::getNotificationClassProperty($notificationClass, 'notificationOptions'),
];
}

/**
* Get a property of a notification class.
*
* @param string $notificationClass The class name.
* @param string $property The name of the property to retrieve.
* @return mixed|null The value of the property or null if not found.
*/
private static function getNotificationClassProperty(string $notificationClass, string $property)
{
if (!class_exists($notificationClass) || !property_exists($notificationClass, $property)) {
return null;
}

$properties = get_class_vars($notificationClass);
return data_get($properties, $property);
}
}
3 changes: 3 additions & 0 deletions src/Notifications/UserCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class UserCreated extends Notification
public ?Company $company;
public ?string $sentAt;
public ?string $notificationId;
public static string $name = 'User Created';
public static string $description = 'Notify when a new user has been created.';
public static string $package = 'core';

/**
* Create a new notification instance.
Expand Down
7 changes: 7 additions & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fleetbase\Providers;

use Fleetbase\Models\Setting;
use Fleetbase\Notifications\NotificationRegistry;
use Fleetbase\Support\Expansion;
use Fleetbase\Support\Utils;
use Illuminate\Console\Scheduling\Schedule;
Expand Down Expand Up @@ -78,6 +79,7 @@ public function boot()
$this->registerObservers();
$this->registerExpansionsFrom();
$this->registerMiddleware();
$this->registerNotifications();
$this->loadRoutesFrom(__DIR__ . '/../routes.php');
$this->loadMigrationsFrom(__DIR__ . '/../../migrations');
$this->mergeConfigFrom(__DIR__ . '/../../config/database.connections.php', 'database.connections');
Expand Down Expand Up @@ -344,6 +346,11 @@ function ($ns) use ($className) {
}
}

private function registerNotifications()
{
NotificationRegistry::register(\Fleetbase\Notifications\UserCreated::class);
}

/**
* Register the middleware groups defined by the service provider.
*/
Expand Down
7 changes: 6 additions & 1 deletion src/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,12 @@ function ($router, $controller) {
}
);
$router->fleetbaseRoutes('transactions');
$router->fleetbaseRoutes('notifications');
$router->fleetbaseRoutes('notifications', function ($router, $controller) {
$router->get('registry', $controller('registry'));
$router->put('mark-as-read', $controller('markAsRead'));
$router->put('mark-all-read', $controller('markAllAsRead'));
$router->delete('bulk-delete', $controller('bulkDelete'));
});
}
);
}
Expand Down

0 comments on commit 7684050

Please sign in to comment.