From 57b1efb065ea1fc21bf6c26e53790d38cbdf435a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Thu, 12 Dec 2024 15:42:35 +0100 Subject: [PATCH] Add registration_from and registration_until to frontend api --- app/Form/Actions/RegisterAction.php | 5 +++++ app/Form/Models/Form.php | 13 +++++++++++++ app/Form/Resources/FormApiResource.php | 1 + database/factories/Form/Models/FormFactory.php | 5 +++-- tests/EndToEnd/Form/FormApiListActionTest.php | 17 ++++++++++++++--- tests/Feature/Form/FormRegisterActionTest.php | 16 ++++++++++++++++ 6 files changed, 52 insertions(+), 5 deletions(-) diff --git a/app/Form/Actions/RegisterAction.php b/app/Form/Actions/RegisterAction.php index 854ed7fd..b4cdfa0d 100644 --- a/app/Form/Actions/RegisterAction.php +++ b/app/Form/Actions/RegisterAction.php @@ -7,6 +7,7 @@ use App\Form\Models\Form; use App\Form\Models\Participant; use App\Member\Member; use Illuminate\Http\JsonResponse; +use Illuminate\Validation\ValidationException; use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\Concerns\AsAction; @@ -19,6 +20,10 @@ class RegisterAction */ public function handle(Form $form, array $input): Participant { + if (!$form->canRegister()) { + throw ValidationException::withMessages(['event' => 'Anmeldung zzt nicht möglich.']); + } + $memberQuery = FieldCollection::fromRequest($form, $input) ->withNamiType() ->reduce(fn ($query, $field) => $field->namiType->performQuery($query, $field->value), (new Member())->newQuery()); diff --git a/app/Form/Models/Form.php b/app/Form/Models/Form.php index 972a348f..45e372c4 100644 --- a/app/Form/Models/Form.php +++ b/app/Form/Models/Form.php @@ -188,4 +188,17 @@ class Form extends Model implements HasMedia { return Sorting::from($this->meta['sorting']); } + + public function canRegister(): bool + { + if ($this->registration_from && $this->registration_from->gt(now())) { + return false; + } + + if ($this->registration_until && $this->registration_until->lt(now())) { + return false; + } + + return true; + } } diff --git a/app/Form/Resources/FormApiResource.php b/app/Form/Resources/FormApiResource.php index 1af1d90b..9543195e 100644 --- a/app/Form/Resources/FormApiResource.php +++ b/app/Form/Resources/FormApiResource.php @@ -35,6 +35,7 @@ class FormApiResource extends JsonResource 'image' => $this->getMedia('headerImage')->first()->getFullUrl('square'), 'is_active' => $this->is_active, 'is_private' => $this->is_private, + 'can_register' => $this->getModel()->canRegister(), ]; } diff --git a/database/factories/Form/Models/FormFactory.php b/database/factories/Form/Models/FormFactory.php index eda27a1f..2dd34757 100644 --- a/database/factories/Form/Models/FormFactory.php +++ b/database/factories/Form/Models/FormFactory.php @@ -7,6 +7,7 @@ use App\Form\Models\Form; use App\Lib\Editor\Condition; use Database\Factories\Traits\FakesMedia; use Illuminate\Database\Eloquent\Factories\Factory; +use Illuminate\Support\Carbon; use Tests\Feature\Form\FormtemplateFieldRequest; use Tests\Feature\Form\FormtemplateSectionRequest; use Tests\RequestFactories\EditorRequestFactory; @@ -48,8 +49,8 @@ class FormFactory extends Factory 'config' => ['sections' => []], 'from' => $this->faker->dateTimeBetween('+1 week', '+4 weeks')->format('Y-m-d H:i:s'), 'to' => $this->faker->dateTimeBetween('+1 week', '+4 weeks')->format('Y-m-d H:i:s'), - 'registration_from' => $this->faker->dateTimeBetween('-2 weeks', 'now')->format('Y-m-d H:i:s'), - 'registration_until' => $this->faker->dateTimeBetween('now', '+2 weeks')->format('Y-m-d H:i:s'), + 'registration_from' => $this->faker->dateTimeBetween(Carbon::parse('-2 weeks'), now())->format('Y-m-d H:i:s'), + 'registration_until' => $this->faker->dateTimeBetween(now(), Carbon::parse('+2 weeks'))->format('Y-m-d H:i:s'), 'mail_top' => EditorRequestFactory::new()->toData(), 'mail_bottom' => EditorRequestFactory::new()->toData(), 'is_active' => true, diff --git a/tests/EndToEnd/Form/FormApiListActionTest.php b/tests/EndToEnd/Form/FormApiListActionTest.php index 96b6a57f..f9bbfb03 100644 --- a/tests/EndToEnd/Form/FormApiListActionTest.php +++ b/tests/EndToEnd/Form/FormApiListActionTest.php @@ -3,14 +3,12 @@ namespace Tests\EndToEnd\Form; use App\Form\Models\Form; -use App\Membership\TestersBlock; use App\Subactivity; -use Carbon\Carbon; use Illuminate\Foundation\Testing\DatabaseTransactions; +use Illuminate\Support\Carbon; use Illuminate\Support\Facades\Storage; use Tests\Feature\Form\FormtemplateSectionRequest; use Tests\RequestFactories\EditorRequestFactory; -use Tests\TestCase; uses(FormTestCase::class); uses(DatabaseTransactions::class); @@ -42,11 +40,24 @@ it('testItDisplaysForms', function () { ->assertJsonPath('data.0.dates', '05.05.2023 - 07.06.2023') ->assertJsonPath('data.0.from_human', '05.05.2023') ->assertJsonPath('data.0.to_human', '07.06.2023') + ->assertJsonPath('data.0.can_register', true) ->assertJsonPath('meta.per_page', 15) ->assertJsonPath('meta.base_url', url('')) ->assertJsonPath('meta.total', 1); }); +it('displays registration not possible', function () { + Storage::fake('temp'); + $this->loginNami()->withoutExceptionHandling(); + Form::factory() + ->registrationFrom(now()->addDay()) + ->withImage('headerImage', 'lala-2.jpg') + ->create(); + + sleep(1); + $this->get('/api/form?perPage=15')->assertJsonPath('data.0.can_register', false); +}); + it('testItDisplaysDefaultValueOfField', function () { Storage::fake('temp'); $this->loginNami()->withoutExceptionHandling(); diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index af619c4f..6ff43c62 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -55,6 +55,22 @@ class FormRegisterActionTest extends FormTestCase $this->assertEquals('Abraham', $participants->first()->data['spitzname']); } + public function testItCannotRegisterWhenRegistrationFromReached(): void + { + $this->login()->loginNami(); + $form = Form::factory()->registrationFrom(now()->addDay())->create(); + + $this->register($form, [])->assertJsonValidationErrors(['event' => 'Anmeldung zzt nicht möglich.']); + } + + public function testItCannotRegisterWhenRegistrationUntilReached(): void + { + $this->login()->loginNami(); + $form = Form::factory()->registrationUntil(now()->subDay())->create(); + + $this->register($form, [])->assertJsonValidationErrors(['event' => 'Anmeldung zzt nicht möglich.']); + } + public function testItSendsEmailToParticipant(): void { $this->login()->loginNami()->withoutExceptionHandling();