From 041f0d9aaa81977aa4e89ef959f9cfcbee48ddfa Mon Sep 17 00:00:00 2001 From: Adrien Gallou Date: Sun, 1 Apr 2018 17:10:24 +0200 Subject: [PATCH] =?UTF-8?q?gestion=20des=20cas=20o=C3=B9=20l'on=20a=20re?= =?UTF-8?q?=C3=A7u=20une=20d=C3=A9sinscription?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../TechletterSubscriptionsRepository.php | 39 +++++++++++++++---- sql/2018-04-01-techletter-unsubscribe.sql | 1 + 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/sources/AppBundle/Association/Model/Repository/TechletterSubscriptionsRepository.php b/sources/AppBundle/Association/Model/Repository/TechletterSubscriptionsRepository.php index 8e01bd11..9caa4881 100644 --- a/sources/AppBundle/Association/Model/Repository/TechletterSubscriptionsRepository.php +++ b/sources/AppBundle/Association/Model/Repository/TechletterSubscriptionsRepository.php @@ -25,16 +25,27 @@ class TechletterSubscriptionsRepository extends Repository implements MetadataIn public function hasUserSubscribed(User $user = null) { - $subscription = null; - if ($user !== null) { - $subscription = $this->getOneBy(['userId' => $user->getId()]); - } - - if ($user === null or $subscription === null) { + if (null === $user) { return false; } - return true; + $query = $this->getPreparedQuery( + "SELECT ats.* + FROM afup_techletter_subscriptions ats + LEFT JOIN afup_personnes_physiques app ON app.id = ats.user_id + LEFT JOIN ( + SELECT afup_techletter_unsubscriptions.email, MAX(afup_techletter_unsubscriptions.unsubscription_date) as max_unsubscriptions_date + FROM afup_techletter_unsubscriptions + GROUP BY afup_techletter_unsubscriptions.email + ) as latest_unsubscriptions ON (app.email = latest_unsubscriptions.email AND latest_unsubscriptions.max_unsubscriptions_date > ats.subscription_date) + WHERE latest_unsubscriptions.email IS NULL + AND ats.user_id = :userId + " + ); + + $query->setParams(['userId' => $user->getId()]); + + return $query->query()->count() > 0; } public function getAllSubscriptionsWithUser() @@ -51,6 +62,12 @@ class TechletterSubscriptionsRepository extends Repository implements MetadataIn LEFT JOIN afup_personnes_physiques app ON app.id = ats.user_id LEFT JOIN afup_personnes_morales apm ON apm.id = app.id_personne_morale LEFT JOIN afup_cotisations ac ON ac.type_personne = IF(apm.id IS NULL, 0, 1) AND ac.id_personne = IFNULL(apm.id, app.id) + LEFT JOIN ( + SELECT afup_techletter_unsubscriptions.email, MAX(afup_techletter_unsubscriptions.unsubscription_date) as max_unsubscriptions_date + FROM afup_techletter_unsubscriptions + GROUP BY afup_techletter_unsubscriptions.email + ) as latest_unsubscriptions ON (app.email = latest_unsubscriptions.email AND latest_unsubscriptions.max_unsubscriptions_date > ats.subscription_date) + WHERE latest_unsubscriptions.email IS NULL GROUP BY app.id ')->query($this->getCollection($hydrator)) ; @@ -67,7 +84,13 @@ class TechletterSubscriptionsRepository extends Repository implements MetadataIn LEFT JOIN afup_personnes_physiques app ON app.id = ats.user_id LEFT JOIN afup_personnes_morales apm ON apm.id = app.id_personne_morale LEFT JOIN afup_cotisations ac ON ac.type_personne = IF(apm.id IS NULL, 0, 1) AND ac.id_personne = IFNULL(apm.id, app.id) - WHERE ac.date_fin > UNIX_TIMESTAMP() + LEFT JOIN ( + SELECT afup_techletter_unsubscriptions.email, MAX(afup_techletter_unsubscriptions.unsubscription_date) as max_unsubscriptions_date + FROM afup_techletter_unsubscriptions + GROUP BY afup_techletter_unsubscriptions.email + ) as latest_unsubscriptions ON (app.email = latest_unsubscriptions.email AND latest_unsubscriptions.max_unsubscriptions_date > ats.subscription_date) + WHERE latest_unsubscriptions.email IS NULL + AND ac.date_fin > UNIX_TIMESTAMP() GROUP BY app.id ')->query($this->getCollection(new HydratorArray())) ; diff --git a/sql/2018-04-01-techletter-unsubscribe.sql b/sql/2018-04-01-techletter-unsubscribe.sql index 345e8d6c..882d184c 100644 --- a/sql/2018-04-01-techletter-unsubscribe.sql +++ b/sql/2018-04-01-techletter-unsubscribe.sql @@ -5,3 +5,4 @@ CREATE TABLE `afup_techletter_unsubscriptions` ( `reason` varchar(255) DEFAULT NULL, `mailchimp_id` varchar(255) DEFAULT NULL ) COMMENT='' ENGINE='InnoDB'; +alter table `afup_techletter_unsubscriptions` convert to character set latin1 collate latin1_general_ci;