Skip to content

Commit

Permalink
DIAM-1862 fix wrong branch key in email notification
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Kolomiiets committed Jun 12, 2017
1 parent 2cabef0 commit cf09d0a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 30 deletions.
3 changes: 3 additions & 0 deletions src/Diamante/AutomationBundle/Automation/Scheduler.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function __construct(Logger $logger)
*/
public function run(AbstractFact $fact)
{
// first move to branch and only then notify by email
asort($this->queue);

foreach ($this->queue as $action) {
try {
$action->getContext()->setFact($fact);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Diamante\AutomationBundle\Automation\Action\Email\EntityNotifier;
use Diamante\AutomationBundle\EventListener\EventTriggeredListener;
use Diamante\DeskBundle\Api\Internal\AttachmentServiceImpl;
use Diamante\DeskBundle\Entity\Ticket;
use Diamante\UserBundle\Entity\DiamanteUser;
use Oro\Bundle\UserBundle\Entity\User as OroUser;

Expand Down Expand Up @@ -99,16 +100,18 @@ public function notify()
$target = $this->fact->getTarget();
$emails = $this->getEmailList();
$provider = $this->getProvider();
$options = $this->getOptions();
$editor = $this->fact->getEditor();
$editor = $this->container->get('diamante.user.service')->getByUser($editor);
$ticketId = $target['ticket']->getId();
$ticket = null;

if (!is_null($ticketId)) {
$ticket = $this->container->get('diamante.ticket.repository')->get($ticketId);
$this->notificationManager->setTicket($ticket);
}

$options = $this->getOptions($ticket);

foreach ($emails as $email) {
if ($this->fact->getAction() == EventTriggeredListener::UPDATED && !array_key_exists('changes', $options)) {
continue;
Expand All @@ -130,12 +133,21 @@ public function notify()
}

/**
* @param Ticket|null $ticket
* @return array
*/
protected function getOptions()
protected function getOptions(Ticket $ticket = null)
{
$target = $this->fact->getTarget();
$options = ['ticketKey' => $target['ticket']->getKey()];

// get actual key if it was moved by previous action
if (is_null($ticket)) {
$key = $target['ticket']->getKey();
} else {
$key = $ticket->getKey();
}

$options = ['ticketKey' => $key];

$changesetDiff = $this->changeset->getDiff();
if (!empty($changesetDiff)) {
Expand Down
71 changes: 44 additions & 27 deletions src/Diamante/DeskBundle/Automation/Action/Email/TicketNotifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Diamante\AutomationBundle\Automation\Action\Email\EntityNotifier;
use Diamante\AutomationBundle\EventListener\EventTriggeredListener;
use Diamante\DeskBundle\Api\Internal\AttachmentServiceImpl;
use Diamante\DeskBundle\Entity\Ticket;
use Diamante\DeskBundle\Model\Ticket\Priority;
use Diamante\DeskBundle\Model\Ticket\Status;
use Diamante\DeskBundle\Model\Ticket\TicketKey;
Expand Down Expand Up @@ -70,10 +71,18 @@ protected function getProvider()
return sprintf($pattern, $targetType, $action);
}

protected function getOptions()
protected function getOptions(Ticket $ticket = null)
{
$target = $this->fact->getTarget();
$options = ['ticketKey' => new TicketKey($target['branch']->getKey(), $target['sequenceNumber']->getValue())];

// get actual key if it was moved by previous action
if (is_null($ticket)) {
$key = new TicketKey($target['branch']->getKey(), $target['sequenceNumber']->getValue());
} else {
$key = $ticket->getKey();
}

$options = ['ticketKey' => $key];

$changesetDiff = $this->changeset->getDiff();
if (!empty($changesetDiff)) {
Expand All @@ -100,42 +109,50 @@ protected function getOptions()
return $options;
}

/**
* @return bool
*/
public function notify()
{
if (!$this->isChanged()) {
return false;
}

$target = $this->fact->getTarget();
$ticket = null;
$ticketId = $target['id'];

if (!is_null($ticketId)) {
$ticket = $this->container->get('diamante.ticket.repository')->get($ticketId);
$this->notificationManager->setTicket($ticket);
}

$emails = $this->getEmailList();
$provider = $this->getProvider();
$options = $this->getOptions();
$target = $this->fact->getTarget();
$options = $this->getOptions($ticket);
$editor = $this->fact->getEditor();
$editor = $this->container->get('diamante.user.service')->getByUser($editor);
$editorName = $this->userService->getFullName($editor);

if ($this->isChanged()) {
$ticketId = $target['id'];

if (!is_null($ticketId)) {
$ticket = $this->container->get('diamante.ticket.repository')->get($ticketId);
$this->notificationManager->setTicket($ticket);
foreach ($emails as $email) {
$recipient = $this->container->get('diamante.user.service')->getUserInstanceByEmail($email);
$options = array_merge(
$options,
[
'recipient' => $recipient,
'editor' => $editorName,
'target' => $target
]
);

if ($recipient instanceof DiamanteUser) {
$options = $this->filterDiamanteUserOptions($options);
}

foreach ($emails as $email) {
$recipient = $this->container->get('diamante.user.service')->getUserInstanceByEmail($email);
$options = array_merge(
$options,
[
'recipient' => $recipient,
'editor' => $editorName,
'target' => $target
]
);

if ($recipient instanceof DiamanteUser) {
$options = $this->filterDiamanteUserOptions($options);
}

$this->notificationManager->notifyByScenario($provider, $recipient, $options);
}
$this->notificationManager->notifyByScenario($provider, $recipient, $options);
}

return true;
}

/**
Expand Down

0 comments on commit cf09d0a

Please sign in to comment.