diff --git a/app/Actions/InsertMembershipsAction.php b/app/Actions/InsertMembershipsAction.php index 959f05ff..301ab989 100644 --- a/app/Actions/InsertMembershipsAction.php +++ b/app/Actions/InsertMembershipsAction.php @@ -79,7 +79,7 @@ class InsertMembershipsAction private function singleStrategy(Member $member, Group $group, NamiMembershipEntry $membershipEntry): ?Membership { $membership = $this->api()->membership($member->nami_id, $membershipEntry->id); - app(ActivityCreator::class)->createFor($this->api(), $membership->groupId); + app(ActivityCreator::class)->createFor($this->api(), $membership->group()); $activity = Activity::nami($membership->activityId); if (!$activity) { diff --git a/app/Activity/Actions/ActivityUpdateAction.php b/app/Activity/Actions/ActivityUpdateAction.php index 8ae53f60..1f45ead2 100644 --- a/app/Activity/Actions/ActivityUpdateAction.php +++ b/app/Activity/Actions/ActivityUpdateAction.php @@ -10,10 +10,16 @@ use Illuminate\Validation\ValidationException; use Lorisleiva\Actions\ActionRequest; use Lorisleiva\Actions\Concerns\AsAction; +/** + * @template Payload of array{name: string, subactivities: array} + */ class ActivityUpdateAction { use AsAction; + /** + * @param Payload $payload + */ public function handle(Activity $activity, array $payload): void { DB::transaction(function() use ($activity, $payload) { @@ -22,6 +28,9 @@ class ActivityUpdateAction }); } + /** + * @return array + */ public function rules(): array { return [ @@ -43,20 +52,21 @@ class ActivityUpdateAction /** * @todo handle this with a model event on the pivot model + * @param Payload $payload */ private function validateNami(Activity $activity, array $payload): void { - if ($activity->name !== data_get($payload, 'name', '')) { + if ($activity->name !== $payload['name']) { throw ValidationException::withMessages(['nami_id' => 'Aktivität ist in NaMi. Update des Namens nicht möglich.']); } - $removingSubactivities = $activity->subactivities()->whereNotIn('id', data_get($payload, 'subactivities'))->pluck('id'); + $removingSubactivities = $activity->subactivities()->whereNotIn('id', $payload['subactivities'])->pluck('id'); if ($removingSubactivities->first(fn ($subactivity) => Subactivity::find($subactivity)->hasNami)) { throw ValidationException::withMessages(['nami_id' => 'Untertätigkeit kann nicht entfernt werden.']); } - $addingSubactivities = collect(data_get($payload, 'subactivities'))->filter(fn ($subactivityId) => $activity->subactivities->doesntContain($subactivityId)); + $addingSubactivities = collect($payload['subactivities'])->filter(fn ($subactivityId) => $activity->subactivities->doesntContain($subactivityId)); if ($addingSubactivities->first(fn ($subactivity) => Subactivity::find($subactivity)->hasNami)) { throw ValidationException::withMessages(['nami_id' => 'Untertätigkeit kann nicht hinzugefügt werden.']); diff --git a/app/Initialize/ActivityCreator.php b/app/Initialize/ActivityCreator.php index fb9d5b92..f0f170a5 100644 --- a/app/Initialize/ActivityCreator.php +++ b/app/Initialize/ActivityCreator.php @@ -3,6 +3,7 @@ namespace App\Initialize; use Zoomyboy\LaravelNami\Api; +use Zoomyboy\LaravelNami\Data\Group; class ActivityCreator { @@ -50,10 +51,10 @@ class ActivityCreator '€ LeiterIn', ]; - public function createFor(Api $api, int $groupId): void + public function createFor(Api $api, Group $groupId): void { - $api->activities($groupId)->each(function ($activity) use ($api) { - $activity = \App\Activity::updateOrCreate(['nami_id' => $activity->id], [ + $api->activities($groupId)->each(function ($activity) { + $localActivity = \App\Activity::updateOrCreate(['nami_id' => $activity->id], [ 'nami_id' => $activity->id, 'name' => $activity->name, 'is_try' => in_array($activity->name, $this->tries), @@ -63,7 +64,7 @@ class ActivityCreator ]); $groups = []; - $api->subactivitiesOf($activity->nami_id)->each(function ($group) use (&$groups) { + $activity->subactivities()->each(function ($group) use (&$groups) { $group = \App\Subactivity::updateOrCreate(['nami_id' => $group->id], [ 'nami_id' => $group->id, 'name' => $group->name, @@ -72,7 +73,7 @@ class ActivityCreator ]); $groups[] = $group->id; }); - $activity->subactivities()->sync($groups); + $localActivity->subactivities()->sync($groups); }); } } diff --git a/app/Initialize/InitializeActivities.php b/app/Initialize/InitializeActivities.php index f1fe452c..7f485c00 100644 --- a/app/Initialize/InitializeActivities.php +++ b/app/Initialize/InitializeActivities.php @@ -2,7 +2,7 @@ namespace App\Initialize; -use DB; +use Illuminate\Support\Facades\DB; use Zoomyboy\LaravelNami\Api; class InitializeActivities @@ -16,8 +16,7 @@ class InitializeActivities public function handle(): void { - $groupId = $this->api->groups()->first()->id; - app(ActivityCreator::class)->createFor($this->api, $groupId); + app(ActivityCreator::class)->createFor($this->api, $this->api->groups()->first()); } public function restore(): void diff --git a/app/Initialize/InitializeGroups.php b/app/Initialize/InitializeGroups.php index d4023b11..880b12b0 100644 --- a/app/Initialize/InitializeGroups.php +++ b/app/Initialize/InitializeGroups.php @@ -5,6 +5,7 @@ namespace App\Initialize; use App\Group; use DB; use Zoomyboy\LaravelNami\Api; +use Zoomyboy\LaravelNami\Data\Group as NamiGroup; class InitializeGroups { @@ -17,18 +18,18 @@ class InitializeGroups public function handle(): void { - $this->api->groups()->each(function ($group) { + $this->api->groups(null)->each(function ($group) { $parent = Group::updateOrCreate(['nami_id' => $group->id], ['nami_id' => $group->id, 'name' => $group->name]); - $this->syncChildren($group->id, $parent); + $this->syncChildren($group, $parent); }); } - private function syncChildren(int $groupId, Group $parent): void + private function syncChildren(NamiGroup $namiParent, Group $parent): void { - $this->api->subgroupsOf($groupId)->each(function ($subgroup) use ($parent) { + $this->api->groups($namiParent)->each(function ($subgroup) use ($parent) { $newParent = Group::updateOrCreate(['nami_id' => $subgroup->id], ['nami_id' => $subgroup->id, 'name' => $subgroup->name, 'parent_id' => $parent->id]); - $this->syncChildren($subgroup->id, $newParent); + $this->syncChildren($subgroup, $newParent); }); } diff --git a/packages/laravel-nami b/packages/laravel-nami index 07d1e676..3c3d0df5 160000 --- a/packages/laravel-nami +++ b/packages/laravel-nami @@ -1 +1 @@ -Subproject commit 07d1e67698ef09fe274a35f3d822ef3dd526ff22 +Subproject commit 3c3d0df50e56ce09a09f8624e9491fdd43369dfa diff --git a/phpstan.neon b/phpstan.neon index 2aa16f05..8b4fe328 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -294,31 +294,6 @@ parameters: count: 1 path: tests/Unit/Mailman/ServiceTest.php - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Activity\\:\\:fromNami\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Activity.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Activity\\:\\:fromNami\\(\\) has parameter \\$item with no type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Activity.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Activity\\:\\:subactivities\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Activity.php - - - - message: "#^Unable to resolve the template type TKey in call to function collect$#" - count: 1 - path: packages/laravel-nami/src/Activity.php - - - - message: "#^Unable to resolve the template type TValue in call to function collect$#" - count: 1 - path: packages/laravel-nami/src/Activity.php - - message: "#^Property Zoomyboy\\\\LaravelNami\\\\Authentication\\\\FakeCookie\\:\\:\\$authenticated type has no value type specified in iterable type array\\.$#" count: 1 @@ -459,21 +434,6 @@ parameters: count: 1 path: packages/laravel-nami/src/Gender.php - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Group\\:\\:fees\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Group.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Group\\:\\:fromResponse\\(\\) has parameter \\$response with no value type specified in iterable type array\\.$#" - count: 1 - path: packages/laravel-nami/src/Group.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Group\\:\\:subgroups\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Group.php - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Logger\\:\\:__construct\\(\\) has parameter \\$options with no type specified\\.$#" count: 1 @@ -674,41 +634,6 @@ parameters: count: 1 path: packages/laravel-nami/src/Region.php - - - message: "#^Call to an undefined static method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:getNullValue\\(\\)\\.$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:fromNami\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:fromNami\\(\\) has parameter \\$item with no type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:getIsNullAttribute\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Subactivity\\:\\:getNameAttribute\\(\\) has no return type specified\\.$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - - - message: "#^Unable to resolve the template type TKey in call to function collect$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - - - message: "#^Unable to resolve the template type TValue in call to function collect$#" - count: 1 - path: packages/laravel-nami/src/Subactivity.php - - message: "#^Method Zoomyboy\\\\LaravelNami\\\\Tests\\\\Stub\\\\Member\\:\\:findByNamiId\\(\\) has no return type specified\\.$#" count: 1 diff --git a/tests/Unit/InitializeGroupsTest.php b/tests/Unit/InitializeGroupsTest.php index 21575936..20bd0ec6 100644 --- a/tests/Unit/InitializeGroupsTest.php +++ b/tests/Unit/InitializeGroupsTest.php @@ -8,7 +8,7 @@ use Illuminate\Foundation\Testing\DatabaseTransactions; use PHPUnit\Framework\MockObject\Stub; use Tests\TestCase; use Zoomyboy\LaravelNami\Api; -use Zoomyboy\LaravelNami\Group; +use Zoomyboy\LaravelNami\Data\Group; class InitializeGroupsTest extends TestCase { @@ -34,13 +34,12 @@ class InitializeGroupsTest extends TestCase public function testItSynchsAGroupWithASingleNodeAndNoChildren(): void { + $parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]); $this->api->method('groups')->will($this->returnValueMap([ - [ - null, - collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]), - ], + [null, collect([$parentGroup])], + [$parentGroup, collect([])], ])); - $this->api->method('subgroupsOf')->willReturn(collect([])); + $this->api->method('groups')->willReturn(collect([])); (new InitializeGroups($this->api))->handle(); @@ -54,13 +53,11 @@ class InitializeGroupsTest extends TestCase public function testItDoesntCreateAGroupTwiceWithTheSameNamiId(): void { GroupModel::factory()->create(['nami_id' => 150]); + $parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]); $this->api->method('groups')->will($this->returnValueMap([ - [ - null, - collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]), - ], + [null, collect([$parentGroup])], + [$parentGroup, collect([])], ])); - $this->api->method('subgroupsOf')->willReturn(collect([])); (new InitializeGroups($this->api))->handle(); @@ -69,17 +66,13 @@ class InitializeGroupsTest extends TestCase public function testItSynchsSubgroups(): void { - GroupModel::factory()->create(['nami_id' => 150]); - $this->api->method('groups')->willReturn( - collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]) - ); - $this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) { - if (150 === $groupId) { - return collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]); - } - - return collect([]); - }); + $parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]); + $subgroup = Group::from(['id' => 200, 'name' => 'subgroup', 'parentId' => 150]); + $this->api->method('groups')->will($this->returnValueMap([ + [null, collect([$parentGroup])], + [$parentGroup, collect([$subgroup])], + [$subgroup, collect([])], + ])); (new InitializeGroups($this->api))->handle(); @@ -91,20 +84,15 @@ class InitializeGroupsTest extends TestCase public function testItSynchsSubgroupsOfSubgroups(): void { - GroupModel::factory()->create(['nami_id' => 150]); - $this->api->method('groups')->willReturn( - collect([(new Group())->setParentId(null)->setId(150)->setName('lorem')]) - ); - $this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) { - if (150 === $groupId) { - return collect([(new Group())->setParentId(150)->setId(200)->setName('subgroup')]); - } - if (200 === $groupId) { - return collect([(new Group())->setParentId(200)->setId(201)->setName('subsubgroup')]); - } - - return collect([]); - }); + $parentGroup = Group::from(['id' => 150, 'name' => 'lorem', 'parentId' => null]); + $subgroup = Group::from(['id' => 200, 'name' => 'subgroup', 'parentId' => 150]); + $subsubgroup = Group::from(['id' => 250, 'name' => 'subsubgroup', 'parentId' => 200]); + $this->api->method('groups')->will($this->returnValueMap([ + [null, collect([$parentGroup])], + [$parentGroup, collect([$subgroup])], + [$subgroup, collect([$subsubgroup])], + [$subsubgroup, collect([])], + ])); (new InitializeGroups($this->api))->handle(); @@ -114,16 +102,13 @@ class InitializeGroupsTest extends TestCase public function testItAssignsIdAndParentToAnExistingSubgroup(): void { GroupModel::factory()->create(['nami_id' => 200]); - $this->api->method('groups')->willReturn( - collect([(new Group())->setParentId(null)->setId(150)->setName('root')]) - ); - $this->api->method('subgroupsOf')->willReturnCallback(function ($groupId) { - if (150 === $groupId) { - return collect([(new Group())->setParentId(150)->setId(200)->setName('child')]); - } - - return collect([]); - }); + $parentGroup = Group::from(['id' => 150, 'name' => 'root', 'parentId' => null]); + $subgroup = Group::from(['id' => 200, 'name' => 'child', 'parentId' => 150]); + $this->api->method('groups')->will($this->returnValueMap([ + [null, collect([$parentGroup])], + [$parentGroup, collect([$subgroup])], + [$subgroup, collect([])], + ])); (new InitializeGroups($this->api))->handle();