1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-05 01:52:49 -05:00

PM-11123: Notification Status Details view (#4848)

* PM-11123: Notification Status Details view

* PM-11123: Test Typo

* PM-11123: New line missing

* PM-11123: Delete unnecessary field

* PM-11123: Moved NotificationStatusDetails to Models/Data
This commit is contained in:
Maciej Zieniuk
2024-10-03 22:13:43 +02:00
committed by GitHub
parent 7e22a6d036
commit 738febf031
16 changed files with 295 additions and 137 deletions

View File

@ -1,9 +1,11 @@
#nullable enable
using AutoMapper;
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Models.Data;
using Bit.Core.NotificationCenter.Models.Filter;
using Bit.Core.NotificationCenter.Repositories;
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
using Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories.Queries;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
@ -20,34 +22,13 @@ public class NotificationRepository : Repository<Core.NotificationCenter.Entitie
public async Task<IEnumerable<Core.NotificationCenter.Entities.Notification>> GetByUserIdAsync(Guid userId,
ClientType clientType)
{
return await GetByUserIdAndStatusAsync(userId, clientType, new NotificationStatusFilter());
}
public async Task<IEnumerable<Core.NotificationCenter.Entities.Notification>> GetByUserIdAndStatusAsync(Guid userId,
ClientType clientType, NotificationStatusFilter? statusFilter)
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var notificationQuery = BuildNotificationQuery(dbContext, userId, clientType);
var notificationStatusDetailsViewQuery = new NotificationStatusDetailsViewQuery(userId, clientType);
if (statusFilter != null && (statusFilter.Read != null || statusFilter.Deleted != null))
{
notificationQuery = from n in notificationQuery
join ns in dbContext.NotificationStatuses on n.Id equals ns.NotificationId
where
ns.UserId == userId &&
(
statusFilter.Read == null ||
(statusFilter.Read == true ? ns.ReadDate != null : ns.ReadDate == null) ||
statusFilter.Deleted == null ||
(statusFilter.Deleted == true ? ns.DeletedDate != null : ns.DeletedDate == null)
)
select n;
}
var notifications = await notificationQuery
var notifications = await notificationStatusDetailsViewQuery.Run(dbContext)
.OrderByDescending(n => n.Priority)
.ThenByDescending(n => n.CreationDate)
.ToListAsync();
@ -55,38 +36,28 @@ public class NotificationRepository : Repository<Core.NotificationCenter.Entitie
return Mapper.Map<List<Core.NotificationCenter.Entities.Notification>>(notifications);
}
private static IQueryable<Notification> BuildNotificationQuery(DatabaseContext dbContext, Guid userId,
ClientType clientType)
public async Task<IEnumerable<NotificationStatusDetails>> GetByUserIdAndStatusAsync(Guid userId,
ClientType clientType, NotificationStatusFilter? statusFilter)
{
var clientTypes = new[] { ClientType.All };
if (clientType != ClientType.All)
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
var notificationStatusDetailsViewQuery = new NotificationStatusDetailsViewQuery(userId, clientType);
var query = notificationStatusDetailsViewQuery.Run(dbContext);
if (statusFilter != null && (statusFilter.Read != null || statusFilter.Deleted != null))
{
clientTypes = [ClientType.All, clientType];
query = from n in query
where statusFilter.Read == null ||
(statusFilter.Read == true ? n.ReadDate != null : n.ReadDate == null) ||
statusFilter.Deleted == null ||
(statusFilter.Deleted == true ? n.DeletedDate != null : n.DeletedDate == null)
select n;
}
return from n in dbContext.Notifications
join ou in dbContext.OrganizationUsers.Where(ou => ou.UserId == userId)
on n.OrganizationId equals ou.OrganizationId into grouping
from ou in grouping.DefaultIfEmpty()
where
clientTypes.Contains(n.ClientType) &&
(
(
n.Global &&
n.UserId == null &&
n.OrganizationId == null
) ||
(
!n.Global &&
n.UserId == userId &&
(n.OrganizationId == null || ou != null)
) ||
(
!n.Global &&
n.UserId == null &&
ou != null
)
)
select n;
return await query
.OrderByDescending(n => n.Priority)
.ThenByDescending(n => n.CreationDate)
.ToListAsync();
}
}

View File

@ -0,0 +1,63 @@
#nullable enable
using Bit.Core.Enums;
using Bit.Core.NotificationCenter.Models.Data;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories.Queries;
public class NotificationStatusDetailsViewQuery(Guid userId, ClientType clientType) : IQuery<NotificationStatusDetails>
{
public IQueryable<NotificationStatusDetails> Run(DatabaseContext dbContext)
{
var clientTypes = new[] { ClientType.All };
if (clientType != ClientType.All)
{
clientTypes = [ClientType.All, clientType];
}
var query = from n in dbContext.Notifications
join ou in dbContext.OrganizationUsers.Where(ou => ou.UserId == userId)
on n.OrganizationId equals ou.OrganizationId into groupingOrganizationUsers
from ou in groupingOrganizationUsers.DefaultIfEmpty()
join ns in dbContext.NotificationStatuses.Where(ns => ns.UserId == userId) on n.Id equals ns.NotificationId
into groupingNotificationStatus
from ns in groupingNotificationStatus.DefaultIfEmpty()
where
clientTypes.Contains(n.ClientType) &&
(
(
n.Global &&
n.UserId == null &&
n.OrganizationId == null
) ||
(
!n.Global &&
n.UserId == userId &&
(n.OrganizationId == null || ou != null)
) ||
(
!n.Global &&
n.UserId == null &&
ou != null
)
)
select new { n, ns };
return query.Select(x => new NotificationStatusDetails
{
Id = x.n.Id,
Priority = x.n.Priority,
Global = x.n.Global,
ClientType = x.n.ClientType,
UserId = x.n.UserId,
OrganizationId = x.n.OrganizationId,
Title = x.n.Title,
Body = x.n.Body,
CreationDate = x.n.CreationDate,
RevisionDate = x.n.RevisionDate,
ReadDate = x.ns != null ? x.ns.ReadDate : null,
DeletedDate = x.ns != null ? x.ns.DeletedDate : null,
});
}
}