From 6ef749f17cb1ab2eb83f5bf4dd7eeb1b0058e157 Mon Sep 17 00:00:00 2001 From: bach Date: Fri, 10 Jun 2022 13:01:18 +0200 Subject: [PATCH] simplenews_mail_subscriptions api use fix --- ...et_subscriptions__usingmailjetmodule.patch | 228 ++++++++++++++---- 1 file changed, 187 insertions(+), 41 deletions(-) diff --git a/patches/simplenews_mailjet_subscriptions__usingmailjetmodule.patch b/patches/simplenews_mailjet_subscriptions__usingmailjetmodule.patch index 6ca50a07..06ed51f5 100644 --- a/patches/simplenews_mailjet_subscriptions__usingmailjetmodule.patch +++ b/patches/simplenews_mailjet_subscriptions__usingmailjetmodule.patch @@ -79,19 +79,31 @@ index 09abe13..4b2ee58 100644 /** diff --git a/simplenews_mailjet_subscriptions.services.yml b/simplenews_mailjet_subscriptions.services.yml new file mode 100644 -index 0000000..cdd4137 +index 0000000..cee6a81 --- /dev/null +++ b/simplenews_mailjet_subscriptions.services.yml -@@ -0,0 +1,4 @@ +@@ -0,0 +1,7 @@ +services: ++ simplenews_mailjet_subscriptions.handler: ++ class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsMailjetHandler ++ arguments: ['@mailjet.client_factory'] + simplenews_mailjet_subscriptions.utilities: + class: Drupal\simplenews_mailjet_subscriptions\SimplenewsMailjetSubscriptionsUtilities -+ arguments: [ '@mailjet.handler' ] ++ arguments: [ '@simplenews_mailjet_subscriptions.handler' ] diff --git a/src/Entity/SimplenewsMailjetSubscriptionEntity.php b/src/Entity/SimplenewsMailjetSubscriptionEntity.php -index 27b7737..b74f89e 100644 +index 27b7737..34c8df5 100644 --- a/src/Entity/SimplenewsMailjetSubscriptionEntity.php +++ b/src/Entity/SimplenewsMailjetSubscriptionEntity.php -@@ -94,53 +94,53 @@ class SimplenewsMailjetSubscriptionEntity extends ConfigEntityBase implements Si +@@ -30,8 +30,6 @@ use Drupal\Core\Config\Entity\ConfigEntityBase; + * config_export = { + * "id", + * "title", +- * "api_key", +- * "secret_key", + * "mapping_table" + * } + * ) +@@ -94,53 +92,53 @@ class SimplenewsMailjetSubscriptionEntity extends ConfigEntityBase implements Si return $this->get('id'); } @@ -350,15 +362,111 @@ index c727d26..37c082d 100644 /** * Sets the entity mapping table. +diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandler.php b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php +new file mode 100644 +index 0000000..a68cbf1 +--- /dev/null ++++ b/src/SimplenewsMailjetSubscriptionsMailjetHandler.php +@@ -0,0 +1,47 @@ ++mailjetClient->post(Resources::$Contact, ["body" => ['Email' => $email]]); ++ ++ if ($response->success()) { ++ return $response->getData(); ++ } ++ ++ return NULL; ++ } ++ ++ /** ++ * {@inheritdoc} ++ * ++ * @return array|null ++ * Return array with result or null. ++ */ ++ public function getMailjetContactByEmail($email): ?array { ++ ++ $response = $this->mailjetClient->get(Resources::$Contact, ['id' => $email]); ++ ++ if ($response->success()) { ++ return $response->getData(); ++ } ++ ++ return NULL; ++ } ++ ++} +\ No newline at end of file +diff --git a/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php +new file mode 100644 +index 0000000..b6484d7 +--- /dev/null ++++ b/src/SimplenewsMailjetSubscriptionsMailjetHandlerInterface.php +@@ -0,0 +1,34 @@ ++mailjetHandler = $mailjetHandler; } @@ -431,8 +540,30 @@ index 950ff8a..05601ca 100644 - return $json; + public static function create(ContainerInterface $container) { + return new static( -+ $container->get('mailjet.handler') ++ $container->get('simplenews_mailjet_subscriptions.handler') + ); ++ } ++ ++ public function checkSubscriber($encoded_mail) { ++ $response = $this->mailjetHandler->getMailjetContactByEmail($encoded_mail); ++ if ($response) { ++ // contact alredy exists ++ $contact_id = $response[0]['ID']; ++ return $contact_id; ++ } ++ ++ return null; ++ } ++ ++ public function createSubscriber($email) { ++ $response = $this->mailjetHandler->createMailjetContact($email); ++ if ($response) { ++ // contact alredy exists ++ $contact_id = $response[0]['ID']; ++ return $contact_id; ++ } ++ ++ return null; } /** @@ -444,18 +575,23 @@ index 950ff8a..05601ca 100644 $contact_property = $category['contact_property']; $newsletter_property_name = $category['newsletter_property_name']; $subscriber_email = $subscriber->getMail(); -- $encoded_mail = urlencode($subscriber_email); + $encoded_mail = urlencode($subscriber_email); - $response = ''; - - $contact_info = self::checkSubscriber($api_key, $secret_key, $encoded_mail); - - $options = $contact_info['Data']; -- + - // Se existe - acrescenta a opções. - if ($options[0]['Data']) { - foreach ($options[0]['Data'] as $property) { - if ($property["Name"] == $contact_property) { -- ++ $contactid = $this->checkSubscriber($encoded_mail); ++ if (!$contactid) { ++ // subscriber does not exists yet, create it ++ $contactid = $this->createSubscriber($subscriber_email); ++ } + - if (is_int(strpos($property['Value'], $newsletter_property_name))) { - $newsletter_property_name = $property['Value']; - } @@ -463,23 +599,31 @@ index 950ff8a..05601ca 100644 - $newsletter_property_name = $property['Value'] . ';' . $newsletter_property_name; - } - } -- } -- } -+ $contact = [ -+ 'Email' => $subscriber_email, -+ 'Properties' => [ -+ $contact_property => $newsletter_property_name -+ ] -+ ]; ++ if ($contactid) { ++ $contact = [ ++ 'Email' => $subscriber_email, ++ // 'Properties' => [ ++ // $contact_property => $newsletter_property_name ++ // ] ++ ]; ++ ++ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce'); ++ if ($response) { ++ // $contact_id = $response['Data'][0]['ContactID']; ++ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list.'); ++ }else{ ++ \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while subscrining ' . $subscriber_email . ' to the ' . $listID . ' list.'); + } ++ ++ } else { ++ \Drupal::logger('simplenews_mailjet_subscriptions')->warning($subscriber_email . ' does not exists and can not be created on mailjet.'); + } - $response = self::updateSubscriber($api_key, $secret_key, $listID, $subscriber_email, $contact_property, $newsletter_property_name, 'addnoforce'); - $contact_id = $response['Data'][0]['ContactID']; - \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $contact_id . ' subscribed to the ' . $contact_property . ' properties with the ' . $newsletter_property_name . ' values in the list ' . $listID . '.'); - return $response; -+ // $response = $this->updateSubscriber($listID, $contact, 'addnoforce'); -+ $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'addnoforce'); -+ // $contact_id = $response['Data'][0]['ContactID']; -+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' subscribed to the ' . $listID . ' list .'); ++ } /** @@ -525,17 +669,19 @@ index 950ff8a..05601ca 100644 - } + $contact = [ + 'Email' => $subscriber_email, -+ 'Properties' => [ -+ $contact_property => $newsletter_property_name -+ ] ++ // 'Properties' => [ ++ // $contact_property => $newsletter_property_name ++ // ] + ]; - return $response; -+ // $response = $this->updateSubscriber($subscriber_email, $newsletter_property_name, 'remove'); + $response = $this->mailjetHandler->syncMailjetContact($listID, $contact, 'remove'); -+ // $contact_id = $response['Data'][0]['ContactID']; -+ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list '); -+ // return $response; ++ if ($response) { ++ // $contact_id = $response['Data'][0]['ContactID']; ++ \Drupal::logger('simplenews_mailjet_subscriptions')->notice('The mailjet user ' . $subscriber_email . ' removed from the ' . $listID . ' list.'); ++ }else{ ++ \Drupal::logger('simplenews_mailjet_subscriptions')->warning('error while removing ' . $subscriber_email . ' from the ' . $listID . ' list.'); ++ } } }