From 54cb77b136a051d80078b24e6fc50fc2842c1d60 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Mon, 24 Apr 2017 13:07:11 -0400 Subject: [PATCH] Getting started on retrieving ticket customer-side --- api/BusinessLogic/Tickets/Ticket.php | 25 ++++--- api/BusinessLogic/Tickets/TicketRetriever.php | 30 ++++++++ .../Tickets/TicketRetrieverTest.php | 73 +++++++++++++++++++ 3 files changed, 116 insertions(+), 12 deletions(-) create mode 100644 api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php diff --git a/api/BusinessLogic/Tickets/Ticket.php b/api/BusinessLogic/Tickets/Ticket.php index f2e7a59a..2f8b0e28 100644 --- a/api/BusinessLogic/Tickets/Ticket.php +++ b/api/BusinessLogic/Tickets/Ticket.php @@ -9,7 +9,10 @@ class Ticket { $ticket->id = intval($row['id']); $ticket->trackingId = $row['trackid']; $ticket->name = $row['name']; - $ticket->email = $row['email']; + if ($row['email'] !== null) { + $emails = str_replace(';', ',', $row['email']); + $ticket->email = explode(',', strtolower($emails)); + } $ticket->categoryId = intval($row['category']); $ticket->priorityId = intval($row['priority']); $ticket->subject = $row['subject']; @@ -37,7 +40,6 @@ class Ticket { $ticket->lastReplier = $row['replierid'] === null ? null : intval($row['replierid']); $ticket->archived = intval($row['archive']) === 1; $ticket->locked = intval($row['locked']) === 1; - $ticket->attachments = array(); if (trim($row['attachments']) !== '') { $attachments = explode(',', $row['attachments']); @@ -72,7 +74,6 @@ class Ticket { } } - $ticket->linkedTicketIds = array(); while ($linkedTicketsRow = hesk_dbFetchAssoc($linkedTicketsRs)) { $ticket->linkedTicketIds[] = $linkedTicketsRow['id']; } @@ -155,7 +156,7 @@ class Ticket { public $name; /** - * @var string|null + * @var array|null */ public $email; @@ -200,9 +201,9 @@ class Ticket { public $closedDate; /** - * @var string[]|null + * @var string[] */ - public $suggestedArticles; + public $suggestedArticles = array(); /** * @var string @@ -275,9 +276,9 @@ class Ticket { public $locked; /** - * @var Attachment[]|null + * @var Attachment[] */ - public $attachments; + public $attachments = array(); function getAttachmentsForDatabase() { $attachmentArray = array(); @@ -292,9 +293,9 @@ class Ticket { } /** - * @var int[]|null + * @var int[] */ - public $mergedTicketIds; + public $mergedTicketIds = array(); /** * @var string @@ -309,7 +310,7 @@ class Ticket { /** * @var int[] */ - public $linkedTicketIds; + public $linkedTicketIds = array(); /** * @var float[]|null @@ -347,5 +348,5 @@ class Ticket { /** * @var Reply[] */ - public $replies; + public $replies = array(); } \ No newline at end of file diff --git a/api/BusinessLogic/Tickets/TicketRetriever.php b/api/BusinessLogic/Tickets/TicketRetriever.php index a9f4f8ec..d19ded3b 100644 --- a/api/BusinessLogic/Tickets/TicketRetriever.php +++ b/api/BusinessLogic/Tickets/TicketRetriever.php @@ -3,6 +3,8 @@ namespace BusinessLogic\Tickets; +use BusinessLogic\Exceptions\ValidationException; +use BusinessLogic\ValidationModel; use DataAccess\Tickets\TicketGateway; class TicketRetriever { @@ -18,4 +20,32 @@ class TicketRetriever { function getTicketById($id, $heskSettings, $userContext) { return $this->ticketGateway->getTicketById($id, $heskSettings); } + + function getTicketByTrackingIdAndEmail($trackingId, $emailAddress, $heskSettings) { + $this->validate($trackingId, $emailAddress, $heskSettings); + + $ticket = $this->ticketGateway->getTicketByTrackingId($trackingId, $heskSettings); + + if ($heskSettings['email_view_ticket'] && !in_array($emailAddress, $ticket->email)) { + throw new \Exception("Email '{$emailAddress}' entered in for ticket '{$trackingId}' does not match!"); + } + + return $ticket; + } + + private function validate($trackingId, $emailAddress, $heskSettings) { + $validationModel = new ValidationModel(); + + if ($trackingId === null || trim($trackingId) === '') { + $validationModel->errorKeys[] = 'MISSING_TRACKING_ID'; + } + + if ($heskSettings['email_view_ticket'] && ($emailAddress === null || trim($emailAddress) === '')) { + $validationModel->errorKeys[] = 'EMAIL_REQUIRED_AND_MISSING'; + } + + if (count($validationModel->errorKeys) > 0) { + throw new ValidationException($validationModel); + } + } } \ No newline at end of file diff --git a/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php b/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php new file mode 100644 index 00000000..c5f0b25b --- /dev/null +++ b/api/Tests/BusinessLogic/Tickets/TicketRetrieverTest.php @@ -0,0 +1,73 @@ +ticketGateway = $this->createMock(TicketGateway::class); + $this->heskSettings = array('email_view_ticket' => 0); + + $this->ticketRetriever = new TicketRetriever($this->ticketGateway); + } + + function testItGetsTheTicketByTrackingId() { + //-- Arrange + $ticket = new Ticket(); + $trackingId = '12345'; + $this->ticketGateway->method('getTicketByTrackingId')->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(); + $email = 'email@example.com'; + $ticket->email = array('email2@example.com;email3@example.com,email4@example.com'); + $trackingId = '12345'; + $this->heskSettings['email_view_ticket'] = 1; + $this->ticketGateway->method('getTicketByTrackingId')->with($trackingId, $this->heskSettings)->willReturn($ticket); + + //-- Assert + $this->expectException(\Exception::class); + $this->expectExceptionMessage("Email 'email@example.com' entered in for ticket '12345' does not match!"); + + //-- Act + $this->ticketRetriever->getTicketByTrackingIdAndEmail($trackingId, $email, $this->heskSettings); + } + + function testItCanHandleTicketsWithMultipleEmails() { + //-- Arrange + $ticket = new Ticket(); + $email = 'email2@example.com'; + $ticket->email = array('email2@example.com','email3@example.com','email4@example.com'); + $trackingId = '12345'; + $this->heskSettings['email_view_ticket'] = 1; + $this->ticketGateway->method('getTicketByTrackingId')->with($trackingId, $this->heskSettings)->willReturn($ticket); + + //-- Act + $actual = $this->ticketRetriever->getTicketByTrackingIdAndEmail($trackingId, $email, $this->heskSettings); + + //-- Assert + self::assertThat($actual, self::equalTo($ticket)); + } + + //-- TODO Validation tests +}