Create ticket endpoint now working, but needs some changes

This commit is contained in:
Mike Koch 2017-02-14 12:45:02 -05:00
parent 1cb4209be2
commit 1b6cca8ddf
6 changed files with 21 additions and 18 deletions

View File

@ -5,19 +5,17 @@ namespace BusinessLogic\Exceptions;
use BusinessLogic\ValidationModel; use BusinessLogic\ValidationModel;
use Exception; use Exception;
class ValidationException extends Exception { class ValidationException extends ApiFriendlyException {
public $validationModel;
/** /**
* ValidationException constructor. * ValidationException constructor.
* @param ValidationModel $validationModel The validation model * @param ValidationModel $validationModel The validation model
* @throws Exception If the validationModel's errorKeys is empty * @throws Exception If the validationModel's errorKeys is empty
*/ */
function __construct($validationModel) { function __construct($validationModel) {
if (count($validationModel->errorKeys) === 0) { if (count($validationModel->errorKeys) === 0 || $validationModel->valid) {
throw new Exception('Tried to throw a ValidationException, but the validation model was valid!'); throw new Exception('Tried to throw a ValidationException, but the validation model was valid or had 0 error keys!');
} }
$this->validationModel = $validationModel; parent::__construct(implode(",", $validationModel->errorKeys), "Validation Failed. Error keys are available in the message section.", 400);
} }
} }

View File

