Skip to content

Commit

Permalink
Adding notifications to fleetbase
Browse files Browse the repository at this point in the history
  • Loading branch information
TemuulenBM committed Oct 18, 2023
1 parent 0b0fb9c commit eb9773b
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 10 deletions.
35 changes: 35 additions & 0 deletions migrations/2023_10_18_080950_create_notifications_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNotificationsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notifications', function (Blueprint $table) {
$table->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');
}
}
27 changes: 27 additions & 0 deletions src/Models/Notification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Fleetbase\Models;

use Fleetbase\Traits\HasApiModelBehavior;
use Fleetbase\Traits\Searchable;
use Illuminate\Notifications\DatabaseNotification;

class Notification extends DatabaseNotification
{
use HasApiModelBehavior;
use Searchable;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['read_at', 'data'];

/**
* The searchable columns.
*
* @var array
*/
protected $searchableColumns = ['data->message'];
}
10 changes: 10 additions & 0 deletions src/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
59 changes: 50 additions & 9 deletions src/Notifications/UserCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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_');
}

/**
Expand All @@ -34,7 +40,7 @@ public function __construct(User $user, Company $company)
*/
public function via($notifiable)
{
return ['mail'];
return ['mail', 'database', 'broadcast'];
}

/**
Expand All @@ -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,
],
];
}
}
9 changes: 9 additions & 0 deletions src/Observers/NotificationObserver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Fleetbase\Observers;

use Fleetbase\Models\Notification;

class NotificationObserver
{
}
6 changes: 5 additions & 1 deletion src/Observers/UserObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Fleetbase\Models\Company;
use Fleetbase\Models\CompanyUser;
use Fleetbase\Models\User;
use Fleetbase\Notifications\UserCreated;

class UserObserver
{
Expand All @@ -16,7 +17,7 @@ class UserObserver
public function created(User $user)
{
// load user company
$user->load(['company']);
$user->load(['company.owner']);

// create company user record
if ($user->company_uuid) {
Expand All @@ -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));
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/Providers/CoreServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];

Expand Down

0 comments on commit eb9773b

Please sign in to comment.