Fix fetching standard newsletters and post-notification history items

MAILPOET-5698
This commit is contained in:
Oluwaseun Olorunsola
2023-11-29 17:28:38 +01:00
committed by Aschepikov
parent f3ecf8366a
commit 65de203ea3
2 changed files with 82 additions and 21 deletions

View File

@@ -526,6 +526,65 @@ class NewslettersRepository extends Repository {
->getResult();
}
/**
* Returns standard newsletters ordered by sentAt
* filter by status STATUS_SCHEDULED, STATUS_SENDING, STATUS_SENT
* @return NewsletterEntity[]
*/
public function getStandardNewsletterListWithMultipleStatuses($limit): array {
$statuses = [
NewsletterEntity::STATUS_SCHEDULED,
NewsletterEntity::STATUS_SENDING,
NewsletterEntity::STATUS_SENT,
];
$query = $this->entityManager->createQueryBuilder()
->select('PARTIAL n.{id,subject,sentAt}')
->addSelect('CASE WHEN n.sentAt IS NULL THEN 1 ELSE 0 END as HIDDEN sent_at_is_null')
->from(NewsletterEntity::class, 'n')
->where('n.type = :typeStandard')
->andWhere('n.status IN (:statuses)')
->andWhere('n.deletedAt IS NULL')
->orderBy('sent_at_is_null', 'DESC')
->addOrderBy('n.sentAt', 'DESC')
->setParameter('typeStandard', NewsletterEntity::TYPE_STANDARD)
->setParameter('statuses', $statuses);
if (is_int($limit)) {
$query->setMaxResults($limit);
}
$result = $query->getQuery()->getResult();
return is_array($result) ? $result : [];
}
/**
* Returns sent post-notification history newsletters ordered by sentAt
* @return NewsletterEntity[]
*/
public function getNotificationHistoryItems($limit): array {
$query = $this->entityManager->createQueryBuilder()
->select('PARTIAL n.{id,subject,sentAt}')
->addSelect('CASE WHEN n.sentAt IS NULL THEN 1 ELSE 0 END as HIDDEN sent_at_is_null')
->from(NewsletterEntity::class, 'n')
->where('n.type = :typeNotificationHistory')
->andWhere('n.status = :status')
->andWhere('n.deletedAt IS NULL')
->orderBy('sent_at_is_null', 'DESC')
->addOrderBy('n.sentAt', 'DESC')
->setParameter('typeNotificationHistory', NewsletterEntity::TYPE_NOTIFICATION_HISTORY)
->setParameter('status', NewsletterEntity::STATUS_SENT);
if (is_int($limit)) {
$query->setMaxResults($limit);
}
$result = $query->getQuery()->getResult();
return is_array($result) ? $result : [];
}
public function prefetchOptions(array $newsletters) {
$this->entityManager->createQueryBuilder()
->select('PARTIAL n.{id}, o, opf')

View File

@@ -7,7 +7,6 @@ use Automattic\WooCommerce\Admin\Marketing\MarketingCampaignType;
use Automattic\WooCommerce\Admin\Marketing\MarketingChannelInterface;
use Automattic\WooCommerce\Admin\Marketing\Price;
use MailPoet\Config\Menu;
use MailPoet\Entities\NewsletterEntity;
use MailPoet\Newsletter\NewslettersRepository;
use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Services\Bridge;
@@ -242,7 +241,9 @@ class MPMarketingChannel implements MarketingChannelInterface {
protected function getStandardNewsletterList(): array {
$result = [];
foreach ($this->newsletterRepository->getStandardNewsletterList() as $newsletter) {
// fetch the most recent newsletters limited to ten
foreach ($this->newsletterRepository->getStandardNewsletterListWithMultipleStatuses(10) as $newsletter) {
$newsLetterId = (string)$newsletter->getId();
$result[] = [
'id' => $newsLetterId,
@@ -256,15 +257,15 @@ class MPMarketingChannel implements MarketingChannelInterface {
],
];
}
return $result;
}
protected function getPostNotificationNewsletters(): array {
$newsletters = $this->newsletterRepository->findActiveByTypes([NewsletterEntity::TYPE_NOTIFICATION]);
$result = [];
foreach ($newsletters as $newsletter) {
// fetch the most recently sent post-notification history newsletters limited to ten
foreach ($this->newsletterRepository->getNotificationHistoryItems(10) as $newsletter) {
$newsLetterId = (string)$newsletter->getId();
$result[] = [
'id' => $newsLetterId,
@@ -278,6 +279,7 @@ class MPMarketingChannel implements MarketingChannelInterface {
],
];
}
return $result;
}