Skip to content

Commit

Permalink
Merge pull request #25 from klaviyo/201904_customer_subscribe_api
Browse files Browse the repository at this point in the history
subscriber api endpoint
  • Loading branch information
remstone7 authored May 1, 2019
2 parents 529a9c7 + a2d2ec5 commit cf91a0d
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 7 deletions.
38 changes: 35 additions & 3 deletions Api/ReclaimInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,45 @@ public function reclaim();
*/
public function product($quote_id, $item_id);

/**
/**
* Returns product by id range
* @api
*
* @api
* @param mixed $quote_id
* @param mixed $item_id
* @return mixed
*/
public function productinspector($start_id, $end_id);

/**
* Returns subscribers by date filter
*
* @api
* @return mixed
*/

public function getSubscribersCount();

/**
* Returns subscribers by date filter
*
* @api
* @param mixed $start
* @param mixed $until
* @param mixed $store_id
* @return mixed
*/
public function getSubscribersByDateRange($start, $until, $store_id=null);

/**
* Returns subscirbers by id
*
* @api
* @param mixed $start_id
* @param mixed $end_id
* @param mixed $store_id
* @return mixed
*/
public function productinspector($start_id, $end_id);
public function getSubscribersById($start_id, $end_id, $store_id=null);

}
99 changes: 98 additions & 1 deletion Model/Reclaim.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,26 @@ class Reclaim implements ReclaimInterface
* @var \Magento\Framework\ObjectManagerInterface
*/
protected $_objectManager = null;
protected $_subscriber;
protected $subscriberCollection;
public $response;

const MAX_QUERY_DAYS = 10;
const SUBSCRIBER_BATCH_SIZE = 500;

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;

}

/**
Expand Down Expand Up @@ -127,6 +135,95 @@ public function productinspector($start_id, $end_id){
return $response;

}

public function getSubscribersCount()
{
$subscriberCount =$this->_subscriberCollection->create()->count();
return $subscriberCount;
}

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'));
}

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()
->addFieldToFilter('subscriber_id', ['gteq' => (int)$start_id])
->addFieldToFilter('subscriber_id', ['lteq' => (int)$end_id])
->addFieldToFilter('store_id', [$storeIdFilter => $storeId]);

$response = $this->_packageSubscribers($subscriberCollection);

return $response;
}

public function getSubscribersByDateRange($start, $until, $storeId=null)
{

if (!$start || !$until ){
throw new NotFoundException(__('Please provide start and until param'));
}
// start and until date formats
// $until = '2019-04-25 18:00:00';
// $start = '2019-04-25 00:00:00';

$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(__('Cannot query 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();
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Klaviyo_Reclaim" setup_version="1.0.6">
<module name="Klaviyo_Reclaim" setup_version="1.0.7">
</module>
</config>
18 changes: 18 additions & 0 deletions etc/webapi.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,24 @@
<resource ref="anonymous"/>
</resources>
</route>
<route url="/V1/klaviyo/reclaim/customers/subscriptions/count" method="GET">
<service class="Klaviyo\Reclaim\Api\ReclaimInterface" method="getSubscribersCount"/>
<resources>
<resource ref="Magento_Customer::group"/>
</resources>
</route>
<route url="/V1/klaviyo/reclaim/customers/subscriptions/periodic" method="GET">
<service class="Klaviyo\Reclaim\Api\ReclaimInterface" method="getSubscribersByDateRange"/>
<resources>
<resource ref="Magento_Customer::group"/>
</resources>
</route>
<route url="/V1/klaviyo/reclaim/customers/subscriptions/historical" method="GET">
<service class="Klaviyo\Reclaim\Api\ReclaimInterface" method="getSubscribersById"/>
<resources>
<resource ref="Magento_Customer::group"/>
</resources>
</route>
<route url="/V1/klaviyo/reclaim/" method="GET">
<service class="Klaviyo\Reclaim\Api\ReclaimInterface" method="reclaim"/>
<resources>
Expand Down
2 changes: 1 addition & 1 deletion module.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
<module name="Klaviyo_Reclaim" setup_version="1.0.6">
<module name="Klaviyo_Reclaim" setup_version="1.0.7">
</module>
<sequence>
<module name="Magento_Checkout"/>
Expand Down

0 comments on commit cf91a0d

Please sign in to comment.