From b01ff9a6778d098fba72f1494299a35d894b6d67 Mon Sep 17 00:00:00 2001
From: philipp lang <philipp@aweos.de>
Date: Mon, 22 Jul 2024 20:27:55 +0200
Subject: [PATCH] Add prevention items

---
 app/Form/Resources/ParticipantResource.php    |  2 ++
 app/Prevention/Enums/Prevention.php           | 30 +++++++++++++++++++
 .../Form/ParticipantIndexActionTest.php       | 15 ++++++++++
 3 files changed, 47 insertions(+)

diff --git a/app/Form/Resources/ParticipantResource.php b/app/Form/Resources/ParticipantResource.php
index 0c8cd739..5808c878 100644
--- a/app/Form/Resources/ParticipantResource.php
+++ b/app/Form/Resources/ParticipantResource.php
@@ -5,6 +5,7 @@ namespace App\Form\Resources;
 use App\Form\Models\Form;
 use App\Form\Models\Participant;
 use App\Form\Scopes\ParticipantFilterScope;
+use App\Prevention\Enums\Prevention;
 use Illuminate\Http\Resources\Json\JsonResource;
 use Illuminate\Support\Collection;
 
@@ -28,6 +29,7 @@ class ParticipantResource extends JsonResource
             'created_at_display' => $this->created_at->format('d.m.Y'),
             'children_count' => $this->children_count,
             'member_id' => $this->member_id,
+            'prevention_items' => $this->member ? Prevention::items($this->member->preventions()) : [],
             'links' => [
                 'assign' => route('participant.assign', ['participant' => $this->getModel()]),
                 'destroy' => route('participant.destroy', ['participant' => $this->getModel()]),
diff --git a/app/Prevention/Enums/Prevention.php b/app/Prevention/Enums/Prevention.php
index 841767f6..01352bf8 100644
--- a/app/Prevention/Enums/Prevention.php
+++ b/app/Prevention/Enums/Prevention.php
@@ -2,6 +2,9 @@
 
 namespace App\Prevention\Enums;
 
+use App\Member\Member;
+use Carbon\Carbon;
+
 enum Prevention
 {
     case EFZ;
@@ -18,4 +21,31 @@ enum Prevention
             static::VK => 'Verhaltenskodex',
         };
     }
+
+    public function tooltip(bool $value): string
+    {
+        return $this->text() . ' ' . ($value ? 'vorhanden' : 'nicht vorhanden');
+    }
+
+    public function letter(): string
+    {
+        return match ($this) {
+            static::EFZ => 'F',
+            static::PS => 'P',
+            static::MOREPS => 'A',
+            static::VK => 'V',
+        };
+    }
+
+    /**
+     * @param array<int, self> $preventions
+     */
+    public static function items(array $preventions)
+    {
+        return collect(static::cases())->map(fn ($case) => [
+            'letter' => $case->letter(),
+            'value' => !in_array($case, $preventions),
+            'tooltip' => $case->tooltip(!in_array($case, $preventions)),
+        ]);
+    }
 }
diff --git a/tests/Feature/Form/ParticipantIndexActionTest.php b/tests/Feature/Form/ParticipantIndexActionTest.php
index b6095202..8cd8df0d 100644
--- a/tests/Feature/Form/ParticipantIndexActionTest.php
+++ b/tests/Feature/Form/ParticipantIndexActionTest.php
@@ -7,6 +7,7 @@ use App\Form\Models\Form;
 use App\Form\Models\Participant;
 use App\Form\Scopes\ParticipantFilterScope;
 use App\Group;
+use App\Member\Member;
 use Carbon\Carbon;
 use Illuminate\Foundation\Testing\DatabaseTransactions;
 
@@ -207,4 +208,18 @@ class ParticipantIndexActionTest extends FormTestCase
             ->assertJsonPath('meta.current_page', 1);
         $this->callFilter('form.participant.index', [], ['form' => $form, 'parent' => $participant->id])->assertJsonPath('data.0.children_count', 0);
     }
+
+    public function testItShowsPreventionState(): void
+    {
+        $this->login()->loginNami()->withoutExceptionHandling();
+        $participant = Participant::factory()->data(['vorname' => 'Max'])
+            ->for(Member::factory()->defaults()->state(['efz' => null]))
+            ->for(Form::factory())
+            ->create();
+
+        $this->callFilter('form.participant.index', [], ['form' => $participant->form])
+            ->assertJsonPath('data.0.prevention_items.0.letter', 'F')
+            ->assertJsonPath('data.0.prevention_items.0.value', false)
+            ->assertJsonPath('data.0.prevention_items.0.tooltip', 'erweitertes Führungszeugnis nicht vorhanden');
+    }
 }