From 08fbb731a8c3c796a3c05ed40273da1d52e4ff37 Mon Sep 17 00:00:00 2001 From: philipp lang Date: Sat, 4 Jul 2020 20:47:30 +0200 Subject: [PATCH] Add memberships --- src/Api.php | 17 ++++++ src/Member.php | 15 +++++ src/Membership.php | 60 +++++++++++++++++++ tests/Unit/PullMembershipsTest.php | 50 ++++++++++++++++ tests/json/membership-68.json | 17 ++++++ tests/json/membership-69.json | 17 ++++++ tests/json/membership-overview.json | 92 +++++++++++++++++++++++++++++ 7 files changed, 268 insertions(+) create mode 100644 src/Membership.php create mode 100644 tests/Unit/PullMembershipsTest.php create mode 100644 tests/json/membership-68.json create mode 100644 tests/json/membership-69.json create mode 100644 tests/json/membership-overview.json diff --git a/src/Api.php b/src/Api.php index 6c6f06f..01400f2 100644 --- a/src/Api.php +++ b/src/Api.php @@ -69,6 +69,23 @@ class Api { return collect($this->http()->get(self::$url.'/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/'.$groupId.'/flist')->json()['data']); } + public function membershipsOf($memberId): Collection { + return collect($this->http()->get(self::$url.'/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/'.$memberId.'/flist')->json()['data']); + } + + 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); + + Log::debug('Membership Request '.$memberId.' - '.$membershipId, [ + 'url' => $url, + 'response' => $response->body(), + 'json' => $response->json() + ]); + + return $response->json()['data']; + } + public function member($groupId, $memberId) { $url = self::$url.'/ica/rest/nami/mitglied/filtered-for-navigation/gruppierung/gruppierung/'.$groupId.'/'.$memberId; $response = $this->http()->get($url); diff --git a/src/Member.php b/src/Member.php index 9ee8d47..fcb9391 100644 --- a/src/Member.php +++ b/src/Member.php @@ -4,6 +4,7 @@ namespace Zoomyboy\LaravelNami; use Carbon\Carbon; use Illuminate\Database\Eloquent\Model; +use Illuminate\Support\LazyCollection; class Member extends Model { @@ -87,4 +88,18 @@ class Member extends Model { return parent::setAttribute($key, $value); } + public function memberships() { + $memberships = Nami::membershipsOf($this->id); + + return LazyCollection::make(function() use ($memberships) { + foreach ($memberships as $membership) { + yield $this->membership($membership['id']); + } + }); + } + + public function membership($id): Membership { + return Membership::fromNami(Nami::membership($this->id, $id)); + } + } diff --git a/src/Membership.php b/src/Membership.php new file mode 100644 index 0000000..a74420c --- /dev/null +++ b/src/Membership.php @@ -0,0 +1,60 @@ + 'id', + 'gruppierungId' => 'group_id', + 'taetigkeitId' => 'activity_id', + 'untergliederungId' => 'subactivity_id', + 'aktivVon' => 'starts_at', + 'aktivBis' => 'ends_at' + ]; + + protected $casts = []; + + protected $nullable = []; + + protected $guarded = []; + + public static function fromNami($item) { + $item = collect($item) + ->only(array_keys(static::$overviewAttributes)) + ->mapWithKeys(function($item, $key) { + return [ data_get(static::$overviewAttributes, $key, $key) => $item ]; + }) + ->toArray(); + return (new self($item)); + } + + public function __construct($membership) { + parent::__construct($membership); + } + + public function setAttribute($key, $value) { + if (in_array($key, $this->nullable) && $value === '') { + return parent::setAttribute($key, null); + } + + return parent::setAttribute($key, $value); + } + + public function setStartsAtAttribute($date) { + $this->attributes['starts_at'] = empty($date) + ? null + : Carbon::parse($date)->format('Y-m-d'); + } + + public function setEndsAtAttribute($date) { + $this->attributes['ends_at'] = empty($date) + ? null + : Carbon::parse($date)->format('Y-m-d'); + } + +} diff --git a/tests/Unit/PullMembershipsTest.php b/tests/Unit/PullMembershipsTest.php new file mode 100644 index 0000000..a502bfd --- /dev/null +++ b/tests/Unit/PullMembershipsTest.php @@ -0,0 +1,50 @@ + ['id', [68, 69]], + 'group_id' => ['group_id', [103,104]], + 'activity_id' => ['activity_id', [33, 34]], + 'subactivity_id' => ['subactivity_id', [55, 56]], + 'starts_at' => ['starts_at', ['2017-02-11', '2017-11-12']], + 'ends_at' => ['ends_at', ['2017-03-11', null]] + ]; + } + + /** + * @dataProvider dataProvider + */ + public function test_get_all_memberships_of_a_member($key, $values) { + Http::fake(array_merge($this->login(), [ + 'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16/flist' => Http::response($this->fakeJson('membership-overview.json'), 200), + 'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16/68' => Http::response($this->fakeJson('membership-68.json'), 200), + 'https://nami.dpsg.de/ica/rest/nami/zugeordnete-taetigkeiten/filtered-for-navigation/gruppierung-mitglied/mitglied/16/69' => Http::response($this->fakeJson('membership-69.json'), 200) + ])); + + $this->setCredentials(); + + Nami::login(); + $member = new Member(['id' => 16]); + + $memberships = $member->memberships(); + + foreach ($memberships as $i => $m) { + $this->assertSame($values[$i], $m->toArray()[$key]); + } + + Http::assertSentCount(5); + } + +} diff --git a/tests/json/membership-68.json b/tests/json/membership-68.json new file mode 100644 index 0000000..0602bb3 --- /dev/null +++ b/tests/json/membership-68.json @@ -0,0 +1,17 @@ +{ + "success": true, + "data": { + "id": 68, + "gruppierung": "Diözesanleitung Köln 100000", + "gruppierungId": 103, + "taetigkeit": "€ ReferentIn", + "taetigkeitId": 33, + "untergliederung": "Pfadfinder", + "untergliederungId": 55, + "aktivVon": "2017-02-11 00:00:00", + "aktivBis": "2017-03-11 00:00:00" + }, + "responseType": null, + "message": null, + "title": null +} diff --git a/tests/json/membership-69.json b/tests/json/membership-69.json new file mode 100644 index 0000000..97495e1 --- /dev/null +++ b/tests/json/membership-69.json @@ -0,0 +1,17 @@ +{ + "success": true, + "data": { + "id": 69, + "gruppierung": "Diözesanleitung Köln 100000", + "gruppierungId": 104, + "taetigkeit": "€ ReferentIn", + "taetigkeitId": 34, + "untergliederung": "Pfadfinder", + "untergliederungId": 56, + "aktivVon": "2017-11-12 00:00:00", + "aktivBis": "" + }, + "responseType": null, + "message": null, + "title": null +} diff --git a/tests/json/membership-overview.json b/tests/json/membership-overview.json new file mode 100644 index 0000000..6d52d39 --- /dev/null +++ b/tests/json/membership-overview.json @@ -0,0 +1,92 @@ +{ + "success": true, + "data": [ + { + "id": 68 + }, + { + "id": 69 + } + ], + "responseType": "OK", + "totalEntries": 14, + "metaData": { + "totalProperty": "totalEntries", + "root": "data", + "id": "id", + "fields": [ + { + "name": "id", + "header": null, + "hidden": false, + "width": 80 + }, + { + "name": "descriptor", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_untergliederung", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_taetigkeit", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_beitragsArt", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_aktivVon", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_caeaGroup", + "header": null, + "hidden": true, + "flex": 3 + }, + { + "name": "entries_aktivBis", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_caeaGroupForGf", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_gruppierung", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_mitglied", + "header": null, + "hidden": false, + "flex": 3 + }, + { + "name": "entries_anlagedatum", + "header": null, + "hidden": false, + "flex": 3 + } + ] + } +}