Skip to content

Commit

Permalink
validations & save consent options
Browse files Browse the repository at this point in the history
  • Loading branch information
AravindRam-Ranium committed Feb 20, 2024
1 parent e9f2fa1 commit 4c3afba
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
1 change: 1 addition & 0 deletions database/migrations/create_consentables_table.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ return new class extends Migration
$table->string('consentable_type');
$table->string('key')->default(NULL)->nullable();
$table->boolean('accepted')->default(0);
$table->json('fields')->nullable();
$table->timestamps();
});
}
Expand Down
28 changes: 28 additions & 0 deletions src/Infolists/Components/ConsentAcceptForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Visualbuilder\FilamentUserConsent\Infolists\Components;

use Filament\Infolists\Components\Entry;

class ConsentAcceptForm extends Entry
{
protected string $view = 'user-consent::infolists.components.consent-accept-form';

public $errorBag = [];

protected $rules = [
'name' => 'required|min:6',
'email' => 'required|email',
];

public function updated($propertyName)
{
$this->validateOnly($propertyName);

Check failure on line 20 in src/Infolists/Components/ConsentAcceptForm.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Visualbuilder\FilamentUserConsent\Infolists\Components\ConsentAcceptForm::validateOnly().
}

public function submit()
{
$validatedData = $this->validate();

Check failure on line 25 in src/Infolists/Components/ConsentAcceptForm.php

View workflow job for this annotation

GitHub Actions / phpstan

Call to an undefined method Visualbuilder\FilamentUserConsent\Infolists\Components\ConsentAcceptForm::validate().

}
}
32 changes: 29 additions & 3 deletions src/Livewire/ConsentOptionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\HtmlString;
use Visualbuilder\FilamentUserConsent\Infolists\Components\ConsentAcceptForm;
use Visualbuilder\FilamentUserConsent\Models\ConsentOption;
use Visualbuilder\FilamentUserConsent\Notifications\ConsentsUpdatedNotification;

Expand Down Expand Up @@ -100,6 +101,7 @@ public function infolist(Infolist $infolist): Infolist
ViewEntry::make('acceptConsent')
->label('')
->view('user-consent::infolists.components.consent-option-checkbox'),
// ConsentAcceptForm::make('Agreement Info'),
TextEntry::make('updated_at')
->label('')
->alignEnd()
Expand Down Expand Up @@ -146,9 +148,25 @@ public function previousConsents($key)

public function validateConsents($action)
{
$validateMandatoryConsents = $this->user->requiredOutstandingConsentsValidate($this->acceptConsents);
$conentIds = [];
$errorBags = [];
foreach($this->acceptConsents as $key => $value) {
if((bool)$value['accepted']) {
$conentIds[] = $key;
}
$consentOption = ConsentOption::find($key);
if((bool)$consentOption->additional_info) {
foreach ($consentOption->fields as $field) {
if((bool)$field['required'] && (!isset($value[$field['name']]) || $value[$field['name']] == "")) {
$errorBags[$key][$field['name']] = $field['name']." field is required";
}
}
}
}

$validateMandatoryConsents = $this->user->requiredOutstandingConsentsValidate($conentIds);

if (! $validateMandatoryConsents) {
if (! $validateMandatoryConsents || count($errorBags) > 0) {
Notification::make()
->title('Please confirm.!')
->body('Please accept all required consent options.')
Expand All @@ -161,14 +179,22 @@ public function validateConsents($action)

public function acceptConsent()
{
$conentIds = [];
foreach($this->acceptConsents as $key => $value) {
if((bool)$value['accepted']) {
$conentIds[] = $key;
}
}

$outstandingConsents = $this->user->outstandingConsents();
foreach ($outstandingConsents as $consentOption) {
$this->user->consents()
->save(
$consentOption,
[
'accepted' => in_array($consentOption->id, $this->acceptConsents),
'accepted' => in_array($consentOption->id, $conentIds),
'key' => $consentOption->key,
'fields' => (bool)$consentOption->additional_info ? $this->acceptConsents[$consentOption->id] : []
]
);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Resources/ConsentOptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ public static function form(Form $form): Form
Section::make('Additional Info')->schema([
Repeater::make('fields')->label('')
->schema([
Forms\Components\TextInput::make('name')->required(),
Forms\Components\TextInput::make('name')
->regex('/^[a-z_]+$/')
->required(),
Forms\Components\Select::make('type')
->options([
'text' => 'Text Input',
Expand Down

0 comments on commit 4c3afba

Please sign in to comment.