From 12d48fe5027a1d6109100088cbbe391a75722789 Mon Sep 17 00:00:00 2001 From: "R. Aravindan" Date: Wed, 3 Apr 2024 18:29:37 +0530 Subject: [PATCH] fetch survey consent options based on product, category and organisations --- .../create_consent_options_table.php.stub | 1 + src/Models/ConsentOption.php | 4 +- src/Resources/ConsentOptionResource.php | 6 +++ src/Traits/HasConsent.php | 52 ++++++++++++++++++- 4 files changed, 60 insertions(+), 3 deletions(-) diff --git a/database/migrations/create_consent_options_table.php.stub b/database/migrations/create_consent_options_table.php.stub index 417a04a..9e2fee5 100644 --- a/database/migrations/create_consent_options_table.php.stub +++ b/database/migrations/create_consent_options_table.php.stub @@ -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); diff --git a/src/Models/ConsentOption.php b/src/Models/ConsentOption.php index 03b5dd8..ad6bf5c 100644 --- a/src/Models/ConsentOption.php +++ b/src/Models/ConsentOption.php @@ -50,6 +50,7 @@ class ConsentOption extends Model 'title', 'label', 'text', + 'is_survey', 'fields', 'is_mandatory', 'force_user_update', @@ -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(); } diff --git a/src/Resources/ConsentOptionResource.php b/src/Resources/ConsentOptionResource.php index 2fff778..7adf829 100644 --- a/src/Resources/ConsentOptionResource.php +++ b/src/Resources/ConsentOptionResource.php @@ -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(), @@ -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(), diff --git a/src/Traits/HasConsent.php b/src/Traits/HasConsent.php index 901dca3..8d9c27f 100644 --- a/src/Traits/HasConsent.php +++ b/src/Traits/HasConsent.php @@ -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(); @@ -62,7 +67,7 @@ public function requiredOutstandingConsentsValidate($acceptedConsents): bool */ public function outstandingConsents() { - return ConsentOption::findbykeys($this->requiredConsentKeys()) + $consents = ConsentOption::findbykeys($this->requiredConsentKeys()) ->whereNotIn( 'id', $this->consents() @@ -70,8 +75,52 @@ public function outstandingConsents() ->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(); } /** @@ -83,7 +132,6 @@ public function consents() ->withTimestamps() ->withPivot('accepted') ->using(ConsentOptionUser::class); - } public function lastConsentByKey($key)