. */ namespace SP\Domain\Notification\Services; use SP\Core\Application; 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\Domain\Common\Services\Service; use SP\Domain\Common\Services\ServiceException; use SP\Domain\Notification\In\NotificationRepositoryInterface; use SP\Domain\Notification\NotificationServiceInterface; use SP\Infrastructure\Common\Repositories\NoSuchItemException; use SP\Infrastructure\Database\QueryResult; use SP\Infrastructure\Notification\Repositories\NotificationRepository; /** * Class NotificationService * * @package SP\Domain\Notification\Services */ final class NotificationService extends Service implements NotificationServiceInterface { protected NotificationRepository $notificationRepository; public function __construct(Application $application, NotificationRepositoryInterface $notificationRepository) { parent::__construct($application); $this->notificationRepository = $notificationRepository; } /** * 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\Infrastructure\Common\Repositories\NoSuchItemException */ public function delete(int $id): NotificationServiceInterface { 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\Infrastructure\Common\Repositories\NoSuchItemException */ public function deleteAdmin(int $id): NotificationServiceInterface { 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 \SP\Domain\Common\Services\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 \SP\Domain\Common\Services\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\Infrastructure\Common\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); } }