mirror of
https://github.com/bitwarden/server.git
synced 2024-11-21 12:05:42 +01: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:
parent
7e22a6d036
commit
738febf031
@ -0,0 +1,25 @@
|
|||||||
|
#nullable enable
|
||||||
|
using System.ComponentModel.DataAnnotations;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.NotificationCenter.Enums;
|
||||||
|
|
||||||
|
namespace Bit.Core.NotificationCenter.Models.Data;
|
||||||
|
|
||||||
|
public class NotificationStatusDetails
|
||||||
|
{
|
||||||
|
// Notification fields
|
||||||
|
public Guid Id { get; set; }
|
||||||
|
public Priority Priority { get; set; }
|
||||||
|
public bool Global { get; set; }
|
||||||
|
public ClientType ClientType { get; set; }
|
||||||
|
public Guid? UserId { get; set; }
|
||||||
|
public Guid? OrganizationId { get; set; }
|
||||||
|
[MaxLength(256)]
|
||||||
|
public string? Title { get; set; }
|
||||||
|
public string? Body { get; set; }
|
||||||
|
public DateTime CreationDate { get; set; }
|
||||||
|
public DateTime RevisionDate { get; set; }
|
||||||
|
// Notification Status fields
|
||||||
|
public DateTime? ReadDate { get; set; }
|
||||||
|
public DateTime? DeletedDate { get; set; }
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using Bit.Core.Context;
|
using Bit.Core.Context;
|
||||||
using Bit.Core.Exceptions;
|
using Bit.Core.Exceptions;
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
using Bit.Core.NotificationCenter.Models.Filter;
|
using Bit.Core.NotificationCenter.Models.Filter;
|
||||||
using Bit.Core.NotificationCenter.Queries.Interfaces;
|
using Bit.Core.NotificationCenter.Queries.Interfaces;
|
||||||
using Bit.Core.NotificationCenter.Repositories;
|
using Bit.Core.NotificationCenter.Repositories;
|
||||||
@ -9,19 +9,20 @@ using Bit.Core.Utilities;
|
|||||||
|
|
||||||
namespace Bit.Core.NotificationCenter.Queries;
|
namespace Bit.Core.NotificationCenter.Queries;
|
||||||
|
|
||||||
public class GetNotificationsForUserQuery : IGetNotificationsForUserQuery
|
public class GetNotificationStatusDetailsForUserQuery : IGetNotificationStatusDetailsForUserQuery
|
||||||
{
|
{
|
||||||
private readonly ICurrentContext _currentContext;
|
private readonly ICurrentContext _currentContext;
|
||||||
private readonly INotificationRepository _notificationRepository;
|
private readonly INotificationRepository _notificationRepository;
|
||||||
|
|
||||||
public GetNotificationsForUserQuery(ICurrentContext currentContext,
|
public GetNotificationStatusDetailsForUserQuery(ICurrentContext currentContext,
|
||||||
INotificationRepository notificationRepository)
|
INotificationRepository notificationRepository)
|
||||||
{
|
{
|
||||||
_currentContext = currentContext;
|
_currentContext = currentContext;
|
||||||
_notificationRepository = notificationRepository;
|
_notificationRepository = notificationRepository;
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Notification>> GetByUserIdStatusFilterAsync(NotificationStatusFilter statusFilter)
|
public async Task<IEnumerable<NotificationStatusDetails>> GetByUserIdStatusFilterAsync(
|
||||||
|
NotificationStatusFilter statusFilter)
|
||||||
{
|
{
|
||||||
if (!_currentContext.UserId.HasValue)
|
if (!_currentContext.UserId.HasValue)
|
||||||
{
|
{
|
@ -0,0 +1,10 @@
|
|||||||
|
#nullable enable
|
||||||
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Filter;
|
||||||
|
|
||||||
|
namespace Bit.Core.NotificationCenter.Queries.Interfaces;
|
||||||
|
|
||||||
|
public interface IGetNotificationStatusDetailsForUserQuery
|
||||||
|
{
|
||||||
|
Task<IEnumerable<NotificationStatusDetails>> GetByUserIdStatusFilterAsync(NotificationStatusFilter statusFilter);
|
||||||
|
}
|
@ -1,10 +0,0 @@
|
|||||||
#nullable enable
|
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
|
||||||
using Bit.Core.NotificationCenter.Models.Filter;
|
|
||||||
|
|
||||||
namespace Bit.Core.NotificationCenter.Queries.Interfaces;
|
|
||||||
|
|
||||||
public interface IGetNotificationsForUserQuery
|
|
||||||
{
|
|
||||||
Task<IEnumerable<Notification>> GetByUserIdStatusFilterAsync(NotificationStatusFilter statusFilter);
|
|
||||||
}
|
|
@ -1,6 +1,7 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
using Bit.Core.NotificationCenter.Entities;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
using Bit.Core.NotificationCenter.Models.Filter;
|
using Bit.Core.NotificationCenter.Models.Filter;
|
||||||
using Bit.Core.Repositories;
|
using Bit.Core.Repositories;
|
||||||
|
|
||||||
@ -23,7 +24,8 @@ public interface INotificationRepository : IRepository<Notification, Guid>
|
|||||||
/// </param>
|
/// </param>
|
||||||
/// <returns>
|
/// <returns>
|
||||||
/// Ordered by priority (highest to lowest) and creation date (descending).
|
/// Ordered by priority (highest to lowest) and creation date (descending).
|
||||||
|
/// Includes all fields from <see cref="Notification"/> and <see cref="NotificationStatus"/>
|
||||||
/// </returns>
|
/// </returns>
|
||||||
Task<IEnumerable<Notification>> GetByUserIdAndStatusAsync(Guid userId, ClientType clientType,
|
Task<IEnumerable<NotificationStatusDetails>> GetByUserIdAndStatusAsync(Guid userId, ClientType clientType,
|
||||||
NotificationStatusFilter? statusFilter);
|
NotificationStatusFilter? statusFilter);
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,7 @@
|
|||||||
using System.Data;
|
using System.Data;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
using Bit.Core.NotificationCenter.Entities;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
using Bit.Core.NotificationCenter.Models.Filter;
|
using Bit.Core.NotificationCenter.Models.Filter;
|
||||||
using Bit.Core.NotificationCenter.Repositories;
|
using Bit.Core.NotificationCenter.Repositories;
|
||||||
using Bit.Core.Settings;
|
using Bit.Core.Settings;
|
||||||
@ -23,12 +24,12 @@ public class NotificationRepository : Repository<Notification, Guid>, INotificat
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
public async Task<IEnumerable<Notification>> GetByUserIdAndStatusAsync(Guid userId,
|
public async Task<IEnumerable<NotificationStatusDetails>> GetByUserIdAndStatusAsync(Guid userId,
|
||||||
ClientType clientType, NotificationStatusFilter? statusFilter)
|
ClientType clientType, NotificationStatusFilter? statusFilter)
|
||||||
{
|
{
|
||||||
await using var connection = new SqlConnection(ConnectionString);
|
await using var connection = new SqlConnection(ConnectionString);
|
||||||
|
|
||||||
var results = await connection.QueryAsync<Notification>(
|
var results = await connection.QueryAsync<NotificationStatusDetails>(
|
||||||
"[dbo].[Notification_ReadByUserIdAndStatus]",
|
"[dbo].[Notification_ReadByUserIdAndStatus]",
|
||||||
new { UserId = userId, ClientType = clientType, statusFilter?.Read, statusFilter?.Deleted },
|
new { UserId = userId, ClientType = clientType, statusFilter?.Read, statusFilter?.Deleted },
|
||||||
commandType: CommandType.StoredProcedure);
|
commandType: CommandType.StoredProcedure);
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using Bit.Core.Enums;
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
using Bit.Core.NotificationCenter.Models.Filter;
|
using Bit.Core.NotificationCenter.Models.Filter;
|
||||||
using Bit.Core.NotificationCenter.Repositories;
|
using Bit.Core.NotificationCenter.Repositories;
|
||||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||||
|
using Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories.Queries;
|
||||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
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,
|
public async Task<IEnumerable<Core.NotificationCenter.Entities.Notification>> GetByUserIdAsync(Guid userId,
|
||||||
ClientType clientType)
|
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();
|
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
||||||
var dbContext = GetDatabaseContext(scope);
|
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))
|
var notifications = await notificationStatusDetailsViewQuery.Run(dbContext)
|
||||||
{
|
|
||||||
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
|
|
||||||
.OrderByDescending(n => n.Priority)
|
.OrderByDescending(n => n.Priority)
|
||||||
.ThenByDescending(n => n.CreationDate)
|
.ThenByDescending(n => n.CreationDate)
|
||||||
.ToListAsync();
|
.ToListAsync();
|
||||||
@ -55,38 +36,28 @@ public class NotificationRepository : Repository<Core.NotificationCenter.Entitie
|
|||||||
return Mapper.Map<List<Core.NotificationCenter.Entities.Notification>>(notifications);
|
return Mapper.Map<List<Core.NotificationCenter.Entities.Notification>>(notifications);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IQueryable<Notification> BuildNotificationQuery(DatabaseContext dbContext, Guid userId,
|
public async Task<IEnumerable<NotificationStatusDetails>> GetByUserIdAndStatusAsync(Guid userId,
|
||||||
ClientType clientType)
|
ClientType clientType, NotificationStatusFilter? statusFilter)
|
||||||
{
|
{
|
||||||
var clientTypes = new[] { ClientType.All };
|
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
||||||
if (clientType != ClientType.All)
|
var dbContext = GetDatabaseContext(scope);
|
||||||
{
|
|
||||||
clientTypes = [ClientType.All, clientType];
|
|
||||||
}
|
|
||||||
|
|
||||||
return from n in dbContext.Notifications
|
var notificationStatusDetailsViewQuery = new NotificationStatusDetailsViewQuery(userId, clientType);
|
||||||
join ou in dbContext.OrganizationUsers.Where(ou => ou.UserId == userId)
|
|
||||||
on n.OrganizationId equals ou.OrganizationId into grouping
|
var query = notificationStatusDetailsViewQuery.Run(dbContext);
|
||||||
from ou in grouping.DefaultIfEmpty()
|
if (statusFilter != null && (statusFilter.Read != null || statusFilter.Deleted != null))
|
||||||
where
|
{
|
||||||
clientTypes.Contains(n.ClientType) &&
|
query = from n in query
|
||||||
(
|
where statusFilter.Read == null ||
|
||||||
(
|
(statusFilter.Read == true ? n.ReadDate != null : n.ReadDate == null) ||
|
||||||
n.Global &&
|
statusFilter.Deleted == null ||
|
||||||
n.UserId == null &&
|
(statusFilter.Deleted == true ? n.DeletedDate != null : n.DeletedDate == null)
|
||||||
n.OrganizationId == null
|
|
||||||
) ||
|
|
||||||
(
|
|
||||||
!n.Global &&
|
|
||||||
n.UserId == userId &&
|
|
||||||
(n.OrganizationId == null || ou != null)
|
|
||||||
) ||
|
|
||||||
(
|
|
||||||
!n.Global &&
|
|
||||||
n.UserId == null &&
|
|
||||||
ou != null
|
|
||||||
)
|
|
||||||
)
|
|
||||||
select n;
|
select n;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return await query
|
||||||
|
.OrderByDescending(n => n.Priority)
|
||||||
|
.ThenByDescending(n => n.CreationDate)
|
||||||
|
.ToListAsync();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -8,12 +8,11 @@ BEGIN
|
|||||||
SET NOCOUNT ON
|
SET NOCOUNT ON
|
||||||
|
|
||||||
SELECT n.*
|
SELECT n.*
|
||||||
FROM [dbo].[NotificationView] n
|
FROM [dbo].[NotificationStatusDetailsView] n
|
||||||
LEFT JOIN [dbo].[OrganizationUserView] ou ON n.[OrganizationId] = ou.[OrganizationId]
|
LEFT JOIN [dbo].[OrganizationUserView] ou ON n.[OrganizationId] = ou.[OrganizationId]
|
||||||
AND ou.[UserId] = @UserId
|
AND ou.[UserId] = @UserId
|
||||||
LEFT JOIN [dbo].[NotificationStatusView] ns ON n.[Id] = ns.[NotificationId]
|
WHERE (n.[NotificationStatusUserId] IS NULL OR n.[NotificationStatusUserId] = @UserId)
|
||||||
AND ns.[UserId] = @UserId
|
AND [ClientType] IN (0, CASE WHEN @ClientType != 0 THEN @ClientType END)
|
||||||
WHERE [ClientType] IN (0, CASE WHEN @ClientType != 0 THEN @ClientType END)
|
|
||||||
AND ([Global] = 1
|
AND ([Global] = 1
|
||||||
OR (n.[UserId] = @UserId
|
OR (n.[UserId] = @UserId
|
||||||
AND (n.[OrganizationId] IS NULL
|
AND (n.[OrganizationId] IS NULL
|
||||||
@ -21,14 +20,14 @@ BEGIN
|
|||||||
OR (n.[UserId] IS NULL
|
OR (n.[UserId] IS NULL
|
||||||
AND ou.[OrganizationId] IS NOT NULL))
|
AND ou.[OrganizationId] IS NOT NULL))
|
||||||
AND ((@Read IS NULL AND @Deleted IS NULL)
|
AND ((@Read IS NULL AND @Deleted IS NULL)
|
||||||
OR (ns.[NotificationId] IS NOT NULL
|
OR (n.[NotificationStatusUserId] IS NOT NULL
|
||||||
AND ((@Read IS NULL
|
AND ((@Read IS NULL
|
||||||
OR IIF((@Read = 1 AND ns.[ReadDate] IS NOT NULL) OR
|
OR IIF((@Read = 1 AND n.[ReadDate] IS NOT NULL) OR
|
||||||
(@Read = 0 AND ns.[ReadDate] IS NULL),
|
(@Read = 0 AND n.[ReadDate] IS NULL),
|
||||||
1, 0) = 1)
|
1, 0) = 1)
|
||||||
OR (@Deleted IS NULL
|
OR (@Deleted IS NULL
|
||||||
OR IIF((@Deleted = 1 AND ns.[DeletedDate] IS NOT NULL) OR
|
OR IIF((@Deleted = 1 AND n.[DeletedDate] IS NOT NULL) OR
|
||||||
(@Deleted = 0 AND ns.[DeletedDate] IS NULL),
|
(@Deleted = 0 AND n.[DeletedDate] IS NULL),
|
||||||
1, 0) = 1))))
|
1, 0) = 1))))
|
||||||
ORDER BY [Priority] DESC, n.[CreationDate] DESC
|
ORDER BY [Priority] DESC, n.[CreationDate] DESC
|
||||||
END
|
END
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
CREATE VIEW [dbo].[NotificationStatusDetailsView]
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
N.*,
|
||||||
|
NS.UserId AS NotificationStatusUserId,
|
||||||
|
NS.ReadDate,
|
||||||
|
NS.DeletedDate
|
||||||
|
FROM
|
||||||
|
[dbo].[Notification] AS N
|
||||||
|
LEFT JOIN
|
||||||
|
[dbo].[NotificationStatus] as NS
|
||||||
|
ON
|
||||||
|
N.[Id] = NS.[NotificationId]
|
@ -1,4 +1,5 @@
|
|||||||
using AutoFixture;
|
#nullable enable
|
||||||
|
using AutoFixture;
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
using Bit.Core.NotificationCenter.Entities;
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
|
|
||||||
@ -24,8 +25,7 @@ public class NotificationCustomization(bool global) : ICustomization
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public class NotificationCustomizeAttribute(bool global = true)
|
public class NotificationCustomizeAttribute(bool global = true) : BitCustomizeAttribute
|
||||||
: BitCustomizeAttribute
|
|
||||||
{
|
{
|
||||||
public override ICustomization GetCustomization() => new NotificationCustomization(global);
|
public override ICustomization GetCustomization() => new NotificationCustomization(global);
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,21 @@
|
|||||||
|
#nullable enable
|
||||||
|
using AutoFixture;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.NotificationCenter.AutoFixture;
|
||||||
|
|
||||||
|
public class NotificationStatusDetailsCustomization : ICustomization
|
||||||
|
{
|
||||||
|
public void Customize(IFixture fixture)
|
||||||
|
{
|
||||||
|
fixture.Customize<NotificationStatusDetails>(composer => composer.With(n => n.Id, Guid.NewGuid())
|
||||||
|
.With(n => n.UserId, Guid.NewGuid())
|
||||||
|
.With(n => n.OrganizationId, Guid.NewGuid()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class NotificationStatusDetailsCustomizeAttribute : BitCustomizeAttribute
|
||||||
|
{
|
||||||
|
public override ICustomization GetCustomization() => new NotificationStatusDetailsCustomization();
|
||||||
|
}
|
@ -1,4 +1,5 @@
|
|||||||
using AutoFixture;
|
#nullable enable
|
||||||
|
using AutoFixture;
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
using Bit.Core.NotificationCenter.Entities;
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
|
|
||||||
|
@ -0,0 +1,55 @@
|
|||||||
|
#nullable enable
|
||||||
|
using Bit.Core.Context;
|
||||||
|
using Bit.Core.Enums;
|
||||||
|
using Bit.Core.Exceptions;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Data;
|
||||||
|
using Bit.Core.NotificationCenter.Models.Filter;
|
||||||
|
using Bit.Core.NotificationCenter.Queries;
|
||||||
|
using Bit.Core.NotificationCenter.Repositories;
|
||||||
|
using Bit.Core.Test.NotificationCenter.AutoFixture;
|
||||||
|
using Bit.Test.Common.AutoFixture;
|
||||||
|
using Bit.Test.Common.AutoFixture.Attributes;
|
||||||
|
using NSubstitute;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Bit.Core.Test.NotificationCenter.Queries;
|
||||||
|
|
||||||
|
[SutProviderCustomize]
|
||||||
|
[NotificationStatusDetailsCustomize]
|
||||||
|
public class GetNotificationStatusDetailsForUserQueryTest
|
||||||
|
{
|
||||||
|
private static void Setup(SutProvider<GetNotificationStatusDetailsForUserQuery> sutProvider,
|
||||||
|
List<NotificationStatusDetails> notificationsStatusDetails, NotificationStatusFilter statusFilter, Guid? userId)
|
||||||
|
{
|
||||||
|
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
|
||||||
|
sutProvider.GetDependency<INotificationRepository>().GetByUserIdAndStatusAsync(
|
||||||
|
userId.GetValueOrDefault(Guid.NewGuid()), Arg.Any<ClientType>(), statusFilter)
|
||||||
|
.Returns(notificationsStatusDetails);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async Task GetByUserIdStatusFilterAsync_NotLoggedIn_NotFoundException(
|
||||||
|
SutProvider<GetNotificationStatusDetailsForUserQuery> sutProvider,
|
||||||
|
List<NotificationStatusDetails> notificationsStatusDetails, NotificationStatusFilter notificationStatusFilter)
|
||||||
|
{
|
||||||
|
Setup(sutProvider, notificationsStatusDetails, notificationStatusFilter, userId: null);
|
||||||
|
|
||||||
|
await Assert.ThrowsAsync<NotFoundException>(() =>
|
||||||
|
sutProvider.Sut.GetByUserIdStatusFilterAsync(notificationStatusFilter));
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[BitAutoData]
|
||||||
|
public async Task GetByUserIdStatusFilterAsync_NotificationsFound_Returned(
|
||||||
|
SutProvider<GetNotificationStatusDetailsForUserQuery> sutProvider,
|
||||||
|
List<NotificationStatusDetails> notificationsStatusDetails, NotificationStatusFilter notificationStatusFilter)
|
||||||
|
{
|
||||||
|
Setup(sutProvider, notificationsStatusDetails, notificationStatusFilter, Guid.NewGuid());
|
||||||
|
|
||||||
|
var actualNotificationsStatusDetails =
|
||||||
|
await sutProvider.Sut.GetByUserIdStatusFilterAsync(notificationStatusFilter);
|
||||||
|
|
||||||
|
Assert.Equal(notificationsStatusDetails, actualNotificationsStatusDetails);
|
||||||
|
}
|
||||||
|
}
|
@ -1,55 +0,0 @@
|
|||||||
#nullable enable
|
|
||||||
using Bit.Core.Context;
|
|
||||||
using Bit.Core.Enums;
|
|
||||||
using Bit.Core.Exceptions;
|
|
||||||
using Bit.Core.NotificationCenter.Models.Filter;
|
|
||||||
using Bit.Core.NotificationCenter.Queries;
|
|
||||||
using Bit.Core.NotificationCenter.Repositories;
|
|
||||||
using Bit.Core.Test.NotificationCenter.AutoFixture;
|
|
||||||
using Bit.Test.Common.AutoFixture;
|
|
||||||
using Bit.Test.Common.AutoFixture.Attributes;
|
|
||||||
|
|
||||||
namespace Bit.Core.Test.NotificationCenter.Queries;
|
|
||||||
|
|
||||||
using Bit.Core.NotificationCenter.Entities;
|
|
||||||
using NSubstitute;
|
|
||||||
using Xunit;
|
|
||||||
|
|
||||||
[SutProviderCustomize]
|
|
||||||
[NotificationCustomize]
|
|
||||||
public class GetNotificationsForUserQueryTest
|
|
||||||
{
|
|
||||||
private static void Setup(SutProvider<GetNotificationsForUserQuery> sutProvider,
|
|
||||||
List<Notification> notifications, NotificationStatusFilter statusFilter, Guid? userId)
|
|
||||||
{
|
|
||||||
sutProvider.GetDependency<ICurrentContext>().UserId.Returns(userId);
|
|
||||||
sutProvider.GetDependency<INotificationRepository>().GetByUserIdAndStatusAsync(
|
|
||||||
userId.GetValueOrDefault(Guid.NewGuid()), Arg.Any<ClientType>(), statusFilter)
|
|
||||||
.Returns(notifications);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[BitAutoData]
|
|
||||||
public async Task GetByUserIdStatusFilterAsync_NotLoggedIn_NotFoundException(
|
|
||||||
SutProvider<GetNotificationsForUserQuery> sutProvider,
|
|
||||||
List<Notification> notifications, NotificationStatusFilter notificationStatusFilter)
|
|
||||||
{
|
|
||||||
Setup(sutProvider, notifications, notificationStatusFilter, userId: null);
|
|
||||||
|
|
||||||
await Assert.ThrowsAsync<NotFoundException>(() =>
|
|
||||||
sutProvider.Sut.GetByUserIdStatusFilterAsync(notificationStatusFilter));
|
|
||||||
}
|
|
||||||
|
|
||||||
[Theory]
|
|
||||||
[BitAutoData]
|
|
||||||
public async Task GetByUserIdStatusFilterAsync_NotificationsFound_Returned(
|
|
||||||
SutProvider<GetNotificationsForUserQuery> sutProvider,
|
|
||||||
List<Notification> notifications, NotificationStatusFilter notificationStatusFilter)
|
|
||||||
{
|
|
||||||
Setup(sutProvider, notifications, notificationStatusFilter, Guid.NewGuid());
|
|
||||||
|
|
||||||
var actualNotifications = await sutProvider.Sut.GetByUserIdStatusFilterAsync(notificationStatusFilter);
|
|
||||||
|
|
||||||
Assert.Equal(notifications, actualNotifications);
|
|
||||||
}
|
|
||||||
}
|
|
@ -0,0 +1,61 @@
|
|||||||
|
-- View NotificationStatusDetailsView
|
||||||
|
|
||||||
|
IF EXISTS(SELECT *
|
||||||
|
FROM sys.views
|
||||||
|
WHERE [Name] = 'NotificationStatusDetailsView')
|
||||||
|
BEGIN
|
||||||
|
DROP VIEW [dbo].[NotificationStatusDetailsView]
|
||||||
|
END
|
||||||
|
GO
|
||||||
|
|
||||||
|
CREATE VIEW [dbo].[NotificationStatusDetailsView]
|
||||||
|
AS
|
||||||
|
SELECT
|
||||||
|
N.*,
|
||||||
|
NS.UserId AS NotificationStatusUserId,
|
||||||
|
NS.ReadDate,
|
||||||
|
NS.DeletedDate
|
||||||
|
FROM
|
||||||
|
[dbo].[Notification] AS N
|
||||||
|
LEFT JOIN
|
||||||
|
[dbo].[NotificationStatus] as NS
|
||||||
|
ON
|
||||||
|
N.[Id] = NS.[NotificationId]
|
||||||
|
GO
|
||||||
|
|
||||||
|
-- Stored Procedure Notification_ReadByUserIdAndStatus
|
||||||
|
|
||||||
|
CREATE OR ALTER PROCEDURE [dbo].[Notification_ReadByUserIdAndStatus]
|
||||||
|
@UserId UNIQUEIDENTIFIER,
|
||||||
|
@ClientType TINYINT,
|
||||||
|
@Read BIT,
|
||||||
|
@Deleted BIT
|
||||||
|
AS
|
||||||
|
BEGIN
|
||||||
|
SET NOCOUNT ON
|
||||||
|
|
||||||
|
SELECT n.*
|
||||||
|
FROM [dbo].[NotificationStatusDetailsView] n
|
||||||
|
LEFT JOIN [dbo].[OrganizationUserView] ou ON n.[OrganizationId] = ou.[OrganizationId]
|
||||||
|
AND ou.[UserId] = @UserId
|
||||||
|
WHERE (n.[NotificationStatusUserId] IS NULL OR n.[NotificationStatusUserId] = @UserId)
|
||||||
|
AND [ClientType] IN (0, CASE WHEN @ClientType != 0 THEN @ClientType END)
|
||||||
|
AND ([Global] = 1
|
||||||
|
OR (n.[UserId] = @UserId
|
||||||
|
AND (n.[OrganizationId] IS NULL
|
||||||
|
OR ou.[OrganizationId] IS NOT NULL))
|
||||||
|
OR (n.[UserId] IS NULL
|
||||||
|
AND ou.[OrganizationId] IS NOT NULL))
|
||||||
|
AND ((@Read IS NULL AND @Deleted IS NULL)
|
||||||
|
OR (n.[NotificationStatusUserId] IS NOT NULL
|
||||||
|
AND ((@Read IS NULL
|
||||||
|
OR IIF((@Read = 1 AND n.[ReadDate] IS NOT NULL) OR
|
||||||
|
(@Read = 0 AND n.[ReadDate] IS NULL),
|
||||||
|
1, 0) = 1)
|
||||||
|
OR (@Deleted IS NULL
|
||||||
|
OR IIF((@Deleted = 1 AND n.[DeletedDate] IS NOT NULL) OR
|
||||||
|
(@Deleted = 0 AND n.[DeletedDate] IS NULL),
|
||||||
|
1, 0) = 1))))
|
||||||
|
ORDER BY [Priority] DESC, n.[CreationDate] DESC
|
||||||
|
END
|
||||||
|
GO
|
Loading…
Reference in New Issue
Block a user