diff --git a/src/Enums/OnOfficeResourceType.php b/src/Enums/OnOfficeResourceType.php index 621e60e..46f3988 100644 --- a/src/Enums/OnOfficeResourceType.php +++ b/src/Enums/OnOfficeResourceType.php @@ -4,6 +4,7 @@ enum OnOfficeResourceType: string { + case Address = 'address'; case Estate = 'estate'; case Fields = 'fields'; case File = 'file'; diff --git a/src/Facades/AddressRepository.php b/src/Facades/AddressRepository.php new file mode 100644 index 0000000..e00f96d --- /dev/null +++ b/src/Facades/AddressRepository.php @@ -0,0 +1,19 @@ +columns; + $filter = $this->getFilters(); + $listLimit = $this->limit; + $listOffset = $this->offset; + $orderBy = $this->getOrderBy(); + + $sortBy = data_get(array_keys($orderBy), 0); + $sortOrder = data_get($orderBy, 0); + + return $this->onOfficeService->requestAll(/** + * @throws OnOfficeException + */ function (int $pageSize, int $offset) use ($sortOrder, $sortBy, $filter, $columns) { + return $this->onOfficeService->requestApi( + OnOfficeAction::Read, + OnOfficeResourceType::Address, + parameters: [ + OnOfficeService::DATA => $columns, + OnOfficeService::FILTER => $filter, + OnOfficeService::LISTLIMIT => $pageSize, + OnOfficeService::LISTOFFSET => $offset, + OnOfficeService::SORTBY => $sortBy, + OnOfficeService::SORTORDER => $sortOrder, + ] + ); + }, pageSize: $listLimit, offset: $listOffset); + } + + /** + * @throws OnOfficeException + */ + public function first(): array + { + $columns = $this->columns; + $filter = $this->getFilters(); + $listLimit = $this->limit; + $listOffset = $this->offset; + $orderBy = $this->getOrderBy(); + + $sortBy = data_get(array_keys($orderBy), 0); + $sortOrder = data_get($orderBy, 0); + + $response = $this->onOfficeService->requestApi( + OnOfficeAction::Read, + OnOfficeResourceType::Address, + parameters: [ + OnOfficeService::DATA => $columns, + OnOfficeService::FILTER => $filter, + OnOfficeService::LISTLIMIT => $listLimit, + OnOfficeService::LISTOFFSET => $listOffset, + OnOfficeService::SORTBY => $sortBy, + OnOfficeService::SORTORDER => $sortOrder, + ] + ); + + return $response->json('response.results.0.data.records.0'); + } + + /** + * @throws OnOfficeException + */ + public function find(int $id): array + { + $columns = $this->columns; + + $response = $this->onOfficeService->requestApi( + OnOfficeAction::Get, + OnOfficeResourceType::Address, + $id, + parameters: [ + OnOfficeService::DATA => $columns, + ] + ); + + return $response->json('response.results.0.data.records.0'); + } +} diff --git a/src/Repositories/AddressRepository.php b/src/Repositories/AddressRepository.php new file mode 100644 index 0000000..4f150ab --- /dev/null +++ b/src/Repositories/AddressRepository.php @@ -0,0 +1,22 @@ +onOfficeService); + } +} diff --git a/src/Services/OnOfficeService.php b/src/Services/OnOfficeService.php index a076da8..4be0cfb 100644 --- a/src/Services/OnOfficeService.php +++ b/src/Services/OnOfficeService.php @@ -27,6 +27,8 @@ class OnOfficeService public const SORTBY = 'sortby'; + public const SORTORDER = 'sortorder'; + private string $token; private string $secret; diff --git a/tests/Query/AddressBuilderTest.php b/tests/Query/AddressBuilderTest.php new file mode 100644 index 0000000..d07d202 --- /dev/null +++ b/tests/Query/AddressBuilderTest.php @@ -0,0 +1,25 @@ + Http::sequence([ + // Each response will have 600 estates to simulate pagination + ReadAddressResponse::make(addressId: 1, count: 1500), + ReadAddressResponse::make(addressId: 2, count: 1500), + ReadAddressResponse::make(addressId: 3, count: 1500), + ]), + ]); + + $addresses = AddressRepository::query() + ->get(); + + expect($addresses) + ->toHaveCount(3) + ->and($addresses->first()['id'])->toBe(1) + ->and($addresses->last()['id'])->toBe(3); +}); diff --git a/tests/Query/EstateBuilderTest.php b/tests/Query/EstateBuilderTest.php index e8735e5..bcf234c 100644 --- a/tests/Query/EstateBuilderTest.php +++ b/tests/Query/EstateBuilderTest.php @@ -1,6 +1,5 @@ and($estates->first()['id'])->toBe(1) ->and($estates->last()['id'])->toBe(3); }); - -it('works with difficult request', function () { - Http::preventStrayRequests(); - Http::fake(); - - EstateRepository::query() - ->select('Id') - ->where('status', 1) - ->where('kaufpreis', '<', 30_000) - ->orderBy('kaufpreis') - ->orderBy('warmmiete') - ->get(); - - Http::assertSent(/** - * @throws JsonException - */ static function (Request $request) { - $body = json_decode($request->body(), true, 512, JSON_THROW_ON_ERROR); - $actual = data_get($body, 'request.actions.0.parameters'); - - expect($actual) - ->toBeArray() - ->and(data_get($actual, 'data.0'))->toBe('Id') - ->and(data_get($actual, 'filter'))->toBe([ - 'status' => [ - 'op' => '=', - 'val' => 1, - ], - 'kaufpreis' => [ - 'op' => '<', - 'val' => 30_000, - ], - ]) - ->and(data_get($actual, 'sortby'))->toBe([ - 'kaufpreis' => 'ASC', - 'warmmiete' => 'ASC', - ]) - ->and(data_get($actual, 'listlimit'))->toBe(500) - ->and(data_get($actual, 'listoffset'))->toBe(0); - - return true; - }); -}); diff --git a/tests/Stubs/ReadAddressResponse.php b/tests/Stubs/ReadAddressResponse.php new file mode 100644 index 0000000..f407bef --- /dev/null +++ b/tests/Stubs/ReadAddressResponse.php @@ -0,0 +1,61 @@ + [ + 'code' => 200, + 'errorcode' => 0, + 'message' => 'OK', + ], + 'response' => [ + 'results' => [ + [ + 'actionid' => 'urn:onoffice-de-ns:smart:2.5:smartml:action:read', + 'resourceid' => '', + 'resourcetype' => 'address', + 'cacheable' => true, + 'identifier' => '', + 'data' => [ + 'meta' => [ + 'cntabsolute' => $count, + ], + 'records' => [ + [ + 'id' => $addressId, + 'type' => 'address', + 'elements' => [ + 'id' => (string) $addressId, + 'Briefanrede' => 'Herr', + 'Vorname' => 'Max', + 'Name' => 'Mustermann', + 'Land' => 'Deutschland', + 'Ort' => 'Musterstadt', + 'Plz' => '12345', + 'Strasse' => 'Musterstraße', + ], + ], + ], + 'status' => [ + 'errorcode' => 0, + 'message' => 'OK', + ], + ], + ], + ], + ], + ], $data); + } +}