Skip to content

Commit

Permalink
feat: enable setting addressIds and estateId on activity
Browse files Browse the repository at this point in the history
- also soft-deprecates the old way of doing things
  • Loading branch information
kauffinger committed Nov 7, 2024
1 parent 9faed94 commit faabd51
Show file tree
Hide file tree
Showing 4 changed files with 246 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ $success = FileRepository::upload()
```
```php
ActivityRepository::query()
->recordIds($recordIds)
->recordIdsAsAddress()
->addressIds($recordIds)
->estateId($estateId)
->create([
'datetime' => $event->getDateFormatted(),
'actionkind' => 'Newsletter',
Expand Down
28 changes: 8 additions & 20 deletions docs/ActivityRepository.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,28 @@
use Innobrain\OnOfficeAdapter\Facades\ActivityRepository;

$activities = ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->estateId(1)
->get();

$activities = ActivityRepository::query()
->addressIds([1, 2])
->get();

$activity = ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->addressIds(1)
->first();

$activity = ActivityRepository::query()
->find(1);

ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->addressIds([1, 2])
->each(function (array $estates) {
// First page
});

$activity = ActivityRepository::query()
->recordIdsAsAddress()
->recordIdsAsEstate()
->estate()
->address()
->recordIds([1, 2, 3])
->addressIds([1, 2, 3])
->create([
'activity_id' => 1,
]);
Expand Down
63 changes: 59 additions & 4 deletions src/Query/ActivityBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Innobrain\OnOfficeAdapter\Query;

use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Innobrain\OnOfficeAdapter\Dtos\OnOfficeRequest;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
Expand All @@ -19,6 +20,10 @@ class ActivityBuilder extends Builder

public string $estateOrAddress = 'estate';

public ?int $estateId = null;

public array $addressIds = [];

/**
* @throws OnOfficeException
*/
Expand All @@ -30,7 +35,7 @@ public function get(): Collection
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => $this->columns,
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::SORTBY => data_get(array_keys($orderBy), 0),
Expand All @@ -53,7 +58,7 @@ public function first(): ?array
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => $this->columns,
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::LISTLIMIT => $this->limit > 0 ? $this->limit : $this->pageSize,
Expand Down Expand Up @@ -102,7 +107,7 @@ public function each(callable $callback): void
OnOfficeAction::Read,
OnOfficeResourceType::Activity,
parameters: [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
OnOfficeService::DATA => $this->columns,
OnOfficeService::FILTER => $this->getFilters(),
OnOfficeService::SORTBY => $sortBy,
Expand All @@ -120,7 +125,7 @@ public function each(callable $callback): void
public function create(array $data): array
{
$data = array_replace($data, [
$this->estateOrAddress => $this->recordIds,
...$this->prepareEstateOrAddressParameters(),
]);

$request = new OnOfficeRequest(
Expand All @@ -133,31 +138,81 @@ public function create(array $data): array
->json('response.results.0.data.records.0');
}

/**
* @deprecated Use estateId() instead
*/
public function estate(): static
{
$this->estateOrAddress = 'estateid';

return $this;
}

/**
* @deprecated Use addressIds() instead
*/
public function address(): static
{
$this->estateOrAddress = 'addressids';

return $this;
}

/**
* @deprecated Use estateId() instead
*/
public function recordIdsAsEstate(): static
{
$this->estate();

return $this;
}

/**
* @deprecated Use addressIds() instead
*/
public function recordIdsAsAddress(): static
{
$this->address();

return $this;
}

public function estateId(int $estateId): static
{
$this->estateId = $estateId;

return $this;
}

public function addressIds(int|array $addressIds): static
{
$this->addressIds = Arr::wrap($addressIds);

return $this;
}

/**
* Function is used to deprecate the usage of recordIdsAsEstate() and recordIdsAsAddress()
* without breaking changes.
*/
private function prepareEstateOrAddressParameters(): array
{
$parameters = [$this->estateOrAddress => $this->recordIds];

// If the estateOrAddress is set to estate, we know the user has not used the old methods.
if ($this->estateOrAddress === 'estate') {
$parameters = [];
}

if (! is_null($this->estateId)) {
$parameters['estateid'] = $this->estateId;
}

if ($this->addressIds !== []) {
$parameters['addressids'] = $this->addressIds;
}

return $parameters;
}
}
177 changes: 177 additions & 0 deletions tests/Query/ActivityBuilderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php

declare(strict_types=1);

use Illuminate\Support\Facades\Http;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeAction;
use Innobrain\OnOfficeAdapter\Enums\OnOfficeResourceType;
use Innobrain\OnOfficeAdapter\Query\ActivityBuilder;
use Innobrain\OnOfficeAdapter\Repositories\ActivityRepository;

describe('deprecated estate/address methods', function () {
it('sets estate parameter correctly', function () {
$builder = new ActivityBuilder;

$builder->estate();
$builder->recordIds([1, 2, 3]);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['estateid' => [1, 2, 3]]);
});

it('sets address parameter correctly', function () {
$builder = new ActivityBuilder;

$builder->address();
$builder->recordIds([1, 2, 3]);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['addressids' => [1, 2, 3]]);
});

it('sets estate parameter via recordIdsAsEstate', function () {
$builder = new ActivityBuilder;

$builder->recordIdsAsEstate();
$builder->recordIds([1, 2, 3]);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['estateid' => [1, 2, 3]]);
});

it('sets address parameter via recordIdsAsAddress', function () {
$builder = new ActivityBuilder;

$builder->recordIdsAsAddress();
$builder->recordIds([1, 2, 3]);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['addressids' => [1, 2, 3]]);
});
});

