From 920f3efbfc72c8bcc3a6d90fbd14e7a1a4252e4c Mon Sep 17 00:00:00 2001 From: philipp lang Date: Mon, 6 Feb 2023 00:57:37 +0100 Subject: [PATCH] Fix pull memberships action for local memberships --- app/Actions/PullMemberAction.php | 2 +- app/Actions/PullMembershipsAction.php | 7 ++- app/Member/Membership.php | 4 +- database/factories/ActivityFactory.php | 1 - .../factories/Member/MembershipFactory.php | 16 +++++ database/factories/SubactivityFactory.php | 1 - ...05_233824_create_memberships_to_column.php | 28 +++++++++ ..._235353_edit_activities_nami_id_column.php | 28 +++++++++ .../Initialize/PullMembershipsActionTest.php | 60 +++++++++++++++++++ 9 files changed, 142 insertions(+), 5 deletions(-) create mode 100644 database/migrations/2023_02_05_233824_create_memberships_to_column.php create mode 100644 database/migrations/2023_02_05_235353_edit_activities_nami_id_column.php diff --git a/app/Actions/PullMemberAction.php b/app/Actions/PullMemberAction.php index 82e14394..d479f0d9 100644 --- a/app/Actions/PullMemberAction.php +++ b/app/Actions/PullMemberAction.php @@ -28,7 +28,7 @@ class PullMemberAction } $region = Region::firstWhere('nami_id', $member->regionId ?: -1); - $m = Member::updateOrCreate(['nami_id' => $member->id], [ + Member::updateOrCreate(['nami_id' => $member->id], [ 'firstname' => $member->firstname, 'lastname' => $member->lastname, 'joined_at' => $member->joinedAt, diff --git a/app/Actions/PullMembershipsAction.php b/app/Actions/PullMembershipsAction.php index 28869833..428f09da 100644 --- a/app/Actions/PullMembershipsAction.php +++ b/app/Actions/PullMembershipsAction.php @@ -28,7 +28,7 @@ class PullMembershipsAction foreach ($memberships as $membership) { $existingMembership = Membership::where('nami_id', $membership->id)->first(); - $group = Group::where('name', $membership->group)->first(); + $group = Group::where('name', $membership->group)->whereNotNull('nami_id')->first(); if (!$group) { continue; } @@ -39,6 +39,9 @@ class PullMembershipsAction $this->singleStrategy($member, $group, $membership); } + + $membershipIds = $memberships->map(fn ($membership) => $membership->id)->toArray(); + $member->memberships()->whereNotIn('nami_id', $membershipIds)->whereNotNull('nami_id')->delete(); } private function overviewStrategy(Member $member, Group $group, NamiMembershipEntry $membership): ?Membership @@ -64,6 +67,7 @@ class PullMembershipsAction return $member->memberships()->updateOrCreate(['nami_id' => $membership->id], [ 'nami_id' => $membership->id, 'from' => $membership->startsAt, + 'to' => $membership->endsAt, 'group_id' => $group->id, 'activity_id' => $activity->id, 'subactivity_id' => $subactivity?->id, @@ -93,6 +97,7 @@ class PullMembershipsAction return $member->memberships()->updateOrCreate(['nami_id' => $membership->id], [ 'nami_id' => $membership->id, 'from' => $membership->startsAt, + 'to' => $membership->endsAt, 'group_id' => $group->id, 'activity_id' => $activity->id, 'subactivity_id' => $subactivity?->id, diff --git a/app/Member/Membership.php b/app/Member/Membership.php index ff7eba25..dc283ca9 100644 --- a/app/Member/Membership.php +++ b/app/Member/Membership.php @@ -16,12 +16,14 @@ class Membership extends Model { use HasFactory; - public $fillable = ['subactivity_id', 'activity_id', 'group_id', 'member_id', 'nami_id', 'from', 'promised_at']; + public $fillable = ['subactivity_id', 'activity_id', 'group_id', 'member_id', 'nami_id', 'from', 'to', 'promised_at']; /** * @var array */ public $casts = [ + 'from' => 'date', + 'to' => 'date', 'promised_at' => 'date', ]; diff --git a/database/factories/ActivityFactory.php b/database/factories/ActivityFactory.php index bb685935..aa73996f 100644 --- a/database/factories/ActivityFactory.php +++ b/database/factories/ActivityFactory.php @@ -46,7 +46,6 @@ class ActivityFactory extends Factory { return [ 'name' => $this->faker->words(5, true), - 'nami_id' => $this->faker->randomNumber(), ]; } diff --git a/database/factories/Member/MembershipFactory.php b/database/factories/Member/MembershipFactory.php index bb419d38..46c6434b 100644 --- a/database/factories/Member/MembershipFactory.php +++ b/database/factories/Member/MembershipFactory.php @@ -35,6 +35,22 @@ class MembershipFactory extends Factory return $this->state(['nami_id' => $namiId]); } + public function local(): self + { + return $this->state(['nami_id' => null]); + } + + public function inLocal(string $activity, ?string $subactivity = null): self + { + $instance = $this->for(Activity::factory()->name($activity)); + + if ($subactivity) { + $instance = $instance->for(Subactivity::factory()->name($subactivity)); + } + + return $instance; + } + public function in(string $activity, int $activityNamiId, ?string $subactivity = null, ?int $subactivityNamiId = null): self { $instance = $this->for(Activity::factory()->name($activity)->inNami($activityNamiId)); diff --git a/database/factories/SubactivityFactory.php b/database/factories/SubactivityFactory.php index cb4fdfe7..5074d989 100644 --- a/database/factories/SubactivityFactory.php +++ b/database/factories/SubactivityFactory.php @@ -40,7 +40,6 @@ class SubactivityFactory extends Factory { return [ 'name' => $this->faker->words(5, true), - 'nami_id' => $this->faker->randomNumber(), ]; } diff --git a/database/migrations/2023_02_05_233824_create_memberships_to_column.php b/database/migrations/2023_02_05_233824_create_memberships_to_column.php new file mode 100644 index 00000000..409f51b7 --- /dev/null +++ b/database/migrations/2023_02_05_233824_create_memberships_to_column.php @@ -0,0 +1,28 @@ +datetime('to')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +}; diff --git a/database/migrations/2023_02_05_235353_edit_activities_nami_id_column.php b/database/migrations/2023_02_05_235353_edit_activities_nami_id_column.php new file mode 100644 index 00000000..f18a3e1c --- /dev/null +++ b/database/migrations/2023_02_05_235353_edit_activities_nami_id_column.php @@ -0,0 +1,28 @@ +unsignedInteger('nami_id')->nullable(true)->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +}; diff --git a/tests/Feature/Initialize/PullMembershipsActionTest.php b/tests/Feature/Initialize/PullMembershipsActionTest.php index 06fda512..6dca7bb2 100644 --- a/tests/Feature/Initialize/PullMembershipsActionTest.php +++ b/tests/Feature/Initialize/PullMembershipsActionTest.php @@ -9,6 +9,7 @@ use App\Fee; use App\Gender; use App\Group; use App\Member\Member; +use App\Member\Membership; use App\Nationality; use App\Payment\Subscription; use App\Subactivity; @@ -139,4 +140,63 @@ class PullMembershipsActionTest extends TestCase 'subactivity_id' => Subactivity::nami(55)->id, ]); } + + public function testUpdateExistingMembership(): void + { + $member = Member::factory() + ->defaults() + ->for(Group::factory()->name('Gruppe')->inNami(90)) + ->inNami(1001) + ->has(Membership::factory()->in('Leiter', 50, 'Rover', 60)->inNami(5060)) + ->create(); + app(MembershipFake::class)->fetches(1001, [ + [ + 'id' => 5060, + 'entries_aktivBis' => '2021-08-23 00:00:00', + 'entries_aktivVon' => '2021-08-22 00:00:00', + 'entries_taetigkeit' => 'Leiter (50)', + 'entries_gruppierung' => 'Gruppe', + 'entries_untergliederung' => 'Rover', + ], + ]); + + app(PullMembershipsAction::class)->handle($member); + + $this->assertDatabaseCount('memberships', 1); + $this->assertDatabaseHas('memberships', [ + 'nami_id' => 5060, + 'from' => '2021-08-22 00:00:00', + 'to' => '2021-08-23 00:00:00', + ]); + } + + public function testDeleteExistingMembership(): void + { + $member = Member::factory() + ->defaults() + ->for(Group::factory()->name('Gruppe')->inNami(90)) + ->inNami(1001) + ->has(Membership::factory()->in('Leiter', 50, 'Rover', 60)->inNami(5060)) + ->create(); + app(MembershipFake::class)->fetches(1001, []); + + app(PullMembershipsAction::class)->handle($member); + + $this->assertDatabaseCount('memberships', 0); + } + + public function testDontDeleteLocalMemberships(): void + { + $member = Member::factory() + ->defaults() + ->for(Group::factory()->name('Gruppe')->inNami(90)) + ->inNami(1001) + ->has(Membership::factory()->inLocal('Leiter', 'Rover')->local()) + ->create(); + app(MembershipFake::class)->fetches(1001, []); + + app(PullMembershipsAction::class)->handle($member); + + $this->assertDatabaseCount('memberships', 1); + } }