diff --git a/api/BusinessLogic/Attachments/AttachmentHandler.php b/api/BusinessLogic/Attachments/AttachmentHandler.php index 862549e9..22399f35 100644 --- a/api/BusinessLogic/Attachments/AttachmentHandler.php +++ b/api/BusinessLogic/Attachments/AttachmentHandler.php @@ -188,7 +188,6 @@ class AttachmentHandler { if (count($errorKeys) > 0) { $validationModel = new ValidationModel(); $validationModel->errorKeys = $errorKeys; - $validationModel->valid = false; throw new ValidationException($validationModel); } } diff --git a/api/BusinessLogic/Exceptions/ValidationException.php b/api/BusinessLogic/Exceptions/ValidationException.php index 6ed8b552..0049f232 100644 --- a/api/BusinessLogic/Exceptions/ValidationException.php +++ b/api/BusinessLogic/Exceptions/ValidationException.php @@ -12,7 +12,7 @@ class ValidationException extends ApiFriendlyException { * @throws Exception If the validationModel's errorKeys is empty */ function __construct($validationModel) { - if (count($validationModel->errorKeys) === 0 || $validationModel->valid) { + if (count($validationModel->errorKeys) === 0) { throw new Exception('Tried to throw a ValidationException, but the validation model was valid or had 0 error keys!'); } diff --git a/api/BusinessLogic/Tickets/Ticket.php b/api/BusinessLogic/Tickets/Ticket.php index 2f8b0e28..73ebf8b4 100644 --- a/api/BusinessLogic/Tickets/Ticket.php +++ b/api/BusinessLogic/Tickets/Ticket.php @@ -11,7 +11,8 @@ class Ticket { $ticket->name = $row['name']; if ($row['email'] !== null) { $emails = str_replace(';', ',', $row['email']); - $ticket->email = explode(',', strtolower($emails)); + $emails = explode(',', strtolower($emails)); + $ticket->email = array_filter($emails); } $ticket->categoryId = intval($row['category']); $ticket->priorityId = intval($row['priority']); diff --git a/api/BusinessLogic/Tickets/TicketCreator.php b/api/BusinessLogic/Tickets/TicketCreator.php index 7e160936..2aed0579 100644 --- a/api/BusinessLogic/Tickets/TicketCreator.php +++ b/api/BusinessLogic/Tickets/TicketCreator.php @@ -86,7 +86,6 @@ class TicketCreator { if (count($validationModel->errorKeys) > 0) { // Validation failed - $validationModel->valid = false; throw new ValidationException($validationModel); } diff --git a/api/BusinessLogic/Tickets/TicketRetriever.php b/api/BusinessLogic/Tickets/TicketRetriever.php index d19ded3b..438af152 100644 --- a/api/BusinessLogic/Tickets/TicketRetriever.php +++ b/api/BusinessLogic/Tickets/TicketRetriever.php @@ -3,6 +3,7 @@ namespace BusinessLogic\Tickets; +use BusinessLogic\Exceptions\ApiFriendlyException; use BusinessLogic\Exceptions\ValidationException; use BusinessLogic\ValidationModel; use DataAccess\Tickets\TicketGateway; @@ -25,9 +26,17 @@ class TicketRetriever { $this->validate($trackingId, $emailAddress, $heskSettings); $ticket = $this->ticketGateway->getTicketByTrackingId($trackingId, $heskSettings); + if ($ticket === null) { + $ticket = $this->ticketGateway->getTicketByMergedTrackingId($trackingId, $heskSettings); + + if ($ticket === null) { + return null; + } + } if ($heskSettings['email_view_ticket'] && !in_array($emailAddress, $ticket->email)) { - throw new \Exception("Email '{$emailAddress}' entered in for ticket '{$trackingId}' does not match!"); + throw new ApiFriendlyException("Email '{$emailAddress}' entered in for ticket '{$trackingId}' does not match!", + "Email Does Not Match", 400); } return $ticket; diff --git a/api/BusinessLogic/ValidationModel.php b/api/BusinessLogic/ValidationModel.php index 740af78e..b3e085cd 100644 --- a/api/BusinessLogic/ValidationModel.php +++ b/api/BusinessLogic/ValidationModel.php @@ -8,13 +8,7 @@ class ValidationModel { */ public $errorKeys; - /** - * @var bool - */ - public $valid; - function __construct() { $this->errorKeys = []; - $this->valid = true; } } \ No newline at end of file diff --git a/api/Controllers/Tickets/CustomerTicketController.php b/api/Controllers/Tickets/CustomerTicketController.php index b1f83ed7..a8b05b9b 100644 --- a/api/Controllers/Tickets/CustomerTicketController.php +++ b/api/Controllers/Tickets/CustomerTicketController.php @@ -6,18 +6,22 @@ use BusinessLogic\Helpers; use BusinessLogic\Tickets\CreateTicketByCustomerModel; use BusinessLogic\Tickets\TicketCreator; use BusinessLogic\Tickets\TicketRetriever; +use BusinessLogic\ValidationModel; use Controllers\JsonRetriever; class CustomerTicketController { - /*function get($id) { - global $applicationContext, $hesk_settings, $userContext; + function get() { + global $applicationContext, $hesk_settings; + + $trackingId = isset($_GET['trackingId']) ? $_GET['trackingId'] : null; + $emailAddress = isset($_GET['email']) ? $_GET['email'] : null; /* @var $ticketRetriever TicketRetriever */ - /*$ticketRetriever = $applicationContext->get[TicketRetriever::class]; + $ticketRetriever = $applicationContext->get[TicketRetriever::class]; - output($ticketRetriever->getTicketById($id, $hesk_settings, $userContext)); - }*/ + output($ticketRetriever->getTicketByTrackingIdAndEmail($trackingId, $emailAddress, $hesk_settings)); + } function post() { global $applicationContext, $hesk_settings, $userContext; @@ -29,17 +33,9 @@ class CustomerTicketController { $ticket = $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $userContext); - //if ticket is a stageTicket, email user - //else if assigned to owner, email new owner - //else email all staff - return output($ticket, 201); } - function delete($id) { - global $applicationContext, $hesk_settings, $userContext; - } - /** * @param $json array * @return CreateTicketByCustomerModel diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index b6f4bd99..7a940bc8 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -90,6 +90,26 @@ class TicketGateway extends CommonDao { return $ticket; } + /** + * @param $trackingId string + * @param $heskSettings array + * @return Ticket|null + */ + function getTicketByMergedTrackingId($trackingId, $heskSettings) { + $this->init(); + + $rs = hesk_dbQuery("SELECT `trackid` FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `merged` LIKE '%#" . hesk_dbEscape($trackingId) . "#%'"); + if (hesk_dbNumRows($rs) === 0) { + return null; + } + $row = hesk_dbFetchAssoc($rs); + $actualTrackingId = $row['trackid']; + + $this->close(); + + return $this->getTicketByTrackingId($actualTrackingId, $heskSettings); + } + /** * @param $ticket Ticket * @param $isEmailVerified diff --git a/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php b/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php index f7009494..2ace5299 100644 --- a/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php +++ b/api/Tests/BusinessLogic/Tickets/NewTicketValidatorTest.php @@ -147,6 +147,7 @@ class NewTicketValidatorTest extends TestCase { function testItSupportsMultipleEmails() { //-- Arrange $this->ticketRequest->email = 'something@email.com;another@valid.email'; + $this->ticketRequest->language = 'English'; $this->heskSettings['multi_eml'] = true; //-- Act @@ -155,7 +156,7 @@ class NewTicketValidatorTest extends TestCase { $this->userContext); //-- Assert - $this->assertThat($validationModel->valid, $this->isTrue()); + self::assertThat(count($validationModel->errorKeys), self::equalTo(0)); } function testItAddsTheProperValidationErrorWhenCategoryIsNotANumber() { diff --git a/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php b/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php index c5f0b25b..6fd6b168 100644 --- a/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php +++ b/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php @@ -36,6 +36,20 @@ class TicketRetrieverTest extends TestCase { self::assertThat($actual, self::equalTo($ticket)); } + function testItGetsTheParentTicketIfTheUserEntersInAMergedTicketId() { + //-- Arrange + $ticket = new Ticket(); + $trackingId = '12345'; + $this->ticketGateway->method('getTicketByTrackingId')->willReturn(null); + $this->ticketGateway->method('getTicketByMergedTrackingId')->with($trackingId, $this->heskSettings)->willReturn($ticket); + + //-- Act + $actual = $this->ticketRetriever->getTicketByTrackingIdAndEmail($trackingId, null, $this->heskSettings); + + //-- Assert + self::assertThat($actual, self::equalTo($ticket)); + } + function testItChecksTheTicketsEmailIfThePageRequiresIt() { //-- Arrange $ticket = new Ticket();