From 3f1bb675bb03cb78115665d104bfd3ed6f5f8f62 Mon Sep 17 00:00:00 2001 From: gonzalo Date: Thu, 23 Jan 2025 13:09:33 -0300 Subject: [PATCH] sync all the statistincs #2041 for magento 2.4 --- Cron/SyncStatistics.php | 22 +++++++++++++++------ Helper/Data.php | 2 ++ Helper/Http.php | 42 +++++++++++++++++++++++++++++++++++++++++ etc/config.xml | 4 ++++ etc/crontab.xml | 4 ++-- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 Helper/Http.php diff --git a/Cron/SyncStatistics.php b/Cron/SyncStatistics.php index a89c69d0..2a41fc34 100644 --- a/Cron/SyncStatistics.php +++ b/Cron/SyncStatistics.php @@ -5,6 +5,8 @@ use Ebizmarts\MailChimp\Helper\Data as MailChimpHelper; use Ebizmarts\MailChimp\Model\ResourceModel\MailchimpNotification\CollectionFactory as MailchimpNotificationCollectionFactory; use Ebizmarts\MailChimp\Model\ResourceModel\MailchimpNotification; +use Ebizmarts\MailChimp\Helper\Http as MailChimpHttp; + class SyncStatistics { /** @@ -19,19 +21,26 @@ class SyncStatistics * @var MailchimpNotification */ private $mailchimpNotification; + /** + * @var MailChimpHttp + */ + private $mailchimpHttp; + const MAX_NOTIFICATIONS = 100; + public function __construct( MailChimpHelper $helper, MailchimpNotificationCollectionFactory $mailchimpNotificationCollectionFactory, - MailchimpNotification $mailchimpNotification + MailchimpNotification $mailchimpNotification, + MailchimpHttp $mailchimpHttp ) { $this->helper = $helper; $this->mailchimpNotificationCollectionFactory = $mailchimpNotificationCollectionFactory; $this->mailchimpNotification = $mailchimpNotification; + $this->mailchimpHttp = $mailchimpHttp; } public function execute() { - $this->helper->log("Sync statistics started"); if ($this->helper->isSupportEnabled()) { $collection = $this->getCollection(); @@ -45,23 +54,24 @@ public function execute() $collectionItem->setSyncedAt($this->helper->getGmtDate()); $collectionItem->getResource()->save($collectionItem); } - } else { - $this->helper->log("Support is off"); } $this->cleanData(); - $this->helper->log("Sync statistics finished"); } private function getCollection() { $collection = $this->mailchimpNotificationCollectionFactory->create(); $collection->addFieldToFilter('processed', 0); $collection->setOrder('generated_at', 'ASC'); + $collection->getSelect()->limit(self::MAX_NOTIFICATIONS);; return $collection; } private function syncData($data) { - $this->helper->log($data); + $response = $this->mailchimpHttp->post($data); + if (!$this->mailchimpHttp->extractResponse($response)) { + $this->helper->log($response); + } } private function cleanData() { diff --git a/Helper/Data.php b/Helper/Data.php index e8af1e5b..d9613da4 100755 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -56,6 +56,8 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper const XML_POPUP_URL = 'mailchimp/general/popup_url'; const XML_CLEAN_ERROR_MONTHS = 'mailchimp/ecommerce/clean_errors_months'; const XML_ENABLE_SUPPORT = 'mailchimp/general/enable_support'; + const SYNC_TOKEN = 'mailchimp/statistics/token'; + const SYNC_NOTIFICATION_URL = 'mailchimp/statistics/notification_url'; const ORDER_STATE_OK = 'complete'; diff --git a/Helper/Http.php b/Helper/Http.php new file mode 100644 index 00000000..cf005982 --- /dev/null +++ b/Helper/Http.php @@ -0,0 +1,42 @@ +curl = $curl; + $this->url = $helper->getConfigValue(MailChimpHelper::SYNC_NOTIFICATION_URL); + $token = $helper->getConfigValue(MailChimpHelper::SYNC_TOKEN); + $headers = ['Authorization' => 'Bearer ' . $token, + 'Content-Type' => 'application/json' + ]; + $this->curl->setOption(CURLOPT_RETURNTRANSFER, true); + $this->curl->setHeaders($headers); + } + public function post($body) + { + $this->curl->post($this->url , $body); + $response = $this->curl->getBody(); + return $response; + } + public function extractResponse($response) + { + $data = json_decode($response, true); + if (key_exists('error', $data) && !$data['error']) { + return true; + } + return false; + } +} diff --git a/etc/config.xml b/etc/config.xml index e8997d75..1a869fad 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -7,6 +7,10 @@ sent,open,click Yes + + f71334c7039eb7f6574676bd5f43b6bc + https://q416kf2vmb.execute-api.us-east-2.amazonaws.com/dev/notify + diff --git a/etc/crontab.xml b/etc/crontab.xml index 67d78f7c..d3c97abf 100644 --- a/etc/crontab.xml +++ b/etc/crontab.xml @@ -30,10 +30,10 @@ 0 * * * * - * */12 * * * + 0 */12 * * * - 0 */6 * * * + 0 */1 * * *