From 2c0b9217ec7e216e32fb6b4eed7b4997fd1c2eba Mon Sep 17 00:00:00 2001 From: philipp lang Date: Fri, 17 Feb 2023 02:14:32 +0100 Subject: [PATCH] Fixed: Dont store local memberships in nami --- .../Actions/MembershipStoreAction.php | 30 +++++++----- tests/Feature/Membership/StoreTest.php | 46 +++++++++++++++++++ 2 files changed, 64 insertions(+), 12 deletions(-) diff --git a/app/Membership/Actions/MembershipStoreAction.php b/app/Membership/Actions/MembershipStoreAction.php index d4bec6de..b3342a49 100644 --- a/app/Membership/Actions/MembershipStoreAction.php +++ b/app/Membership/Actions/MembershipStoreAction.php @@ -25,25 +25,31 @@ class MembershipStoreAction { $from = now()->startOfDay(); - try { - $namiId = $settings->login()->putMembership($member->nami_id, NamiMembership::from([ - 'startsAt' => $from, - 'groupId' => $member->group->nami_id, - 'activityId' => $activity->nami_id, - 'subactivityId' => $subactivity ? $subactivity->nami_id : null, - ])); - } catch (HttpException $e) { - throw ValidationException::withMessages(['nami' => htmlspecialchars($e->getMessage())]); + $subactivity = $subactivity ?: new Subactivity(['nami_id' => null, 'id' => null]); + + if ($activity->hasNami && ($subactivity->id === null || $subactivity->hasNami)) { + try { + $namiId = $settings->login()->putMembership($member->nami_id, NamiMembership::from([ + 'startsAt' => $from, + 'groupId' => $member->group->nami_id, + 'activityId' => $activity->nami_id, + 'subactivityId' => $subactivity->nami_id, + ])); + } catch (HttpException $e) { + throw ValidationException::withMessages(['nami' => htmlspecialchars($e->getMessage())]); + } } $membership = $member->memberships()->create([ 'activity_id' => $activity->id, - 'subactivity_id' => $subactivity ? $subactivity->id : null, + 'subactivity_id' => $subactivity->id, 'promised_at' => $promisedAt, - ...['nami_id' => $namiId, 'group_id' => $member->group->id, 'from' => $from], + ...['nami_id' => $namiId ?? null, 'group_id' => $member->group->id, 'from' => $from], ]); - $member->syncVersion(); + if ($activity->hasNami && ($subactivity->id === null || $subactivity->hasNami)) { + $member->syncVersion(); + } return $membership; } diff --git a/tests/Feature/Membership/StoreTest.php b/tests/Feature/Membership/StoreTest.php index a4fa79de..d85b4256 100644 --- a/tests/Feature/Membership/StoreTest.php +++ b/tests/Feature/Membership/StoreTest.php @@ -63,6 +63,52 @@ class StoreTest extends TestCase ]); } + public function testItDoesntFireNamiWhenMembershipIsLocal(): void + { + $this->withoutExceptionHandling(); + $member = Member::factory() + ->defaults() + ->for(Group::factory()->inNami(1400)) + ->inNami(6) + ->create(); + $activity = Activity::factory()->hasAttached(Subactivity::factory())->create(); + + $this->from('/member')->post( + "/member/{$member->id}/membership", + MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create() + ); + + $this->assertDatabaseHas('memberships', [ + 'member_id' => $member->id, + 'activity_id' => $activity->id, + 'subactivity_id' => $activity->subactivities->first()->id, + 'nami_id' => null, + ]); + } + + public function testItDoesntFireNamiWhenSubactivityIsLocal(): void + { + $this->withoutExceptionHandling(); + $member = Member::factory() + ->defaults() + ->for(Group::factory()->inNami(1400)) + ->inNami(6) + ->create(); + $activity = Activity::factory()->inNami(666)->hasAttached(Subactivity::factory())->create(); + + $this->from('/member')->post( + "/member/{$member->id}/membership", + MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create() + ); + + $this->assertDatabaseHas('memberships', [ + 'member_id' => $member->id, + 'activity_id' => $activity->id, + 'subactivity_id' => $activity->subactivities->first()->id, + 'nami_id' => null, + ]); + } + public function testActivityIsRequired(): void { $member = Member::factory()