Skip to content

Commit

Permalink
Add customer update endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
vollborn committed Aug 2, 2024
1 parent 64cdb4b commit 7f785c7
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "drei-d/laravel-jtl-api",
"description": "a JTL api wrapper for Laravel",
"type": "library",
"version": "1.0.0",
"version": "1.1.0",
"license": "MIT",
"require": {
"php": "^8.2",
Expand Down
1 change: 1 addition & 0 deletions config/jtl-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,6 @@
Permission::CreateSalesOrder,
Permission::CreateSalesOrderLineItem,
Permission::CreateCustomer,
Permission::UpdateCustomer,
]
];
1 change: 1 addition & 0 deletions src/Enums/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ enum Permission: string
// Customers
case QueryCustomers = 'customer.querycustomers';
case CreateCustomer = 'customer.createcustomer';
case UpdateCustomer = 'customer.updatecustomer';

// Color Codes
case QueryColorCodes = 'colorcode.querycolorcodes';
Expand Down
51 changes: 51 additions & 0 deletions src/Modules/Customer/CustomerRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use DREID\LaravelJtlApi\Exceptions\UnhandledResponseException;
use DREID\LaravelJtlApi\Modules\Customer\Requests\CreateCustomerRequest;
use DREID\LaravelJtlApi\Modules\Customer\Requests\QueryCustomersRequest;
use DREID\LaravelJtlApi\Modules\Customer\Requests\UpdateCustomerRequest;
use DREID\LaravelJtlApi\Modules\Customer\Responses\CreateCustomerResponse;
use DREID\LaravelJtlApi\Modules\Customer\Responses\QueryCustomersResponse;
use DREID\LaravelJtlApi\Repository;
Expand Down Expand Up @@ -104,4 +105,54 @@ public function createCustomer(CreateCustomerRequest $request): CreateCustomerRe
$this->throwExceptionsIfPossible($response);
throw new UnhandledResponseException($response);
}

/**
* @throws MissingApiKeyException
* @throws MissingLicenseException
* @throws MissingPermissionException
* @throws UnauthorizedException
* @throws UnhandledResponseException
* @throws ConnectionException
*/
public function updateCustomer(UpdateCustomerRequest $request): UpdateCustomerResponse
{
$permissions = [Permission::UpdateCustomer];

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

$body = [
'Number' => $request->number,
'CustomerGroupId' => $request->customerGroupId,
'BillingAddress' => $this->mapAddress($request->billingAddress),
'Shipmentaddress' => $this->mapAddress($request->shipmentAddress),
'CustomAddress' => $this->mapAddress($request->customAddress),
'CustomerSince' => $request->customerSince,
'LastChange' => $request->lastChange,
'LanguageIso' => $request->languageIso,
'InternalCompanyId' => $request->internalCompanyId,
'CustomerCategoryId' => $request->customerCategoryId,
'TaxIdentificationNumber' => $request->taxIdentificationNumber,
'AccountsReceivableNumber' => $request->accountsReceivableNumber,
'CommercialRegisterNumber' => $request->commercialRegisterNumber,
'Website' => $request->website,
'InitialContact' => $request->initialContact,
'EbayUsername' => $request->ebayUsername,
'Birthday' => $request->birthday,
'IsLocked' => $request->isLocked,
'IsCashRegisterBased' => $request->isCashRegisterBased,
];

$body = $this->deleteNullValues($body);

$response = $this->patch('/v1/customers/' . $request->customerId, $body);

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

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

namespace DREID\LaravelJtlApi\Modules\Customer\Requests;

readonly class UpdateCustomerAddressRequest
{
public function __construct(
public ?string $company = null,
public ?string $company2 = null,
public ?string $formOfAddress = null,
public ?string $title = null,
public ?string $firstName = null,
public ?string $lastName = null,
public ?string $street = null,
public ?string $address2 = null,
public ?string $postalCode = null,
public ?string $city = null,
public ?string $state = null,
public ?string $countryIso = null,
public ?string $vatId = null,
public ?string $phoneNumber = null,
public ?string $mobilePhoneNumber = null,
public ?string $emailAddress = null,
public ?string $fax = null,
) {}
}
29 changes: 29 additions & 0 deletions src/Modules/Customer/Requests/UpdateCustomerRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Customer\Requests;

readonly class UpdateCustomerRequest
{
public function __construct(
public int $customerId,
public ?string $number = null,
public ?int $customerGroupId = null,
public ?UpdateCustomerAddressRequest $billingAddress = null,
public ?UpdateCustomerAddressRequest $shipmentAddress = null,
public ?UpdateCustomerAddressRequest $customAddress = null,
public ?string $customerSince = null,
public ?string $lastChange = null,
public ?string $languageIso = null,
public ?int $internalCompanyId = null,
public ?int $customerCategoryId = null,
public ?string $taxIdentificationNumber = null,
public ?string $accountsReceivableNumber = null,
public ?string $commercialRegisterNumber = null,
public ?string $website = null,
public ?string $initialContact = null,
public ?string $ebayUsername = null,
public ?string $birthday = null,
public ?bool $isLocked = null,
public ?bool $isCashRegisterBased = null,
) {}
}
16 changes: 16 additions & 0 deletions src/Modules/Customer/UpdateCustomerResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace DREID\LaravelJtlApi\Modules\Customer;

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

readonly class UpdateCustomerResponse
{
public CustomerDto $customer;

public function __construct(public ApiResponse $response)
{
$this->customer = CustomerDto::fromResponse($response->json);
}
}
34 changes: 34 additions & 0 deletions src/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Repository
{
public const string METHOD_GET = 'get';
public const string METHOD_POST = 'post';
public const string METHOD_PATCH = 'patch';

/**
* @throws MissingApiKeyException
Expand Down Expand Up @@ -80,6 +81,39 @@ protected function post(
);
}

/**
* @throws MissingApiKeyException
* @throws ConnectionException
*/
protected function patch(
string $uri,
?array $body = null,
?array $headers = null,
): ApiResponse
{
$body ??= [];

$parsedUri = $this->parseUri($uri);
$parsedHeaders = $this->parseHeaders($headers ?? []);

try {
$response = Http::withHeaders($parsedHeaders)->patch(
$parsedUri,
$body
);
} catch (\Illuminate\Http\Client\ConnectionException $exception) {
throw new ConnectionException($exception);
}

return ApiResponse::fromResponse(
$response,
$parsedUri,
self::METHOD_PATCH,
$body,
$parsedHeaders
);
}

/**
* @throws UnauthorizedException
* @throws MissingLicenseException
Expand Down

0 comments on commit 7f785c7

Please sign in to comment.