Skip to content

Commit

Permalink
Add Real Time notification
Browse files Browse the repository at this point in the history
  • Loading branch information
AlpetGexha committed Jun 28, 2023
1 parent 09f888b commit 61ea0f0
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 17 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,3 @@ Password: admin
Email : [email protected]
Password: user
```

Change the email configuration to get all the feature
```bash
MAIL_MAILER=smtp
MAIL_HOST=mailpit
MAIL_PORT=1025
MAIL_USERNAME=null
MAIL_PASSWORD=null
MAIL_ENCRYPTION=null
```
28 changes: 25 additions & 3 deletions app/Actions/FilamentCompanies/InviteCompanyEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
use Wallo\FilamentCompanies\FilamentCompanies;
use Wallo\FilamentCompanies\Mail\CompanyInvitation;
use Wallo\FilamentCompanies\Rules\Role;
use Filament\Notifications\Notification;
use Filament\Notifications\Actions\Action;
use Illuminate\Support\Facades\URL;


class InviteCompanyEmployee implements InvitesCompanyEmployees
{
Expand All @@ -37,7 +41,25 @@ public function invite(User $user, Company $company, string $email, string|null
'role' => $role,
]);

Mail::to($email)->send(new CompanyInvitation($invitation));
if (config('filament-companies.employee_invite.email'))
Mail::to($email)->send(new CompanyInvitation($invitation));

if (config('filament-companies.employee_invite.notification')) {
Notification::make()
->title('Join Our Team')
->body('You have been invited to join our company: ' . $invitation->company->name)
->icon('heroicon-o-user-add')
->actions(
[
Action::make('JOIN!')
// url with signedRoute
->url(URL::signedRoute('company-invitations.accept', [
'invitation' => $invitation,
])),
]
)
->sendToDatabase(User::where('email', $email)->first());
}
}

/**
Expand Down Expand Up @@ -70,8 +92,8 @@ protected function rules(Company $company): array
}),
],
'role' => FilamentCompanies::hasRoles()
? ['required', 'string', new Role]
: null,
? ['required', 'string', new Role]
: null,
]);
}

Expand Down
18 changes: 16 additions & 2 deletions app/Actions/FilamentCompanies/RemoveCompanyEmployee.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Facades\Gate;
use Illuminate\Validation\ValidationException;
use Filament\Notifications\Notification;
use Wallo\FilamentCompanies\Contracts\RemovesCompanyEmployees;
use Wallo\FilamentCompanies\Events\CompanyEmployeeRemoved;

Expand All @@ -26,6 +27,17 @@ public function remove(User $user, Company $company, User $companyEmployee): voi
$company->removeUser($companyEmployee);

CompanyEmployeeRemoved::dispatch($company, $companyEmployee);

if (config('filament-companies.employee_invite.notification')) {
Notification::make()
->title('You have been removed from a team')
->message('You have been removed from a team.')
->sendToDatabase($companyEmployee);
}

// if ($companyEmployee->id === $user->id) {
// auth()->logout();
// }
}

/**
Expand All @@ -35,8 +47,10 @@ public function remove(User $user, Company $company, User $companyEmployee): voi
*/
protected function authorize(User $user, Company $company, User $companyEmployee): void
{
if (! Gate::forUser($user)->check('removeCompanyEmployee', $company) &&
$user->id !== $companyEmployee->id) {
if (
!Gate::forUser($user)->check('removeCompanyEmployee', $company) &&
$user->id !== $companyEmployee->id
) {
throw new AuthorizationException;
}
}
Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"bezhansalleh/filament-shield": "^2.4",
"cms-multi/filament-clear-cache": "^1.0",
"filament/filament": "^2.17",
"filament/notifications": "^2.17",
"flowframe/laravel-trend": "^0.1.5",
"guzzlehttp/guzzle": "^7.2",
"husam-tariq/filament-database-schedule": "^1.0",
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions config/filament-companies.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,9 @@

'profile_photo_disk' => 'public',

'employee_invite' => [
'email' => false,
'notification' => true,
]

];
2 changes: 1 addition & 1 deletion config/filament.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
*/

'database_notifications' => [
'enabled' => false,
'enabled' => true,
'polling_interval' => '30s',
],

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('notifications', function (Blueprint $table) {
$table->uuid('id')->primary();
$table->string('type');
$table->morphs('notifiable');
$table->text('data');
$table->timestamp('read_at')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('notifications');
}
};

0 comments on commit 61ea0f0

Please sign in to comment.