@ -238,8 +238,11 @@ class Ticket {
function getAttachmentsForDatabase() { function getAttachmentsForDatabase() {
$attachmentArray = array(); $attachmentArray = array();
foreach ($this->attachments as $attachment) {
$attachmentArray[] = $attachment->id . '#' . $attachment->fileName . '#' . $attachment->savedName; if ($this->attachments !== null) {
foreach ($this->attachments as $attachment) {
$attachmentArray[] = $attachment->id . '#' . $attachment->fileName . '#' . $attachment->savedName;
}
} }
return implode(',', $attachmentArray); return implode(',', $attachmentArray);

View File

@ -17,6 +17,6 @@ class JsonRetriever {
*/ */
static function getJsonData() { static function getJsonData() {
$json = file_get_contents('php://input'); $json = file_get_contents('php://input');
return json_decode($json); return json_decode($json, true);
} }
} }

View File

@ -27,7 +27,9 @@ class TicketController {
$jsonRequest = JsonRetriever::getJsonData(); $jsonRequest = JsonRetriever::getJsonData();
$ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $modsForHeskSettings, $userContext); $ticket = $ticketCreator->createTicketByCustomer($this->buildTicketRequestFromJson($jsonRequest), $hesk_settings, $modsForHeskSettings, $userContext);
return output($ticket);
} }
/** /**

View File

@ -124,10 +124,10 @@ class TicketGateway extends CommonDao {
$userAgent = $ticket->userAgent !== null ? $ticket->userAgent : ''; $userAgent = $ticket->userAgent !== null ? $ticket->userAgent : '';
$screenResolutionWidth = $ticket->screenResolution !== null $screenResolutionWidth = $ticket->screenResolution !== null
&& isset($ticket->screenResolution[0]) && isset($ticket->screenResolution[0])
&& $ticket->screenResolution[0] !== null ? intval($ticket->screenResolution[0]) : ''; && $ticket->screenResolution[0] !== null ? intval($ticket->screenResolution[0]) : 'NULL';
$screenResolutionHeight = $ticket->screenResolution !== null $screenResolutionHeight = $ticket->screenResolution !== null
&& isset($ticket->screenResolution[1]) && isset($ticket->screenResolution[1])
&& $ticket->screenResolution[1] !== null ? intval($ticket->screenResolution[1]) : ''; && $ticket->screenResolution[1] !== null ? intval($ticket->screenResolution[1]) : 'NULL';
$sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets` $sql = "INSERT INTO `" . hesk_dbEscape($heskSettings['db_pfix']) . "tickets`
( (
@ -169,14 +169,14 @@ class TicketGateway extends CommonDao {
'" . hesk_dbEscape($ticket->message) . "', '" . hesk_dbEscape($ticket->message) . "',
NOW(), NOW(),
NOW(), NOW(),
" . $suggestedArticles . ", '" . $suggestedArticles . "',
'" . hesk_dbEscape($ticket->ipAddress) . "', '" . hesk_dbEscape($ticket->ipAddress) . "',
'" . hesk_dbEscape($language) . "', '" . hesk_dbEscape($language) . "',
'" . intval($ticket->openedBy) . "', '" . intval($ticket->openedBy) . "',
'" . intval($ticket->ownerId) . "', '" . intval($ticket->ownerId) . "',
'" . hesk_dbEscape($ticket->getAttachmentsForDatabase()) . "', '" . hesk_dbEscape($ticket->getAttachmentsForDatabase()) . "',
'', '',
'" . intval($ticket->statusId) . "', " . intval($ticket->statusId) . ",
'" . hesk_dbEscape($latitude) . "', '" . hesk_dbEscape($latitude) . "',
'" . hesk_dbEscape($longitude) . "', '" . hesk_dbEscape($longitude) . "',
'" . hesk_dbEscape($ticket->usesHtml) . "', '" . hesk_dbEscape($ticket->usesHtml) . "',
@ -192,7 +192,7 @@ class TicketGateway extends CommonDao {
hesk_dbQuery($sql); hesk_dbQuery($sql);
$rs = hesk_dbQuery('SELECT `dt`, `lastchange` FROM `' . hesk_dbEscape($heskSettings['db_pfix']) . 'tickets` WHERE `id` = ' . intval(hesk_dbInsertID())); $rs = hesk_dbQuery('SELECT `dt`, `lastchange` FROM `' . hesk_dbEscape($heskSettings['db_pfix']) . 'tickets` WHERE `id` = ' . intval(hesk_dbInsertID()));
$row = hesk_dbFetchRow($rs); $row = hesk_dbFetchAssoc($rs);
$generatedFields = new TicketGatewayGeneratedFields(); $generatedFields = new TicketGatewayGeneratedFields();
$generatedFields->dateCreated = $row['dt']; $generatedFields->dateCreated = $row['dt'];

View File

@ -41,13 +41,13 @@ function errorHandler($errorNumber, $errorMessage, $errorFile, $errorLine) {
* @param $exception Exception * @param $exception Exception
*/ */
function exceptionHandler($exception) { function exceptionHandler($exception) {
if (exceptionIsOfType($exception, 'ApiFriendlyException')) { if (exceptionIsOfType($exception, \BusinessLogic\Exceptions\ApiFriendlyException::class)) {
/* @var $castedException \BusinessLogic\Exceptions\ApiFriendlyException */ /* @var $castedException \BusinessLogic\Exceptions\ApiFriendlyException */
$castedException = $exception; $castedException = $exception;
print_error($castedException->title, $castedException->getMessage(), $castedException->httpResponseCode); print_error($castedException->title, $castedException->getMessage(), $castedException->httpResponseCode);
} else { } else {
if (exceptionIsOfType($exception, 'SQLException')) { if (exceptionIsOfType($exception, \Core\Exceptions\SQLException::class)) {
/* @var $castedException \Core\Exceptions\SQLException */ /* @var $castedException \Core\Exceptions\SQLException */
$castedException = $exception; $castedException = $exception;
print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString())); print_error("Fought an uncaught exception", sprintf("%s\n\n%s", $castedException->failingQuery, $exception->getTraceAsString()));
@ -66,7 +66,7 @@ function exceptionHandler($exception) {
* @return bool * @return bool
*/ */
function exceptionIsOfType($exception, $class) { function exceptionIsOfType($exception, $class) {
return strpos(get_class($exception), $class) !== false; return is_a($exception, $class);
} }
function fatalErrorShutdownHandler() { function fatalErrorShutdownHandler() {