Fixed: Add group to membership

This commit is contained in:
Philipp Lang 2023-02-21 13:02:31 +01:00
parent de4daf705f
commit 58fdfe1e81
6 changed files with 43 additions and 11 deletions

View File

@ -19,7 +19,7 @@ class Membership extends Model
use HasNamiField; use HasNamiField;
/** @var array<int, string> */ /** @var array<int, string> */
public $fillable = ['subactivity_id', 'activity_id', 'group_id', 'member_id', 'nami_id', 'from', 'to', 'promised_at']; public $guarded = [];
/** @var array<string, string> */ /** @var array<string, string> */
public $casts = [ public $casts = [

View File

@ -3,6 +3,7 @@
namespace App\Membership\Actions; namespace App\Membership\Actions;
use App\Activity; use App\Activity;
use App\Group;
use App\Member\Member; use App\Member\Member;
use App\Member\Membership; use App\Member\Membership;
use App\Setting\NamiSettings; use App\Setting\NamiSettings;
@ -21,7 +22,7 @@ class MembershipStoreAction
{ {
use AsAction; 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(); $from = now()->startOfDay();
@ -31,7 +32,7 @@ class MembershipStoreAction
try { try {
$namiId = $settings->login()->putMembership($member->nami_id, NamiMembership::from([ $namiId = $settings->login()->putMembership($member->nami_id, NamiMembership::from([
'startsAt' => $from, 'startsAt' => $from,
'groupId' => $member->group->nami_id, 'groupId' => $group->nami_id,
'activityId' => $activity->nami_id, 'activityId' => $activity->nami_id,
'subactivityId' => $subactivity->nami_id, 'subactivityId' => $subactivity->nami_id,
])); ]));
@ -44,7 +45,9 @@ class MembershipStoreAction
'activity_id' => $activity->id, 'activity_id' => $activity->id,
'subactivity_id' => $subactivity->id, 'subactivity_id' => $subactivity->id,
'promised_at' => $promisedAt, '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)) { if ($activity->hasNami && ($subactivity->id === null || $subactivity->hasNami)) {
@ -85,6 +88,7 @@ class MembershipStoreAction
$member, $member,
Activity::find($request->activity_id), Activity::find($request->activity_id),
$request->subactivity_id ? Subactivity::find($request->subactivity_id) : null, $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, $request->promised_at ? Carbon::parse($request->promised_at) : null,
$settings, $settings,
); );

@ -1 +1 @@
Subproject commit 0ee77550774715324e2fa66ca2b821d8a0e19880 Subproject commit 315be84cf38f1a01f890180099d3b2156bf344b9

View File

@ -42,7 +42,7 @@ class StoreTest extends TestCase
$response = $this->from('/member')->post( $response = $this->from('/member')->post(
"/member/{$member->id}/membership", "/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'); $response->assertRedirect('/member');
@ -53,6 +53,7 @@ class StoreTest extends TestCase
'subactivity_id' => $activity->subactivities->first()->id, 'subactivity_id' => $activity->subactivities->first()->id,
'nami_id' => 133, 'nami_id' => 133,
'promised_at' => now()->format('Y-m-d'), 'promised_at' => now()->format('Y-m-d'),
'group_id' => $member->group->id,
]); ]);
app(MembershipFake::class)->assertCreated(6, [ app(MembershipFake::class)->assertCreated(6, [
'untergliederungId' => 4, 'untergliederungId' => 4,
@ -75,7 +76,7 @@ class StoreTest extends TestCase
$this->from('/member')->post( $this->from('/member')->post(
"/member/{$member->id}/membership", "/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', [ $this->assertDatabaseHas('memberships', [
@ -98,7 +99,7 @@ class StoreTest extends TestCase
$this->from('/member')->post( $this->from('/member')->post(
"/member/{$member->id}/membership", "/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', [ $this->assertDatabaseHas('memberships', [
@ -157,6 +158,27 @@ class StoreTest extends TestCase
$this->assertErrors(['subactivity_id' => 'Untertätigkeit ist nicht vorhanden.'], $response); $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 public function testSubactivityCanBeEmpty(): void
{ {
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
@ -173,7 +195,7 @@ class StoreTest extends TestCase
$this->post( $this->post(
"/member/{$member->id}/membership", "/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); $this->assertEquals(1506, $member->fresh()->version);
@ -211,7 +233,7 @@ class StoreTest extends TestCase
$response = $this->post( $response = $this->post(
"/member/{$member->id}/membership", "/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); $this->assertErrors(['nami' => $validationError], $response);

View File

@ -24,7 +24,7 @@ class UpdateTest extends TestCase
$this->login()->loginNami(); $this->login()->loginNami();
} }
public function testItCreatesAMembership(): void public function testItUpdatesAMembership(): void
{ {
$this->withoutExceptionHandling(); $this->withoutExceptionHandling();
$activity = Activity::factory()->hasAttached(Subactivity::factory())->create(); $activity = Activity::factory()->hasAttached(Subactivity::factory())->create();

View File

@ -3,6 +3,7 @@
namespace Tests\RequestFactories; namespace Tests\RequestFactories;
use App\Activity; use App\Activity;
use App\Group;
use App\Subactivity; use App\Subactivity;
use Carbon\Carbon; use Carbon\Carbon;
use Worksome\RequestFactories\RequestFactory; use Worksome\RequestFactories\RequestFactory;
@ -54,4 +55,9 @@ class MembershipRequestFactory extends RequestFactory
'promised_at' => $value->format('Y-m-d'), 'promised_at' => $value->format('Y-m-d'),
]); ]);
} }
public function group(Group $group): self
{
return $this->state(['group_id' => $group->id]);
}
} }