Add memberships
This commit is contained in:
parent
2e07084706
commit
08fbb731a8
17
src/Api.php
17
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);
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\LazyCollection;
|
||||
|
||||
class Membership extends Model {
|
||||
|
||||
protected static $overviewAttributes = [
|
||||
'id' => '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');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Zoomyboy\LaravelNami\Tests\Unit;
|
||||
|
||||
use Zoomyboy\LaravelNami\Nami;
|
||||
use Zoomyboy\LaravelNami\Tests\TestCase;
|
||||
use Illuminate\Support\Facades\Http;
|
||||
use Zoomyboy\LaravelNami\Member;
|
||||
|
||||
class PullMembershipsTest extends TestCase
|
||||
{
|
||||
public $groupsResponse = '{"success":true,"data":[{"descriptor":"Group","name":"","representedClass":"de.iconcept.nami.entity.org.Gruppierung","id":103}],"responseType":"OK"}';
|
||||
public $unauthorizedResponse = '{"success":false,"data":null,"responseType":"EXCEPTION","message":"Access denied - no right for requested operation","title":"Exception"}';
|
||||
|
||||
public function dataProvider() {
|
||||
return [
|
||||
'id' => ['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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
|
@ -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
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue