diff --git a/app/Member/Membership.php b/app/Member/Membership.php index 3423ba8e..48df23cd 100644 --- a/app/Member/Membership.php +++ b/app/Member/Membership.php @@ -19,7 +19,7 @@ class Membership extends Model use HasNamiField; /** @var array */ - public $fillable = ['subactivity_id', 'activity_id', 'group_id', 'member_id', 'nami_id', 'from', 'to', 'promised_at']; + public $guarded = []; /** @var array */ public $casts = [ diff --git a/app/Membership/Actions/MembershipStoreAction.php b/app/Membership/Actions/MembershipStoreAction.php index b3342a49..dbccb97e 100644 --- a/app/Membership/Actions/MembershipStoreAction.php +++ b/app/Membership/Actions/MembershipStoreAction.php @@ -3,6 +3,7 @@ namespace App\Membership\Actions; use App\Activity; +use App\Group; use App\Member\Member; use App\Member\Membership; use App\Setting\NamiSettings; @@ -21,7 +22,7 @@ class MembershipStoreAction { use AsAction; - public function handle(Member $member, Activity $activity, ?Subactivity $subactivity, ?Carbon $promisedAt, NamiSettings $settings): Membership + public function handle(Member $member, Activity $activity, ?Subactivity $subactivity, Group $group, ?Carbon $promisedAt, NamiSettings $settings): Membership { $from = now()->startOfDay(); @@ -31,7 +32,7 @@ class MembershipStoreAction try { $namiId = $settings->login()->putMembership($member->nami_id, NamiMembership::from([ 'startsAt' => $from, - 'groupId' => $member->group->nami_id, + 'groupId' => $group->nami_id, 'activityId' => $activity->nami_id, 'subactivityId' => $subactivity->nami_id, ])); @@ -44,7 +45,9 @@ class MembershipStoreAction 'activity_id' => $activity->id, 'subactivity_id' => $subactivity->id, 'promised_at' => $promisedAt, - ...['nami_id' => $namiId ?? null, 'group_id' => $member->group->id, 'from' => $from], + 'group_id' => $group->id, + 'from' => $from, + 'nami_id' => $namiId ?? null, ]); if ($activity->hasNami && ($subactivity->id === null || $subactivity->hasNami)) { @@ -85,6 +88,7 @@ class MembershipStoreAction $member, Activity::find($request->activity_id), $request->subactivity_id ? Subactivity::find($request->subactivity_id) : null, + Group::findOrFail($request->input('group_id', -1)), $request->promised_at ? Carbon::parse($request->promised_at) : null, $settings, ); diff --git a/packages/laravel-nami b/packages/laravel-nami index 0ee77550..315be84c 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 0ee77550774715324e2fa66ca2b821d8a0e19880 +Subproject commit 315be84cf38f1a01f890180099d3b2156bf344b9 diff --git a/tests/Feature/Membership/StoreTest.php b/tests/Feature/Membership/StoreTest.php index d85b4256..de2715fc 100644 --- a/tests/Feature/Membership/StoreTest.php +++ b/tests/Feature/Membership/StoreTest.php @@ -42,7 +42,7 @@ class StoreTest extends TestCase $response = $this->from('/member')->post( "/member/{$member->id}/membership", - MembershipRequestFactory::new()->promise(now())->in($activity, $activity->subactivities->first())->create() + MembershipRequestFactory::new()->promise(now())->in($activity, $activity->subactivities->first())->group($member->group)->create() ); $response->assertRedirect('/member'); @@ -53,6 +53,7 @@ class StoreTest extends TestCase 'subactivity_id' => $activity->subactivities->first()->id, 'nami_id' => 133, 'promised_at' => now()->format('Y-m-d'), + 'group_id' => $member->group->id, ]); app(MembershipFake::class)->assertCreated(6, [ 'untergliederungId' => 4, @@ -75,7 +76,7 @@ class StoreTest extends TestCase $this->from('/member')->post( "/member/{$member->id}/membership", - MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create() + MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->group($member->group)->create() ); $this->assertDatabaseHas('memberships', [ @@ -98,7 +99,7 @@ class StoreTest extends TestCase $this->from('/member')->post( "/member/{$member->id}/membership", - MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create() + MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->group($member->group)->create() ); $this->assertDatabaseHas('memberships', [ @@ -157,6 +158,27 @@ class StoreTest extends TestCase $this->assertErrors(['subactivity_id' => 'Untertätigkeit ist nicht vorhanden.'], $response); } + public function testItCanAddAnotherGroup(): void + { + app(MembershipFake::class)->createsSuccessfully(6, 133); + app(MemberFake::class)->shows(1400, 6, ['version' => 1506]); + $member = Member::factory()->defaults()->for(Group::factory()->inNami(1400))->inNami(6)->create(); + $group = Group::factory()->inNami(1401)->create(); + $activity = Activity::factory()->inNami(7)->hasAttached(Subactivity::factory()->inNami(8))->create(); + + $this->post( + "/member/{$member->id}/membership", + MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->group($group)->create() + ); + + $this->assertDatabaseHas('memberships', [ + 'group_id' => $group->id, + ]); + app(MembershipFake::class)->assertCreated(6, [ + 'gruppierungId' => 1401, + ]); + } + public function testSubactivityCanBeEmpty(): void { $this->withoutExceptionHandling(); @@ -173,7 +195,7 @@ class StoreTest extends TestCase $this->post( "/member/{$member->id}/membership", - MembershipRequestFactory::new()->in($activity, null)->create() + MembershipRequestFactory::new()->in($activity, null)->group($member->group)->create() ); $this->assertEquals(1506, $member->fresh()->version); @@ -211,7 +233,7 @@ class StoreTest extends TestCase $response = $this->post( "/member/{$member->id}/membership", - MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->create() + MembershipRequestFactory::new()->in($activity, $activity->subactivities->first())->group($member->group)->create() ); $this->assertErrors(['nami' => $validationError], $response); diff --git a/tests/Feature/Membership/UpdateTest.php b/tests/Feature/Membership/UpdateTest.php index 3ad327e5..16c503f6 100644 --- a/tests/Feature/Membership/UpdateTest.php +++ b/tests/Feature/Membership/UpdateTest.php @@ -24,7 +24,7 @@ class UpdateTest extends TestCase $this->login()->loginNami(); } - public function testItCreatesAMembership(): void + public function testItUpdatesAMembership(): void { $this->withoutExceptionHandling(); $activity = Activity::factory()->hasAttached(Subactivity::factory())->create(); diff --git a/tests/RequestFactories/MembershipRequestFactory.php b/tests/RequestFactories/MembershipRequestFactory.php index d6183046..fe227e87 100644 --- a/tests/RequestFactories/MembershipRequestFactory.php +++ b/tests/RequestFactories/MembershipRequestFactory.php @@ -3,6 +3,7 @@ namespace Tests\RequestFactories; use App\Activity; +use App\Group; use App\Subactivity; use Carbon\Carbon; use Worksome\RequestFactories\RequestFactory; @@ -54,4 +55,9 @@ class MembershipRequestFactory extends RequestFactory 'promised_at' => $value->format('Y-m-d'), ]); } + + public function group(Group $group): self + { + return $this->state(['group_id' => $group->id]); + } }