describe('new estate/address methods', function () {
it('sets estateId parameter correctly', function () {
$builder = new ActivityBuilder;

$builder->estateId(123);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['estateid' => 123]);
});

it('sets addressIds parameter correctly with single ID', function () {
$builder = new ActivityBuilder;

$builder->addressIds(123);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['addressids' => [123]]);
});

it('sets addressIds parameter correctly with array of IDs', function () {
$builder = new ActivityBuilder;

$builder->addressIds([1, 2, 3]);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe(['addressids' => [1, 2, 3]]);
});

it('combines estateId and addressIds parameters correctly', function () {
$builder = new ActivityBuilder;

$builder
->estateId(123)
->addressIds([1, 2, 3]);

$m = new ReflectionMethod($builder, 'prepareEstateOrAddressParameters');
$m->setAccessible(true);
$parameters = $m->invoke($builder);

expect($parameters)->toBe([
'estateid' => 123,
'addressids' => [1, 2, 3],
]);
});
});

describe('CRUD operations', function () {
beforeEach(function () {
Http::preventStrayRequests();
Http::fake([
'https://api.onoffice.de/api/stable/api.php/' => Http::response([
'status' => [
'code' => 200,
],
'response' => [
'results' => [
[
'data' => [
'records' => [
['id' => 1, 'type' => 'activity'],
],
],
],
],
],
]),
]);
});

it('creates activity with estate parameters', function () {
$builder = new ActivityBuilder;

$builder
->setRepository(new ActivityRepository)
->estateId(123)
->create(['note' => 'Test activity']);

Http::assertSent(function (Illuminate\Http\Client\Request $request) {
$body = json_decode($request->body(), true);

return data_get($body, 'request.actions.0.parameters.estateid') === 123
&& data_get($body, 'request.actions.0.parameters.note') === 'Test activity';
});
});

it('gets activities with combined parameters', function () {
$builder = new ActivityBuilder;

$builder
->setRepository(new ActivityRepository)
->estateId(123)
->addressIds([1, 2])
->get();

Http::assertSent(function (Illuminate\Http\Client\Request $request) {
$body = json_decode($request->body(), true);

return data_get($body, 'request.actions.0.parameters.estateid') === 123
&& data_get($body, 'request.actions.0.parameters.addressids') === [1, 2]
&& data_get($body, 'request.actions.0.actionid') === OnOfficeAction::Read->value
&& data_get($body, 'request.actions.0.resourcetype') === OnOfficeResourceType::Activity->value;
});
});
});

0 comments on commit faabd51

Please sign in to comment.