From 8b138b97824f65b5e6ea4e2eb43f0c4103254945 Mon Sep 17 00:00:00 2001 From: remstone7 Date: Thu, 25 Apr 2019 16:04:59 -0400 Subject: [PATCH 1/5] subscriber api endpoint --- Api/ReclaimInterface.php | 22 ++++++++++ Model/Reclaim.php | 87 ++++++++++++++++++++++++++++++++++++++++ etc/webapi.xml | 12 ++++++ 3 files changed, 121 insertions(+) diff --git a/Api/ReclaimInterface.php b/Api/ReclaimInterface.php index c8f896f..3705591 100644 --- a/Api/ReclaimInterface.php +++ b/Api/ReclaimInterface.php @@ -29,6 +29,28 @@ public function reclaim(); */ public function product($quote_id, $item_id); + /** + * Returns subscribers by date filter + * + * @api + * @param mixed $start + * @param mixed $until + * @param mixed $storeId + * @return mixed + */ + public function customersubscription($start, $until, $storeId=null); + + /** + * Returns subscirbers by id + * + * @api + * @param mixed $start_id + * @param mixed $end_id + * @param mixed $storeId + * @return mixed + */ + public function historicalcustomersubscription($start_id, $end_id, $storeId=null); + /** * Returns product by id range * @api diff --git a/Model/Reclaim.php b/Model/Reclaim.php index abef3e9..adf3f81 100644 --- a/Model/Reclaim.php +++ b/Model/Reclaim.php @@ -12,20 +12,29 @@ class Reclaim implements ReclaimInterface * @var \Magento\Framework\ObjectManagerInterface */ protected $_objectManager = null; + protected $_subscriber; + protected $subscriberCollection; public $response; + const MAX_QUERY_DAYS = 10; + public function __construct( \Magento\Framework\ObjectManagerInterface $objectManager, \Magento\Quote\Model\QuoteFactory $quoteFactory, + \Magento\Newsletter\Model\Subscriber $subscriber, + \Magento\Newsletter\Model\ResourceModel\Subscriber\CollectionFactory $subscriberCollection, \Klaviyo\Reclaim\Helper\Data $klaviyoHelper ) { $this->quoteFactory = $quoteFactory; $this->_objectManager = $objectManager; + $this->_subscriber= $subscriber; + $this->subscriberCollection = $subscriberCollection; $this->_klaviyoHelper = $klaviyoHelper; } + /** * Returns extension version * @@ -127,6 +136,84 @@ public function productinspector($start_id, $end_id){ return $response; } + + public function historicalcustomersubscription($start_id, $end_id, $storeId=null) + { + + if (!$start_id || !$end_id ){ + throw new NotFoundException(__('Please provide start_id and end_id')); + } + + $storeIdFilter = $this->_storeFilter($storeId); + + $subscriberCollection =$this->subscriberCollection->create() + ->addFieldToFilter('subscriber_id', ['gteq' => (int)$start_id]) + ->addFieldToFilter('subscriber_id', ['lteq' => (int)$end_id]) + ->addFieldToFilter('store_id', [$storeIdFilter => $storeId]); + + $subscriberCollection =$this->subscriberCollection->create() + ->addFieldToFilter('subscriber_id', ['gt' => 1]); + + $response = $this->_packageSubscribers($subscriberCollection); + + return $response; + } + + public function customersubscription($start, $until, $storeId=null) + { + + if (!$start || !$until ){ + throw new NotFoundException(__('Please provide start_id and end_id')); + return array('error' => 'Please provide start and until'); + } + // start and until date formats + // $until = '2019-04-25 18:00:00'; + // $start = '2019-04-25 00:00:00'; + + // don't want any big queries, we limit to 10 days + $until_date = strtotime($until); + $start_date = strtotime($start); + $datediff = $now - $start_date; + + if (abs(round($datediff / (60 * 60 * 24))) > self::MAX_QUERY_DAYS){ + throw new NotFoundException(__('Please don\'t query for more than 10 days')); + } + + + + $storeIdFilter = $this->_storeFilter($storeId); + + $subscriberCollection =$this->subscriberCollection->create() + ->addFieldToFilter('change_status_at', ['gteq' => $start]) + ->addFieldToFilter('change_status_at', ['lteq' => $until]) + ->addFieldToFilter('store_id', [$storeIdFilter => $storeId]); + + $response = $this->_packageSubscribers($subscriberCollection); + + return $response; + + } + public function _packageSubscribers($subscriberCollection) + { + $response = array(); + foreach ($subscriberCollection as $subscriber){ + $response[]= array( + 'email' => $subscriber->getEmail(), + 'subscribe_status' => $subscriber->getSubscriberStatus() + ); + } + return $response; + } + + public function _storeFilter($storeId) + { + $storeIdFilter = 'eq'; + if (!$storeId){ + $storeIdFilter = 'nlike'; + } + return $storeIdFilter; + } + public function _getImages($product){ $images = $product->getMediaGalleryImages(); $image_array = array(); diff --git a/etc/webapi.xml b/etc/webapi.xml index 2979cb5..6b60dfa 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -18,6 +18,18 @@ + + + + + + + + + + + + From 56a686095cba13965122c78b39cbfafbe7b47a63 Mon Sep 17 00:00:00 2001 From: remstone7 Date: Thu, 25 Apr 2019 16:59:30 -0400 Subject: [PATCH 2/5] rename some functions and pr fixes --- Api/ReclaimInterface.php | 17 ++++------------- Model/Reclaim.php | 19 ++++++++----------- etc/webapi.xml | 4 ++-- 3 files changed, 14 insertions(+), 26 deletions(-) diff --git a/Api/ReclaimInterface.php b/Api/ReclaimInterface.php index 3705591..a4a9c7e 100644 --- a/Api/ReclaimInterface.php +++ b/Api/ReclaimInterface.php @@ -35,10 +35,10 @@ public function product($quote_id, $item_id); * @api * @param mixed $start * @param mixed $until - * @param mixed $storeId + * @param mixed $store_id * @return mixed */ - public function customersubscription($start, $until, $storeId=null); + public function getSubscribersByDateRange($start, $until, $store_id=null); /** * Returns subscirbers by id @@ -46,18 +46,9 @@ public function customersubscription($start, $until, $storeId=null); * @api * @param mixed $start_id * @param mixed $end_id - * @param mixed $storeId + * @param mixed $store_id * @return mixed */ - public function historicalcustomersubscription($start_id, $end_id, $storeId=null); - - /** - * Returns product by id range - * @api - * @param mixed $start_id - * @param mixed $end_id - * @return mixed - */ - public function productinspector($start_id, $end_id); + public function getSubscribersById($start_id, $end_id, $store_id=null); } \ No newline at end of file diff --git a/Model/Reclaim.php b/Model/Reclaim.php index adf3f81..3346bb4 100644 --- a/Model/Reclaim.php +++ b/Model/Reclaim.php @@ -137,13 +137,16 @@ public function productinspector($start_id, $end_id){ } - public function historicalcustomersubscription($start_id, $end_id, $storeId=null) + public function getSubscribersById($start_id, $end_id, $storeId=null) { - if (!$start_id || !$end_id ){ throw new NotFoundException(__('Please provide start_id and end_id')); } + if ($start_id > $end_id){ + throw new NotFoundException(__('end_id should be larger than start_id')); + } + $storeIdFilter = $this->_storeFilter($storeId); $subscriberCollection =$this->subscriberCollection->create() @@ -151,20 +154,16 @@ public function historicalcustomersubscription($start_id, $end_id, $storeId=null ->addFieldToFilter('subscriber_id', ['lteq' => (int)$end_id]) ->addFieldToFilter('store_id', [$storeIdFilter => $storeId]); - $subscriberCollection =$this->subscriberCollection->create() - ->addFieldToFilter('subscriber_id', ['gt' => 1]); - $response = $this->_packageSubscribers($subscriberCollection); return $response; } - public function customersubscription($start, $until, $storeId=null) + public function getSubscribersByDateRange($start, $until, $storeId=null) { if (!$start || !$until ){ - throw new NotFoundException(__('Please provide start_id and end_id')); - return array('error' => 'Please provide start and until'); + throw new NotFoundException(__('Please provide start and until param')); } // start and until date formats // $until = '2019-04-25 18:00:00'; @@ -173,14 +172,12 @@ public function customersubscription($start, $until, $storeId=null) // don't want any big queries, we limit to 10 days $until_date = strtotime($until); $start_date = strtotime($start); - $datediff = $now - $start_date; + $datediff = $until_date - $start_date; if (abs(round($datediff / (60 * 60 * 24))) > self::MAX_QUERY_DAYS){ throw new NotFoundException(__('Please don\'t query for more than 10 days')); } - - $storeIdFilter = $this->_storeFilter($storeId); $subscriberCollection =$this->subscriberCollection->create() diff --git a/etc/webapi.xml b/etc/webapi.xml index 6b60dfa..a7bb099 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -19,13 +19,13 @@ - + - + From 1ea8268f13bea35c5129a0a01f4116faa76c02ce Mon Sep 17 00:00:00 2001 From: remstone7 Date: Wed, 1 May 2019 11:05:24 -0400 Subject: [PATCH 3/5] pr feedback --- Api/ReclaimInterface.php | 9 +++++++++ Model/Reclaim.php | 27 ++++++++++++++++++++------- etc/webapi.xml | 10 ++++++++-- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/Api/ReclaimInterface.php b/Api/ReclaimInterface.php index a4a9c7e..6f71123 100644 --- a/Api/ReclaimInterface.php +++ b/Api/ReclaimInterface.php @@ -29,6 +29,15 @@ public function reclaim(); */ public function product($quote_id, $item_id); + /** + * Returns subscribers by date filter + * + * @api + * @return mixed + */ + + public function getSubscribersCount(); + /** * Returns subscribers by date filter * diff --git a/Model/Reclaim.php b/Model/Reclaim.php index 3346bb4..b5d049a 100644 --- a/Model/Reclaim.php +++ b/Model/Reclaim.php @@ -17,6 +17,7 @@ class Reclaim implements ReclaimInterface public $response; const MAX_QUERY_DAYS = 10; + const SUBSCRIBER_BATCH_SIZE = 500; public function __construct( \Magento\Framework\ObjectManagerInterface $objectManager, @@ -29,12 +30,10 @@ public function __construct( $this->quoteFactory = $quoteFactory; $this->_objectManager = $objectManager; $this->_subscriber= $subscriber; - $this->subscriberCollection = $subscriberCollection; + $this->_subscriberCollection = $subscriberCollection; $this->_klaviyoHelper = $klaviyoHelper; - } - /** * Returns extension version * @@ -137,6 +136,12 @@ public function productinspector($start_id, $end_id){ } + public function getSubscribersCount() + { + $subscriberCount =$this->_subscriberCollection->create()->count(); + return $subscriberCount; + } + public function getSubscribersById($start_id, $end_id, $storeId=null) { if (!$start_id || !$end_id ){ @@ -147,9 +152,13 @@ public function getSubscribersById($start_id, $end_id, $storeId=null) throw new NotFoundException(__('end_id should be larger than start_id')); } + if (($end_id - $start_id) > self::SUBSCRIBER_BATCH_SIZE){ + throw new NotFoundException(__('Max batch size is 500')); + } + $storeIdFilter = $this->_storeFilter($storeId); - $subscriberCollection =$this->subscriberCollection->create() + $subscriberCollection =$this->_subscriberCollection->create() ->addFieldToFilter('subscriber_id', ['gteq' => (int)$start_id]) ->addFieldToFilter('subscriber_id', ['lteq' => (int)$end_id]) ->addFieldToFilter('store_id', [$storeIdFilter => $storeId]); @@ -169,18 +178,22 @@ public function getSubscribersByDateRange($start, $until, $storeId=null) // $until = '2019-04-25 18:00:00'; // $start = '2019-04-25 00:00:00'; - // don't want any big queries, we limit to 10 days $until_date = strtotime($until); $start_date = strtotime($start); + if (!$until_date || !$start_date){ + throw new NotFoundException(__('Please use a valid date format YYYY-MM-DD HH:MM:SS')); + } + + // don't want any big queries, we limit to 10 days $datediff = $until_date - $start_date; if (abs(round($datediff / (60 * 60 * 24))) > self::MAX_QUERY_DAYS){ - throw new NotFoundException(__('Please don\'t query for more than 10 days')); + throw new NotFoundException(__('Cannot query more than 10 days')); } $storeIdFilter = $this->_storeFilter($storeId); - $subscriberCollection =$this->subscriberCollection->create() + $subscriberCollection =$this->_subscriberCollection->create() ->addFieldToFilter('change_status_at', ['gteq' => $start]) ->addFieldToFilter('change_status_at', ['lteq' => $until]) ->addFieldToFilter('store_id', [$storeIdFilter => $storeId]); diff --git a/etc/webapi.xml b/etc/webapi.xml index a7bb099..8576c09 100644 --- a/etc/webapi.xml +++ b/etc/webapi.xml @@ -18,16 +18,22 @@ + + + + + + - + - + From dac9223b9f1ec496fae424de47a8bc113860331e Mon Sep 17 00:00:00 2001 From: remstone7 Date: Wed, 1 May 2019 11:22:07 -0400 Subject: [PATCH 4/5] bump the version! --- composer.json | 2 +- etc/module.xml | 2 +- module.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 4fd84fb..3574aca 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "klaviyo/magento2-extension", "description": "Klaviyo extension for Magento 2. Allows pushing newsletters to Klaviyo's platform and more.", "type": "magento2-module", - "version": "1.0.6", + "version": "1.0.7", "autoload": { "files": [ "registration.php" diff --git a/etc/module.xml b/etc/module.xml index f6a2a93..b5b575d 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,5 +1,5 @@ - + diff --git a/module.xml b/module.xml index b0dc9c0..ac2d644 100644 --- a/module.xml +++ b/module.xml @@ -1,6 +1,6 @@ - + From a2d2ec58d4608a5ee21076050ecc2ec55a8218b9 Mon Sep 17 00:00:00 2001 From: remstone7 Date: Wed, 1 May 2019 11:25:10 -0400 Subject: [PATCH 5/5] dont remove that method --- Api/ReclaimInterface.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/Api/ReclaimInterface.php b/Api/ReclaimInterface.php index 6f71123..e351638 100644 --- a/Api/ReclaimInterface.php +++ b/Api/ReclaimInterface.php @@ -29,6 +29,16 @@ public function reclaim(); */ public function product($quote_id, $item_id); + /** + * Returns product by id range + * + * @api + * @param mixed $quote_id + * @param mixed $item_id + * @return mixed + */ + public function productinspector($start_id, $end_id); + /** * Returns subscribers by date filter *