public async Task <Paging <Notification> > ListNotificationsAsync(
            NotificationListRequest request,
            NotificationRetrievalOptions options = default,
            CancellationToken cancellationToken  = default)
        {
            var query = _context.Notifications
                        .WithOptions(options ?? new NotificationRetrievalOptions())
                        .AddFilter(request.Filter)
                        .AddOrder(request.OrderBy, request.Descending);

            if (request.Filter.AccessibleOnly)
            {
                query = await _notificationAccessControlService
                        .AddAccessFilterAsync(query, cancellationToken);
            }

            return(await Paging <Notification> .CreateAsync(query, request, cancellationToken));
        }
        public static IQueryable <Notification> WithOptions(
            this IQueryable <Notification> query,
            NotificationRetrievalOptions options)
        {
            if (!options.ForUpdate)
            {
                query = query.AsNoTracking();
            }

            if (options.LoadEvent)
            {
                query = query.Include(n => n.EventInfo);
            }

            if (options.LoadProduct)
            {
                query = query.Include(n => n.Product);
            }

            if (options.LoadOrganization)
            {
                query = query.Include(n => n.Organization);
            }

            if (options.LoadRecipients)
            {
                query = query.Include(n => n.Recipients)
                        .ThenInclude(r => r.RecipientUser);
            }

            if (options.LoadSender)
            {
                query = query.Include(n => n.CreatedByUser);
            }

            if (options.LoadStatistics)
            {
                query = query.Include(n => n.Statistics);
            }

            return(query);
        }
        public async Task <Notification> GetNotificationByIdAsync(int id,
                                                                  NotificationRetrievalOptions options = default,
                                                                  CancellationToken cancellationToken  = default)
        {
            var notification = await _context.Notifications
                               .WithOptions(options ?? new NotificationRetrievalOptions())
                               .Where(n => n.NotificationId == id)
                               .FirstOrDefaultAsync(cancellationToken) ??
                               throw new NotFoundException($"Notification {id} not found");

            if (options?.ForUpdate == true)
            {
                await _notificationAccessControlService
                .CheckNotificationUpdateAccessAsync(notification, cancellationToken);
            }
            else
            {
                await _notificationAccessControlService
                .CheckNotificationReadAccessAsync(notification, cancellationToken);
            }

            return(notification);
        }