diff --git a/src/Activity.php b/src/Activity.php new file mode 100644 index 0000000..a2009fd --- /dev/null +++ b/src/Activity.php @@ -0,0 +1,27 @@ +only(['descriptor', 'id']) + ->mapWithKeys(function($item,$key) { + if ($key == 'id') { return ['id' => $item]; } + return ['name' => $item]; + })->toArray(); + + return (new self($item)); + } + + public function subactivities() { + return Nami::subactivitiesOf($this->id); + } + +} diff --git a/src/Api.php b/src/Api.php index 01400f2..f847279 100644 --- a/src/Api.php +++ b/src/Api.php @@ -73,6 +73,12 @@ class Api { return collect($this->http()->get(self::$url.'/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/'.$memberId.'/flist')->json()['data']); } + public function subactivitiesOf($activityId) { + return collect($this->http()->get(self::$url.'/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/'.$activityId)->json()['data'])->map(function($subactivity) { + return Subactivity::fromNami($subactivity); + });; + } + public function membership($memberId, $membershipId) { $url = self::$url.'/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/'.$memberId.'/'.$membershipId; $response = $this->http()->get($url); @@ -148,6 +154,12 @@ class Api { }); } + public function activities($groupId) { + return collect($this->http()->get(self::$url."/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/".$groupId)['data'])->map(function($activity) { + return Activity::fromNami($activity); + }); + } + private function singleMemberFallback($groupId, $memberId) { $url = self::$url.'/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/'.$groupId.'/flist'; $response = $this->http()->get($url); @@ -190,14 +202,6 @@ class Api { return json_decode((string)$response->getBody()); } - public function activities() { - $response = $this->client->get("/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/{$this->user->getNamiGroupId()}", [ - 'cookies' => $this->cookie - ]); - - return json_decode((string)$response->getBody()); - } - public function groupForActivity($activityId) { $response = $this->client->get("/ica/rest//nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/{$activityId}", [ diff --git a/src/Group.php b/src/Group.php index ce56444..65e5843 100644 --- a/src/Group.php +++ b/src/Group.php @@ -2,6 +2,8 @@ namespace Zoomyboy\LaravelNami; +use Illuminate\Support\Collection; +use Illuminate\Support\LazyCollection; use Illuminate\Contracts\Support\Arrayable; class Group implements Arrayable { @@ -43,4 +45,8 @@ class Group implements Arrayable { return Member::fromNami(Nami::member($this->id, $id)); } + public function activities(): Collection { + return Nami::activities($this->id); + } + } diff --git a/src/Subactivity.php b/src/Subactivity.php new file mode 100644 index 0000000..b44b023 --- /dev/null +++ b/src/Subactivity.php @@ -0,0 +1,31 @@ +only(['descriptor', 'id']) + ->mapWithKeys(function($item,$key) { + if ($key == 'id') { return ['id' => $item]; } + return ['name' => $item]; + })->toArray(); + + return (new self($item)); + } + + public function getNameAttribute() { + return ucfirst($this->attributes['name']); + } + + public function getIsNullAttribute() { + return $this->attributes['id'] == self::getNullValue(); + } + +} diff --git a/tests/Unit/PullActivitiesTest.php b/tests/Unit/PullActivitiesTest.php new file mode 100644 index 0000000..58a8f3d --- /dev/null +++ b/tests/Unit/PullActivitiesTest.php @@ -0,0 +1,55 @@ +login(),[ + 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' => Http::response($this->groupsResponse, 200), + 'https://nami.dpsg.de/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/103' => Http::response($this->fakeJson('activities.json'), 200) + ])); + + $this->setCredentials(); + + Nami::login(); + + $this->assertSame([ + [ 'name' => 'Ac1', 'id' => 4 ], + [ 'name' => 'Ac2', 'id' => 3 ] + ], Nami::group(103)->activities()->toArray()); + + Http::assertSentCount(4); + } + + public function test_get_all_subactivities() { + Http::fake(array_merge($this->login(),[ + 'https://nami.dpsg.de/ica/rest/nami/gruppierungen/filtered-for-navigation/gruppierung/node/root' => Http::response($this->groupsResponse, 200), + 'https://nami.dpsg.de/ica/rest/nami/taetigkeitaufgruppierung/filtered/gruppierung/gruppierung/103' => Http::response($this->fakeJson('activities.json'), 200), + 'https://nami.dpsg.de/ica/rest/nami/untergliederungauftaetigkeit/filtered/untergliederung/taetigkeit/4' => Http::response($this->fakeJson('subactivities-4.json'), 200) + ])); + + $this->setCredentials(); + + Nami::login(); + + $this->assertSame([ + [ 'name' => 'Biber', 'id' => 40 ], + [ 'name' => 'Wölfling', 'id' => 30 ] + ], Nami::group(103)->activities()->first()->subactivities()->toArray()); + + Http::assertSentCount(5); + } + +} diff --git a/tests/json/activities.json b/tests/json/activities.json new file mode 100644 index 0000000..98c4a6e --- /dev/null +++ b/tests/json/activities.json @@ -0,0 +1,18 @@ +{ + "success": true, + "data": [ + { + "descriptor": "Ac1", + "name": "", + "representedClass": "de.iconcept.nami.entity.taetigkeit.FunktionsTaetigkeit", + "id": 4 + }, + { + "descriptor": "Ac2", + "name": "", + "representedClass": "de.iconcept.nami.entity.taetigkeit.FunktionsTaetigkeit", + "id": 3 + } + ], + "responseType": "OK" +} diff --git a/tests/json/subactivities-4.json b/tests/json/subactivities-4.json new file mode 100644 index 0000000..82ef975 --- /dev/null +++ b/tests/json/subactivities-4.json @@ -0,0 +1,18 @@ +{ + "success": true, + "data": [ + { + "descriptor": "Biber", + "name": "", + "representedClass": "de.iconcept.nami.entity.taetigkeit.Stufe", + "id": 40 + }, + { + "descriptor": "Wölfling", + "name": "", + "representedClass": "de.iconcept.nami.entity.taetigkeit.Stufe", + "id": 30 + } + ], + "responseType": "OK" +}