Interrupt automation run creation if a run already exists and only_once setting is set

[MAILPOET-4966]
This commit is contained in:
David Remer
2023-02-16 08:31:43 +02:00
committed by Aschepikov
parent 38e7346b6e
commit fcf6e738c7
5 changed files with 81 additions and 2 deletions

View File

@@ -4,6 +4,7 @@ namespace MailPoet\Automation\Engine\Storage;
use MailPoet\Automation\Engine\Data\Automation;
use MailPoet\Automation\Engine\Data\AutomationRun;
use MailPoet\Automation\Engine\Data\Subject;
use MailPoet\Automation\Engine\Exceptions;
use wpdb;
@@ -108,6 +109,26 @@ class AutomationRunStorage {
);
}
/**
* @param Automation $automation
* @return int
*/
public function countRunsForAutomationAndSubject(Automation $automation, Subject $subject): int {
$table = esc_sql($this->table);
$subjectTable = esc_sql($this->subjectTable);
$sql = "SELECT count(runs.id) as count from $table as runs
JOIN $subjectTable as subjects on runs.id = subjects.automation_run_id
WHERE runs.automation_id = %d
AND subjects.hash = %s";
$result = $this->wpdb->get_col(
(string)$this->wpdb->prepare($sql, $automation->getId(), $subject->hash())
);
return $result ? (int)current($result) : 0;
}
public function getCountForAutomation(Automation $automation, string ...$status): int {
if (!count($status)) {
return 0;