From 7ab54c965e22767c5d9c8e2cd40adbb6e9f53758 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Toma=CC=81s=CC=8C=20Ludvik?= Date: Fri, 18 Oct 2024 12:27:03 +0200 Subject: [PATCH] added getCustomerDetailByOrderUuid to Convertim --- composer.json | 1 + src/Controller/CustomerController.php | 20 ++ src/Model/Customer/CustomerDetailFactory.php | 181 +++++++++++++++---- 3 files changed, 167 insertions(+), 35 deletions(-) diff --git a/composer.json b/composer.json index 428ae27..de99c30 100644 --- a/composer.json +++ b/composer.json @@ -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", diff --git a/src/Controller/CustomerController.php b/src/Controller/CustomerController.php index d818de4..4d379e6 100644 --- a/src/Controller/CustomerController.php +++ b/src/Controller/CustomerController.php @@ -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); + } + } } diff --git a/src/Model/Customer/CustomerDetailFactory.php b/src/Model/Customer/CustomerDetailFactory.php index 86fcb3a..33cf59b 100644 --- a/src/Model/Customer/CustomerDetailFactory.php +++ b/src/Model/Customer/CustomerDetailFactory.php @@ -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; @@ -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), - ); } /** @@ -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(), @@ -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(), @@ -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(), @@ -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(), + ); + } }