opensupports/server/controllers/staff/assign-ticket.php

105 lines
3.0 KiB
PHP
Raw Normal View History

<?php
use Respect\Validation\Validator as DataValidator;
DataValidator::with('CustomValidations', true);
2017-04-18 06:39:55 +02:00
/**
* @api {post} /staff/assign-ticket Assign ticket
2018-09-20 22:19:47 +02:00
* @apiVersion 4.3.0
2017-04-18 06:39:55 +02:00
*
* @apiName Assign ticket
*
* @apiGroup Staff
2017-04-18 06:39:55 +02:00
*
* @apiDescription This path assigns a ticket to a staff member.
*
* @apiPermission staff1
2017-04-18 06:39:55 +02:00
*
2017-04-21 08:09:24 +02:00
* @apiParam {Number} ticketNumber The number of the ticket to assign.
* @apiParam {Number} staffId The id of the staff.
2017-04-21 08:09:24 +02:00
*
* @apiUse NO_PERMISSION
* @apiUse INVALID_TICKET
* @apiUse TICKET_ALREADY_ASSIGNED
* @apiUse INVALID_DEPARTMENT
2018-02-28 06:43:34 +01:00
*
2017-04-21 08:09:24 +02:00
* @apiSuccess {Object} data Empty object
2017-04-18 06:39:55 +02:00
*
*/
class AssignStaffController extends Controller {
const PATH = '/assign-ticket';
const METHOD = 'POST';
private $ticket;
private $staffToAssign;
public function validations() {
return [
'permission' => 'staff_1',
'requestData' => [
'ticketNumber' => [
'validation' => DataValidator::validTicketNumber(),
'error' => ERRORS::INVALID_TICKET
]
]
];
}
public function handler() {
$ticketNumber = Controller::request('ticketNumber');
$staffId = Controller::request('staffId');
$this->ticket = Ticket::getByTicketNumber($ticketNumber);
if($staffId) {
$this->staffToAssign = Staff::getDataStore($staffId, 'id');
if($this->staffToAssign->isNull()) {
throw new Exception(ERRORS::INVALID_STAFF);
}
if(!$this->staffToAssign->sharedDepartmentList->includesId($this->ticket->department->id)) {
throw new Exception(ERRORS::INVALID_DEPARTMENT);
}
} else {
$this->staffToAssign = Controller::getLoggedUser();
}
if($this->ticket->owner) {
2018-03-09 19:17:28 +01:00
throw new Exception(ERRORS::TICKET_ALREADY_ASSIGNED);
}
if(!$this->ticketHasStaffDepartment()) {
2018-03-09 19:17:28 +01:00
throw new Exception(ERRORS::INVALID_DEPARTMENT);
} else {
$this->staffToAssign->sharedTicketList->add($this->ticket);
$this->ticket->owner = $this->staffToAssign;
$this->ticket->unread = !$this->ticket->isAuthor($this->staffToAssign);
$event = Ticketevent::getEvent(Ticketevent::ASSIGN);
$event->setProperties(array(
'authorStaff' => Controller::getLoggedUser(),
'date' => Date::getCurrentDate(),
'content' => $this->staffToAssign->name,
));
$this->ticket->addEvent($event);
$this->ticket->store();
$this->staffToAssign->store();
Response::respondSuccess();
}
}
public function ticketHasStaffDepartment() {
$departmentMatch = false;
foreach ($this->staffToAssign->sharedDepartmentList as $department) {
if($this->ticket->department->id === $department->id) {
$departmentMatch = true;
}
}
return $departmentMatch;
}
}