Skip to content

Commit

Permalink
Redirect after accept consents and events & notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindRam-Ranium committed Feb 2, 2024
1 parent e022739 commit cfb9f22
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 69 deletions.
23 changes: 23 additions & 0 deletions config/filament-user-consent.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<?php

use Visualbuilder\FilamentUserConsent\Events\ConsentsUpdatedComplete;
use Visualbuilder\FilamentUserConsent\Events\ConsentUpdated;
use Visualbuilder\FilamentUserConsent\Listeners\NotifyConsentsUpdated;

return [

//To which authenticatable models consents should be applied?
Expand All @@ -15,6 +19,8 @@
'App\Models\EndUser' => 'EndUser',
],

'notify' => ['mail'],

Check failure on line 22 in config/filament-user-consent.php

View workflow job for this annotation

GitHub Actions / phpstan

Array has 2 duplicate keys with value 'notify' ('notify', 'notify').

'routes' => [
'prefix' => 'consent-options',
],
Expand All @@ -23,4 +29,21 @@
'sort' => 50,
'group' => 'Content',
],

'listeners' => [
//Event triggered after a consent updated
ConsentUpdated::class => [
// Default listeners for this event
// You may want to update mailchump if consent withdrawn for marketing
],
//Event triggered after all consents updated
ConsentsUpdatedComplete::class => [
NotifyConsentsUpdated::class
],
],

//send user an email with a copy of the consent after saving.
'notify' => ['mail'],

'email-template' => 'vendor.ekoukltd.laraconsent.layouts.email',
];
3 changes: 3 additions & 0 deletions src/FilamentUserConsentServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Visualbuilder\FilamentUserConsent\Commands\FilamentUserConsentCommand;
use Visualbuilder\FilamentUserConsent\Providers\EventServiceProvider;
use Visualbuilder\FilamentUserConsent\Testing\TestsFilamentUserConsent;

class FilamentUserConsentServiceProvider extends PackageServiceProvider
Expand Down Expand Up @@ -56,6 +57,8 @@ public function configurePackage(Package $package): void
if (file_exists($package->basePath('/../resources/views'))) {
$package->hasViews(static::$viewNamespace);
}

$this->app->register(EventServiceProvider::class);
}

