From 489f191a13deba8ebe5ffb3daef6bbe498f51c50 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 12 Feb 2017 22:10:42 -0500 Subject: [PATCH] Majority of create ticket logic done. Just need to add attachments(?) and retriever SQL-specific fields --- api/BusinessLogic/Tickets/Autoassigner.php | 21 ++++++ .../Tickets/CreateTicketByCustomerModel.php | 9 ++- api/BusinessLogic/Tickets/Ticket.php | 8 +-- api/BusinessLogic/Tickets/TicketCreator.php | 32 +++++++-- api/DataAccess/Tickets/TicketGateway.php | 3 + .../CreateTicketForCustomerTest.php | 66 ++++++++++++++++++- 6 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 api/BusinessLogic/Tickets/Autoassigner.php diff --git a/api/BusinessLogic/Tickets/Autoassigner.php b/api/BusinessLogic/Tickets/Autoassigner.php new file mode 100644 index 00000000..ae9faa80 --- /dev/null +++ b/api/BusinessLogic/Tickets/Autoassigner.php @@ -0,0 +1,21 @@ +newTicketValidator = $newTicketValidator; $this->trackingIdGenerator = $trackingIdGenerator; + $this->autoassigner = $autoassigner; $this->ticketGateway = $ticketGateway; } @@ -50,15 +56,27 @@ class TicketCreator { $ticket = new Ticket(); $ticket->trackingId = $this->trackingIdGenerator->generateTrackingId($heskSettings); - //-- TODO suggested kb articles - - //-- TODO owner/autoassign logic + if ($heskSettings['autoassign']) { + $ticket->ownerId = $this->autoassigner->getNextUserForTicket($ticketRequest->category, $heskSettings); + } - //-- TODO latitude/longitude + // Transform one-to-one properties + $ticket->name = $ticketRequest->name; + $ticket->email = $ticketRequest->email; + $ticket->priorityId = $ticketRequest->priority; + $ticket->categoryId = $ticketRequest->category; + $ticket->subject = $ticketRequest->subject; + $ticket->message = $ticketRequest->message; + $ticket->usesHtml = $ticketRequest->html; + $ticket->customFields = $ticketRequest->customFields; + $ticket->location = $ticketRequest->location; + $ticket->suggestedArticles = $ticketRequest->suggestedKnowledgebaseArticleIds; + $ticket->userAgent = $ticketRequest->userAgent; + $ticket->screenResolution = $ticketRequest->screenResolution; - //-- TODO HTML flag + $ticket = $this->ticketGateway->createTicket($ticket, $heskSettings); - $this->ticketGateway->createTicket($ticket, $heskSettings); + //-- TODO get SQL-generated fields return $ticket; } diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index f2101c7a..50c6e7ff 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -86,6 +86,7 @@ class TicketGateway extends CommonDao { /** * @param $ticket Ticket * @param $heskSettings + * @return Ticket */ function createTicket($ticket, $heskSettings) { global $hesklang; @@ -186,5 +187,7 @@ class TicketGateway extends CommonDao { {$customWhat} ) "; + + return $ticket; } } \ No newline at end of file diff --git a/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php b/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php index f46f8faf..8b54b62e 100644 --- a/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php +++ b/api/Tests/BusinessLogic/Tickets/TicketCreatorTests/CreateTicketForCustomerTest.php @@ -10,6 +10,7 @@ namespace BusinessLogic\Tickets\TicketCreatorTests; use BusinessLogic\Security\UserContext; +use BusinessLogic\Tickets\Autoassigner; use BusinessLogic\Tickets\CreateTicketByCustomerModel; use BusinessLogic\Tickets\NewTicketValidator; use BusinessLogic\Tickets\TicketCreator; @@ -36,6 +37,11 @@ class CreateTicketTest extends TestCase { */ private $trackingIdGenerator; + /** + * @var $autoassigner \PHPUnit_Framework_MockObject_MockObject + */ + private $autoassigner; + /** * @var $ticketRequest CreateTicketByCustomerModel */ @@ -65,8 +71,9 @@ class CreateTicketTest extends TestCase { $this->ticketGateway = $this->createMock(TicketGateway::class); $this->newTicketValidator = $this->createMock(NewTicketValidator::class); $this->trackingIdGenerator = $this->createMock(TrackingIdGenerator::class); + $this->autoassigner = $this->createMock(Autoassigner::class); - $this->ticketCreator = new TicketCreator($this->newTicketValidator, $this->trackingIdGenerator, $this->ticketGateway); + $this->ticketCreator = new TicketCreator($this->newTicketValidator, $this->trackingIdGenerator, $this->autoassigner, $this->ticketGateway); $this->ticketRequest = new CreateTicketByCustomerModel(); $this->ticketRequest->name = 'Name'; @@ -82,12 +89,15 @@ class CreateTicketTest extends TestCase { 'require_subject' => 1, 'require_message' => 1, 'custom_fields' => array(), + 'autoassign' => 0, ); $this->modsForHeskSettings = array(); $this->userContext = new UserContext(); $this->newTicketValidator->method('validateNewTicketForCustomer')->willReturn(new ValidationModel()); $this->trackingIdGenerator->method('generateTrackingId')->willReturn('123-456-7890'); + $this->autoassigner->method('getNextUserForTicket')->willReturn(1); + $this->ticketGateway->method('createTicket')->will($this->returnArgument(0)); } function testItSavesTheTicketToTheDatabase() { @@ -105,4 +115,58 @@ class CreateTicketTest extends TestCase { //-- Assert self::assertThat($ticket->trackingId, self::equalTo('123-456-7890')); } + + function testItSetsTheNextUserForAutoassign() { + //-- Arrange + $this->heskSettings['autoassign'] = 1; + + //-- Act + $ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext); + + //-- Assert + self::assertThat($ticket->ownerId, self::equalTo(1)); + } + + function testItDoesntCallTheAutoassignerWhenDisabledInHesk() { + //-- Act + $ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext); + + //-- Assert + self::assertThat($ticket->ownerId, self::equalTo(null)); + } + + function testItTransformsTheBasicProperties() { + //-- Arrange + $this->ticketRequest->name = 'Name'; + $this->ticketRequest->email = 'some@email.test'; + $this->ticketRequest->priority = Priority::MEDIUM; + $this->ticketRequest->category = 1; + $this->ticketRequest->subject = 'Subject'; + $this->ticketRequest->message = 'Message'; + $this->ticketRequest->html = false; + $this->ticketRequest->customFields = array( + 1 => 'something' + ); + $this->ticketRequest->location = ['10.157', '-10.177']; + $this->ticketRequest->suggestedKnowledgebaseArticleIds = [1, 2, 3]; + $this->ticketRequest->userAgent = 'UserAgent'; + $this->ticketRequest->screenResolution = [1400, 900]; + + //-- Act + $ticket = $this->ticketCreator->createTicketByCustomer($this->ticketRequest, $this->heskSettings, $this->modsForHeskSettings, $this->userContext); + + //-- Assert + self::assertThat($ticket->name, self::equalTo($this->ticketRequest->name)); + self::assertThat($ticket->email, self::equalTo($this->ticketRequest->email)); + self::assertThat($ticket->priorityId, self::equalTo($this->ticketRequest->priority)); + self::assertThat($ticket->categoryId, self::equalTo($this->ticketRequest->category)); + self::assertThat($ticket->subject, self::equalTo($this->ticketRequest->subject)); + self::assertThat($ticket->message, self::equalTo($this->ticketRequest->message)); + self::assertThat($ticket->usesHtml, self::equalTo($this->ticketRequest->html)); + self::assertThat($ticket->customFields[1], self::equalTo($this->ticketRequest->customFields[1])); + self::assertThat($ticket->location, self::equalTo($this->ticketRequest->location)); + self::assertThat($ticket->suggestedArticles, self::equalTo($this->ticketRequest->suggestedKnowledgebaseArticleIds)); + self::assertThat($ticket->userAgent, self::equalTo($this->ticketRequest->userAgent)); + self::assertThat($ticket->screenResolution, self::equalTo($this->ticketRequest->screenResolution)); + } }