From 69a3ed2a60dd5c03b4b56bfdd39498fd0252b71a Mon Sep 17 00:00:00 2001 From: philipp lang Date: Tue, 2 Jul 2024 16:04:07 +0200 Subject: [PATCH] Add member_id for participants --- app/Form/Actions/RegisterAction.php | 4 +- app/Form/Fields/NamiField.php | 2 +- ...5058_update_member_participants_column.php | 46 +++++++++++++++++++ tests/Feature/Form/FormRegisterActionTest.php | 10 ++-- 4 files changed, 54 insertions(+), 8 deletions(-) create mode 100644 database/migrations/2024_07_02_155058_update_member_participants_column.php diff --git a/app/Form/Actions/RegisterAction.php b/app/Form/Actions/RegisterAction.php index f4f513c9..854ed7fd 100644 --- a/app/Form/Actions/RegisterAction.php +++ b/app/Form/Actions/RegisterAction.php @@ -22,11 +22,11 @@ class RegisterAction $memberQuery = FieldCollection::fromRequest($form, $input) ->withNamiType() ->reduce(fn ($query, $field) => $field->namiType->performQuery($query, $field->value), (new Member())->newQuery()); - $mglnr = $form->getFields()->withNamiType()->count() && $memberQuery->count() === 1 ? $memberQuery->first()->mitgliedsnr : null; + $member = $form->getFields()->withNamiType()->count() && $memberQuery->count() === 1 ? $memberQuery->first() : null; $participant = $form->participants()->create([ 'data' => $input, - 'mitgliedsnr' => $mglnr, + 'member_id' => $member?->id, ]); $form->getFields()->each(fn ($field) => $field->afterRegistration($form, $participant, $input)); diff --git a/app/Form/Fields/NamiField.php b/app/Form/Fields/NamiField.php index bce954bd..443c51a8 100644 --- a/app/Form/Fields/NamiField.php +++ b/app/Form/Fields/NamiField.php @@ -116,7 +116,7 @@ class NamiField extends Field } $data[$this->key] = []; - $form->participants()->create(['data' => $data, 'mitgliedsnr' => $memberData['id'], 'parent_id' => $participant->id]); + $form->participants()->create(['data' => $data, 'member_id' => $member?->id, 'parent_id' => $participant->id]); } } diff --git a/database/migrations/2024_07_02_155058_update_member_participants_column.php b/database/migrations/2024_07_02_155058_update_member_participants_column.php new file mode 100644 index 00000000..5c1a1d3e --- /dev/null +++ b/database/migrations/2024_07_02_155058_update_member_participants_column.php @@ -0,0 +1,46 @@ +renameColumn('mitgliedsnr', 'member_id')->nullable(); + }); + + Schema::table('participants', function (Blueprint $table) { + $table->unsignedBigInteger('member_id')->nullable(true)->change(); + }); + + foreach (Participant::whereNotNull('member_id')->get() as $p) { + $p->update(['member_id' => Member::firstWhere('mitgliedsnr', $p->member_id)?->id]); + } + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('participants', function (Blueprint $table) { + $table->string('member_id')->nullable(true)->change(); + }); + + Schema::table('participants', function (Blueprint $table) { + $table->renameColumn('member_id', 'mitgliedsnr'); + }); + } +}; diff --git a/tests/Feature/Form/FormRegisterActionTest.php b/tests/Feature/Form/FormRegisterActionTest.php index ce0583eb..9af378ac 100644 --- a/tests/Feature/Form/FormRegisterActionTest.php +++ b/tests/Feature/Form/FormRegisterActionTest.php @@ -394,7 +394,7 @@ class FormRegisterActionTest extends FormTestCase public function testItSetsMitgliedsnrForMainMember(): void { $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster']); + $member = $this->createMember(['mitgliedsnr' => '9966', 'email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster']); $form = Form::factory()->fields([ $this->textField('email')->namiType(NamiType::EMAIL), $this->textField('firstname')->namiType(NamiType::FIRSTNAME), @@ -403,7 +403,7 @@ class FormRegisterActionTest extends FormTestCase ->create(); $this->register($form, ['email' => 'max@muster.de', 'firstname' => 'Max', 'lastname' => 'Muster'])->assertOk(); - $this->assertEquals('9966', $form->participants->first()->mitgliedsnr); + $this->assertEquals($member->id, $form->participants->first()->member_id); } public function testItDoesntSetMitgliedsnrWhenFieldDoesntHaveType(): void @@ -416,7 +416,7 @@ class FormRegisterActionTest extends FormTestCase ->create(); $this->register($form, ['email' => 'max@muster.de'])->assertOk(); - $this->assertNull($form->participants->first()->mitgliedsnr); + $this->assertNull($form->participants->first()->member_id); } public function testItDoesntSyncMembersWhenTwoMembersMatch(): void @@ -710,14 +710,14 @@ class FormRegisterActionTest extends FormTestCase public function testParticipantsHaveRelationToActualMember(): void { $this->login()->loginNami(); - $this->createMember(['mitgliedsnr' => '5505']); + $member = $this->createMember(['mitgliedsnr' => '5505']); $form = Form::factory()->fields([ $this->namiField('members'), ]) ->create(); $this->register($form, ['members' => [['id' => '5505']]])->assertOk(); - $this->assertEquals('5505', $form->participants->get(1)->mitgliedsnr); + $this->assertEquals($member->id, $form->participants->get(1)->member_id); } public function testItSetsRegionIdAndGroupIdOfParentGroup(): void