Skip to content

Commit

Permalink
fetch survey consent options based on product, category and organisat…
Browse files Browse the repository at this point in the history
…ions
  • Loading branch information
AravindRam-Ranium committed Apr 3, 2024
1 parent e9591b6 commit 12d48fe
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 3 deletions.
1 change: 1 addition & 0 deletions database/migrations/create_consent_options_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('title')->nullable();
$table->string('label')->nullable();
$table->text('text')->nullable();
$table->boolean('is_survey')->default(false);
$table->string('additional_info_title')->nullable();
$table->boolean('is_mandatory')->default(0);
$table->boolean('is_current')->default(0);
Expand Down
4 changes: 3 additions & 1 deletion src/Models/ConsentOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class ConsentOption extends Model
'title',
'label',
'text',
'is_survey',
'fields',
'is_mandatory',
'force_user_update',
Expand Down Expand Up @@ -143,12 +144,13 @@ public static function modelBasename($model)
return substr($model, strrpos($model, '\\') + 1);
}

public static function getAllActiveKeysbyUserClass($className): array
public static function getAllActiveKeysbyUserClass($className, $survey = false): array
{
return self::where('models', 'like', "%$className%")
->where('is_current', true)
->where('enabled', true)
->where('published_at', '<=', \Illuminate\Support\Carbon::now())
->where('is_survey', $survey)
->pluck('key')
->toArray();
}
Expand Down
6 changes: 6 additions & 0 deletions src/Resources/ConsentOptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public static function form(Form $form): Form
Forms\Components\Toggle::make('enabled')
->label('Enable this contract')
->required(),
Forms\Components\Toggle::make('is_survey')
->label('Is this survey consent?')
->required(),
Forms\Components\Toggle::make('is_mandatory')
->required(),

Expand Down Expand Up @@ -143,6 +146,9 @@ public static function table(Table $table): Table
Tables\Columns\IconColumn::make('is_mandatory')
->boolean()
->sortable(),
Tables\Columns\IconColumn::make('is_survey')
->boolean()
->sortable(),
Tables\Columns\IconColumn::make('is_current')
->boolean()
->sortable(),
Expand Down
52 changes: 50 additions & 2 deletions src/Traits/HasConsent.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public function requiredConsentKeys(): array
return ConsentOption::getAllActiveKeysbyUserClass(class_basename($this));
}

public function requiredConsentSurveyKeys(): array
{
return ConsentOption::getAllActiveKeysbyUserClass(class_basename($this), true);
}

public function outstandingConsentValidators()
{
$consents = $this->outstandingConsents();
Expand Down Expand Up @@ -62,16 +67,60 @@ public function requiredOutstandingConsentsValidate($acceptedConsents): bool
*/
public function outstandingConsents()
{
return ConsentOption::findbykeys($this->requiredConsentKeys())
$consents = ConsentOption::findbykeys($this->requiredConsentKeys())
->whereNotIn(
'id',
$this->consents()
->pluck('consent_options.id')
->toArray()
)
->orderBy('sort_order')
->where('is_survey', false)
->get();
$allConsents = $consents->merge($this->getSurveyConsents());
return $allConsents;
}

public function getSurveyConsents()
{
$user = auth()->user();
return ConsentOption::findbykeys($this->requiredConsentSurveyKeys())
->whereNotIn(
'id',
$this->consents()
->pluck('consent_options.id')
->toArray()
)
->whereExists(function ($orgQuery) use ($user) {
$orgQuery->from('consent_option_organisation')
->whereColumn('consent_option_organisation.consent_option_id', 'consent_options.id')
->whereExists(function($orderQuery) use($user){
$orderQuery->from('orders')
->where('orders.end_user_id', $user->id)
->whereColumn('orders.organisation_id', 'consent_option_organisation.organisation_id');
});
})
->orWhereExists(function($proQuery) use($user) {
$proQuery->from('consent_option_product')
->whereColumn('consent_option_product.consent_option_id', 'consent_options.id')
->whereExists(function($orderQuery) use($user){
$orderQuery->from('orders')->where('orders.end_user_id', $user->id)
->join('line_items', 'orders.id', 'line_items.order_id')
->whereColumn('line_items.product_id', 'consent_option_product.product_id');
});
})
->orWhereExists(function($catQuery) use($user) {
$catQuery->from('consent_option_product_category')
->whereColumn('consent_option_product_category.consent_option_id', 'consent_options.id')
->whereExists(function($orderQuery) use($user){
$orderQuery->from('orders')->where('orders.end_user_id', $user->id)
->join('line_items', 'orders.id', 'line_items.order_id')
->join('products', 'line_items.product_id', 'products.id')
->whereColumn('products.product_category_id', 'consent_option_product_category.product_category_id');
});
})
->orderBy('sort_order')
->get();
}

/**
Expand All @@ -83,7 +132,6 @@ public function consents()
->withTimestamps()
->withPivot('accepted')
->using(ConsentOptionUser::class);

}

public function lastConsentByKey($key)
Expand Down

0 comments on commit 12d48fe

Please sign in to comment.