From eb9773b59c8b48e981636d9a100d830cd48a5c03 Mon Sep 17 00:00:00 2001 From: TemuulenBM Date: Wed, 18 Oct 2023 17:36:32 +0800 Subject: [PATCH] Adding notifications to fleetbase --- ...0_18_080950_create_notifications_table.php | 35 +++++++++++ src/Models/Notification.php | 27 +++++++++ src/Models/User.php | 10 ++++ src/Notifications/UserCreated.php | 59 ++++++++++++++++--- src/Observers/NotificationObserver.php | 9 +++ src/Observers/UserObserver.php | 6 +- src/Providers/CoreServiceProvider.php | 1 + 7 files changed, 137 insertions(+), 10 deletions(-) create mode 100644 migrations/2023_10_18_080950_create_notifications_table.php create mode 100644 src/Models/Notification.php create mode 100644 src/Observers/NotificationObserver.php diff --git a/migrations/2023_10_18_080950_create_notifications_table.php b/migrations/2023_10_18_080950_create_notifications_table.php new file mode 100644 index 0000000..5f8c2dd --- /dev/null +++ b/migrations/2023_10_18_080950_create_notifications_table.php @@ -0,0 +1,35 @@ +uuid('id')->primary(); + $table->string('type'); + $table->uuidMorphs('notifiable'); + $table->text('data'); + $table->timestamp('read_at')->nullable(); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('notifications'); + } +} diff --git a/src/Models/Notification.php b/src/Models/Notification.php new file mode 100644 index 0000000..9d7ec18 --- /dev/null +++ b/src/Models/Notification.php @@ -0,0 +1,27 @@ +message']; +} diff --git a/src/Models/User.php b/src/Models/User.php index 8ea78af..d61a7d9 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -477,6 +477,16 @@ public function routeNotificationForTwilio() return $this->phone; } + /** + * The channels the user receives notification broadcasts on. + * + * @return string + */ + public function receivesBroadcastNotificationsOn() + { + return 'user.' . $this->uuid; + } + /** * Accessor to get the types associated with the model instance. * diff --git a/src/Notifications/UserCreated.php b/src/Notifications/UserCreated.php index a2f1e02..f4ee3b5 100644 --- a/src/Notifications/UserCreated.php +++ b/src/Notifications/UserCreated.php @@ -5,16 +5,20 @@ use Fleetbase\Models\Company; use Fleetbase\Models\User; use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; +// use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Notifications\Messages\MailMessage; +use Illuminate\Notifications\Messages\BroadcastMessage; use Illuminate\Notifications\Notification; +use Illuminate\Support\Carbon; -class UserCreated extends Notification implements ShouldQueue +class UserCreated extends Notification { use Queueable; public ?User $user; public ?Company $company; + public ?string $sentAt; + public ?string $notificationId; /** * Create a new notification instance. @@ -25,6 +29,8 @@ public function __construct(User $user, Company $company) { $this->user = $user; $this->company = $company; + $this->sentAt = Carbon::now()->toDateTimeString(); + $this->notificationId = uniqid('notification_'); } /** @@ -34,7 +40,7 @@ public function __construct(User $user, Company $company) */ public function via($notifiable) { - return ['mail']; + return ['mail', 'database', 'broadcast']; } /** @@ -45,11 +51,46 @@ public function via($notifiable) public function toMail($notifiable) { return (new MailMessage()) - ->subject('🥳 New Fleetbase Signup!') - ->line('View user details below.') - ->line('Name: ' . $this->user->name) - ->line('Email: ' . $this->user->email) - ->line('Phone: ' . $this->user->phone) - ->line('Company: ' . $this->company->name); + ->subject('🥳 New Fleetbase Signup!') + ->line('View user details below.') + ->line('Name: ' . $this->user->name) + ->line('Email: ' . $this->user->email) + ->line('Phone: ' . $this->user->phone) + ->line('Company: ' . $this->company->name); + } + + /** + * Get the broadcastable representation of the notification. + * + * @param mixed $notifiable + * @return \Illuminate\Notifications\Messages\BroadcastMessage + */ + public function toBroadcast($notifiable) + { + return new BroadcastMessage($this->toArray($notifiable)); + } + + /** + * Get the array representation of the notification. + * + * @param mixed $notifiable + * @return array + */ + 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, + ], + ]; } } diff --git a/src/Observers/NotificationObserver.php b/src/Observers/NotificationObserver.php new file mode 100644 index 0000000..e5429d1 --- /dev/null +++ b/src/Observers/NotificationObserver.php @@ -0,0 +1,9 @@ +load(['company']); + $user->load(['company.owner']); // create company user record if ($user->company_uuid) { @@ -25,6 +26,9 @@ public function created(User $user) // invite user to join company $user->sendInviteFromCompany(); + + // Notify the company owner a user has been created + $user->company->owner->notify(new UserCreated($user, $user->company)); } /** diff --git a/src/Providers/CoreServiceProvider.php b/src/Providers/CoreServiceProvider.php index 7e9c0f3..79429f9 100644 --- a/src/Providers/CoreServiceProvider.php +++ b/src/Providers/CoreServiceProvider.php @@ -26,6 +26,7 @@ class CoreServiceProvider extends ServiceProvider public $observers = [ \Fleetbase\Models\User::class => \Fleetbase\Observers\UserObserver::class, \Fleetbase\Models\ApiCredential::class => \Fleetbase\Observers\ApiCredentialObserver::class, + \Fleetbase\Models\Notification::class => \Fleetbase\Observers\NotificationObserver::class, \Spatie\Activitylog\Models\Activity::class => \Fleetbase\Observers\ActivityObserver::class, ];