Add matching for member

This commit is contained in:
philipp lang 2024-03-07 22:38:46 +01:00
parent 171dd51200
commit 43846d1703
4 changed files with 58 additions and 3 deletions

View File

@ -5,6 +5,7 @@ namespace App\Form\Actions;
use App\Form\Fields\Field;
use App\Form\Models\Form;
use App\Form\Models\Participant;
use App\Member\Member;
use Illuminate\Http\JsonResponse;
use Lorisleiva\Actions\ActionRequest;
use Lorisleiva\Actions\Concerns\AsAction;
@ -18,8 +19,12 @@ class RegisterAction
*/
public function handle(Form $form, array $input): Participant
{
$memberQuery = $form->getFields()->withNamiType()->reduce(fn ($query, $field) => $field->namiType->performQuery($query, data_get($input, $field->key)), (new Member())->newQuery());
$mglnr = $form->getFields()->withNamiType()->count() && $memberQuery->count() === 1 ? $memberQuery->first()->mitgliedsnr : null;
$participant = $form->participants()->create([
'data' => $input
'data' => $input,
'mitgliedsnr' => $mglnr,
]);
$form->getFields()->each(fn ($field) => $field->afterRegistration($form, $participant, $input));

View File

@ -17,6 +17,11 @@ class FieldCollection extends Collection
return $this->filter(fn ($field) => $field->forMembers === true);
}
public function withNamiType(): self
{
return $this->filter(fn ($field) => $field->namiType !== null);
}
public function noNamiType(): self
{
return $this->filter(fn ($field) => $field->namiType === null);

View File

@ -4,6 +4,7 @@ namespace App\Form\Enums;
use App\Group\Enums\Level;
use App\Member\Member;
use Illuminate\Database\Eloquent\Builder;
enum NamiType: string
{
@ -36,6 +37,22 @@ enum NamiType: string
};
}
/**
* @param Builder<Member> $query
* @return Builder<Member>
*/
public function performQuery(Builder $query, mixed $value): Builder
{
return match ($this) {
static::FIRSTNAME => $query->where('firstname', $value),
static::LASTNAME => $query->where('lastname', $value),
static::BIRTHDAY => $query->where('birthday', $value),
static::REGION => $query,
static::STAMM => $query,
static::EMAIL => $query->where('email', $value),
};
}
protected function matchGroup(Member $member): ?int
{
if ($member->group->level === Level::GROUP) {

View File

@ -10,7 +10,6 @@ use App\Member\Member;
use Carbon\Carbon;
use Generator;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Support\Arr;
use Illuminate\Testing\TestResponse;
class FormRegisterActionTest extends FormTestCase
@ -269,7 +268,7 @@ class FormRegisterActionTest extends FormTestCase
public function testItSetsMitgliedsnrForMainMember(): void
{
$this->login()->loginNami();
$this->createMember(['mitgliedsnr' => '9966']);
$this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
$this->textField('email')->namiType(NamiType::EMAIL),
@ -282,6 +281,35 @@ class FormRegisterActionTest extends FormTestCase
$this->assertEquals('9966', $form->participants->first()->mitgliedsnr);
}
public function testItDoesntSetMitgliedsnrWhenFieldDoesntHaveType(): void
{
$this->login()->loginNami();
$this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
$this->textField('email'),
])])
->create();
$this->register($form, ['email' => 'max@muster.de'])->assertOk();
$this->assertNull($form->participants->first()->mitgliedsnr);
}
public function testItDoesntSyncMembersWhenTwoMembersMatch(): void
{
$this->login()->loginNami();
$this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de']);
$this->createMember(['mitgliedsnr' => '9967', 'email' => 'max@muster.de']);
$form = Form::factory()
->sections([FormtemplateSectionRequest::new()->fields([
$this->textField('email')->namiType(NamiType::EMAIL),
])])
->create();
$this->register($form, ['email' => 'max@muster.de'])->assertOk();
$this->assertNull($form->participants->first()->mitgliedsnr);
}
// --------------------------- NamiField Tests ---------------------------
// ***********************************************************************
public function testItAddsMitgliedsnrFromMembers(): void