Add reactivation of recently active subscribers

[MAILPOET-1791]
This commit is contained in:
Rostislav Wolny
2019-04-16 15:06:26 +02:00
committed by M. Shull
parent 9edace78f0
commit 7b433a4273
2 changed files with 89 additions and 0 deletions

View File

@ -23,6 +23,16 @@ class InactiveSubscribersController {
return $this->deactivateSubscribers($threshold_date, $batch_size);
}
/**
* @param int $days_to_inactive
* @param int $batch_size
* @return int
*/
function markActiveSubscribers($days_to_inactive, $batch_size) {
$threshold_date = $this->getThresholdDate($days_to_inactive);
return $this->activateSubscribers($threshold_date, $batch_size);
}
/**
* @param int $days_to_inactive
* @return Carbon
@ -85,4 +95,38 @@ class InactiveSubscribersController {
));
return count($ids_to_deactivate);
}
/**
* @param Carbon $threshold_date
* @param int $batch_size
* @return int
*/
private function activateSubscribers(Carbon $threshold_date, $batch_size) {
$subscribers_table = Subscriber::$_table;
$stats_opens_table = StatisticsOpens::$_table;
$ids_to_activate = \ORM::forTable($subscribers_table)->select("$subscribers_table.id")
->leftOuterJoin($stats_opens_table, "$subscribers_table.id = $stats_opens_table.subscriber_id AND $stats_opens_table.created_at > '$threshold_date'")
->whereLt("$subscribers_table.created_at", $threshold_date)
->where("$subscribers_table.status", Subscriber::STATUS_INACTIVE)
->whereRaw("$stats_opens_table.id IS NOT NULL")
->limit($batch_size)
->groupByExpr("$subscribers_table.id")
->findArray();
$ids_to_activate = array_map(
function($id) {
return (int)$id['id'];
}, $ids_to_activate
);
if (!count($ids_to_activate)) {
return 0;
}
\ORM::rawExecute(sprintf(
"UPDATE %s SET status='" . Subscriber::STATUS_SUBSCRIBED . "' WHERE id IN (%s);",
$subscribers_table,
implode(',', $ids_to_activate)
));
return count($ids_to_activate);
}
}

View File

@ -129,6 +129,51 @@ class InactiveSubscribersControllerTest extends \MailPoetTest {
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
}
function testItActivatesSubscriberWhoRecentlyOpenedEmail() {
list($task, $queue) = $this->createCompletedSendingTask(2);
$subscriber = $this->createSubscriber('s1@email.com', 10, Subscriber::STATUS_INACTIVE);
$this->addSubcriberToTask($subscriber, $task);
$this->addEmailOpenedRecord($subscriber, $queue, 2);
$result = $this->controller->markActiveSubscribers(5, 100);
expect($result)->equals(1);
$subscriber = Subscriber::findOne($subscriber->id);
expect($subscriber->status)->equals(Subscriber::STATUS_SUBSCRIBED);
}
function testItActivatesLimitedNumberOfSubscribers() {
list($task, $queue) = $this->createCompletedSendingTask(2);
$subscriber1 = $this->createSubscriber('s1@email.com', 10, Subscriber::STATUS_INACTIVE);
$subscriber2 = $this->createSubscriber('s2@email.com', 10, Subscriber::STATUS_INACTIVE);
$this->addSubcriberToTask($subscriber1, $task);
$this->addSubcriberToTask($subscriber2, $task);
$this->addEmailOpenedRecord($subscriber1, $queue, 2);
$this->addEmailOpenedRecord($subscriber2, $queue, 2);
$result = $this->controller->markActiveSubscribers(5, 1);
expect($result)->equals(1);
$subscriber1 = Subscriber::findOne($subscriber1->id);
$subscriber2 = Subscriber::findOne($subscriber2->id);
expect($subscriber1->status === Subscriber::STATUS_INACTIVE || $subscriber2->status === Subscriber::STATUS_INACTIVE)->true();
expect($subscriber1->status === Subscriber::STATUS_SUBSCRIBED || $subscriber2->status === Subscriber::STATUS_SUBSCRIBED)->true();
$result = $this->controller->markActiveSubscribers(5, 1);
expect($result)->equals(1);
$subscriber1 = Subscriber::findOne($subscriber1->id);
$subscriber2 = Subscriber::findOne($subscriber2->id);
expect($subscriber1->status)->equals(Subscriber::STATUS_SUBSCRIBED);
expect($subscriber2->status)->equals(Subscriber::STATUS_SUBSCRIBED);
}
function testItDoesNotActivateOldSubscribersWithUnopenedEmail() {
list($task) = $this->createCompletedSendingTask(2);
$subscriber = $this->createSubscriber('s1@email.com', 10, Subscriber::STATUS_INACTIVE);
$this->addSubcriberToTask($subscriber, $task);
$result = $this->controller->markActiveSubscribers(5, 100);
expect($result)->equals(0);
$subscriber = Subscriber::findOne($subscriber->id);
expect($subscriber->status)->equals(Subscriber::STATUS_INACTIVE);
}
/**
* @param $email
* @param int $created_days_ago