Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update group members #124

Merged
merged 3 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- Request Ajax-Api ([AjaxApi-Trait](src/Requests/Traits/AjaxApi.php))
- Update Song-Arrangement ([PR121](https://github.com/5pm-HDH/churchtools-api/pull/121))
- Update Song ([PR122](https://github.com/5pm-HDH/churchtools-api/pull/122))
- Update GroupMembers ([PR124](https://github.com/5pm-HDH/churchtools-api/pull/124))

### Changed

Expand Down
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

![example workflow](https://github.com/5pm-HDH/churchtools-api/actions/workflows/static-code-analysis.yml/badge.svg)


ChurchTools-API Client is a php based wrapper of the ChurchTools api. This api is tested with the ChurchTools version <version>3.89.0</version>
ChurchTools-API Client is a php based wrapper of the ChurchTools api. This api is tested with the ChurchTools
version <version>3.90.0</version>

## Installation

Expand Down Expand Up @@ -50,11 +50,12 @@ From now on all features of the ChurchTools-api are available.

### Requests and Models

The whole ChurchTools-api client is build on top of the Requests and Models. [Requests](/docs/out/Requests.md) provide an
interface to specify your api call by adding filtering, pagination and sorting. [Models](/docs/out/Models.md) represent the data, that
the Requests retrieve. More informations can be found in the documentation.
The whole ChurchTools-api client is build on top of the Requests and Models. [Requests](/docs/out/Requests.md) provide
an interface to specify your api call by adding filtering, pagination and sorting. [Models](/docs/out/Models.md)
represent the data, that the Requests retrieve. More informations can be found in the documentation.

All APIs with examples:

* [Person-API](/docs/out/PersonAPI.md)
* [Group-API](/docs/out/GroupAPI.md)
* [Calendar-API](/docs/out/CalendarAPI.md)
Expand All @@ -69,7 +70,8 @@ All APIs with examples:
* [File-API](/docs/out/FileAPI.md)
* [Search-API](/docs/out/SearchAPI.md)

The following short examples show the power of this ChurchTools-api client and gives a rough overview over the possibilities:
The following short examples show the power of this ChurchTools-api client and gives a rough overview over the
possibilities:

#### Example: Person-API

Expand Down Expand Up @@ -228,10 +230,13 @@ CTLog::getLog()->error("Error accourd here!");
Further information on [CTLog-Page](/docs/out/CTLog.md):

### Error-Handling

The API-Wrapper provides custom exceptions. More on this page: [Error-Handling](/docs/out/ErrorHandling.md)

### Doc-Generator
The Doc-Generator processes all Doc-Files and executes the PHP-Code examples to ensure that they are valid. More on this page: [Doc-Generator](/docs/Docs.md)

The Doc-Generator processes all Doc-Files and executes the PHP-Code examples to ensure that they are valid. More on this
page: [Doc-Generator](/docs/Docs.md)

## License

Expand Down
28 changes: 27 additions & 1 deletion docs/out/GroupAPI.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# GroupAPI

Group-Request & Group-Data:
## Group-Request & Group-Data:

```php
use CTApi\Requests\GroupRequest;
Expand Down Expand Up @@ -187,4 +187,30 @@ Group-Request & Group-Data:

//$group->requestGroupImage()?->upload("new-group-image.png");

```

## Add, remove and update group-members:

```php
use CTApi\Requests\GroupMemberRequest;

$groupId = 21;
$personId = 221;

// Create Group-Membership
$groupMember = GroupMemberRequest::addMember($groupId, $personId);

// Update Group-Membership
$groupMember->setComment("Add User via CT-Api.");
$groupMember->setFields([]);
$groupMember->setGroupTypeRoleId("21");
$groupMember->setMemberEndDate("2040-01-01");
$groupMember->setMemberStartDate("2020-01-01");
$groupMember->setWaitinglistPosition("22");

GroupMemberRequest::updateMember($groupId, $groupMember);

// Delete Group-Membership
GroupMemberRequest::removeMember($groupId, $personId);

```
8 changes: 6 additions & 2 deletions docs/src/ressources/GroupAPI.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# GroupAPI

Group-Request & Group-Data:
## Group-Request & Group-Data:

{{ \Tests\Unit\Docs\GroupRequestTest.testDocExample }}
{{ \Tests\Unit\Docs\GroupRequestTest.testDocExample }}

## Add, remove and update group-members:

{{ \Tests\Unit\Docs\GroupMemberUpdateRequestTest.testAddGroupMember }}
39 changes: 36 additions & 3 deletions src/Models/GroupMember.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@
namespace CTApi\Models;


use CTApi\Models\Interfaces\UpdatableModel;
use CTApi\Models\Traits\ExtractData;
use CTApi\Models\Traits\FillWithData;
use CTApi\Requests\PersonRequest;

class GroupMember extends AbstractModel
class GroupMember extends AbstractModel implements UpdatableModel
{
use FillWithData;
use FillWithData, ExtractData;

protected ?string $personId = null;
protected ?Person $person = null;
protected ?string $groupTypeRoleId = null;
protected ?string $memberStartDate = null;
protected ?string $comment = null;
protected ?string $memberStartDate = null;
protected ?string $memberEndDate = null;
protected ?string $waitinglistPosition = null;
protected array $fields = [];
protected array $personFields = [];


protected function fillArrayType(string $key, array $data): void
Expand All @@ -32,6 +35,18 @@ protected function fillArrayType(string $key, array $data): void
}
}

static function getModifiableAttributes(): array
{
return [
"comment",
"fields",
"groupTypeRoleId",
"memberEndDate",
"memberStartDate",
"waitinglistPos"
];
}

public function requestPerson(): ?Person
{
if ($this->getPersonId() != null) {
Expand Down Expand Up @@ -194,4 +209,22 @@ public function setFields(array $fields): GroupMember
$this->fields = $fields;
return $this;
}

/**
* @return array
*/
public function getPersonFields(): array
{
return $this->personFields;
}

/**
* @param array $personFields
* @return GroupMember
*/
public function setPersonFields(array $personFields): GroupMember
{
$this->personFields = $personFields;
return $this;
}
}
30 changes: 30 additions & 0 deletions src/Requests/GroupMemberRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php


namespace CTApi\Requests;


use CTApi\Models\GroupMember;

class GroupMemberRequest
{
public static function get(int $groupId): GroupMemberRequestBuilder
{
return new GroupMemberRequestBuilder($groupId);
}

public static function addMember(int $groupId, int $personId): GroupMember
{
return (new GroupMemberUpdateRequestBuilder($groupId))->addMember($personId);
}

public static function updateMember(int $groupId, GroupMember $groupMember): void
{
(new GroupMemberUpdateRequestBuilder($groupId))->updateMember($groupMember);
}

public static function removeMember(int $groupId, int $personId): void
{
(new GroupMemberUpdateRequestBuilder($groupId))->removeMember($personId);
}
}
48 changes: 48 additions & 0 deletions src/Requests/GroupMemberUpdateRequestBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php


namespace CTApi\Requests;


use CTApi\CTClient;
use CTApi\Exceptions\CTModelException;
use CTApi\Models\GroupMember;
use CTApi\Utils\CTResponseUtil;

class GroupMemberUpdateRequestBuilder
{

public function __construct(
private int $groupId)
{
}

public function addMember(int $personId): GroupMember
{
$client = CTClient::getClient();
$response = $client->put("/api/groups/" . $this->groupId . "/members/" . $personId);
$data = CTResponseUtil::dataAsArray($response);
return GroupMember::createModelFromData($data);
}

public function updateMember(GroupMember $groupMember)
{
if (is_null($groupMember->getPersonId())) {
throw new CTModelException("Person Id of GroupMember cannot be null.");
}
$updateAttributes = $groupMember->getModifiableAttributes();
$allData = $groupMember->extractData();
$updateAttributes = array_intersect_key($allData, array_flip($updateAttributes));

$client = CTClient::getClient();
$response = $client->put("/api/groups/" . $this->groupId . "/members/" . $groupMember->getPersonId(), [
"json" => $updateAttributes
]);
}

public function removeMember(int $personId): void
{
$client = CTClient::getClient();
$client->delete("/api/groups/" . $this->groupId . "/members/" . $personId);
}
}
107 changes: 107 additions & 0 deletions tests/integration/Requests/GroupUpdateRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php


namespace Tests\Integration\Requests;


use CTApi\CTConfig;
use CTApi\Models\GroupMember;
use CTApi\Requests\GroupMemberRequest;
use Tests\Integration\TestCaseAuthenticated;
use Tests\Integration\TestData;

class GroupUpdateRequestTest extends TestCaseAuthenticated
{
private int $groupId;
private int $personId;

protected function setUp(): void
{
parent::setUp();
$this->checkIfTestSuiteIsEnabled("GROUP_MEMBER_UPDATE");
$this->groupId = TestData::getValueAsInteger("GROUP_MEMBER_UPDATE_GROUP_ID");
$this->personId = TestData::getValueAsInteger("GROUP_MEMBER_UPDATE_PERSON_ID");

// Add Person to Group
GroupMemberRequest::addMember($this->groupId, $this->personId);
$this->assertPersonIsGroupMember(true);
}

private function assertPersonIsGroupMember(bool $expectAsGroupMember): ?GroupMember
{
$groupMember = GroupMemberRequest::get($this->groupId)->get();
$foundGroupMember = null;
foreach ($groupMember as $groupMember) {
if (is_a($groupMember, GroupMember::class)) {
if ($groupMember->getPersonId() == $this->personId) {
$foundGroupMember = $groupMember;
}
}
}
if ($expectAsGroupMember) {
$this->assertNotNull($foundGroupMember, "Could not found person #" . $this->personId . " in group #" . $this->groupId);
} else {
$this->assertNull($foundGroupMember, "Found person #" . $this->personId . " in group #" . $this->groupId . " but expected not to be member.");
}
return $foundGroupMember;
}

public function testRemoveAndAddPerson()
{
// Remove
GroupMemberRequest::removeMember($this->groupId, $this->personId);
$this->assertPersonIsGroupMember(false);

// Add Person
$groupMember = GroupMemberRequest::addMember($this->groupId, $this->personId);

$this->assertEquals($this->personId, $groupMember->getPersonId());
$today = date("Y-m-d");
$this->assertEquals($today, $groupMember->getMemberStartDate());
}

public function testUpdateGroupMemberFields()
{
CTConfig::enableDebugging();
$groupMember = $this->assertPersonIsGroupMember(true);
$this->assertNotNull($groupMember);

/**
* First Update
*/
$groupMember->setComment("New Member");
$lastMonth = date("Y-m-d", strtotime("- 1 Month"));
$nextMonth = date("Y-m-d", strtotime("+ 1 Month"));

$groupMember->setMemberStartDate($lastMonth);
$groupMember->setMemberEndDate($nextMonth);

// Update
GroupMemberRequest::updateMember($this->groupId, $groupMember);
$groupMemberReloaded = $this->assertPersonIsGroupMember(true);
$this->assertNotNull($groupMemberReloaded);

$this->assertEquals("New Member", $groupMemberReloaded->getComment());
$this->assertEquals($lastMonth, $groupMemberReloaded->getMemberStartDate());
$this->assertEquals($nextMonth, $groupMemberReloaded->getMemberEndDate());

/**
* Second Update
*/
$groupMember->setComment("Old Member");
$lastYear = date("Y-m-d", strtotime("- 1 Year"));
$nextYear = date("Y-m-d", strtotime("+ 1 Year"));

$groupMember->setMemberStartDate($lastYear);
$groupMember->setMemberEndDate($nextYear);

// Update
GroupMemberRequest::updateMember($this->groupId, $groupMember);
$groupMemberReloaded = $this->assertPersonIsGroupMember(true);
$this->assertNotNull($groupMemberReloaded);

$this->assertEquals("Old Member", $groupMemberReloaded->getComment());
$this->assertEquals($lastYear, $groupMemberReloaded->getMemberStartDate());
$this->assertEquals($nextYear, $groupMemberReloaded->getMemberEndDate());
}
}
9 changes: 7 additions & 2 deletions tests/integration/testdata.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ SEARCH_PERSON_LAST_NAME = Doe
SONG_ARRANGEMENT_UPDATE = YES
SONG_ARRANGEMENT_UPDATE_SONG_ID = 24

# enable SONG_UPDATE testsuite with YES and siable with NO
# enable SONG_UPDATE testsuite with YES and disable with NO
SONG_UPDATE = YES
SONG_UPDATE_SONG_ID = 24
SONG_UPDATE_SONG_ID = 24

# enable GROUP_MEMBER_UPDATE testsuite with YES and disable with NO
GROUP_MEMBER_UPDATE = YES
GROUP_MEMBER_UPDATE_GROUP_ID = 78
GROUP_MEMBER_UPDATE_PERSON_ID = 72
Loading