From 99630f85aa5513fdc6ac515d0acac15b3e43d98e Mon Sep 17 00:00:00 2001 From: John Oleksowicz Date: Fri, 21 Jul 2023 14:55:32 -0500 Subject: [PATCH] Reset engagement score for inactive subscribers If someone hasn't received at least 3 emails in the last year, we can't trust that their level of engagement would be the same now as it was over a year ago. MAILPOET-5410 --- .../lib/Statistics/StatisticsOpensRepository.php | 1 + .../Statistics/StatisticsOpensRepositoryTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/mailpoet/lib/Statistics/StatisticsOpensRepository.php b/mailpoet/lib/Statistics/StatisticsOpensRepository.php index 8e8d78e6b9..e648d8565b 100644 --- a/mailpoet/lib/Statistics/StatisticsOpensRepository.php +++ b/mailpoet/lib/Statistics/StatisticsOpensRepository.php @@ -33,6 +33,7 @@ class StatisticsOpensRepository extends Repository { ->getQuery() ->getSingleScalarResult(); if ($newslettersSentCount < 3) { + $subscriber->setEngagementScore(null); $this->entityManager->flush(); return; } diff --git a/mailpoet/tests/integration/Statistics/StatisticsOpensRepositoryTest.php b/mailpoet/tests/integration/Statistics/StatisticsOpensRepositoryTest.php index da54bc09cf..11c53c5689 100644 --- a/mailpoet/tests/integration/Statistics/StatisticsOpensRepositoryTest.php +++ b/mailpoet/tests/integration/Statistics/StatisticsOpensRepositoryTest.php @@ -78,6 +78,19 @@ class StatisticsOpensRepositoryTest extends \MailPoetTest { expect($averageScoreUpdatedAt->isAfter((new CarbonImmutable())->subMinutes(5)))->true(); } + public function testItResetsSubscriberScoreIfNotEnoughNewsletters() { + $subscriber = $this->createSubscriber(); + $segment = $this->createSegment(); + $this->createSubscriberSegment($subscriber, $segment); + $subscriber->setEngagementScore(5); + $this->entityManager->flush(); + $this->entityManager->refresh($subscriber); + expect($subscriber->getEngagementScore())->equals(5); + $this->repository->recalculateSubscriberScore($subscriber); + $this->entityManager->refresh($subscriber); + expect($subscriber->getEngagementScore())->null(); + } + public function testItUpdatesScore() { $subscriber = $this->createSubscriber(); $segment = $this->createSegment();