Nextcloud-App/lib/Service/TicketService.php

138 lines
4.4 KiB
PHP

<?php
namespace OCA\UPschooling\Service;
use Aryess\PhpMatrixSdk\Exceptions\MatrixException;
use Aryess\PhpMatrixSdk\Exceptions\MatrixHttpLibException;
use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException;
use Exception;
use OCA\UPschooling\Db\MatrixTicket;
use OCA\UPschooling\Db\MatrixUser;
use OCA\UPschooling\Db\TicketMapper;
use OCA\UPschooling\Db\UserMapper;
use OCA\UPschooling\Exceptions\RoomNotFoundException;
use OCA\UPschooling\Exceptions\TicketNotFoundException;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Db\MultipleObjectsReturnedException;
class TicketService {
/** @var MatrixService */
private $matrix;
/** @var TicketMapper */
private $ticketMapper;
/** @var UserMapper */
private $userMapper;
public function __construct(MatrixService $matrix, TicketMapper $ticketMapper, UserMapper $userMapper) {
$this->matrix = $matrix;
$this->ticketMapper = $ticketMapper;
$this->userMapper = $userMapper;
}
public function findAll(string $userId): array {
$dbTickets = $this->ticketMapper->findAllForUser($this->getOrCreateUser($userId));
return array_map(function ($ticket) { return $this->resolveTicket($ticket); }, $dbTickets);
}
/**
* @throws MatrixRequestException
* @throws MatrixHttpLibException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
* @throws TicketNotFoundException
* @throws MatrixException
*/
public function find($id, $userId): array {
try {
return $this->resolveTicket($this->ticketMapper->findForUser($id, $this->getOrCreateUser($userId)));
} catch (DoesNotExistException $e) {
throw new TicketNotFoundException($id);
}
}
public function create($title, $content, $userId): array {
$matrixUser = $this->getOrCreateUser($userId);
// FIXME: room must be joined on the remote support-platform Nextcloud
$roomId = $this->matrix->createRoom();
$this->matrix->setProperty($roomId, "upschooling.ticket", array(
// id is to be set from the remote support-platform Nextcloud
"title" => $title,
"description" => $content,
"status" => "open",
"version" => "1",
));
$ticket = new MatrixTicket();
$ticket->setMatrixRoom($roomId);
$ticket->setMatrixAssistedUser($matrixUser->getMatrixUser());
$ticket->setStatus("open");
$ticket->setVersion(1);
return $this->resolveTicket($this->ticketMapper->insert($ticket));
}
public function update($id, $title, $content, $userId) {
throw new Exception("Not implemented");
}
public function delete($id, $userId) {
throw new Exception("Not implemented");
}
/**
* @param MatrixTicket $ticket the database object.
* @return array a JSON serializable representation of the resolved ticket, for the frontend.
*/
private function resolveTicket(MatrixTicket $ticket): array
{
try {
$matrixTicketContent = $this->matrix->getProperty($ticket->getMatrixRoom(), "upschooling.ticket");
$ticketId = array_get($matrixTicketContent, "id", $ticket->getId());
$title = array_get($matrixTicketContent, "title", "Untitled");
$description = array_get($matrixTicketContent, "description", "");
$lastModified = $this->matrix->getLastEventDate($ticket->getMatrixRoom());
$matrixHelperUser = $ticket->getMatrixHelperUser();
$matrixHelperUserName = $matrixHelperUser ? "Helfer Name" : "nicht zugewiesen";
//FIXME : set $maxHelpTime somewhere as an option or at least available for other functions
$maxHelpTime = 7 * 24 * 60 * 60 * 1000;
$expirationDate = $lastModified + $maxHelpTime;
return array(
'ticketId' => $ticketId,
'status' => $ticket->getStatus(),
'lastModified' => $lastModified,
'title' => $title,
'description' => $description,
'matrixHelperUser' => $matrixHelperUser,
'matrixHelferName' => $matrixHelperUserName,
'expirationDate' => $expirationDate
);
} catch (MatrixException | RoomNotFoundException $e) {
return array(
'ticketId' => $ticket->getId(),
'status' => 'error',
);
}
}
/**
* @param $userId string Nextcloud user id
* @throws MatrixException
* @throws MatrixHttpLibException
* @throws MatrixRequestException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
*/
private function getOrCreateUser(string $userId): MatrixUser
{
try {
return $this->userMapper->find($userId);
} catch (DoesNotExistException $e) {
$matrixUser = $this->matrix->registerNewUser();
$matrixUser->setUserId($userId);
$this->userMapper->insert($matrixUser);
return $matrixUser;
}
}
}