]> _ Git - fluidbook-toolbox.git/commitdiff
wait #6703 @1.5
authorVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 1 Feb 2024 08:37:52 +0000 (09:37 +0100)
committerVincent Vanwaelscappel <vincent@cubedesigners.com>
Thu, 1 Feb 2024 08:37:52 +0000 (09:37 +0100)
app/Jobs/MailjetSyncList.php

index cb105f33a4998ea6ae1df833b2d5953ce3b624c3..d2754ad024b0fa6ec618f29a1c1abd99d681cd61 100644 (file)
@@ -19,11 +19,20 @@ class MailjetSyncList extends Base
 {
     const LIST_ID = '10296433';
 
+    const ENABLED = false;
+
+    const BATCH = 500;
+
     /**
      * @throws \Exception
      */
     public function handle()
     {
+        if (!self::ENABLED) {
+            $this->deleteContacts();
+            return;
+        }
+
         $this->syncStatusFromMailjet();
         dump('Synced status from mailjet');
 
@@ -111,6 +120,74 @@ class MailjetSyncList extends Base
 
     }
 
+    protected function deleteContacts()
+    {
+        $mj = static::_api();
+        $i = 0;
+        $remove = $this->getRecipientsList();
+        $emails=$this->getEmailsFromContactIDs($remove);
+        $contacts = [];
+        foreach ($emails as $email) {
+            $contacts[] = ['Email' => $email];
+        }
+        $this->addContactsToList($contacts, 'remove');
+    }
+
+    protected function getRecipientsList($filter = null)
+    {
+        $i = 0;
+        $res = [];
+        $mj = static::_api();
+
+
+        $filters = ['ContactsList' => static::LIST_ID,
+            'Limit' => self::BATCH,
+            'Offset' => 0];
+        if (null !== $filter) {
+            $filters = array_merge($filters, $filter);
+        }
+
+        while (true) {
+            $filters['Offset'] = $i * self::BATCH;
+            $response = $mj->get(Resources::$Listrecipient, ['filters' => $filters]);
+            foreach ($response->getData() as $contact) {
+                $res[] = $contact['ContactID'];
+            }
+
+            if ($response->getCount() < self::BATCH) {
+                break;
+            }
+
+            $i++;
+        }
+        return $res;
+    }
+
+    protected function getEmailsFromContactIDs($contactIDs)
+    {
+        $i = 0;
+        $res = [];
+        $mj = static::_api();
+
+        while (true) {
+            $response = $mj->get(Resources::$Contact, ['filters' => ['ContactsList' => static::LIST_ID,
+                'Limit' => self::BATCH,
+                'Offset' => $i * self::BATCH,
+            ]]);
+
+            foreach ($response->getData() as $contact) {
+                if (in_array($contact['ID'], $contactIDs)) {
+                    $res[] = $contact['Email'];
+                }
+            }
+            if ($response->getCount() < self::BATCH) {
+                break;
+            }
+            $i++;
+        }
+        return $res;
+    }
+
     protected function addContactsToList($contacts, $action = 'addnoforce')
     {
         if (empty($contacts)) {
@@ -141,41 +218,37 @@ class MailjetSyncList extends Base
     {
         $mj = static::_api();
         $i = 0;
-        $batchs = 500;
         $unsub = [];
-        $unsubEmails = [];
-        while (true) {
-            $response = $mj->get(Resources::$Listrecipient, ['filters' => ['ContactsList' => static::LIST_ID,
-                'Limit' => $batchs,
-                'Offset' => $i * $batchs,
-                'Unsub' => "true"]]);
 
+        $timestamp = time() - (3600 * 24 * 90); // 90 days in the past
 
-            foreach ($response->getData() as $contact) {
-                $unsub[] = $contact['ContactID'];
-            }
-
-            if ($response->getCount() < $batchs) {
-                break;
-            }
-            $i++;
-        }
+        // Check bounces
         while (true) {
-            $response = $mj->get(Resources::$Contact, ['filters' => ['ContactsList' => static::LIST_ID,
-                'Limit' => $batchs,
-                'Offset' => $i * $batchs,
+            $response = $mj->get(Resources::$Bouncestatistics, ['filters' => [
+                'Limit' => self::BATCH,
+                'Offset' => $i * self::BATCH,
+                'FromTS' => $timestamp,
             ]]);
 
             foreach ($response->getData() as $contact) {
-                if (in_array($contact['ID'], $unsub)) {
-                    $unsubEmails[] = $contact['Email'];
+                if ($contact['IsBlocked'] || $contact['IsStatePermanent']) {
+                    $unsub[] = $contact['ContactID'];
                 }
             }
-            if ($response->getCount() < $batchs) {
+
+            if ($response->getCount() < self::BATCH) {
                 break;
             }
             $i++;
         }
+
+        // Check unsubscribed users
+        $filters = ['Unsub' => 'true', 'IsExcludedFromCampaigns' => 'true'];
+        foreach ($filters as $filter => $value) {
+            $unsub = array_merge($unsub, $this->getRecipientsList([$filter => $value]));
+        }
+
+        $unsubEmails = $this->getEmailsFromContactIDs($unsub);
         DB::table('extranet_users.user')->whereIn('email', $unsubEmails)->update(['marketing' => 0]);
     }