From 2ff27e197bdff9f3c35e5f465457c7139946f422 Mon Sep 17 00:00:00 2001 From: Mike Koch Date: Sun, 23 Apr 2017 12:00:05 -0400 Subject: [PATCH] Add ability to delete ticket --- .../Attachments/AttachmentHandler.php | 1 + api/BusinessLogic/Tickets/TicketDeleter.php | 6 ++- api/DataAccess/Tickets/TicketGateway.php | 30 ++++++++++-- .../Attachments/AttachmentHandlerTest.php | 7 ++- .../Tickets/TicketDeleterTest.php | 46 ++++++++++++++++++- 5 files changed, 82 insertions(+), 8 deletions(-) diff --git a/api/BusinessLogic/Attachments/AttachmentHandler.php b/api/BusinessLogic/Attachments/AttachmentHandler.php index 333a5ec8..d51ce710 100644 --- a/api/BusinessLogic/Attachments/AttachmentHandler.php +++ b/api/BusinessLogic/Attachments/AttachmentHandler.php @@ -137,6 +137,7 @@ class AttachmentHandler { } else { $attachments = $ticket->replies[$replyId]->attachments; unset($attachments[$indexToRemove]); + $this->ticketGateway->updateAttachmentsForReply($replyId, $attachments, $heskSettings); } } diff --git a/api/BusinessLogic/Tickets/TicketDeleter.php b/api/BusinessLogic/Tickets/TicketDeleter.php index 6b181e4e..826a4731 100644 --- a/api/BusinessLogic/Tickets/TicketDeleter.php +++ b/api/BusinessLogic/Tickets/TicketDeleter.php @@ -36,7 +36,11 @@ class TicketDeleter { $this->attachmentHandler->deleteAttachmentFromTicket($ticketId, $attachment->id, $userContext, $heskSettings); } - //-- TODO Delete Replies + $this->ticketGateway->deleteReplyDraftsForTicket($ticketId, $heskSettings); + + $this->ticketGateway->deleteRepliesForTicket($ticketId, $heskSettings); + + $this->ticketGateway->deleteNotesForTicket($ticketId, $heskSettings); $this->ticketGateway->deleteTicket($ticketId, $heskSettings); } diff --git a/api/DataAccess/Tickets/TicketGateway.php b/api/DataAccess/Tickets/TicketGateway.php index 8ce266d8..a326a537 100644 --- a/api/DataAccess/Tickets/TicketGateway.php +++ b/api/DataAccess/Tickets/TicketGateway.php @@ -56,7 +56,7 @@ class TicketGateway extends CommonDao { while ($row = hesk_dbFetchAssoc($rs)) { $linkedTicketsRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `parent` = " . intval($row['id'])); - $repliesRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "replies` WHERE `replyto` = " . intval($id) . " ORDER BY `id` ASC"); + $repliesRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "replies` WHERE `replyto` = " . intval($row['id']) . " ORDER BY `id` ASC"); $tickets[] = Ticket::fromDatabaseRow($row, $linkedTicketsRs, $repliesRs, $heskSettings); } @@ -74,14 +74,14 @@ class TicketGateway extends CommonDao { function getTicketByTrackingId($trackingId, $heskSettings) { $this->init(); - $rs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `id` = " . intval($trackingId)); + $rs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `trackid` = " . intval($trackingId)); if (hesk_dbNumRows($rs) === 0) { return null; } $row = hesk_dbFetchAssoc($rs); $linkedTicketsRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` WHERE `parent` = " . intval($trackingId)); - $repliesRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "replies` WHERE `replyto` = " . intval($id) . " ORDER BY `id` ASC"); + $repliesRs = hesk_dbQuery("SELECT * FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "replies` WHERE `replyto` = " . intval($row['id']) . " ORDER BY `id` ASC"); $ticket = Ticket::fromDatabaseRow($row, $linkedTicketsRs, $repliesRs, $heskSettings); @@ -250,6 +250,30 @@ class TicketGateway extends CommonDao { $this->close(); } + function deleteRepliesForTicket($ticketId, $heskSettings) { + $this->init(); + + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "replies` WHERE `replyto` = " . intval($ticketId)); + + $this->close(); + } + + function deleteReplyDraftsForTicket($ticketId, $heskSettings) { + $this->init(); + + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "reply_drafts` WHERE `ticket`=" . intval($ticketId)); + + $this->close(); + } + + function deleteNotesForTicket($ticketId, $heskSettings) { + $this->init(); + + hesk_dbQuery("DELETE FROM `" . hesk_dbEscape($heskSettings['db_pfix']) . "notes` WHERE `ticket`='" . intval($ticketId) . "'"); + + $this->close(); + } + /** * @param $ticketId int * @param $heskSettings array diff --git a/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php b/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php index fb60f537..7ee2eb84 100644 --- a/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php +++ b/api/Tests/BusinessLogic/Attachments/AttachmentHandlerTest.php @@ -275,6 +275,7 @@ class AttachmentHandlerTest extends TestCase { $attachment->savedName = 'foobar.txt'; $this->heskSettings['attach_dir'] = 'attach-dir'; $ticket->attachments = array($attachment); + $ticket->replies = array(); $this->ticketGateway->method('getTicketById')->willReturn($ticket); $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true); @@ -289,6 +290,7 @@ class AttachmentHandlerTest extends TestCase { //-- Arrange $ticketId = 1; $ticket = new Ticket(); + $ticket->replies = array(); $attachment = new Attachment(); $attachment->id = 5; $attachment->savedName = 'foobar.txt'; @@ -309,17 +311,18 @@ class AttachmentHandlerTest extends TestCase { $ticketId = 1; $ticket = new Ticket(); $reply = new Reply(); + $reply->id = 10; $attachment = new Attachment(); $attachment->id = 5; $attachment->savedName = 'foobar.txt'; $this->heskSettings['attach_dir'] = 'attach-dir'; $reply->attachments = array($attachment); - $ticket->replies = array($reply); + $ticket->replies = array(10 => $reply); $this->ticketGateway->method('getTicketById')->willReturn($ticket); $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true); //-- Assert - $this->ticketGateway->expects($this->once())->method('updateAttachmentsForTicket'); + $this->ticketGateway->expects($this->once())->method('updateAttachmentsForReply'); //-- Act $this->attachmentHandler->deleteAttachmentFromTicket($ticketId, 5, $this->userContext, $this->heskSettings); diff --git a/api/Tests/BusinessLogic/Tickets/TicketDeleterTest.php b/api/Tests/BusinessLogic/Tickets/TicketDeleterTest.php index 60b5c164..4ac994a8 100644 --- a/api/Tests/BusinessLogic/Tickets/TicketDeleterTest.php +++ b/api/Tests/BusinessLogic/Tickets/TicketDeleterTest.php @@ -58,6 +58,7 @@ class TicketDeleterTest extends TestCase { $attachmentTwo->id = 2; $attachments = array($attachmentOne, $attachmentTwo); $ticket->attachments = $attachments; + $ticket->replies = array(); $this->ticketGateway->method('getTicketById')->willReturn($ticket); $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true); @@ -70,16 +71,57 @@ class TicketDeleterTest extends TestCase { function testItDeletesAllRepliesForTheTicket() { //-- Arrange - - //-- Act + $ticket = new Ticket(); + $ticket->attachments = array(); + $ticket->replies = array(); + $ticket->id = 1; + $this->ticketGateway->method('getTicketById')->willReturn($ticket); + $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true); //-- Assert + $this->ticketGateway->expects($this->once())->method('deleteRepliesForTicket')->with(1, $this->heskSettings); + + //-- Act + $this->ticketDeleter->deleteTicket(1, $this->userContext, $this->heskSettings); + } + + function testItDeleteAllReplyDrafts() { + //-- Arrange + $ticket = new Ticket(); + $ticket->attachments = array(); + $ticket->replies = array(); + $ticket->id = 1; + $this->ticketGateway->method('getTicketById')->willReturn($ticket); + $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true); + + //-- Assert + $this->ticketGateway->expects($this->once())->method('deleteReplyDraftsForTicket')->with(1, $this->heskSettings); + + //-- Act + $this->ticketDeleter->deleteTicket(1, $this->userContext, $this->heskSettings); + } + + function testItDeletesTheTicketNotes() { + //-- Arrange + $ticket = new Ticket(); + $ticket->attachments = array(); + $ticket->replies = array(); + $ticket->id = 1; + $this->ticketGateway->method('getTicketById')->willReturn($ticket); + $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true); + + //-- Assert + $this->ticketGateway->expects($this->once())->method('deleteNotesForTicket')->with(1, $this->heskSettings); + + //-- Act + $this->ticketDeleter->deleteTicket(1, $this->userContext, $this->heskSettings); } function testItDeletesTheTicket() { //-- Arrange $ticket = new Ticket(); $ticket->attachments = array(); + $ticket->replies = array(); $ticket->id = 1; $this->ticketGateway->method('getTicketById')->willReturn($ticket); $this->userToTicketChecker->method('isTicketAccessibleToUser')->willReturn(true);