. */ namespace SP\Services\Notification; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use SP\Core\Exceptions\ConstraintException; use SP\Core\Exceptions\QueryException; use SP\Core\Exceptions\SPException; use SP\DataModel\ItemSearchData; use SP\DataModel\NotificationData; use SP\Repositories\NoSuchItemException; use SP\Repositories\Notification\NotificationRepository; use SP\Services\Service; use SP\Services\ServiceException; use SP\Storage\Database\QueryResult; /** * Class NotificationService * * @package SP\Services\Notification */ final class NotificationService extends Service { protected ?NotificationRepository $notificationRepository = null; /** * Creates an item * * @throws ConstraintException * @throws QueryException */ public function create(NotificationData $itemData): int { return $this->notificationRepository->create($itemData)->getLastId(); } /** * Updates an item * * @throws ConstraintException * @throws QueryException */ public function update(NotificationData $itemData): int { return $this->notificationRepository->update($itemData); } /** * Devolver los elementos con los ids especificados * * @param int[] $ids * * @return NotificationData[] * @throws ConstraintException * @throws QueryException */ public function getByIdBatch(array $ids): array { return $this->notificationRepository ->getByIdBatch($ids) ->getDataAsArray(); } /** * Deletes an item preserving the sticky ones * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Repositories\NoSuchItemException */ public function delete(int $id): NotificationService { if ($this->notificationRepository->delete($id) === 0) { throw new NoSuchItemException( __u('Notification not found'), SPException::INFO ); } return $this; } /** * Deletes an item * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Repositories\NoSuchItemException */ public function deleteAdmin(int $id): NotificationService { if ($this->notificationRepository->deleteAdmin($id) === 0) { throw new NoSuchItemException( __u('Notification not found'), SPException::INFO ); } return $this; } /** * Deletes an item * * @param int[] $ids * * @throws ConstraintException * @throws QueryException * @throws ServiceException */ public function deleteAdminBatch(array $ids): int { $count = $this->notificationRepository->deleteAdminBatch($ids); if ($count !== count($ids)) { throw new ServiceException( __u('Error while deleting the notifications'), SPException::WARNING ); } return $count; } /** * Deletes all the items for given ids * * @param int[] $ids * * @throws ConstraintException * @throws QueryException * @throws ServiceException */ public function deleteByIdBatch(array $ids): int { $count = $this->notificationRepository->deleteByIdBatch($ids); if ($count !== count($ids)) { throw new ServiceException( __u('Error while deleting the notifications'), SPException::WARNING ); } return $count; } /** * Returns the item for given id * * @throws ConstraintException * @throws QueryException * @throws NoSuchItemException */ public function getById(int $id): NotificationData { $result = $this->notificationRepository->getById($id); if ($result->getNumRows() === 0) { throw new NoSuchItemException( __u('Notification not found'), SPException::INFO ); } return $result->getData(); } /** * Returns all the items * * @return NotificationData[] * @throws ConstraintException * @throws QueryException */ public function getAll(): array { return $this->notificationRepository->getAll()->getDataAsArray(); } /** * Marcar una notificación como leída * * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException * @throws \SP\Repositories\NoSuchItemException */ public function setCheckedById(int $id): void { if ($this->notificationRepository->setCheckedById($id) === 0) { throw new NoSuchItemException( __u('Notification not found'), SPException::INFO ); } } /** * Devolver las notificaciones de un usuario para una fecha y componente determinados * * @return NotificationData[] * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function getForUserIdByDate(string $component, int $id): array { return $this->notificationRepository ->getForUserIdByDate($component, $id) ->getDataAsArray(); } /** * @return NotificationData[] * @throws ConstraintException * @throws QueryException */ public function getAllForUserId(int $id): array { return $this->notificationRepository ->getAllForUserId($id) ->getDataAsArray(); } /** * @return NotificationData[] * @throws \SP\Core\Exceptions\ConstraintException * @throws \SP\Core\Exceptions\QueryException */ public function getAllActiveForUserId(int $id): array { if ($this->context->getUserData()->getIsAdminApp()) { return $this->notificationRepository ->getAllActiveForAdmin($id) ->getDataAsArray(); } return $this->notificationRepository ->getAllActiveForUserId($id) ->getDataAsArray(); } /** * Searches for items by a given filter * * @throws ConstraintException * @throws QueryException */ public function search(ItemSearchData $itemSearchData): QueryResult { $userData = $this->context->getUserData(); if ($userData->getIsAdminApp()) { return $this->notificationRepository ->searchForAdmin($itemSearchData, $userData->getId()); } return $this->notificationRepository ->searchForUserId($itemSearchData, $userData->getId()); } /** * Searches for items by a given filter * * @throws ConstraintException * @throws QueryException */ public function searchForUserId( ItemSearchData $itemSearchData, int $userId ): QueryResult { return $this->notificationRepository ->searchForUserId($itemSearchData, $userId); } /** * @throws ContainerExceptionInterface * @throws NotFoundExceptionInterface */ protected function initialize(): void { $this->notificationRepository = $this->dic->get(NotificationRepository::class); } }