Skip to content

Commit

Permalink
added getCustomerDetailByOrderUuid to Convertim
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasLudvik committed Oct 30, 2024
1 parent 27ccaad commit 7ab54c9
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 35 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"convertim/convertim-php": "^2.0.19",
"shopsys/form-types-bundle": "16.0.x-dev",
"shopsys/framework": "16.0.x-dev",
"shopsys/frontend-api": "16.0.x-dev",
"shopsys/migrations": "16.0.x-dev",
"shopsys/plugin-interface": "16.0.x-dev",
"symfony/config": "^5.4",
Expand Down
20 changes: 20 additions & 0 deletions src/Controller/CustomerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,24 @@ public function getCustomerDetail(Request $request): Response
return $this->convertimLogger->logGenericException($e);
}
}

/**
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Symfony\Component\HttpFoundation\Response
*/
#[Route('/get-customer-details-by-order/{orderUuid}')]
public function getCustomerDetailByOrderUuid(Request $request): Response
{
if ($this->isProtectedRequest($request) === false) {
return $this->invalidAuthorizationResponse();
}

try {
return new JsonResponse($this->customerDetailFactory->createCustomerDetailByOrderUuid($request->attributes->get('orderUuid')));
} catch (ConvertimException $e) {
return $this->convertimLogger->logConvertimException($e);
} catch (Exception $e) {
return $this->convertimLogger->logGenericException($e);
}
}
}
181 changes: 146 additions & 35 deletions src/Model/Customer/CustomerDetailFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
use Shopsys\FrameworkBundle\Component\Domain\Domain;
use Shopsys\FrameworkBundle\Model\Customer\BillingAddress;
use Shopsys\FrameworkBundle\Model\Customer\DeliveryAddress;
use Shopsys\FrameworkBundle\Model\Customer\Exception\CustomerUserNotFoundException;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser;
use Shopsys\FrameworkBundle\Model\Customer\User\CustomerUserFacade as FrameworkCustomerUserFacade;
use Shopsys\FrameworkBundle\Model\Order\Exception\OrderNotFoundException;
use Shopsys\FrameworkBundle\Model\Order\Order;
use Shopsys\FrameworkBundle\Model\Order\OrderFacade;
use Shopsys\FrameworkBundle\Model\Transport\TransportTypeEnum;
Expand All @@ -39,41 +41,48 @@ public function __construct(
*/
public function createCustomerDetail(string $userUuid): CustomerDetail
{
$customerUser = $this->customerUserFacade->getByUuid($userUuid);
$telephone = $customerUser->getTelephone();
$lastOrders = $this->orderFacade->getLastCustomerOrdersByLimit($customerUser->getCustomer(), 1, $this->domain->getLocale());
/** @var \Shopsys\FrameworkBundle\Model\Order\Order|null $lastOrder */
$lastOrder = count($lastOrders) > 0 ? reset($lastOrders) : null;
try {
$customerUser = $this->customerUserFacade->getByUuid($userUuid);
$telephone = $customerUser->getTelephone();
$lastOrders = $this->orderFacade->getLastCustomerOrdersByLimit($customerUser->getCustomer(), 1, $this->domain->getLocale());
/** @var \Shopsys\FrameworkBundle\Model\Order\Order|null $lastOrder */
$lastOrder = count($lastOrders) > 0 ? reset($lastOrders) : null;

$billingAddress = $customerUser->getCustomer()->getBillingAddress();
$deliveryAddresses = $customerUser->getCustomer()->getDeliveryAddresses();
$billingAddress = $customerUser->getCustomer()->getBillingAddress();
$deliveryAddresses = $customerUser->getCustomer()->getDeliveryAddresses();

if (count($deliveryAddresses) > 0) {
/** @var \Shopsys\FrameworkBundle\Model\Customer\DeliveryAddress $lastDeliveryAddress */
$lastDeliveryAddress = end($deliveryAddresses);
if (count($deliveryAddresses) > 0) {
/** @var \Shopsys\FrameworkBundle\Model\Customer\DeliveryAddress $lastDeliveryAddress */
$lastDeliveryAddress = end($deliveryAddresses);

$deliveryAddress = $this->createDeliveryAddressFromDeliveryAddress($lastDeliveryAddress, $customerUser);
$convertimDeliveryAddress = $this->createConvertimDeliveryAddressFromDeliveryAddress($lastDeliveryAddress, $customerUser);

if ($telephone === null) {
$telephone = $lastDeliveryAddress->getTelephone();
}
} else {
if ($billingAddress->getStreet() === null) {
throw new CustomerDetailsNotFoundException(['customerUserUuid' => $userUuid]);
if ($telephone === null) {
$telephone = $lastDeliveryAddress->getTelephone();
}
} else {
if (!$billingAddress->isBillingAddressFilled()) {
throw new CustomerDetailsNotFoundException([
'customerUserUuid' => $userUuid,
'additionalInfo' => 'Customer user billing address is empty',
]);
}

$convertimDeliveryAddress = $this->createConvertimDeliveryAddressFromBillingAddress($billingAddress, $customerUser);
}

$deliveryAddress = $this->createDeliveryAddressFromBillingAddress($billingAddress, $customerUser);
return new CustomerDetail(
$customerUser->getEmail(),
$telephone ?: '',
$convertimDeliveryAddress,
$this->createConvertimBillingAddress($billingAddress, $customerUser),
$lastOrder?->getPayment()->getUuid(),
$lastOrder?->getTransport()->getUuid(),
$this->createLastSelectedPickupPoint($lastOrder),
);
} catch (CustomerUserNotFoundException) {
throw new CustomerDetailsNotFoundException(['customerUserUuid' => $userUuid]);
}

return new CustomerDetail(
$customerUser->getEmail(),
$telephone ?: '',
$deliveryAddress,
$this->createBillingAddress($billingAddress, $customerUser),
$lastOrder?->getPayment()->getUuid(),
$lastOrder?->getTransport()->getUuid(),
$this->createLastSelectedPickupPoint($lastOrder),
);
}

/**
Expand Down Expand Up @@ -108,16 +117,16 @@ protected function createLastSelectedPickupPoint(?Order $order): ?LastSelectedPi
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser
* @return \Convertim\Customer\BillingAddress|null
*/
protected function createBillingAddress(
protected function createConvertimBillingAddress(
?BillingAddress $billingAddress,
CustomerUser $customerUser,
): ?ConvertimBillingAddress {
if ($billingAddress === null || $billingAddress->getStreet() === null) {
if ($billingAddress === null || !$billingAddress->isBillingAddressFilled()) {
return null;
}

return new ConvertimBillingAddress(
(string)$billingAddress->getId(),
$billingAddress->getUuid(),
$customerUser->getFirstName(),
$customerUser->getLastName(),
$billingAddress->getStreet(),
Expand All @@ -135,12 +144,12 @@ protected function createBillingAddress(
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser
* @return \Convertim\Customer\DeliveryAddress
*/
protected function createDeliveryAddressFromDeliveryAddress(
protected function createConvertimDeliveryAddressFromDeliveryAddress(
DeliveryAddress $lastDeliveryAddress,
CustomerUser $customerUser,
): ConvertimDeliveryAddress {
return new ConvertimDeliveryAddress(
(string)$lastDeliveryAddress->getId(),
$lastDeliveryAddress->getUuid(),
$customerUser->getFirstName(),
$customerUser->getLastName(),
$lastDeliveryAddress->getStreet(),
Expand All @@ -156,12 +165,12 @@ protected function createDeliveryAddressFromDeliveryAddress(
* @param \Shopsys\FrameworkBundle\Model\Customer\User\CustomerUser $customerUser
* @return \Convertim\Customer\DeliveryAddress
*/
protected function createDeliveryAddressFromBillingAddress(
protected function createConvertimDeliveryAddressFromBillingAddress(
BillingAddress $billingAddress,
CustomerUser $customerUser,
): ConvertimDeliveryAddress {
return new ConvertimDeliveryAddress(
(string)$billingAddress->getId(),
$billingAddress->getUuid(),
$customerUser->getFirstName(),
$customerUser->getLastName(),
$billingAddress->getStreet(),
Expand All @@ -171,4 +180,106 @@ protected function createDeliveryAddressFromBillingAddress(
$billingAddress->getCompanyName(),
);
}

/**
* @param string $orderUuid
* @return \Convertim\Customer\CustomerDetail
*/
public function createCustomerDetailByOrderUuid(string $orderUuid): CustomerDetail
{
try {
$order = $this->orderFacade->getByUuid($orderUuid);

$convertimBillingAddress = null;

if ($order->isDeliveryAddressSameAsBillingAddress()) {
$convertimDeliveryAddress = $this->createConvertimDeliveryAddressFromOrderBillingAddress($order);
} else {
$convertimDeliveryAddress = $this->createConvertimDeliveryAddressFromOrderDeliveryAddress($order);
$convertimBillingAddress = $this->createConvertimBillingAddressFromOrder($order);
}

return new CustomerDetail(
$order->getEmail(),
$order->getTelephone(),
$convertimDeliveryAddress,
$convertimBillingAddress,
$order->getPayment()->getUuid(),
$order->getTransport()->getUuid(),
$this->createLastSelectedPickupPoint($order),
);
} catch (OrderNotFoundException) {
throw new CustomerDetailsNotFoundException(['orderUuid' => $orderUuid]);
}
}

/**
* @param \Shopsys\FrameworkBundle\Model\Order\Order $order
* @return bool
*/
protected function isOrderPersonalPickup(Order $order): bool
{
return in_array($order->getTransport()->getType(), [TransportTypeEnum::TYPE_PERSONAL_PICKUP, TransportTypeEnum::TYPE_PACKETERY], true);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Order\Order $order
* @return \Convertim\Customer\DeliveryAddress
*/
protected function createConvertimDeliveryAddressFromOrderBillingAddress(Order $order): ConvertimDeliveryAddress
{
$isOrderPersonalPickup = $this->isOrderPersonalPickup($order);

return new ConvertimDeliveryAddress(
'',
$order->getFirstName(),
$order->getLastName(),
$isOrderPersonalPickup ? '' : $order->getStreet(),
$isOrderPersonalPickup ? '' : $order->getCity(),
$isOrderPersonalPickup ? '' : $order->getPostcode(),
$isOrderPersonalPickup ? '' : $order->getCountry()->getCode(),
$isOrderPersonalPickup ? '' : $order->getCompanyName(),
);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Order\Order $order
* @return \Convertim\Customer\DeliveryAddress
*/
protected function createConvertimDeliveryAddressFromOrderDeliveryAddress(Order $order): ConvertimDeliveryAddress
{
$isOrderPersonalPickup = $this->isOrderPersonalPickup($order);

return new ConvertimDeliveryAddress(
'',
$order->getFirstName(),
$order->getLastName(),
$isOrderPersonalPickup ? '' : $order->getDeliveryStreet(),
$isOrderPersonalPickup ? '' : $order->getDeliveryCity(),
$isOrderPersonalPickup ? '' : $order->getDeliveryPostcode(),
$isOrderPersonalPickup ? '' : $order->getDeliveryCountry()?->getCode(),
$isOrderPersonalPickup ? '' : $order->getDeliveryCompanyName(),
$order->getDeliveryTelephone(),
);
}

/**
* @param \Shopsys\FrameworkBundle\Model\Order\Order $order
* @return \Convertim\Customer\BillingAddress
*/
private function createConvertimBillingAddressFromOrder(Order $order): ConvertimBillingAddress
{
return new ConvertimBillingAddress(
'',
$order->getFirstName(),
$order->getLastName(),
$order->getStreet(),
$order->getCity(),
$order->getPostcode(),
$order->getCountry()->getCode(),
$order->getCompanyName(),
$order->getCompanyNumber(),
$order->getCompanyTaxNumber(),
);
}
}

0 comments on commit 7ab54c9

Please sign in to comment.