diff --git a/README.md b/README.md index cc410c6..2a88aa7 100644 --- a/README.md +++ b/README.md @@ -115,8 +115,8 @@ $success = FileRepository::upload() ``` ```php ActivityRepository::query() - ->recordIds($recordIds) - ->recordIdsAsAddress() + ->addressIds($recordIds) + ->estateId($estateId) ->create([ 'datetime' => $event->getDateFormatted(), 'actionkind' => 'Newsletter', diff --git a/docs/ActivityRepository.md b/docs/ActivityRepository.md index 1c34628..56276e2 100644 --- a/docs/ActivityRepository.md +++ b/docs/ActivityRepository.md @@ -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, ]); diff --git a/src/Query/ActivityBuilder.php b/src/Query/ActivityBuilder.php index 9a14a0e..d875843 100644 --- a/src/Query/ActivityBuilder.php +++ b/src/Query/ActivityBuilder.php @@ -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; @@ -19,6 +20,10 @@ class ActivityBuilder extends Builder public string $estateOrAddress = 'estate'; + public ?int $estateId = null; + + public array $addressIds = []; + /** * @throws OnOfficeException */ @@ -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), @@ -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, @@ -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, @@ -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( @@ -133,6 +138,9 @@ public function create(array $data): array ->json('response.results.0.data.records.0'); } + /** + * @deprecated Use estateId() instead + */ public function estate(): static { $this->estateOrAddress = 'estateid'; @@ -140,6 +148,9 @@ public function estate(): static return $this; } + /** + * @deprecated Use addressIds() instead + */ public function address(): static { $this->estateOrAddress = 'addressids'; @@ -147,6 +158,9 @@ public function address(): static return $this; } + /** + * @deprecated Use estateId() instead + */ public function recordIdsAsEstate(): static { $this->estate(); @@ -154,10 +168,51 @@ public function recordIdsAsEstate(): static 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; + } } diff --git a/tests/Query/ActivityBuilderTest.php b/tests/Query/ActivityBuilderTest.php new file mode 100644 index 0000000..8494cd7 --- /dev/null +++ b/tests/Query/ActivityBuilderTest.php @@ -0,0 +1,177 @@ +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; + }); + }); +});