From 924a32b0b89b54081cba999aec77dd01eeebd711 Mon Sep 17 00:00:00 2001 From: Oliver Vollborn Date: Thu, 8 Aug 2024 11:10:24 +0200 Subject: [PATCH] Add category create endpoint & move UpdateCustomerResponse --- config/jtl-api.php | 1 + src/Enums/Permission.php | 1 + src/Helpers/CategoryHelper.php | 42 +++++++++++++++++++ src/Modules/Category/CategoryRepository.php | 36 ++++++++++++++++ .../Requests/CreateCategoryRequest.php | 14 +++++++ .../Responses/CreateCategoryResponse.php | 16 +++++++ src/Modules/Customer/CustomerRepository.php | 1 + .../UpdateCustomerResponse.php | 2 +- 8 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 src/Helpers/CategoryHelper.php create mode 100644 src/Modules/Category/Requests/CreateCategoryRequest.php create mode 100644 src/Modules/Category/Responses/CreateCategoryResponse.php rename src/Modules/Customer/{ => Responses}/UpdateCustomerResponse.php (85%) diff --git a/config/jtl-api.php b/config/jtl-api.php index 64f7052..910c459 100644 --- a/config/jtl-api.php +++ b/config/jtl-api.php @@ -27,5 +27,6 @@ Permission::CreateSalesOrderLineItem, Permission::CreateCustomer, Permission::UpdateCustomer, + Permission::CreateCategory, ] ]; diff --git a/src/Enums/Permission.php b/src/Enums/Permission.php index 3f6917d..6960965 100644 --- a/src/Enums/Permission.php +++ b/src/Enums/Permission.php @@ -31,6 +31,7 @@ enum Permission: string // Categories case QueryCategories = 'category.querycategories'; + case CreateCategory = 'category.createcategory'; // Stock case QueryStocksPerItem = 'stock.querystocksperitem'; diff --git a/src/Helpers/CategoryHelper.php b/src/Helpers/CategoryHelper.php new file mode 100644 index 0000000..543730c --- /dev/null +++ b/src/Helpers/CategoryHelper.php @@ -0,0 +1,42 @@ +queryCategories(new QueryCategoriesRequest(pageNumber: $page)); + array_push($categories, ...$response->items); + + $hasMore = $response->hasNextPage; + $page++; + } + + return $categories; + } +} diff --git a/src/Modules/Category/CategoryRepository.php b/src/Modules/Category/CategoryRepository.php index f180d79..81a546f 100644 --- a/src/Modules/Category/CategoryRepository.php +++ b/src/Modules/Category/CategoryRepository.php @@ -9,7 +9,9 @@ use DREID\LaravelJtlApi\Exceptions\MissingPermissionException; use DREID\LaravelJtlApi\Exceptions\UnauthorizedException; use DREID\LaravelJtlApi\Exceptions\UnhandledResponseException; +use DREID\LaravelJtlApi\Modules\Category\Requests\CreateCategoryRequest; use DREID\LaravelJtlApi\Modules\Category\Requests\QueryCategoriesRequest; +use DREID\LaravelJtlApi\Modules\Category\Responses\CreateCategoryResponse; use DREID\LaravelJtlApi\Modules\Category\Responses\QueryCategoriesResponse; use DREID\LaravelJtlApi\Repository; @@ -43,4 +45,38 @@ public function queryCategories(QueryCategoriesRequest $request): QueryCategorie $this->throwExceptionsIfPossible($response); throw new UnhandledResponseException($response); } + + /** + * @throws MissingApiKeyException + * @throws MissingLicenseException + * @throws MissingPermissionException + * @throws UnauthorizedException + * @throws UnhandledResponseException + * @throws ConnectionException + */ + public function createCategory(CreateCategoryRequest $request): CreateCategoryResponse + { + $permissions = [Permission::CreateCategory]; + + if (!Permission::allowsOneOf($permissions)) { + throw MissingPermissionException::oneOf($permissions); + } + + $body = $this->deleteNullValues([ + 'Name' => $request->name, + 'Description' => $request->description, + 'ParentCategoryId' => $request->parentCategoryId, + 'SortNumber' => $request->sortNumber, + 'ActiveSalesChannels' => $request->activeSalesChannels, + ]); + + $response = $this->post('/v1/categories', $body); + + if ($response->wasSuccessful) { + return new CreateCategoryResponse($response); + } + + $this->throwExceptionsIfPossible($response); + throw new UnhandledResponseException($response); + } } diff --git a/src/Modules/Category/Requests/CreateCategoryRequest.php b/src/Modules/Category/Requests/CreateCategoryRequest.php new file mode 100644 index 0000000..069ed9e --- /dev/null +++ b/src/Modules/Category/Requests/CreateCategoryRequest.php @@ -0,0 +1,14 @@ +category = CategoryDto::fromResponse($this->response->json); + } +} diff --git a/src/Modules/Customer/CustomerRepository.php b/src/Modules/Customer/CustomerRepository.php index 37b1547..d315bdc 100644 --- a/src/Modules/Customer/CustomerRepository.php +++ b/src/Modules/Customer/CustomerRepository.php @@ -14,6 +14,7 @@ use DREID\LaravelJtlApi\Modules\Customer\Requests\UpdateCustomerRequest; use DREID\LaravelJtlApi\Modules\Customer\Responses\CreateCustomerResponse; use DREID\LaravelJtlApi\Modules\Customer\Responses\QueryCustomersResponse; +use DREID\LaravelJtlApi\Modules\Customer\Responses\UpdateCustomerResponse; use DREID\LaravelJtlApi\Repository; use DREID\LaravelJtlApi\Traits\MapAddress; diff --git a/src/Modules/Customer/UpdateCustomerResponse.php b/src/Modules/Customer/Responses/UpdateCustomerResponse.php similarity index 85% rename from src/Modules/Customer/UpdateCustomerResponse.php rename to src/Modules/Customer/Responses/UpdateCustomerResponse.php index a83b696..8e586a6 100644 --- a/src/Modules/Customer/UpdateCustomerResponse.php +++ b/src/Modules/Customer/Responses/UpdateCustomerResponse.php @@ -1,6 +1,6 @@