public function packageRegistered(): void
Expand Down
17 changes: 12 additions & 5 deletions src/Http/Middleware/ForceRedirectToUnapprovedConsents.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,29 @@ class ForceRedirectToUnapprovedConsents
{
public function handle(Request $request, Closure $next)
{
$isConsentRoute = str_contains($request->route()->getName(), 'consent-options');
if (Auth::guard('admin')->check()) {
$guard = 'admin';
} else if (Auth::guard('enduser')->check()) {
$guard = 'enduser';
} else if (Auth::guard('practitioner')->check()) {
$guard = 'practitioner';
}

$isConsentRoute = str_contains($request->route()->getName(), 'consent-options');
if (
//must be logged in
Auth::user()
Auth::guard($guard)->user()

Check failure on line 24 in src/Http/Middleware/ForceRedirectToUnapprovedConsents.php

View workflow job for this annotation

GitHub Actions / phpstan

Variable $guard might not be defined.
//have the trait installed
&& method_exists(Auth::user(), 'hasRequiredConsents')
&& method_exists(Auth::guard($guard)->user(), 'hasRequiredConsents')

Check failure on line 26 in src/Http/Middleware/ForceRedirectToUnapprovedConsents.php

View workflow job for this annotation

GitHub Actions / phpstan

Variable $guard might not be defined.
//Not be a consent route
&& ! $isConsentRoute
//Not an ajax call
&& ! $request->ajax()
//Not have required consents signed
&& ! Auth::user()->hasRequiredConsents()
&& ! Auth::guard($guard)->user()->hasRequiredConsents()

Check failure on line 32 in src/Http/Middleware/ForceRedirectToUnapprovedConsents.php

View workflow job for this annotation

GitHub Actions / phpstan

Variable $guard might not be defined.
) {
//Save current request URL
// $request->session()->put('url.saved', $request->fullUrl());
$request->session()->put('url.saved', $request->fullUrl());
//Redirect user to ask for consent
return redirect()->route('consent-option-request');
}
Expand Down
20 changes: 20 additions & 0 deletions src/Listeners/LogConsentUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace Ekoukltd\LaraConsent\Listeners;

use Ekoukltd\LaraConsent\Events\ConsentUpdated;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class LogConsentUpdated
{
public function handle(ConsentUpdated $event)

Check failure on line 11 in src/Listeners/LogConsentUpdated.php

View workflow job for this annotation

GitHub Actions / phpstan

Parameter $event of method Ekoukltd\LaraConsent\Listeners\LogConsentUpdated::handle() has invalid type Ekoukltd\LaraConsent\Events\ConsentUpdated.
{
if (($event->consentOption->is_mandatory&&config('laraconsent.logging.mandatory'))

Check failure on line 13 in src/Listeners/LogConsentUpdated.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $consentOption on an unknown class Ekoukltd\LaraConsent\Events\ConsentUpdated.
||(!$event->consentOption->is_mandatory&&config('laraconsent.logging.optional'))

Check failure on line 14 in src/Listeners/LogConsentUpdated.php

View workflow job for this annotation

GitHub Actions / phpstan

Access to property $consentOption on an unknown class Ekoukltd\LaraConsent\Events\ConsentUpdated.
){
$status = $event->accepted?'accepted':'refused';
Log::info("Consent: ".$event->consentOption->key." $status by ".Auth::user()->email);
}
}
}
16 changes: 16 additions & 0 deletions src/Listeners/NotifyConsentsUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Visualbuilder\FilamentUserConsent\Listeners;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;
use Visualbuilder\FilamentUserConsent\Events\ConsentsUpdatedComplete;
use Visualbuilder\FilamentUserConsent\Notifications\ConsentsUpdatedNotification;

class NotifyConsentsUpdated
{
public function handle(ConsentsUpdatedComplete $event)
{
$event->user->notify(new ConsentsUpdatedNotification());
}
}
136 changes: 72 additions & 64 deletions src/Livewire/ConsentOptionRequset.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Infolists\Components\Actions;
use Filament\Infolists\Components\Actions\Action;
use Filament\Infolists\Components\Fieldset;
use Filament\Infolists\Components\Group;
use Filament\Infolists\Components\RepeatableEntry;
use Filament\Infolists\Components\Section;
Expand All @@ -19,6 +20,9 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Visualbuilder\FilamentUserConsent\Models\ConsentOption;
use Illuminate\Support\Facades\Validator;
use Visualbuilder\FilamentUserConsent\Events\ConsentsUpdatedComplete;
use Visualbuilder\FilamentUserConsent\Events\ConsentUpdated;

class ConsentOptionRequset extends SimplePage
{
Expand All @@ -41,7 +45,7 @@ public function mount(): void
$this->user = Auth::guard('practitioner')->user();
}

if (! $this->user) {
if (!$this->user) {
abort(403, 'Only authenticated users can set consent options');
}

Expand Down Expand Up @@ -73,47 +77,51 @@ public function infolist(Infolist $infolist): Infolist
return $infolist
->record($this->user)
->schema([
Section::make('User Info')
Fieldset::make('User Info')
->schema([
TextEntry::make('fullName'),
TextEntry::make('email'),
])
->columns(2),
RepeatableEntry::make('collections')
->label('Your Consent is required')
->schema([
Section::make(fn (ConsentOption $record) => "{$record->title} V{$record->version}")
->description(function (ConsentOption $record) {
$suffix = $this->previousConsents($record->key);
$mandatory = $record->is_mandatory ? 'Mandatory' : 'Optional';
if ($suffix) {
$mandatory .= " - ( $suffix )";
}

return $mandatory;
Fieldset::make('Your Consent is required')->schema([
RepeatableEntry::make('collections')
->label('')
->schema([
Section::make(fn (ConsentOption $record) => "{$record->title} V{$record->version}")
->description(function (ConsentOption $record) {
$suffix = $this->previousConsents($record->key);
$mandatory = $record->is_mandatory ? 'Mandatory' : 'Optional';
if ($suffix) {
$mandatory .= " - ( $suffix )";
}
return $mandatory;
})
->icon(fn (ConsentOption $record) => $record->is_mandatory ? 'heroicon-o-check-badge' : 'heroicon-o-question-mark-circle')
->iconColor(fn (ConsentOption $record) => $record->is_mandatory ? 'success' : 'info')
->schema([
TextEntry::make('text')->label('')
->markdown(),
Group::make()->schema([
ViewEntry::make('acceptConsent')
->label('')
->view('vendor.user-consent.infolists.components.consent-option-checkbox'),
TextEntry::make('updated_at')->label('Last Updated')
])->columns(2)
]),
])
->columns(2)
->columnSpanFull(),
Actions::make([
Action::make('saveConsents')
->icon('heroicon-o-check-circle')
->color('success')
->before(function (Action $action) {
$this->validateConsents($action);
})
->icon(fn (ConsentOption $record) => $record->is_mandatory ? 'heroicon-o-check-badge' : 'heroicon-o-question-mark-circle')
->iconColor(fn (ConsentOption $record) => $record->is_mandatory ? 'success' : 'info')
->schema([
TextEntry::make('text')->label('')
->markdown(),
Group::make()->schema([
ViewEntry::make('acceptConsent')
->label('')
->view('vendor.user-consent.infolists.components.consent-option-checkbox'),
TextEntry::make('updated_at')->label('Last Updated'),
])->columns(2),
]),
])
->columns(2)
->columnSpanFull(),
Actions::make([
Action::make('saveConsents')
->icon('heroicon-o-check-circle')
->color('success')
->action(function (array $data) {
$this->acceptConsent();
}),
->action(function (array $data) {
$this->acceptConsent();
})
]),
]),
])->columns(3);
}
Expand All @@ -127,44 +135,44 @@ public function previousConsents($key)
}
}

public function acceptConsent()
public function validateConsents($action)
{
$validateMandatoryConsents = $this->user->requiredOutstandingConsentsValidate($this->acceptConsents);

if (! $validateMandatoryConsents) {
if (!$validateMandatoryConsents) {
Notification::make()
->title('Please confirm.!')
->body('Please accept all required consent options.')
->icon('heroicon-o-check-circle')
->color('danger')
->send();
} else {

$outstandingConsents = $this->user->outstandingConsents();
foreach ($outstandingConsents as $consentOption) {
$this->user->consents()
->save(
$consentOption,
[
'accepted' => in_array($consentOption->id, $this->acceptConsents),
'key' => $consentOption->key,
]
);
// event(new ConsentUpdated($consentOption, $request->consent_option[ $consentOption->id ]));
}
Notification::make()
->title('Welcome.!')
->body('Your submitted all consent options are saved.')
->icon('heroicon-o-check-circle')
->color('success')
->send();

// event(new ConsentsUpdatedComplete($outstandingConsents, $user));
$action->cancel();
}
}

// return Redirect::intended(
// $request->session()
// ->get('url.saved')
// );
public function acceptConsent()
{
$outstandingConsents = $this->user->outstandingConsents();
foreach ($outstandingConsents as $consentOption) {
$this->user->consents()
->save(
$consentOption,
[
'accepted' => in_array($consentOption->id, $this->acceptConsents),
'key' => $consentOption->key
]
);
event(new ConsentUpdated($consentOption, in_array($consentOption->id, $this->acceptConsents)));
}
Notification::make()
->title('Welcome.!')
->body('Your submitted all consent options are saved.')
->icon('heroicon-o-check-circle')
->color('success')
->send();

event(new ConsentsUpdatedComplete($outstandingConsents, $this->user));

return redirect(request()->session()->get('url.saved'));
}
}
35 changes: 35 additions & 0 deletions src/Mail/ConsentsUpdatedMail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace Visualbuilder\FilamentUserConsent\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;

class ConsentsUpdatedMail extends Mailable
{
use Queueable, SerializesModels;


protected $user;

/**
* Create a new message instance.
*
* @return void
*/
public function __construct($user)
{
$this->user = $user;
}

/**
* Build the message.
*
* @return $this
*/
public function build()
{

}
}
Loading

0 comments on commit cfb9f22

Please sign in to comment.