Fix send action checkSendingStatus to support multiple emails per subscriber.

It is possible that one email (e.g., purchase in category) is sent multiple times
to the same subscriber.

AutomationEmailScheduler::getScheduledTaskSubscriber was selecting the task based on subscriber and newsletter.
In the case of multiple emails sent to one subscriber, the method failed to pick ScheduledTaskSubsrciberEntity because
the query was fetching multiple results, but getOneOrNullResult expects only one result.

This commit fixes it by adding additional filtering by $runId to get the ScheduledTaskSubsriberEntity associated
with the correct run.

I did the filtering in PHP because an alternative would be using LIKE %% in the query. The meta column is text.
[MAILPOET-6155]
This commit is contained in:
Rostislav Wolny
2024-07-24 12:52:35 +02:00
committed by Aschepikov
parent 38b3fbe6fc
commit e5ab65f28e
4 changed files with 80 additions and 6 deletions

View File

@@ -64,8 +64,8 @@ class AutomationEmailScheduler {
return $task;
}
public function getScheduledTaskSubscriber(NewsletterEntity $email, SubscriberEntity $subscriber): ?ScheduledTaskSubscriberEntity {
$result = $this->entityManager->createQueryBuilder()
public function getScheduledTaskSubscriber(NewsletterEntity $email, SubscriberEntity $subscriber, int $runId): ?ScheduledTaskSubscriberEntity {
$results = $this->entityManager->createQueryBuilder()
->select('sts')
->from(ScheduledTaskSubscriberEntity::class, 'sts')
->join('sts.task', 'st')
@@ -75,7 +75,19 @@ class AutomationEmailScheduler {
->setParameter('newsletter', $email)
->setParameter('subscriber', $subscriber)
->getQuery()
->getOneOrNullResult();
->getResult();
$result = null;
foreach ($results as $scheduledTaskSubscriber) {
$task = $scheduledTaskSubscriber->getTask();
if (!$task instanceof ScheduledTaskEntity) {
continue;
}
$meta = $task->getMeta();
if (($meta['automation']['run_id'] ?? null) === $runId) {
$result = $scheduledTaskSubscriber;
break;
}
}
return $result instanceof ScheduledTaskSubscriberEntity ? $result : null;
}