Add filtering for participant index
continuous-integration/drone/push Build was killed Details

This commit is contained in:
philipp lang 2024-12-12 00:30:59 +01:00
parent 82baf67a73
commit 6a91e857d2
13 changed files with 113 additions and 35 deletions

View File

@ -8,7 +8,6 @@ use App\Form\Resources\ParticipantResource;
use App\Form\Scopes\ParticipantFilterScope; use App\Form\Scopes\ParticipantFilterScope;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection; use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Pagination\LengthAwarePaginator;
use Laravel\Scout\Builder; use Laravel\Scout\Builder;
use Lorisleiva\Actions\Concerns\AsAction; use Lorisleiva\Actions\Concerns\AsAction;
@ -25,22 +24,14 @@ class ParticipantIndexAction
->query(fn ($q) => $q->withCount('children')->with('form')); ->query(fn ($q) => $q->withCount('children')->with('form'));
} }
/**
* @return LengthAwarePaginator<Participant>
*/
public function handle(Form $form, ParticipantFilterScope $filter): LengthAwarePaginator
{
return $this->getQuery($form, $filter)->paginate(15);
}
public function asController(Form $form, ?int $parent = null): AnonymousResourceCollection public function asController(Form $form, ?int $parent = null): AnonymousResourceCollection
{ {
$filter = ParticipantFilterScope::fromRequest(request()->input('filter')); $filter = ParticipantFilterScope::fromRequest(request()->input('filter', ''))->parent($parent);
$data = match ($parent) { $data = match ($parent) {
null => $this->handle($form, $filter), null => $this->getQuery($form, $filter)->paginate(15), // initial all elements - paginate
-1 => $this->getQuery($form, $filter)->where('parent_id', null)->paginate(15), -1 => $this->getQuery($form, $filter)->paginate(15), // initial root elements - parinate
default => $this->getQuery($form, $filter)->where('parent_id', $parent)->get(), default => $this->getQuery($form, $filter)->get(), // specific parent element - show all
}; };
return ParticipantResource::collection($data)->additional(['meta' => ParticipantResource::meta($form)]); return ParticipantResource::collection($data)->additional(['meta' => ParticipantResource::meta($form)]);

View File

@ -14,7 +14,7 @@ class UpdateParticipantSearchIndexAction
$form->searchableUsing()->updateIndexSettings( $form->searchableUsing()->updateIndexSettings(
$form->participantsSearchableAs(), $form->participantsSearchableAs(),
[ [
'filterableAttributes' => $form->getFields()->filterables()->getKeys(), 'filterableAttributes' => [...$form->getFields()->filterables()->getKeys(), 'parent-id'],
'searchableAttributes' => $form->getFields()->searchables()->getKeys(), 'searchableAttributes' => $form->getFields()->searchables()->getKeys(),
'sortableAttributes' => [], 'sortableAttributes' => [],
'displayedAttributes' => [...$form->getFields()->filterables()->getKeys(), ...$form->getFields()->searchables()->getKeys(), 'id'], 'displayedAttributes' => [...$form->getFields()->filterables()->getKeys(), ...$form->getFields()->searchables()->getKeys(), 'id'],

View File

@ -0,0 +1,9 @@
<?php
namespace App\Form\Contracts;
interface Filterable
{
/** @param mixed $value */
public function filter($value): string;
}

View File

@ -2,6 +2,7 @@
namespace App\Form\Data; namespace App\Form\Data;
use App\Form\Contracts\Filterable;
use App\Form\Enums\SpecialType; use App\Form\Enums\SpecialType;
use App\Form\Fields\Field; use App\Form\Fields\Field;
use App\Form\Fields\NamiField; use App\Form\Fields\NamiField;
@ -125,7 +126,7 @@ class FieldCollection extends Collection
public function filterables(): self public function filterables(): self
{ {
return $this; return $this->filter(fn ($field) => $field instanceof Filterable);
} }
/** /**

View File

@ -2,6 +2,7 @@
namespace App\Form\Fields; namespace App\Form\Fields;
use App\Form\Contracts\Filterable;
use App\Form\Matchers\BooleanMatcher; use App\Form\Matchers\BooleanMatcher;
use App\Form\Matchers\Matcher; use App\Form\Matchers\Matcher;
use App\Form\Models\Form; use App\Form\Models\Form;
@ -9,9 +10,8 @@ use App\Form\Models\Participant;
use App\Form\Presenters\BooleanPresenter; use App\Form\Presenters\BooleanPresenter;
use App\Form\Presenters\Presenter; use App\Form\Presenters\Presenter;
use Faker\Generator; use Faker\Generator;
use Illuminate\Validation\Rule;
class CheckboxField extends Field class CheckboxField extends Field implements Filterable
{ {
public bool $required; public bool $required;
public string $description; public string $description;
@ -86,4 +86,11 @@ class CheckboxField extends Field
{ {
return app(BooleanMatcher::class); return app(BooleanMatcher::class);
} }
public function filter($value): string
{
$asString = $value ? 'true' : 'false';
return "{$this->key} = $asString";
}
} }

View File

@ -2,6 +2,7 @@
namespace App\Form\Fields; namespace App\Form\Fields;
use App\Form\Contracts\Filterable;
use App\Form\Matchers\Matcher; use App\Form\Matchers\Matcher;
use App\Form\Matchers\SingleValueMatcher; use App\Form\Matchers\SingleValueMatcher;
use App\Form\Models\Form; use App\Form\Models\Form;
@ -9,7 +10,7 @@ use App\Form\Models\Participant;
use Faker\Generator; use Faker\Generator;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
class DropdownField extends Field class DropdownField extends Field implements Filterable
{ {
public bool $required; public bool $required;
/** @var array<int, string> */ /** @var array<int, string> */
@ -87,4 +88,14 @@ class DropdownField extends Field
{ {
return app(SingleValueMatcher::class); return app(SingleValueMatcher::class);
} }
/** @inheritdoc */
public function filter($value): string
{
if (is_null($value)) {
return "{$this->key} IS NULL";
}
return $this->key . ' = \'' . $value . '\'';
}
} }

View File

@ -2,6 +2,7 @@
namespace App\Form\Fields; namespace App\Form\Fields;
use App\Form\Contracts\Filterable;
use App\Form\Matchers\Matcher; use App\Form\Matchers\Matcher;
use App\Form\Matchers\SingleValueMatcher; use App\Form\Matchers\SingleValueMatcher;
use App\Form\Models\Form; use App\Form\Models\Form;
@ -9,7 +10,7 @@ use App\Form\Models\Participant;
use Faker\Generator; use Faker\Generator;
use Illuminate\Validation\Rule; use Illuminate\Validation\Rule;
class RadioField extends Field class RadioField extends Field implements Filterable
{ {
public bool $required; public bool $required;
/** @var array<int, string> */ /** @var array<int, string> */
@ -87,4 +88,13 @@ class RadioField extends Field
{ {
return app(SingleValueMatcher::class); return app(SingleValueMatcher::class);
} }
public function filter($value): string
{
if (is_null($value)) {
return "{$this->key} IS NULL";
}
return $this->key . ' = \'' . $value . '\'';
}
} }

View File

@ -111,6 +111,6 @@ class Participant extends Model implements Preventable
public function toSearchableArray(): array public function toSearchableArray(): array
{ {
return $this->data; return [...$this->data, 'parent-id' => $this->parent_id];
} }
} }

View File

@ -21,7 +21,7 @@ class ParticipantFilterScope extends ScoutFilter
{ {
public static string $nan = 'deeb3ef4-d185-44b1-a4bc-0a4e7addebc3d8900c6f-a344-4afb-b54e-065ed483a7ba'; public static string $nan = 'deeb3ef4-d185-44b1-a4bc-0a4e7addebc3d8900c6f-a344-4afb-b54e-065ed483a7ba';
public Form $form; private Form $form;
/** /**
* @param array<string, mixed> $data * @param array<string, mixed> $data
@ -30,6 +30,7 @@ class ParticipantFilterScope extends ScoutFilter
public array $data = [], public array $data = [],
public string $search = '', public string $search = '',
public array $options = [], public array $options = [],
public ?int $parent = null
) { ) {
} }
@ -37,7 +38,28 @@ class ParticipantFilterScope extends ScoutFilter
{ {
$this->search = $this->search ?: ''; $this->search = $this->search ?: '';
return Participant::search($this->search)->within($this->form->participantsSearchableAs()); return Participant::search($this->search, function ($engine, string $query, array $options) {
$filter = collect([]);
foreach ($this->form->getFields()->filterables() as $field) {
if ($this->data[$field->key] === static::$nan) {
continue;
}
$filter->push($field->filter($this->data[$field->key]));
}
if ($this->parent === -1) {
$filter->push('parent-id IS NULL');
}
if ($this->parent !== null && $this->parent !== -1) {
$filter->push('parent-id = ' . $this->parent);
}
$options['filter'] = $filter->map(fn ($expression) => "($expression)")->implode(' AND ');
return $engine->search($query, [...$this->options, ...$options]);
})->within($this->form->participantsSearchableAs());
} }
public function setForm(Form $form): self public function setForm(Form $form): self
@ -53,18 +75,10 @@ class ParticipantFilterScope extends ScoutFilter
return $this; return $this;
} }
/** public function parent(?int $parent): self
* @inheritdoc
*/
public function apply(Builder $query): Builder
{ {
foreach ($this->data as $key => $value) { $this->parent = $parent;
if ($value === static::$nan) {
continue;
}
$query = $query->where('data->' . $key, $value);
}
return $query; return $this;
} }
} }

View File

@ -0,0 +1,29 @@
<?php
use App\Form\Actions\UpdateParticipantSearchIndexAction;
use App\Form\Models\Form;
use Illuminate\Database\Migrations\Migration;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
foreach (Form::get() as $form) {
UpdateParticipantSearchIndexAction::run($form);
foreach ($form->participants as $participant) {
$participant->searchable();
}
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

View File

@ -28,7 +28,7 @@ export function useApiIndex(firstUrl, siteName = null) {
inner.meta.value = response.meta; inner.meta.value = response.meta;
} }
async function reloadPage(page) { async function reloadPage(page, p = {}) {
inner.meta.value.current_page = page; inner.meta.value.current_page = page;
await reload(false); await reload(false);
} }

View File

@ -23,6 +23,7 @@
</ui-popup> </ui-popup>
<page-filter breakpoint="lg"> <page-filter breakpoint="lg">
<template #buttons> <template #buttons>
<f-text id="search" v-model="innerFilter.search" name="search" label="Suchen" size="sm"></f-text>
<ui-icon-button icon="plus" @click="editing = {participant: null, preview: JSON.stringify(meta.form_config)}">Hinzufügen</ui-icon-button> <ui-icon-button icon="plus" @click="editing = {participant: null, preview: JSON.stringify(meta.form_config)}">Hinzufügen</ui-icon-button>
<f-switch v-if="meta.has_nami_field" id="group_participants" v-model="groupParticipants" label="Gruppieren" size="sm" name="group_participants"></f-switch> <f-switch v-if="meta.has_nami_field" id="group_participants" v-model="groupParticipants" label="Gruppieren" size="sm" name="group_participants"></f-switch>
<f-multipleselect id="active_columns" v-model="activeColumnsConfig" :options="meta.columns" label="Aktive Spalten" size="sm"></f-multipleselect> <f-multipleselect id="active_columns" v-model="activeColumnsConfig" :options="meta.columns" label="Aktive Spalten" size="sm"></f-multipleselect>
@ -117,7 +118,7 @@
</template> </template>
</table> </table>
<div class="px-6"> <div class="px-6">
<ui-pagination class="mt-4" :value="meta" @reload="reloadPage"></ui-pagination> <ui-pagination class="mt-4" :value="meta" @reload="reloadPage($event, {filter: toFilterString(innerFilter)})"></ui-pagination>
</div> </div>
</div> </div>
</template> </template>

View File

@ -91,6 +91,9 @@ it('testItFiltersParticipantsByCheckboxValue', function () {
->has(Participant::factory()->data(['check' => false])->count(2)) ->has(Participant::factory()->data(['check' => false])->count(2))
->create(); ->create();
sleep(2);
$this->callFilter('form.participant.index', ['data' => []], ['form' => $form])
->assertJsonCount(3, 'data');
$this->callFilter('form.participant.index', ['data' => ['check' => ParticipantFilterScope::$nan]], ['form' => $form]) $this->callFilter('form.participant.index', ['data' => ['check' => ParticipantFilterScope::$nan]], ['form' => $form])
->assertJsonCount(3, 'data'); ->assertJsonCount(3, 'data');
$this->callFilter('form.participant.index', ['data' => ['check' => true]], ['form' => $form]) $this->callFilter('form.participant.index', ['data' => ['check' => true]], ['form' => $form])
@ -139,6 +142,8 @@ it('testItFiltersParticipantsByDropdownValue', function () {
->assertJsonCount(2, 'data'); ->assertJsonCount(2, 'data');
$this->callFilter('form.participant.index', ['data' => ['drop' => 'B']], ['form' => $form]) $this->callFilter('form.participant.index', ['data' => ['drop' => 'B']], ['form' => $form])
->assertJsonCount(4, 'data'); ->assertJsonCount(4, 'data');
$this->callFilter('form.participant.index', ['data' => ['drop' => 'Z*Z']], ['form' => $form])
->assertJsonCount(0, 'data');
}); });
it('testItFiltersParticipantsByRadioValue', function () { it('testItFiltersParticipantsByRadioValue', function () {