Nextcloud-App/lib/Service/TicketService.php

156 lines
5 KiB
PHP
Raw Normal View History

<?php
2021-08-21 14:25:23 +00:00
namespace OCA\UPschooling\Service;
2021-09-19 14:55:30 +00:00
use Aryess\PhpMatrixSdk\Exceptions\MatrixException;
use Aryess\PhpMatrixSdk\Exceptions\MatrixHttpLibException;
use Aryess\PhpMatrixSdk\Exceptions\MatrixRequestException;
use Exception;
use OCA\UPschooling\Db\MatrixTicket;
2021-09-19 14:55:30 +00:00
use OCA\UPschooling\Db\MatrixUser;
use OCA\UPschooling\Db\TicketMapper;
2021-09-19 14:55:30 +00:00
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;
2021-08-22 15:03:19 +00:00
class TicketService {
2021-09-19 14:55:30 +00:00
/** @var MatrixService */
private $matrix;
2021-08-22 15:03:19 +00:00
/** @var TicketMapper */
2021-09-19 14:55:30 +00:00
private $ticketMapper;
2021-09-19 14:55:30 +00:00
/** @var UserMapper */
private $userMapper;
2021-09-19 14:55:30 +00:00
public function __construct(MatrixService $matrix, TicketMapper $ticketMapper, UserMapper $userMapper) {
$this->matrix = $matrix;
2021-09-19 14:55:30 +00:00
$this->ticketMapper = $ticketMapper;
$this->userMapper = $userMapper;
}
public function findAll(string $userId): array {
$dbTickets = $this->ticketMapper->findAllForUser($this->getOrCreateUser($userId));
2021-09-19 14:55:30 +00:00
return array_map(function ($ticket) { return $this->resolveTicket($ticket); }, $dbTickets);
}
/**
* @throws MatrixRequestException
* @throws MatrixHttpLibException
* @throws MultipleObjectsReturnedException
* @throws \OCP\DB\Exception
* @throws TicketNotFoundException
* @throws MatrixException
*/
2021-09-19 14:55:30 +00:00
public function find($id, $userId): array {
try {
return $this->resolveTicket($this->ticketMapper->findForUser($id, $this->getOrCreateUser($userId)));
} catch (DoesNotExistException $e) {
throw new TicketNotFoundException($id);
}
}
2021-09-19 14:55:30 +00:00
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,
2021-09-21 18:22:56 +00:00
"status" => "open",
"version" => "1",
2021-09-19 14:55:30 +00:00
));
$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 assign($id, $userId): array{
$matrixUser = $this->getOrCreateUser($userId);
$ticket = $this->ticketMapper->findTicket($id);
$roomID= $ticket->getMatrixRoom();
$this->matrix->setProperty($roomID, "upschooling.ticket", array(
"matrixHelperUser" => $matrixUser->getMatrixUser()
));
$ticket->setMatrixHelperUser($matrixUser->getMatrixUser());
return $this->resolveTicket($this->ticketMapper->update($ticket));
}
2021-09-19 14:55:30 +00:00
public function update($id, $title, $content, $userId) {
throw new Exception("Not implemented");
}
2021-09-19 14:55:30 +00:00
public function delete($id, $userId) {
throw new Exception("Not implemented");
}
2021-09-19 14:55:30 +00:00
/**
2021-10-10 12:26:31 +00:00
* matrixTicketContent has all the syncronized data
2021-09-19 14:55:30 +00:00
* @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 {
2021-09-19 14:55:30 +00:00
$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());
2021-10-10 17:16:46 +00:00
<<<<<<< HEAD
$matrixHelperUser = array_get($matrixTicketContent, "matrixHelperUser", "nicht zugewiesen");
2021-10-10 17:16:46 +00:00
=======
2021-10-10 12:26:31 +00:00
$matrixHelperUserName = array_get($matrixTicketContent, "matrixHelperUserName", "nicht zugewiesen");
2021-10-10 17:16:46 +00:00
>>>>>>> ae0ffa811f99d5ee49597ff70a01aed705377b0c
2021-10-10 12:26:31 +00:00
$expirationDate = array_get($matrixTicketContent, "expiration date", "");
2021-09-19 14:55:30 +00:00
return array(
'ticketId' => $ticketId,
'status' => $ticket->getStatus(),
'lastModified' => $lastModified,
'title' => $title,
'description' => $description,
2021-10-10 17:16:46 +00:00
<<<<<<< HEAD
'matrixHelperUser' => $matrixHelperUser,
2021-10-10 17:16:46 +00:00
=======
'matrixHelferName' => $matrixHelperUserName,
2021-10-10 17:16:46 +00:00
>>>>>>> ae0ffa811f99d5ee49597ff70a01aed705377b0c
'expirationDate' => $expirationDate
2021-09-19 14:55:30 +00:00
);
} catch (MatrixException | RoomNotFoundException $e) {
return array(
'ticketId' => $ticket->getId(),
'status' => 'error',
);
}
}
2021-09-19 14:55:30 +00:00
/**
* @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 {
2021-09-19 14:55:30 +00:00
return $this->userMapper->find($userId);
} catch (DoesNotExistException $e) {
$matrixUser = $this->matrix->registerNewUser();
$matrixUser->setUserId($userId);
$this->userMapper->insert($matrixUser);
return $matrixUser;
}
}
}