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

Added a flag for forced person creation #107

Merged
merged 7 commits into from
Sep 15, 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 @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [Serialize Models to Data-Array for JSON-Export](https://github.com/5pm-HDH/churchtools-api/pull/103)
- [Retrieve Birthdays](https://github.com/5pm-HDH/churchtools-api/pull/104)
- [Retrieve Tags of Person](https://github.com/5pm-HDH/churchtools-api/pull/110)
- [Added force flag to person creation, to add persons with same name](https://github.com/5pm-HDH/churchtools-api/pull/107)
- [Absence-API](https://github.com/5pm-HDH/churchtools-api/pull/111)

### Changed
Expand Down
22 changes: 22 additions & 0 deletions docs/out/PersonAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,28 @@

```

Sometimes it will happen that you have to add a person with the same name
as an existing one. ChurchTools will respond with an error to prevent you from
adding duplicates accidently.

Therefore you can add the `force` parameter and set it to `true`.

```php
use CTApi\Models\Person;
use CTApi\Requests\PersonRequest;

$newPerson = new Person();
$newPerson->setFirstName("John")
->setLastName("Doe")
->setBirthday("1970-01-01");
//add further attributes

PersonRequest::create($newPerson, force: true);

```

This will make ChurchTools to insert the record, even if there is a second John Doe.

## Update a person's data

Use the setters of the person model to modify its data and utilize the
Expand Down
10 changes: 10 additions & 0 deletions docs/src/ressources/PersonAPI.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@

{{ \Tests\Unit\Docs\PersonRequestTest.testCreatePerson }}

Sometimes it will happen that you have to add a person with the same name
as an existing one. ChurchTools will respond with an error to prevent you from
adding duplicates accidently.

Therefore you can add the `force` parameter and set it to `true`.

{{ \Tests\Unit\Docs\PersonRequestTest.testCreatePersonWithEqualName }}

This will make ChurchTools to insert the record, even if there is a second John Doe.

## Update a person's data

Use the setters of the person model to modify its data and utilize the
Expand Down
25 changes: 21 additions & 4 deletions src/Requests/AbstractRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,12 @@ public function get(): array
* Send Create-Request for given Model.
*
* @param UpdatableModel $model Model
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
*/
protected function createDataForModel(UpdatableModel $model): void
protected function createDataForModel(UpdatableModel $model, bool $force = false): void
{
$createAttributes = $model->getModifiableAttributes();

Expand All @@ -80,7 +84,7 @@ protected function createDataForModel(UpdatableModel $model): void
return !is_null($value);
});

$responseData = $this->createData($data);
$responseData = $this->createData($data, $force);

// Some data like the ID is created on Churchtools and we have to sync it
// back to the model.
Expand All @@ -100,13 +104,26 @@ protected function createDataForModel(UpdatableModel $model): void
* Sends the data to the API endpoint to create a new record at ChurchTools.
* When the action was successfull, the actual data from ChurchTools is returned
* as an array.
*
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
*/
protected function createData(array $data): array
protected function createData(array $data, bool $force = false): array
{
$url = $this->getApiEndpoint();
$query = [];

if ($force) {
$query['force'] = '1';
}

$client = CTClient::getClient();
$response = $client->post($url, ['json' => $data]);
$response = $client->post($url, [
'query' => $query,
'json' => $data,
]);

$response->getBody();
return CTResponseUtil::dataAsArray($response);
Expand Down
9 changes: 7 additions & 2 deletions src/Requests/PersonRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ public static function find(int $id): ?Person

/**
* Add the person to ChurchTools.
*
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
*/
public static function create(Person $person): void
public static function create(Person $person, bool $force = false): void
{
(new PersonRequestBuilder())->create($person);
(new PersonRequestBuilder())->create($person, $force);
}

/**
Expand Down
14 changes: 11 additions & 3 deletions src/Requests/PersonRequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,23 @@ public function get(): array
return Person::createModelsFromArray($data);
}


public function create(Person $person): void
/**
* Add the person to ChurchTools.
*
* @param bool $force
* If the request fails because a duplicate is found (person with same name)
* set the $force param to `true` to create this person even if a
* duplicate is found.
* @throws CTModelException If the passed person already has an ID.
*/
public function create(Person $person, bool $force = false): void
{
$id = $person->getId();
if (!is_null($id)) {
throw new CTModelException("ID of a new Person has to be null.");
}

$this->createDataForModel($person);
$this->createDataForModel($person, $force);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions tests/unit/Docs/PersonRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,20 @@ public function testCreatePerson()
PersonRequest::create($newPerson);
}

/**
* @doesNotPerformAssertions
*/
public function testCreatePersonWithEqualName()
{
$newPerson = new Person();
$newPerson->setFirstName("John")
->setLastName("Doe")
->setBirthday("1970-01-01");
//add further attributes

PersonRequest::create($newPerson, force: true);
}

/**
* @doesNotPerformAssertions
*/
Expand Down