123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- <?php
- declare(strict_types=1);
- namespace OCA\DAV\Controller;
- use DateTimeImmutable;
- use OCA\DAV\ResponseDefinitions;
- use OCA\DAV\Service\AbsenceService;
- use OCP\AppFramework\Db\DoesNotExistException;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\Http\Attribute\NoAdminRequired;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\OCSController;
- use OCP\IRequest;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\User\IAvailabilityCoordinator;
- class OutOfOfficeController extends OCSController {
- public function __construct(
- string $appName,
- IRequest $request,
- private IUserManager $userManager,
- private ?IUserSession $userSession,
- private AbsenceService $absenceService,
- private IAvailabilityCoordinator $coordinator,
- ) {
- parent::__construct($appName, $request);
- }
-
-
- public function getCurrentOutOfOfficeData(string $userId): DataResponse {
- $user = $this->userManager->get($userId);
- if ($user === null) {
- return new DataResponse(null, Http::STATUS_NOT_FOUND);
- }
- try {
- $data = $this->absenceService->getCurrentAbsence($user);
- if ($data === null) {
- return new DataResponse(null, Http::STATUS_NOT_FOUND);
- }
- } catch (DoesNotExistException) {
- return new DataResponse(null, Http::STATUS_NOT_FOUND);
- }
- return new DataResponse($data->jsonSerialize());
- }
-
-
- public function getOutOfOffice(string $userId): DataResponse {
- try {
- $data = $this->absenceService->getAbsence($userId);
- if ($data === null) {
- return new DataResponse(null, Http::STATUS_NOT_FOUND);
- }
- } catch (DoesNotExistException) {
- return new DataResponse(null, Http::STATUS_NOT_FOUND);
- }
- return new DataResponse([
- 'id' => $data->getId(),
- 'userId' => $data->getUserId(),
- 'firstDay' => $data->getFirstDay(),
- 'lastDay' => $data->getLastDay(),
- 'status' => $data->getStatus(),
- 'message' => $data->getMessage(),
- ]);
- }
-
-
- public function setOutOfOffice(
- string $firstDay,
- string $lastDay,
- string $status,
- string $message,
- ): DataResponse {
- $user = $this->userSession?->getUser();
- if ($user === null) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
- $parsedFirstDay = new DateTimeImmutable($firstDay);
- $parsedLastDay = new DateTimeImmutable($lastDay);
- if ($parsedFirstDay->getTimestamp() > $parsedLastDay->getTimestamp()) {
- return new DataResponse(['error' => 'firstDay'], Http::STATUS_BAD_REQUEST);
- }
- $data = $this->absenceService->createOrUpdateAbsence(
- $user,
- $firstDay,
- $lastDay,
- $status,
- $message,
- );
- $this->coordinator->clearCache($user->getUID());
- return new DataResponse([
- 'id' => $data->getId(),
- 'userId' => $data->getUserId(),
- 'firstDay' => $data->getFirstDay(),
- 'lastDay' => $data->getLastDay(),
- 'status' => $data->getStatus(),
- 'message' => $data->getMessage(),
- ]);
- }
-
-
- public function clearOutOfOffice(): DataResponse {
- $user = $this->userSession?->getUser();
- if ($user === null) {
- return new DataResponse(null, Http::STATUS_UNAUTHORIZED);
- }
- $this->absenceService->clearAbsence($user);
- $this->coordinator->clearCache($user->getUID());
- return new DataResponse(null);
- }
- }
|