diff --git a/application/Espo/Core/Webhook/Queue.php b/application/Espo/Core/Webhook/Queue.php index e609afffe9..b9c7597aba 100644 --- a/application/Espo/Core/Webhook/Queue.php +++ b/application/Espo/Core/Webhook/Queue.php @@ -30,6 +30,7 @@ namespace Espo\Core\Webhook; use Espo\Core\Field\DateTime; +use Espo\Core\Field\LinkParent; use Espo\Core\Name\Field; use Espo\Entities\User; use Espo\Entities\Webhook; @@ -94,8 +95,14 @@ class Queue protected function createQueueFromEvent(WebhookEventQueueItem $item): void { - $webhookList = $this->entityManager - ->getRDBRepository(Webhook::ENTITY_TYPE) + if (!$item->getTargetId() || !$item->getTargetType()) { + return; + } + + $target = LinkParent::create($item->getTargetType(), $item->getTargetId()); + + $webhooks = $this->entityManager + ->getRDBRepositoryByClass(Webhook::class) ->where([ 'event' => $item->getEvent(), 'isActive' => true, @@ -103,16 +110,18 @@ class Queue ->order(Field::CREATED_AT) ->find(); - foreach ($webhookList as $webhook) { - $this->entityManager->createEntity(WebhookQueueItem::ENTITY_TYPE, [ - 'webhookId' => $webhook->getId(), - 'event' => $item->getEvent(), - 'targetId' => $item->getTargetId(), - 'targetType' => $item->getTargetType(), - 'status' => WebhookQueueItem::STATUS_PENDING, - 'data' => $item->getData(), - 'attempts' => 0, - ]); + foreach ($webhooks as $webhook) { + $queueItem = $this->entityManager->getRDBRepositoryByClass(WebhookQueueItem::class)->getNew(); + + $queueItem + ->setEvent($item->getEvent()) + ->setWebhook($webhook) + ->setTarget($target) + ->setStatus(WebhookQueueItem::STATUS_PENDING) + ->setAttempts(0) + ->setData($item->getData()); + + $this->entityManager->saveEntity($queueItem); } } @@ -122,7 +131,10 @@ class Queue $groupedItemList = $this->entityManager ->getRDBRepository(WebhookQueueItem::ENTITY_TYPE) - ->select(['webhookId', 'number']) + ->select([ + 'webhookId', + 'number', + ]) ->where( Cond::in( Cond::column('number'), diff --git a/application/Espo/Entities/WebhookQueueItem.php b/application/Espo/Entities/WebhookQueueItem.php index 7c6e634d5c..e66f9b1f53 100644 --- a/application/Espo/Entities/WebhookQueueItem.php +++ b/application/Espo/Entities/WebhookQueueItem.php @@ -30,6 +30,7 @@ namespace Espo\Entities; use Espo\Core\Field\DateTime; +use Espo\Core\Field\LinkParent; use Espo\Core\ORM\Entity; use stdClass; @@ -48,16 +49,17 @@ class WebhookQueueItem extends Entity public function setStatus(string $status): self { - $this->set('status', $status); + return $this->set('status', $status); + } - return $this; + public function setData(stdClass $data): self + { + return $this->set('data', $data); } public function setAttempts(?int $attempts): self { - $this->set('attempts', $attempts); - - return $this; + return $this->set('attempts', $attempts); } public function setProcessAt(?DateTime $processAt): self @@ -100,4 +102,19 @@ class WebhookQueueItem extends Entity { return $this->get('data') ?? (object) []; } + + public function setEvent(string $event): self + { + return $this->set('event', $event); + } + + public function setWebhook(Webhook $webhook): self + { + return $this->setRelatedLinkOrEntity('webhook', $webhook); + } + + public function setTarget(Entity|LinkParent $target): self + { + return $this->setRelatedLinkOrEntity('target', $target); + } }