From b7de49a2aa264c12e50d08df47fad8863d3c978a Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Mon, 17 Oct 2022 11:22:44 +0300 Subject: [PATCH] ref --- application/Espo/Controllers/Notification.php | 39 +++++++++----- .../Espo/Tools/Notification/RecordService.php | 53 +++++++------------ 2 files changed, 45 insertions(+), 47 deletions(-) diff --git a/application/Espo/Controllers/Notification.php b/application/Espo/Controllers/Notification.php index 96823a1286..70cbb5faeb 100644 --- a/application/Espo/Controllers/Notification.php +++ b/application/Espo/Controllers/Notification.php @@ -37,8 +37,9 @@ use Espo\Core\{ Api\Response, Exceptions\BadRequest, Exceptions\Error, - Exceptions\Forbidden -}; + Exceptions\Forbidden, + Select\SearchParams, + Select\Where\Item as WhereItem}; use stdClass; @@ -53,22 +54,34 @@ class Notification extends RecordBase */ public function getActionList(Request $request, Response $response): stdClass { - $userId = $this->user->getId(); + $searchParamsAux = $this->searchParamsFetcher->fetch($request); - $searchParams = $this->searchParamsFetcher->fetch($request); - - $offset = $searchParams->getOffset(); - $maxSize = $searchParams->getMaxSize(); + $offset = $searchParamsAux->getOffset(); + $maxSize = $searchParamsAux->getMaxSize(); $after = $request->getQueryParam('after'); - $params = [ - 'offset' => $offset, - 'maxSize' => $maxSize, - 'after' => $after, - ]; + $searchParams = SearchParams + ::create() + ->withOffset($offset) + ->withMaxSize($maxSize); - $recordCollection = $this->getNotificationService()->get($userId, $params); + if ($after) { + $searchParams = $searchParams + ->withWhereAdded( + WhereItem + ::createBuilder() + ->setAttribute('createdAt') + ->setType(WhereItem\Type::AFTER) + ->setValue($after) + ->build() + ); + + } + + $userId = $this->user->getId(); + + $recordCollection = $this->getNotificationService()->get($userId, $searchParams); return (object) [ 'total' => $recordCollection->getTotal(), diff --git a/application/Espo/Tools/Notification/RecordService.php b/application/Espo/Tools/Notification/RecordService.php index be65e676af..392de4baab 100644 --- a/application/Espo/Tools/Notification/RecordService.php +++ b/application/Espo/Tools/Notification/RecordService.php @@ -32,6 +32,8 @@ namespace Espo\Tools\Notification; use Espo\Core\Acl; use Espo\Core\Exceptions\Error; use Espo\Core\Record\Collection as RecordCollection; +use Espo\Core\Select\SearchParams; +use Espo\Core\Select\SelectBuilderFactory; use Espo\Core\Utils\Metadata; use Espo\Entities\Note; use Espo\Entities\Notification; @@ -46,38 +48,37 @@ class RecordService private Acl $acl; private Metadata $metadata; private NoteAccessControl $noteAccessControl; + private SelectBuilderFactory $selectBuilderFactory; public function __construct( EntityManager $entityManager, Acl $acl, Metadata $metadata, - NoteAccessControl $noteAccessControl + NoteAccessControl $noteAccessControl, + SelectBuilderFactory $selectBuilderFactory ) { $this->entityManager = $entityManager; $this->acl = $acl; $this->metadata = $metadata; $this->noteAccessControl = $noteAccessControl; + $this->selectBuilderFactory = $selectBuilderFactory; } /** - * @todo Use params class FetchParams. + * Get notifications for a user. * - * @param array{ - * after?: ?string, - * offset?: ?int, - * maxSize?: ?int, - * } $params * @return RecordCollection * @throws Error */ - public function get(string $userId, array $params = []): RecordCollection + public function get(string $userId, SearchParams $searchParams): RecordCollection { - $queryBuilder = $this->entityManager - ->getQueryBuilder() - ->select() - ->from(Notification::ENTITY_TYPE); - - $whereClause = ['userId' => $userId]; + $queryBuilder = $this->selectBuilderFactory + ->create() + ->from(Notification::ENTITY_TYPE) + ->withSearchParams($searchParams) + ->buildQueryBuilder() + ->where(['userId' => $userId]) + ->order('number', SearchParams::ORDER_DESC); $user = $this->entityManager ->getRDBRepositoryByClass(User::class) @@ -87,33 +88,17 @@ class RecordService throw new Error("User not found."); } - if (!empty($params['after'])) { - $whereClause['createdAt>'] = $params['after']; - } - $ignoreScopeList = $this->getIgnoreScopeList(); - if (!empty($ignoreScopeList)) { - $where = []; - - $where[] = [ + if ($ignoreScopeList !== []) { + $queryBuilder->where([ 'OR' => [ 'relatedParentType' => null, 'relatedParentType!=' => $ignoreScopeList, - ] - ]; - - $whereClause[] = $where; + ], + ]); } - $offset = $params['offset'] ?? null; - $maxSize = $params['maxSize'] ?? null; - - $queryBuilder - ->limit($offset, $maxSize) - ->order('createdAt', 'DESC') - ->where($whereClause); - $query = $queryBuilder->build(); $collection = $this->entityManager