From 0b73f6ccacbb74dd6b0ea51ca65306e735fdc269 Mon Sep 17 00:00:00 2001 From: Vincent Vanwaelscappel Date: Thu, 1 Feb 2024 09:37:52 +0100 Subject: [PATCH] wait #6703 @1.5 --- app/Jobs/MailjetSyncList.php | 117 ++++++++++++++++++++++++++++------- 1 file changed, 95 insertions(+), 22 deletions(-) diff --git a/app/Jobs/MailjetSyncList.php b/app/Jobs/MailjetSyncList.php index cb105f33a..d2754ad02 100644 --- a/app/Jobs/MailjetSyncList.php +++ b/app/Jobs/MailjetSyncList.php @@ -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]); } -- 2.39.5