Skip to content

Commit

Permalink
Add query warehouses & storage locations
Browse files Browse the repository at this point in the history
  • Loading branch information
vollborn committed Sep 16, 2024
1 parent c390cb4 commit fdc9462
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/Enums/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ enum Permission: string
// Shipping Methods
case QueryShippingMethods = 'shippingmethod.queryshippingmethods';

// Warehouses
case QueryWarehouses = 'warehouse.querywarehouses';

// Storage Locations
case QueryStorageLocations = 'warehouse.querystoragelocations';

// Suppliers
case QuerySuppliers = 'supplier.querysuppliers';

Expand Down
35 changes: 35 additions & 0 deletions src/Modules/Warehouse/DataTransferObjects/StorageLocationDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse\DataTransferObjects;

use DREID\LaravelJtlApi\Services\DataTransferObjectService;

readonly class StorageLocationDto
{
public function __construct(
public int $id,
public int $warehouseId,
public string $name,
public ?int $sortNumber,
public ?int $priority,
public bool $lockForShipment,
public bool $lockForAvailability,
public ?string $comment,
) {}

public static function fromResponse(array $data): static
{
$service = app(DataTransferObjectService::class);

return new self(
$data['Id'],
$data['WarehouseId'],
$data['Name'],
$service->getArrayValue($data, 'SortNumber'),
$service->getArrayValue($data, 'Priority'),
$service->getArrayValue($data, 'LockForShipment'),
$service->getArrayValue($data, 'LockForAvailability'),
$service->getArrayValue($data, 'Comment'),
);
}
}
33 changes: 33 additions & 0 deletions src/Modules/Warehouse/DataTransferObjects/WarehouseDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse\DataTransferObjects;

readonly class WarehouseDto
{
public function __construct(
public int $id,
public string $name,
public ?string $code,
public ?string $description,
public ?int $priority,
public ?int $companyId,
public bool $lockForShipment,
public bool $lockForAvailability,
public bool $isActive,
) {}

public static function fromResponse(array $data): static
{
return new self(
$data['Id'],
$data['Name'],
$data['Code'] ?? null,
$data['Description'] ?? null,
$data['Priority'] ?? null,
$data['CompanyId'] ?? null,
$data['LockForShipment'],
$data['LockForAvailability'],
$data['IsActive'],
);
}
}
12 changes: 12 additions & 0 deletions src/Modules/Warehouse/Requests/QueryStorageLocationsRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse\Requests;

readonly class QueryStorageLocationsRequest
{
public function __construct(
public int $warehouseId,
public ?int $pageNumber = null,
public ?int $pageSize = null,
) {}
}
12 changes: 12 additions & 0 deletions src/Modules/Warehouse/Requests/QueryWarehousesRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse\Requests;

readonly class QueryWarehousesRequest
{
public function __construct(
public ?int $pageNumber = null,
public ?int $pageSize = null,
public ?bool $isActive = null,
) {}
}
17 changes: 17 additions & 0 deletions src/Modules/Warehouse/Responses/QueryStorageLocationsResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse\Responses;

use DREID\LaravelJtlApi\ApiResponse;
use DREID\LaravelJtlApi\Modules\Warehouse\DataTransferObjects\StorageLocationDto;
use DREID\LaravelJtlApi\PaginatedResponse;

readonly class QueryStorageLocationsResponse extends PaginatedResponse
{
public function __construct(ApiResponse $response)
{
parent::__construct($response, array_map(static function ($item) {
return StorageLocationDto::fromResponse($item);
}, $response->json['Items']));
}
}
17 changes: 17 additions & 0 deletions src/Modules/Warehouse/Responses/QueryWarehousesResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse\Responses;

use DREID\LaravelJtlApi\ApiResponse;
use DREID\LaravelJtlApi\Modules\Warehouse\DataTransferObjects\WarehouseDto;
use DREID\LaravelJtlApi\PaginatedResponse;

readonly class QueryWarehousesResponse extends PaginatedResponse
{
public function __construct(ApiResponse $response)
{
parent::__construct($response, array_map(static function ($item) {
return WarehouseDto::fromResponse($item);
}, $response->json['Items']));
}
}
46 changes: 46 additions & 0 deletions src/Modules/Warehouse/StorageLocationRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse;

use DREID\LaravelJtlApi\Enums\Permission;
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\Warehouse\Requests\QueryStorageLocationsRequest;
use DREID\LaravelJtlApi\Modules\Warehouse\Responses\QueryStorageLocationsResponse;
use DREID\LaravelJtlApi\Repository;

class StorageLocationRepository extends Repository
{
/**
* @throws MissingApiKeyException
* @throws MissingLicenseException
* @throws MissingPermissionException
* @throws UnauthorizedException
* @throws UnhandledResponseException
* @throws ConnectionException
*/
public function queryStorageLocations(QueryStorageLocationsRequest $request): QueryStorageLocationsResponse
{
$permissions = [Permission::AllRead, Permission::QueryStorageLocations];

if (!Permission::allowsOneOf($permissions)) {
throw MissingPermissionException::oneOf($permissions);
}

$response = $this->get('/v1/warehouses/' . $request->warehouseId . '/storagelocations', [
'pageNumber' => $request->pageNumber,
'pageSize' => $request->pageSize,
]);

if ($response->wasSuccessful) {
return new QueryStorageLocationsResponse($response);
}

$this->throwExceptionsIfPossible($response);
throw new UnhandledResponseException($response);
}
}
47 changes: 47 additions & 0 deletions src/Modules/Warehouse/WarehouseRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Warehouse;

use DREID\LaravelJtlApi\Enums\Permission;
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\Warehouse\Requests\QueryWarehousesRequest;
use DREID\LaravelJtlApi\Modules\Warehouse\Responses\QueryWarehousesResponse;
use DREID\LaravelJtlApi\Repository;

class WarehouseRepository extends Repository
{
/**
* @throws MissingApiKeyException
* @throws MissingLicenseException
* @throws MissingPermissionException
* @throws UnauthorizedException
* @throws UnhandledResponseException
* @throws ConnectionException
*/
public function queryWarehouses(QueryWarehousesRequest $request): QueryWarehousesResponse
{
$permissions = [Permission::AllRead, Permission::QueryWarehouses];

if (!Permission::allowsOneOf($permissions)) {
throw MissingPermissionException::oneOf($permissions);
}

$response = $this->get('/v1/warehouses', [
'isActive' => $request->isActive,
'pageNumber' => $request->pageNumber,
'pageSize' => $request->pageSize,
]);

if ($response->wasSuccessful) {
return new QueryWarehousesResponse($response);
}

$this->throwExceptionsIfPossible($response);
throw new UnhandledResponseException($response);
}
}
2 changes: 1 addition & 1 deletion src/Services/DataTransferObjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public function getArrayValue(array $array, string $key): mixed

$value = $array[$key];

if (is_array($value)) {
if (is_array($value) || is_bool($value)) {
return $value;
}

Expand Down

0 comments on commit fdc9462

Please sign in to comment.