Make the endpoint only for ticket message attachments, not replies

remotes/upstream/api-rewrite
Mike Koch 7 years ago
parent 0556d07a56
commit 5112a6a13b

@ -47,7 +47,7 @@ class AttachmentHandler {
$cleanedFileName, $fileParts['extension']); $cleanedFileName, $fileParts['extension']);
$ticketAttachment->displayName = $cleanedFileName; $ticketAttachment->displayName = $cleanedFileName;
$ticketAttachment->ticketTrackingId = $ticket->trackingId; $ticketAttachment->ticketTrackingId = $ticket->trackingId;
$ticketAttachment->type = $createAttachmentModel->type; $ticketAttachment->type = 0;
$ticketAttachment->downloadCount = 0; $ticketAttachment->downloadCount = 0;
$ticketAttachment->fileSize = $ticketAttachment->fileSize =
@ -88,10 +88,6 @@ class AttachmentHandler {
$errorKeys[] = 'TICKET_ID_MISSING'; $errorKeys[] = 'TICKET_ID_MISSING';
} }
if (!in_array($createAttachmentModel->type, array(AttachmentType::MESSAGE, AttachmentType::REPLY))) {
$errorKeys[] = 'INVALID_ATTACHMENT_TYPE';
}
$fileParts = pathinfo($createAttachmentModel->displayName); $fileParts = pathinfo($createAttachmentModel->displayName);
if (!isset($fileParts['extension']) || !in_array(".{$fileParts['extension']}", $heskSettings['attachments']['allowed_types'])) { if (!isset($fileParts['extension']) || !in_array(".{$fileParts['extension']}", $heskSettings['attachments']['allowed_types'])) {
$errorKeys[] = 'EXTENSION_NOT_PERMITTED'; $errorKeys[] = 'EXTENSION_NOT_PERMITTED';

@ -6,7 +6,4 @@ namespace BusinessLogic\Attachments;
class CreateAttachmentForTicketModel extends CreateAttachmentModel { class CreateAttachmentForTicketModel extends CreateAttachmentModel {
/* @var $ticketId int */ /* @var $ticketId int */
public $ticketId; public $ticketId;
/* @var $type int [use <code>AttachmentType</code] */
public $type;
} }

@ -10,7 +10,7 @@ use BusinessLogic\Helpers;
use Controllers\JsonRetriever; use Controllers\JsonRetriever;
class StaffTicketAttachmentsController { class StaffTicketAttachmentsController {
function post() { function post($ticketId) {
global $hesk_settings, $applicationContext; global $hesk_settings, $applicationContext;
if (!$hesk_settings['attachments']['use']) { if (!$hesk_settings['attachments']['use']) {
@ -20,19 +20,18 @@ class StaffTicketAttachmentsController {
/* @var $attachmentHandler AttachmentHandler */ /* @var $attachmentHandler AttachmentHandler */
$attachmentHandler = $applicationContext->get[AttachmentHandler::class]; $attachmentHandler = $applicationContext->get[AttachmentHandler::class];
$createAttachmentForTicketModel = $this->createModel(JsonRetriever::getJsonData()); $createAttachmentForTicketModel = $this->createModel(JsonRetriever::getJsonData(), $ticketId);
$createdAttachment = $attachmentHandler->createAttachmentForTicket($createAttachmentForTicketModel, $hesk_settings); $createdAttachment = $attachmentHandler->createAttachmentForTicket($createAttachmentForTicketModel, $hesk_settings);
return output($createdAttachment, 201); return output($createdAttachment, 201);
} }
private function createModel($json) { private function createModel($json, $ticketId) {
$model = new CreateAttachmentForTicketModel(); $model = new CreateAttachmentForTicketModel();
$model->attachmentContents = Helpers::safeArrayGet($json, 'data'); $model->attachmentContents = Helpers::safeArrayGet($json, 'data');
$model->displayName = Helpers::safeArrayGet($json, 'displayName'); $model->displayName = Helpers::safeArrayGet($json, 'displayName');
$model->ticketId = Helpers::safeArrayGet($json, 'ticketId'); $model->ticketId = $ticketId;
$model->type = Helpers::safeArrayGet($json, 'type');
return $model; return $model;
} }

@ -135,18 +135,6 @@ class AttachmentHandlerTest extends TestCase {
$this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel, $this->heskSettings); $this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel, $this->heskSettings);
} }
function testThatValidateThrowsAnExceptionWhenTheAttachmentTypeIsNeitherMessageNorReply() {
//-- Arrange
$this->createAttachmentForTicketModel->type = 5;
//-- Assert
$this->expectException(ValidationException::class);
$this->expectExceptionMessageRegExp('/INVALID_ATTACHMENT_TYPE/');
//-- Act
$this->attachmentHandler->createAttachmentForTicket($this->createAttachmentForTicketModel, $this->heskSettings);
}
function testThatValidateThrowsAnExceptionWhenTheFileExtensionIsNotPermitted() { function testThatValidateThrowsAnExceptionWhenTheFileExtensionIsNotPermitted() {
//-- Arrange //-- Arrange
$this->heskSettings['attachments']['allowed_types'] = array('.gif'); $this->heskSettings['attachments']['allowed_types'] = array('.gif');
@ -183,7 +171,7 @@ class AttachmentHandlerTest extends TestCase {
$ticketAttachment = new TicketAttachment(); $ticketAttachment = new TicketAttachment();
$ticketAttachment->displayName = $this->createAttachmentForTicketModel->displayName; $ticketAttachment->displayName = $this->createAttachmentForTicketModel->displayName;
$ticketAttachment->ticketTrackingId = $ticket->trackingId; $ticketAttachment->ticketTrackingId = $ticket->trackingId;
$ticketAttachment->type = $this->createAttachmentForTicketModel->type; $ticketAttachment->type = 0;
$ticketAttachment->downloadCount = 0; $ticketAttachment->downloadCount = 0;
$ticketAttachment->id = 50; $ticketAttachment->id = 50;
@ -196,7 +184,7 @@ class AttachmentHandlerTest extends TestCase {
//-- Assert //-- Assert
self::assertThat($actual->id, self::equalTo(50)); self::assertThat($actual->id, self::equalTo(50));
self::assertThat($actual->downloadCount, self::equalTo(0)); self::assertThat($actual->downloadCount, self::equalTo(0));
self::assertThat($actual->type, self::equalTo($this->createAttachmentForTicketModel->type)); self::assertThat($actual->type, self::equalTo(AttachmentType::MESSAGE));
self::assertThat($actual->ticketTrackingId, self::equalTo($ticket->trackingId)); self::assertThat($actual->ticketTrackingId, self::equalTo($ticket->trackingId));
self::assertThat($actual->displayName, self::equalTo($this->createAttachmentForTicketModel->displayName)); self::assertThat($actual->displayName, self::equalTo($this->createAttachmentForTicketModel->displayName));
} }
@ -211,7 +199,7 @@ class AttachmentHandlerTest extends TestCase {
$ticketAttachment = new TicketAttachment(); $ticketAttachment = new TicketAttachment();
$ticketAttachment->displayName = $this->createAttachmentForTicketModel->displayName; $ticketAttachment->displayName = $this->createAttachmentForTicketModel->displayName;
$ticketAttachment->ticketTrackingId = $ticket->trackingId; $ticketAttachment->ticketTrackingId = $ticket->trackingId;
$ticketAttachment->type = $this->createAttachmentForTicketModel->type; $ticketAttachment->type = AttachmentType::MESSAGE;
$ticketAttachment->downloadCount = 0; $ticketAttachment->downloadCount = 0;
$ticketAttachment->id = 50; $ticketAttachment->id = 50;

@ -154,7 +154,7 @@ Link::all(array(
'/v1/tickets' => \Controllers\Tickets\TicketController::class, '/v1/tickets' => \Controllers\Tickets\TicketController::class,
// Attachments // Attachments
'/v1/staff/attachments' => \Controllers\Attachments\StaffTicketAttachmentsController::class, '/v1/staff/tickets/{i}/attachments' => \Controllers\Attachments\StaffTicketAttachmentsController::class,
// Any URL that doesn't match goes to the 404 handler // Any URL that doesn't match goes to the 404 handler
'404' => 'handle404' '404' => 'handle404'

Loading…
Cancel
Save