BitBagCommerce / SyliusMailChimpPlugin

Simple MailChimp integration with Sylius eCommerce framework.
MIT License
42 stars 27 forks source link

Updating email for customer subscribed to newsletter results in UniqueConstraintViolationException #34

Closed mbabker closed 3 years ago

mbabker commented 4 years ago

If editing a customer's email address through the admin interface and the customer is subscribed to the newsletter, the NewsletterSubscriptionHandler performs changes that results in a unique constraint violation because it is trying to create a new customer record with the updated email address.

Doctrine\DBAL\Exception\UniqueConstraintViolationException: An exception occurred while executing 'UPDATE sylius_customer SET email = ?, email_canonical = ?, updated_at = ? WHERE id = ?' with params ["test-email@example.com", "test-email@example.com", "2020-09-28 17:55:43", 3781]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'test-email@example.com' for key 'UNIQ_7E82D5E6E7927C74'
#25 /vendor/doctrine/dbal/lib/Doctrine/DBAL/Driver/AbstractMySQLDriver.php(59): Doctrine\DBAL\Driver\AbstractMySQLDriver::convertException
#24 /vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php(172): Doctrine\DBAL\DBALException::wrapException
#23 /vendor/doctrine/dbal/lib/Doctrine/DBAL/DBALException.php(151): Doctrine\DBAL\DBALException::driverExceptionDuringQuery
#22 /vendor/doctrine/dbal/lib/Doctrine/DBAL/Connection.php(1087): Doctrine\DBAL\Connection::executeUpdate
#21 /vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(500): Doctrine\ORM\Persisters\Entity\BasicEntityPersister::updateTable
#20 /vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/Entity/BasicEntityPersister.php(389): Doctrine\ORM\Persisters\Entity\BasicEntityPersister::update
#19 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(1171): Doctrine\ORM\UnitOfWork::executeUpdates
#18 /vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php(406): Doctrine\ORM\UnitOfWork::commit
#17 /vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(370): Doctrine\ORM\EntityManager::flush
#16 /var/cache/prod/ContainerBrx3oPf/EntityManager_9a5be93.php(86): EntityManager_9a5be93::flush
#15 /vendor/sylius/resource-bundle/src/Bundle/Doctrine/ORM/EntityRepository.php(32): Sylius\Bundle\ResourceBundle\Doctrine\ORM\EntityRepository::add
#14 /vendor/bitbag/mailchimp-plugin/src/Handler/NewsletterSubscriptionHandler.php(108): BitBag\SyliusMailChimpPlugin\Handler\NewsletterSubscriptionHandler::createNewCustomer
#13 /vendor/bitbag/mailchimp-plugin/src/Handler/NewsletterSubscriptionHandler.php(76): BitBag\SyliusMailChimpPlugin\Handler\NewsletterSubscriptionHandler::subscribe
#12 /vendor/bitbag/mailchimp-plugin/src/EventListener/CustomerNewsletterListener.php(55): BitBag\SyliusMailChimpPlugin\EventListener\CustomerNewsletterListener::subscribe
#11 /vendor/bitbag/mailchimp-plugin/src/EventListener/CustomerNewsletterListener.php(42): BitBag\SyliusMailChimpPlugin\EventListener\CustomerNewsletterListener::customerCreateEvent
#10 /vendor/bitbag/mailchimp-plugin/src/EventListener/CustomerNewsletterListener.php(50): BitBag\SyliusMailChimpPlugin\EventListener\CustomerNewsletterListener::customerUpdateEvent
#9 /vendor/symfony/event-dispatcher/EventDispatcher.php(304): Symfony\Component\EventDispatcher\EventDispatcher::Symfony\Component\EventDispatcher\{closure}
#8 /vendor/symfony/event-dispatcher/EventDispatcher.php(264): Symfony\Component\EventDispatcher\EventDispatcher::doDispatch
#7 /vendor/symfony/event-dispatcher/EventDispatcher.php(239): Symfony\Component\EventDispatcher\EventDispatcher::callListeners
#6 /vendor/symfony/event-dispatcher/EventDispatcher.php(73): Symfony\Component\EventDispatcher\EventDispatcher::dispatch
#5 /vendor/sylius/resource-bundle/src/Bundle/Controller/EventDispatcher.php(76): Sylius\Bundle\ResourceBundle\Controller\EventDispatcher::dispatchPreEvent
#4 /vendor/sylius/resource-bundle/src/Bundle/Controller/ResourceController.php(268): Sylius\Bundle\ResourceBundle\Controller\ResourceController::updateAction
#3 /vendor/symfony/http-kernel/HttpKernel.php(158): Symfony\Component\HttpKernel\HttpKernel::handleRaw
#2 /vendor/symfony/http-kernel/HttpKernel.php(80): Symfony\Component\HttpKernel\HttpKernel::handle
#1 /vendor/symfony/http-kernel/Kernel.php(201): Symfony\Component\HttpKernel\Kernel::handle
#0 /public/index.php(25): null
Keimpy commented 3 years ago

I think this is caused because the subscribtion handler is called in the pre_update event instead of the post_update event. You can temporary fix this by redefining the following service and change 'sylius.customer.pre_update' to 'sylius.customer.post_update'

bitbag_sylius_mailchimp_plugin.event_listener.customer_listener:
        class: BitBag\SyliusMailChimpPlugin\EventListener\CustomerNewsletterListener
        public: true
        arguments:
            - "@bitbag_sylius_mailchimp_plugin.handler.newsletter_subscription_handler"
        tags:
            - { name: "kernel.event_listener", event: "sylius.customer.post_register", method: customerCreateEvent }
            - { name: "kernel.event_listener", event: "sylius.customer.pre_update", method: customerUpdateEvent }
Keimpy commented 3 years ago

See #36

tbuczen commented 3 years ago

Thank You for this report, changing sylius.customer.pre_update' to 'sylius.customer.post_update' is just a part of the solution as the old email has to be removed from Mailchimp audience itself. The fix for this is in progress at the moment.