Skip to content

Commit

Permalink
Add category create endpoint & move UpdateCustomerResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
vollborn committed Aug 8, 2024
1 parent 7f785c7 commit 924a32b
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions config/jtl-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,6 @@
Permission::CreateSalesOrderLineItem,
Permission::CreateCustomer,
Permission::UpdateCustomer,
Permission::CreateCategory,
]
];
1 change: 1 addition & 0 deletions src/Enums/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ enum Permission: string

// Categories
case QueryCategories = 'category.querycategories';
case CreateCategory = 'category.createcategory';

// Stock
case QueryStocksPerItem = 'stock.querystocksperitem';
Expand Down
42 changes: 42 additions & 0 deletions src/Helpers/CategoryHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace DREID\LaravelJtlApi\Helpers;

use DREID\LaravelJtlApi\Exceptions\ConnectionException;
use DREID\LaravelJtlApi\Exceptions\MissingApiKeyException;
use DREID\LaravelJtlApi\Exceptions\MissingLicenseException;
use DREID\LaravelJtlApi\Exceptions\MissingPermissionException;
use DREID\LaravelJtlApi\Exceptions\UnauthorizedException;
use DREID\LaravelJtlApi\Exceptions\UnhandledResponseException;
use DREID\LaravelJtlApi\Modules\Category\CategoryRepository;
use DREID\LaravelJtlApi\Modules\Category\Requests\QueryCategoriesRequest;

class CategoryHelper
{
/**
* @throws UnhandledResponseException
* @throws UnauthorizedException
* @throws ConnectionException
* @throws MissingLicenseException
* @throws MissingApiKeyException
* @throws MissingPermissionException
*/
public function loadAllCategories(): array
{
$categories = [];
$repository = app(CategoryRepository::class);

$page = 1;
$hasMore = true;

while ($hasMore) {
$response = $repository->queryCategories(new QueryCategoriesRequest(pageNumber: $page));
array_push($categories, ...$response->items);

$hasMore = $response->hasNextPage;
$page++;
}

return $categories;
}
}
36 changes: 36 additions & 0 deletions src/Modules/Category/CategoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions src/Modules/Category/Requests/CreateCategoryRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Category\Requests;

readonly class CreateCategoryRequest
{
public function __construct(
public string $name,
public ?string $description = null,
public ?int $parentCategoryId = null,
public ?int $sortNumber = null,
public ?string $activeSalesChannels = null,
) {}
}
16 changes: 16 additions & 0 deletions src/Modules/Category/Responses/CreateCategoryResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Category\Responses;

use DREID\LaravelJtlApi\ApiResponse;
use DREID\LaravelJtlApi\Modules\Category\DataTransferObjects\CategoryDto;

readonly class CreateCategoryResponse
{
public CategoryDto $category;

public function __construct(public ApiResponse $response)
{
$this->category = CategoryDto::fromResponse($this->response->json);
}
}
1 change: 1 addition & 0 deletions src/Modules/Customer/CustomerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Customer;
namespace DREID\LaravelJtlApi\Modules\Customer\Responses;

use DREID\LaravelJtlApi\ApiResponse;
use DREID\LaravelJtlApi\Modules\Customer\DataTransferObjects\CustomerDto;
Expand Down

0 comments on commit 924a32b

Please sign in to comment.