From 56daee0582586ce4bc5307b86d0a2bf086b75cef Mon Sep 17 00:00:00 2001 From: Temuulen Bayanmunkh Date: Fri, 20 Oct 2023 18:04:36 +0800 Subject: [PATCH] working notifications --- .../Internal/v1/NotificationController.php | 15 +++++++++++ src/Http/Filter/NotificationFilter.php | 26 +++++++++++++++++++ src/Models/Notification.php | 6 ++--- src/Notifications/UserCreated.php | 21 +++++++-------- src/routes.php | 1 + 5 files changed, 54 insertions(+), 15 deletions(-) create mode 100644 src/Http/Controllers/Internal/v1/NotificationController.php create mode 100644 src/Http/Filter/NotificationFilter.php diff --git a/src/Http/Controllers/Internal/v1/NotificationController.php b/src/Http/Controllers/Internal/v1/NotificationController.php new file mode 100644 index 0000000..57947f1 --- /dev/null +++ b/src/Http/Controllers/Internal/v1/NotificationController.php @@ -0,0 +1,15 @@ +builder->where(function ($q) { + $q->where('notifiable_id', $this->session->get('company')); + $q->orWhere('notifiable_id', $this->session->get('user')); + }); + } + + public function query(?string $query) + { + $this->builder->search($query); + } + + public function unread(?bool $unread) + { + if ($unread) { + $this->builder->whereNull('read_at'); + } + } +} diff --git a/src/Models/Notification.php b/src/Models/Notification.php index 9d7ec18..595e847 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -2,21 +2,21 @@ namespace Fleetbase\Models; +use Fleetbase\Traits\Filterable; use Fleetbase\Traits\HasApiModelBehavior; use Fleetbase\Traits\Searchable; use Illuminate\Notifications\DatabaseNotification; class Notification extends DatabaseNotification { - use HasApiModelBehavior; - use Searchable; + use HasApiModelBehavior, Searchable, Filterable; /** * The attributes that are mass assignable. * * @var array */ - protected $fillable = ['read_at', 'data']; + protected $fillable = ['read_at', 'data', 'notifiable_id', 'notifiable_type', 'type']; /** * The searchable columns. diff --git a/src/Notifications/UserCreated.php b/src/Notifications/UserCreated.php index f4ee3b5..80b5e27 100644 --- a/src/Notifications/UserCreated.php +++ b/src/Notifications/UserCreated.php @@ -79,18 +79,15 @@ public function toBroadcast($notifiable) public function toArray($notifiable) { return [ - 'id' => $this->notificationId, - 'created_at' => $this->sentAt, - 'notifiable' => $notifiable->{$notifiable->getKeyName()}, - 'data' => [ - 'subject' => '🥳 New Fleetbase Signup!', - 'message' => 'New user ' . $this->user->name . ' added to organization ' . $this->company->name, - 'id' => $this->user->uuid, - 'email' => $this->user->email, - 'phone' => $this->user->phone, - 'companyId' => $this->company->uuid, - 'company' => $this->company->name, - ], + 'notification_id' => $this->notificationId, + 'sent_at' => $this->sentAt, + 'subject' => '🥳 New Fleetbase Signup!', + 'message' => 'New user ' . $this->user->name . ' added to organization ' . $this->company->name, + 'id' => $this->user->uuid, + 'email' => $this->user->email, + 'phone' => $this->user->phone, + 'companyId' => $this->company->uuid, + 'company' => $this->company->name, ]; } } diff --git a/src/routes.php b/src/routes.php index dae1ec4..74dea65 100644 --- a/src/routes.php +++ b/src/routes.php @@ -157,6 +157,7 @@ function ($router, $controller) { } ); $router->fleetbaseRoutes('transactions'); + $router->fleetbaseRoutes('notifications'); } ); }