Skip to content

Commit

Permalink
Merge pull request #732 from coreshop/issue-707
Browse files Browse the repository at this point in the history
Disable Customer Deletion if bounded Orders are available
  • Loading branch information
dpfaffenbauer authored Mar 22, 2019
2 parents 9526953 + 80f2193 commit b14b925
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 1 deletion.
27 changes: 27 additions & 0 deletions features/order/customer_deletion.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@order @order_customer_deletion
Feature: Create a new order

Background:
Given the site operates on a store in "Austria"
And the site operates on locale "en"
And the site has a tax rate "AT" with "20%" rate
And the site has a tax rule group "AT"
And the tax rule group has a tax rule for country "Austria" with tax rate "AT"
And the site has a product "T-Shirt" priced at 2000
And the product has the tax rule group "AT"
And the site has a customer "some-customer@something.com"
And the customer "some-customer@something.com" has an address with country "Austria", "4600", "Wels", "Freiung", "9-11/N3"

And the cart belongs to customer "some-customer@something.com"

Scenario: Create a new order and delete the customer
Given I add the product "T-Shirt" to my cart
And the cart ships to customer "some-customer@something.com" address with postcode "4600"
And the cart invoices to customer "some-customer@something.com" address with postcode "4600"
And I create an order from my cart
Then It should throw an error deleting the customer "some-customer@something.com"

Scenario: Just delete an Customer Account
Given the site has a customer "some-other@something.com"
And the customer "some-other@something.com" has an address with country "Austria", "4600", "Wels", "Freiung", "9-11/N3"
Then It should not throw an error deleting the customer "some-other@something.com"
18 changes: 18 additions & 0 deletions src/CoreShop/Behat/Context/Domain/CustomerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ public function iShouldBeLoggedInWithEmail(CustomerInterface $customer)
)
);
}

/**
* @Then /^It should throw an error deleting the (customer "[^"]+")$/
*/
public function itShouldThrowAnErrorDeletingCustomer(CustomerInterface $customer)
{
Assert::throws(function() use ($customer) {
$customer->delete();
});
}

/**
* @Then /^It should not throw an error deleting the (customer "[^"]+")$/
*/
public function itShouldNotThrowAnErrorDeletingTheCustomer(CustomerInterface $customer)
{
$customer->delete();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ default:
- coreshop.behat.context.setup.shipping

- coreshop.behat.context.domain.order
- coreshop.behat.context.domain.customer
- coreshop.behat.context.domain.notification_rule
filters:
tags: "@order"
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* CoreShop.
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @copyright Copyright (c) 2015-2017 Dominik Pfaffenbauer (https://www.pfaffenbauer.at)
* @license https://www.coreshop.org/license GNU General Public License version 3 (GPLv3)
*/

namespace CoreShop\Bundle\CoreBundle\EventListener;

use CoreShop\Component\Core\Model\CustomerInterface;
use CoreShop\Component\Order\Repository\OrderRepositoryInterface;
use Pimcore\Event\Model\DataObjectDeleteInfoEvent;
use Pimcore\Event\Model\DataObjectEvent;

final class CustomerOrderDeletionListener
{
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

/**
* @param OrderRepositoryInterface $orderRepository
*/
public function __construct(
OrderRepositoryInterface $orderRepository
) {
$this->orderRepository = $orderRepository;
}

/**
* @param DataObjectDeleteInfoEvent $event
*/
public function checkCustomerDeletionAllowed(DataObjectDeleteInfoEvent $event)
{
$object = $event->getObject();

if (!$object instanceof CustomerInterface) {
return;
}

$hasOrders = $this->orderRepository->hasCustomerOrders($object);

if ($hasOrders) {
$event->setDeletionAllowed(false);
$event->setReason('Cannot delete a customer with orders');
}
}

/**
* @param DataObjectEvent $event
*/
public function checkCustomerOrdersBeforeDeletion(DataObjectEvent $event)
{
$object = $event->getObject();

if (!$object instanceof CustomerInterface) {
return;
}

$hasOrders = $this->orderRepository->hasCustomerOrders($object);

if ($hasOrders) {
throw new \InvalidArgumentException(sprintf('Cannot delete a customer with orders'));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ services:
- '@pimcore.http.request_helper'
- '@session'
tags:
- { name: kernel.event_listener, event: kernel.request, method: checkCartAvailability, priority: 7 }
- { name: kernel.event_listener, event: kernel.request, method: checkCartAvailability, priority: 7 }

coreshop.listener.customer_odrer_deletion:
class: CoreShop\Bundle\CoreBundle\EventListener\CustomerOrderDeletionListener
arguments:
- '@coreshop.repository.order'
tags:
- { name: kernel.event_listener, event: pimcore.dataobject.preDelete, method: checkCustomerOrdersBeforeDeletion }
- { name: kernel.event_listener, event: pimcore.dataobject.deleteInfo, method: checkCustomerDeletionAllowed }
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ public function findByCustomer(CustomerInterface $customer)
return $list->getObjects();
}

/**
* {@inheritdoc}
*/
public function hasCustomerOrders(CustomerInterface $customer)
{
$list = $this->getList();
$list->setCondition('customer__id = ?', [$customer->getId()]);

return $list->getTotalCount() > 0;
}

/**
* {@inheritdoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ interface OrderRepositoryInterface extends PimcoreRepositoryInterface
*/
public function findByCustomer(CustomerInterface $customer);

/**
* @param CustomerInterface $customer
*
* @return boolean
*/
public function hasCustomerOrders(CustomerInterface $customer);

/**
* @param int $days
*
Expand Down

0 comments on commit b14b925

Please sign in to comment.