From c4aa9b1051f1e06d954cf3cfdb0c6e8ed842a562 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 27 May 2024 18:30:05 +0200 Subject: [PATCH] Add inactive forms --- app/Form/Actions/FormStoreAction.php | 1 + app/Form/Actions/FormUpdateAction.php | 1 + app/Form/Models/Form.php | 2 ++ app/Form/Resources/FormResource.php | 2 ++ app/Form/Scopes/FormFilterScope.php | 5 +++++ config/scout.php | 4 ++-- resources/js/views/form/Index.vue | 6 +++++- tests/EndToEnd/Form/FormIndexActionTest.php | 14 ++++++++++++++ tests/Feature/Form/FormStoreActionTest.php | 2 +- tests/Feature/Form/FormUpdateActionTest.php | 11 +++++++++++ 10 files changed, 44 insertions(+), 4 deletions(-) diff --git a/app/Form/Actions/FormStoreAction.php b/app/Form/Actions/FormStoreAction.php index ea9b0466..f5171fd2 100644 --- a/app/Form/Actions/FormStoreAction.php +++ b/app/Form/Actions/FormStoreAction.php @@ -32,6 +32,7 @@ class FormStoreAction 'mail_bottom' => 'array', 'header_image' => 'required|exclude', 'mailattachments' => 'present|array|exclude', + 'is_active' => 'boolean', ]; } diff --git a/app/Form/Actions/FormUpdateAction.php b/app/Form/Actions/FormUpdateAction.php index 0303bfbe..c765afbf 100644 --- a/app/Form/Actions/FormUpdateAction.php +++ b/app/Form/Actions/FormUpdateAction.php @@ -31,6 +31,7 @@ class FormUpdateAction 'registration_until' => 'present|nullable|date', 'mail_top' => 'array', 'mail_bottom' => 'array', + 'is_active' => 'boolean', ]; } diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index 70916e6d..e78b0a6c 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -31,6 +31,7 @@ class Form extends Model implements HasMedia 'description' => 'json', 'mail_top' => 'json', 'mail_bottom' => 'json', + 'is_active' => 'boolean', ]; /** @var array */ @@ -133,6 +134,7 @@ class Form extends Model implements HasMedia 'from' => $this->from->timestamp, 'to' => $this->to->timestamp, 'name' => $this->name, + 'is_active' => $this->is_active, ]; } diff --git a/app/Form/Resources/FormResource.php b/app/Form/Resources/FormResource.php index ace294d1..db087b4e 100644 --- a/app/Form/Resources/FormResource.php +++ b/app/Form/Resources/FormResource.php @@ -43,6 +43,7 @@ class FormResource extends JsonResource 'registration_until' => $this->registration_until?->format('Y-m-d H:i:s'), 'config' => $this->config, 'participants_count' => $this->participants_count, + 'is_active' => $this->is_active, 'links' => [ 'participant_index' => route('form.participant.index', ['form' => $this->getModel()]), 'update' => route('form.update', ['form' => $this->getModel()]), @@ -71,6 +72,7 @@ class FormResource extends JsonResource 'specialTypes' => SpecialType::forSelect(), 'default' => [ 'description' => [], + 'is_active' => true, 'name' => '', 'excerpt' => '', 'from' => null, diff --git a/app/Form/Scopes/FormFilterScope.php b/app/Form/Scopes/FormFilterScope.php index 8d8825ac..54f86951 100644 --- a/app/Form/Scopes/FormFilterScope.php +++ b/app/Form/Scopes/FormFilterScope.php @@ -19,6 +19,7 @@ class FormFilterScope extends ScoutFilter public function __construct( public ?string $search = '', public bool $past = false, + public bool $inactive = false, ) { } @@ -35,6 +36,10 @@ class FormFilterScope extends ScoutFilter $filters->push('to > ' . now()->timestamp); } + if ($this->inactive === false) { + $filters->push('is_active = true'); + } + $options['filter'] = $filters->implode(' AND '); return $engine->search($query, $options); diff --git a/config/scout.php b/config/scout.php index 7b97ea3d..19d413f2 100644 --- a/config/scout.php +++ b/config/scout.php @@ -146,9 +146,9 @@ return [ ] ], Form::class => [ - 'filterableAttributes' => ['to'], + 'filterableAttributes' => ['to', 'is_active'], 'searchableAttributes' => ['name'], - 'sortableAttributes' => ['from',], + 'sortableAttributes' => ['from'], 'displayedAttributes' => ['from', 'name', 'id', 'to'], 'pagination' => [ 'maxTotalHits' => 1000000, diff --git a/resources/js/views/form/Index.vue b/resources/js/views/form/Index.vue index 6fdf5bff..6b6dd968 100644 --- a/resources/js/views/form/Index.vue +++ b/resources/js/views/form/Index.vue @@ -39,7 +39,10 @@
- +
+ + +
+ diff --git a/tests/EndToEnd/Form/FormIndexActionTest.php b/tests/EndToEnd/Form/FormIndexActionTest.php index bff15561..fe8f5396 100644 --- a/tests/EndToEnd/Form/FormIndexActionTest.php +++ b/tests/EndToEnd/Form/FormIndexActionTest.php @@ -46,6 +46,7 @@ class FormIndexActionTest extends FormTestCase ->assertInertiaPath('data.data.0.from', '2023-05-05') ->assertInertiaPath('data.data.0.participants_count', 5) ->assertInertiaPath('data.data.0.to', '2023-06-07') + ->assertInertiaPath('data.data.0.is_active', true) ->assertInertiaPath('data.data.0.registration_from', '2023-05-06 04:00:00') ->assertInertiaPath('data.data.0.registration_until', '2023-04-01 05:00:00') ->assertInertiaPath('data.data.0.links.participant_index', route('form.participant.index', ['form' => $form])) @@ -56,6 +57,7 @@ class FormIndexActionTest extends FormTestCase ->assertInertiaPath('data.meta.default.name', '') ->assertInertiaPath('data.meta.default.description', []) ->assertInertiaPath('data.meta.default.excerpt', '') + ->assertInertiaPath('data.meta.default.is_active', true) ->assertInertiaPath('data.meta.default.mailattachments', []) ->assertInertiaPath('data.meta.default.config', null) ->assertInertiaPath('data.meta.base_url', url('')) @@ -77,6 +79,18 @@ class FormIndexActionTest extends FormTestCase ->assertInertiaCount('data.data', 2); } + public function testItDoesntReturnInactiveForms(): void + { + $this->withoutExceptionHandling()->login()->loginNami(); + Form::factory()->isActive(false)->count(1)->create(); + Form::factory()->isActive(true)->count(2)->create(); + + sleep(1); + $this->callFilter('form.index', [])->assertInertiaCount('data.data', 2); + $this->callFilter('form.index', ['inactive' => true])->assertInertiaCount('data.data', 3); + $this->callFilter('form.index', ['inactive' => false])->assertInertiaCount('data.data', 2); + } + public function testItOrdersByStartDateDesc(): void { $this->withoutExceptionHandling()->login()->loginNami(); diff --git a/tests/Feature/Form/FormStoreActionTest.php b/tests/Feature/Form/FormStoreActionTest.php index b59e8875..cb286b4f 100644 --- a/tests/Feature/Form/FormStoreActionTest.php +++ b/tests/Feature/Form/FormStoreActionTest.php @@ -8,7 +8,6 @@ use App\Lib\Events\Succeeded; use Illuminate\Foundation\Testing\DatabaseTransactions; use Illuminate\Support\Facades\Event; use Generator; -use Illuminate\Support\Facades\Storage; use Tests\RequestFactories\EditorRequestFactory; class FormStoreActionTest extends FormTestCase @@ -42,6 +41,7 @@ class FormStoreActionTest extends FormTestCase $this->assertEquals(json_decode('{"time":1,"blocks":[{"id":11,"type":"paragraph","data":{"text":"lala"},"tunes":{"condition":{"mode":"all","ifs":[]}}}],"version":"1.0"}', true), $form->mail_top); $this->assertEquals(json_decode('{"time":1,"blocks":[{"id":12,"type":"paragraph","data":{"text":"lalab"},"tunes":{"condition":{"mode":"all","ifs":[]}}}],"version":"1.0"}', true), $form->mail_bottom); $this->assertEquals('2023-05-04 01:00', $form->registration_from->format('Y-m-d H:i')); + $this->assertEquals(true, $form->is_active); $this->assertEquals('2023-07-07 01:00', $form->registration_until->format('Y-m-d H:i')); $this->assertEquals('2023-07-07', $form->from->format('Y-m-d')); $this->assertEquals('2023-07-08', $form->to->format('Y-m-d')); diff --git a/tests/Feature/Form/FormUpdateActionTest.php b/tests/Feature/Form/FormUpdateActionTest.php index 8dadc212..f99901d1 100644 --- a/tests/Feature/Form/FormUpdateActionTest.php +++ b/tests/Feature/Form/FormUpdateActionTest.php @@ -48,6 +48,17 @@ class FormUpdateActionTest extends FormTestCase $this->assertEquals(['firstname'], $form->fresh()->meta['active_columns']); } + public function testItUpdatesActiveState(): void + { + $this->login()->loginNami()->withoutExceptionHandling(); + $form = Form::factory()->create(); + + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(false)->create())->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertFalse($form->fresh()->is_active); + $this->patchJson(route('form.update', ['form' => $form]), FormRequest::new()->isActive(true)->create())->assertSessionDoesntHaveErrors()->assertOk(); + $this->assertTrue($form->fresh()->is_active); + } + public function testItUpdatesActiveColumnsWhenFieldsAdded(): void { $this->login()->loginNami()->withoutExceptionHandling();