Add registration_from and registration_until to frontend api
continuous-integration/drone/push Build is passing Details

This commit is contained in:
philipp lang 2024-12-12 15:42:35 +01:00
parent 3e563f8390
commit 57b1efb065
6 changed files with 52 additions and 5 deletions

View File

@ -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());

View File

@ -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;
}
}

View File

@ -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(),
];
}

View File

@ -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,

View File

@ -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();

View File

@ -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();