mirror of
https://github.com/bitwarden/server
synced 2026-01-07 19:13:50 +00:00
[PM-10560] Create notification database storage (#4688)
* Add new tables * Add stored procedures * Add core entities and models * Setup EF * Add repository interfaces * Add dapper repos * Add EF repos * Add order by * EF updates * PM-10560: Notifications repository matching requirements. * PM-10560: Notifications repository matching requirements. * PM-10560: Migration scripts * PM-10560: EF index optimizations * PM-10560: Cleanup * PM-10560: Priority in natural order, Repository, sql simplifications * PM-10560: Title column update * PM-10560: Incorrect EF migration removal * PM-10560: EF migrations * PM-10560: Added views, SP naming simplification * PM-10560: Notification entity Title update, EF migrations * PM-10560: Removing Notification_ReadByUserId * PM-10560: Notification ReadByUserIdAndStatus fix * PM-10560: Notification ReadByUserIdAndStatus fix to be in line with requirements and EF --------- Co-authored-by: Maciej Zieniuk <mzieniuk@bitwarden.com> Co-authored-by: Matt Bishop <mbishop@bitwarden.com>
This commit is contained in:
27
src/Core/NotificationCenter/Entities/Notification.cs
Normal file
27
src/Core/NotificationCenter/Entities/Notification.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
#nullable enable
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Entities;
|
||||
using Bit.Core.NotificationCenter.Enums;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.NotificationCenter.Entities;
|
||||
|
||||
public class Notification : ITableObject<Guid>
|
||||
{
|
||||
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; }
|
||||
|
||||
public void SetNewId()
|
||||
{
|
||||
Id = CoreHelpers.GenerateComb();
|
||||
}
|
||||
}
|
||||
10
src/Core/NotificationCenter/Entities/NotificationStatus.cs
Normal file
10
src/Core/NotificationCenter/Entities/NotificationStatus.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
#nullable enable
|
||||
namespace Bit.Core.NotificationCenter.Entities;
|
||||
|
||||
public class NotificationStatus
|
||||
{
|
||||
public Guid NotificationId { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public DateTime? ReadDate { get; set; }
|
||||
public DateTime? DeletedDate { get; set; }
|
||||
}
|
||||
18
src/Core/NotificationCenter/Enums/ClientType.cs
Normal file
18
src/Core/NotificationCenter/Enums/ClientType.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
#nullable enable
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Bit.Core.NotificationCenter.Enums;
|
||||
|
||||
public enum ClientType : byte
|
||||
{
|
||||
[Display(Name = "All")]
|
||||
All = 0,
|
||||
[Display(Name = "Web Vault")]
|
||||
Web = 1,
|
||||
[Display(Name = "Browser Extension")]
|
||||
Browser = 2,
|
||||
[Display(Name = "Desktop App")]
|
||||
Desktop = 3,
|
||||
[Display(Name = "Mobile App")]
|
||||
Mobile = 4
|
||||
}
|
||||
18
src/Core/NotificationCenter/Enums/Priority.cs
Normal file
18
src/Core/NotificationCenter/Enums/Priority.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
#nullable enable
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
|
||||
namespace Bit.Core.NotificationCenter.Enums;
|
||||
|
||||
public enum Priority : byte
|
||||
{
|
||||
[Display(Name = "Informational")]
|
||||
Informational = 0,
|
||||
[Display(Name = "Low")]
|
||||
Low = 1,
|
||||
[Display(Name = "Medium")]
|
||||
Medium = 2,
|
||||
[Display(Name = "High")]
|
||||
High = 3,
|
||||
[Display(Name = "Critical")]
|
||||
Critical = 4
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#nullable enable
|
||||
namespace Bit.Core.NotificationCenter.Models.Filter;
|
||||
|
||||
public class NotificationStatusFilter
|
||||
{
|
||||
public bool? Read { get; set; }
|
||||
public bool? Deleted { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
#nullable enable
|
||||
using Bit.Core.NotificationCenter.Entities;
|
||||
using Bit.Core.NotificationCenter.Enums;
|
||||
using Bit.Core.NotificationCenter.Models.Filter;
|
||||
using Bit.Core.Repositories;
|
||||
|
||||
namespace Bit.Core.NotificationCenter.Repositories;
|
||||
|
||||
public interface INotificationRepository : IRepository<Notification, Guid>
|
||||
{
|
||||
/// <summary>
|
||||
/// Get notifications for a user with the given filters.
|
||||
/// Includes global notifications.
|
||||
/// </summary>
|
||||
/// <param name="userId">User Id</param>
|
||||
/// <param name="clientType">
|
||||
/// Filter for notifications by client type. Always includes notifications with <see cref="ClientType.All"/>.
|
||||
/// </param>
|
||||
/// <param name="statusFilter">
|
||||
/// Filters notifications by status.
|
||||
/// If both <see cref="NotificationStatusFilter.Read"/> and <see cref="NotificationStatusFilter.Deleted"/>
|
||||
/// are not set, includes notifications without a status.
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Ordered by priority (highest to lowest) and creation date (descending).
|
||||
/// </returns>
|
||||
Task<IEnumerable<Notification>> GetByUserIdAndStatusAsync(Guid userId, ClientType clientType,
|
||||
NotificationStatusFilter? statusFilter);
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#nullable enable
|
||||
using Bit.Core.NotificationCenter.Entities;
|
||||
|
||||
namespace Bit.Core.NotificationCenter.Repositories;
|
||||
|
||||
public interface INotificationStatusRepository
|
||||
{
|
||||
Task<NotificationStatus?> GetByNotificationIdAndUserIdAsync(Guid notificationId, Guid userId);
|
||||
Task<NotificationStatus> CreateAsync(NotificationStatus notificationStatus);
|
||||
Task UpdateAsync(NotificationStatus notificationStatus);
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using Bit.Core.AdminConsole.Repositories;
|
||||
using Bit.Core.Auth.Repositories;
|
||||
using Bit.Core.Billing.Repositories;
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.SecretsManager.Repositories;
|
||||
using Bit.Core.Tools.Repositories;
|
||||
@@ -8,6 +9,7 @@ using Bit.Core.Vault.Repositories;
|
||||
using Bit.Infrastructure.Dapper.AdminConsole.Repositories;
|
||||
using Bit.Infrastructure.Dapper.Auth.Repositories;
|
||||
using Bit.Infrastructure.Dapper.Billing.Repositories;
|
||||
using Bit.Infrastructure.Dapper.NotificationCenter.Repositories;
|
||||
using Bit.Infrastructure.Dapper.Repositories;
|
||||
using Bit.Infrastructure.Dapper.SecretsManager.Repositories;
|
||||
using Bit.Infrastructure.Dapper.Tools.Repositories;
|
||||
@@ -52,6 +54,8 @@ public static class DapperServiceCollectionExtensions
|
||||
services.AddSingleton<IWebAuthnCredentialRepository, WebAuthnCredentialRepository>();
|
||||
services.AddSingleton<IProviderPlanRepository, ProviderPlanRepository>();
|
||||
services.AddSingleton<IProviderInvoiceItemRepository, ProviderInvoiceItemRepository>();
|
||||
services.AddSingleton<INotificationRepository, NotificationRepository>();
|
||||
services.AddSingleton<INotificationStatusRepository, NotificationStatusRepository>();
|
||||
|
||||
if (selfHosted)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#nullable enable
|
||||
using System.Data;
|
||||
using Bit.Core.NotificationCenter.Entities;
|
||||
using Bit.Core.NotificationCenter.Enums;
|
||||
using Bit.Core.NotificationCenter.Models.Filter;
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Infrastructure.Dapper.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace Bit.Infrastructure.Dapper.NotificationCenter.Repositories;
|
||||
|
||||
public class NotificationRepository : Repository<Notification, Guid>, INotificationRepository
|
||||
{
|
||||
public NotificationRepository(GlobalSettings globalSettings)
|
||||
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
public NotificationRepository(string connectionString, string readOnlyConnectionString)
|
||||
: base(connectionString, readOnlyConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Notification>> GetByUserIdAndStatusAsync(Guid userId,
|
||||
ClientType clientType, NotificationStatusFilter? statusFilter)
|
||||
{
|
||||
await using var connection = new SqlConnection(ConnectionString);
|
||||
|
||||
var results = await connection.QueryAsync<Notification>(
|
||||
"[dbo].[Notification_ReadByUserIdAndStatus]",
|
||||
new { UserId = userId, ClientType = clientType, statusFilter?.Read, statusFilter?.Deleted },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
#nullable enable
|
||||
using System.Data;
|
||||
using Bit.Core.NotificationCenter.Entities;
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Core.Settings;
|
||||
using Bit.Infrastructure.Dapper.Repositories;
|
||||
using Dapper;
|
||||
using Microsoft.Data.SqlClient;
|
||||
|
||||
namespace Bit.Infrastructure.Dapper.NotificationCenter.Repositories;
|
||||
|
||||
public class NotificationStatusRepository : BaseRepository, INotificationStatusRepository
|
||||
{
|
||||
public NotificationStatusRepository(GlobalSettings globalSettings)
|
||||
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
public NotificationStatusRepository(string connectionString, string readOnlyConnectionString)
|
||||
: base(connectionString, readOnlyConnectionString)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<NotificationStatus?> GetByNotificationIdAndUserIdAsync(Guid notificationId, Guid userId)
|
||||
{
|
||||
await using var connection = new SqlConnection(ConnectionString);
|
||||
|
||||
return await connection.QueryFirstOrDefaultAsync<NotificationStatus>(
|
||||
"[dbo].[NotificationStatus_ReadByNotificationIdAndUserId]",
|
||||
new { NotificationId = notificationId, UserId = userId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
|
||||
public async Task<NotificationStatus> CreateAsync(NotificationStatus notificationStatus)
|
||||
{
|
||||
await using var connection = new SqlConnection(ConnectionString);
|
||||
|
||||
await connection.ExecuteAsync("[dbo].[NotificationStatus_Create]",
|
||||
notificationStatus, commandType: CommandType.StoredProcedure);
|
||||
|
||||
return notificationStatus;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(NotificationStatus notificationStatus)
|
||||
{
|
||||
await using var connection = new SqlConnection(ConnectionString);
|
||||
|
||||
await connection.ExecuteAsync("[dbo].[NotificationStatus_Update]",
|
||||
notificationStatus, commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
using Bit.Core.Auth.Repositories;
|
||||
using Bit.Core.Billing.Repositories;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Core.Repositories;
|
||||
using Bit.Core.SecretsManager.Repositories;
|
||||
using Bit.Core.Tools.Repositories;
|
||||
@@ -9,6 +10,7 @@ using Bit.Core.Vault.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Auth.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Billing.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.SecretsManager.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Tools.Repositories;
|
||||
@@ -89,6 +91,8 @@ public static class EntityFrameworkServiceCollectionExtensions
|
||||
services.AddSingleton<IWebAuthnCredentialRepository, WebAuthnCredentialRepository>();
|
||||
services.AddSingleton<IProviderPlanRepository, ProviderPlanRepository>();
|
||||
services.AddSingleton<IProviderInvoiceItemRepository, ProviderInvoiceItemRepository>();
|
||||
services.AddSingleton<INotificationRepository, NotificationRepository>();
|
||||
services.AddSingleton<INotificationStatusRepository, NotificationStatusRepository>();
|
||||
|
||||
if (selfHosted)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#nullable enable
|
||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Configurations;
|
||||
|
||||
public class NotificationEntityTypeConfiguration : IEntityTypeConfiguration<Notification>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<Notification> builder)
|
||||
{
|
||||
builder
|
||||
.Property(n => n.Id)
|
||||
.ValueGeneratedNever();
|
||||
|
||||
builder
|
||||
.HasKey(n => n.Id)
|
||||
.IsClustered();
|
||||
|
||||
builder
|
||||
.HasIndex(n => new { n.ClientType, n.Global, n.UserId, n.OrganizationId, n.Priority, n.CreationDate })
|
||||
.IsDescending(false, false, false, false, true, true)
|
||||
.IsClustered(false);
|
||||
|
||||
builder
|
||||
.HasIndex(n => n.OrganizationId)
|
||||
.IsClustered(false);
|
||||
|
||||
builder
|
||||
.HasIndex(n => n.UserId)
|
||||
.IsClustered(false);
|
||||
|
||||
builder.ToTable(nameof(Notification));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
#nullable enable
|
||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Configurations;
|
||||
|
||||
public class NotificationStatusEntityTypeConfiguration : IEntityTypeConfiguration<NotificationStatus>
|
||||
{
|
||||
public void Configure(EntityTypeBuilder<NotificationStatus> builder)
|
||||
{
|
||||
builder
|
||||
.HasKey(ns => new { ns.UserId, ns.NotificationId })
|
||||
.IsClustered();
|
||||
|
||||
builder.ToTable(nameof(NotificationStatus));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
using AutoMapper;
|
||||
using Bit.Infrastructure.EntityFramework.AdminConsole.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
|
||||
public class Notification : Core.NotificationCenter.Entities.Notification
|
||||
{
|
||||
public virtual User User { get; set; }
|
||||
public virtual Organization Organization { get; set; }
|
||||
}
|
||||
|
||||
public class NotificationMapperProfile : Profile
|
||||
{
|
||||
public NotificationMapperProfile()
|
||||
{
|
||||
CreateMap<Core.NotificationCenter.Entities.Notification, Notification>()
|
||||
.PreserveReferences()
|
||||
.ReverseMap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
using AutoMapper;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
|
||||
public class NotificationStatus : Core.NotificationCenter.Entities.NotificationStatus
|
||||
{
|
||||
public virtual Notification Notification { get; set; }
|
||||
public virtual User User { get; set; }
|
||||
}
|
||||
|
||||
public class NotificationStatusMapperProfile : Profile
|
||||
{
|
||||
public NotificationStatusMapperProfile()
|
||||
{
|
||||
CreateMap<Core.NotificationCenter.Entities.NotificationStatus, NotificationStatus>()
|
||||
.PreserveReferences()
|
||||
.ReverseMap();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
#nullable enable
|
||||
using AutoMapper;
|
||||
using Bit.Core.NotificationCenter.Enums;
|
||||
using Bit.Core.NotificationCenter.Models.Filter;
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories;
|
||||
|
||||
public class NotificationRepository : Repository<Core.NotificationCenter.Entities.Notification, Notification, Guid>,
|
||||
INotificationRepository
|
||||
{
|
||||
public NotificationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.Notifications)
|
||||
{
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
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
|
||||
.OrderByDescending(n => n.Priority)
|
||||
.ThenByDescending(n => n.CreationDate)
|
||||
.ToListAsync();
|
||||
|
||||
return Mapper.Map<List<Core.NotificationCenter.Entities.Notification>>(notifications);
|
||||
}
|
||||
|
||||
private static IQueryable<Notification> BuildNotificationQuery(DatabaseContext dbContext, Guid userId,
|
||||
ClientType clientType)
|
||||
{
|
||||
var clientTypes = new[] { ClientType.All };
|
||||
if (clientType != ClientType.All)
|
||||
{
|
||||
clientTypes = [ClientType.All, clientType];
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
#nullable enable
|
||||
using AutoMapper;
|
||||
using Bit.Core.NotificationCenter.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.NotificationCenter.Repositories;
|
||||
|
||||
public class NotificationStatusRepository : BaseEntityFrameworkRepository, INotificationStatusRepository
|
||||
{
|
||||
public NotificationStatusRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper) : base(
|
||||
serviceScopeFactory,
|
||||
mapper)
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<Bit.Core.NotificationCenter.Entities.NotificationStatus?> GetByNotificationIdAndUserIdAsync(Guid notificationId, Guid userId)
|
||||
{
|
||||
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var entity = await dbContext.NotificationStatuses
|
||||
.Where(ns =>
|
||||
ns.NotificationId == notificationId && ns.UserId == userId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
return Mapper.Map<Bit.Core.NotificationCenter.Entities.NotificationStatus?>(entity);
|
||||
}
|
||||
|
||||
public async Task<Bit.Core.NotificationCenter.Entities.NotificationStatus> CreateAsync(Bit.Core.NotificationCenter.Entities.NotificationStatus notificationStatus)
|
||||
{
|
||||
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var entity = Mapper.Map<NotificationStatus>(notificationStatus);
|
||||
await dbContext.AddAsync(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return notificationStatus;
|
||||
}
|
||||
|
||||
public async Task UpdateAsync(Bit.Core.NotificationCenter.Entities.NotificationStatus notificationStatus)
|
||||
{
|
||||
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var entity = await dbContext.NotificationStatuses
|
||||
.Where(ns =>
|
||||
ns.NotificationId == notificationStatus.NotificationId && ns.UserId == notificationStatus.UserId)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
if (entity != null)
|
||||
{
|
||||
entity.DeletedDate = notificationStatus.DeletedDate;
|
||||
entity.ReadDate = notificationStatus.ReadDate;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ using Bit.Infrastructure.EntityFramework.Auth.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Billing.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Converters;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Bit.Infrastructure.EntityFramework.NotificationCenter.Models;
|
||||
using Bit.Infrastructure.EntityFramework.SecretsManager.Models;
|
||||
using Bit.Infrastructure.EntityFramework.Vault.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
@@ -71,6 +72,8 @@ public class DatabaseContext : DbContext
|
||||
public DbSet<WebAuthnCredential> WebAuthnCredentials { get; set; }
|
||||
public DbSet<ProviderPlan> ProviderPlans { get; set; }
|
||||
public DbSet<ProviderInvoiceItem> ProviderInvoiceItems { get; set; }
|
||||
public DbSet<Notification> Notifications { get; set; }
|
||||
public DbSet<NotificationStatus> NotificationStatuses { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
CREATE PROCEDURE [dbo].[NotificationStatus_Create]
|
||||
@NotificationId UNIQUEIDENTIFIER,
|
||||
@UserId UNIQUEIDENTIFIER,
|
||||
@ReadDate DATETIME2(7),
|
||||
@DeletedDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
INSERT INTO [dbo].[NotificationStatus] (
|
||||
[NotificationId],
|
||||
[UserId],
|
||||
[ReadDate],
|
||||
[DeletedDate]
|
||||
)
|
||||
VALUES (
|
||||
@NotificationId,
|
||||
@UserId,
|
||||
@ReadDate,
|
||||
@DeletedDate
|
||||
)
|
||||
END
|
||||
@@ -0,0 +1,12 @@
|
||||
CREATE PROCEDURE [dbo].[NotificationStatus_ReadByNotificationIdAndUserId]
|
||||
@NotificationId UNIQUEIDENTIFIER,
|
||||
@UserId UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT TOP 1 *
|
||||
FROM [dbo].[NotificationStatusView]
|
||||
WHERE [NotificationId] = @NotificationId
|
||||
AND [UserId] = @UserId
|
||||
END
|
||||
@@ -0,0 +1,15 @@
|
||||
CREATE PROCEDURE [dbo].[NotificationStatus_Update]
|
||||
@NotificationId UNIQUEIDENTIFIER,
|
||||
@UserId UNIQUEIDENTIFIER,
|
||||
@ReadDate DATETIME2(7),
|
||||
@DeletedDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
UPDATE [dbo].[NotificationStatus]
|
||||
SET [ReadDate] = @ReadDate,
|
||||
[DeletedDate] = @DeletedDate
|
||||
WHERE [NotificationId] = @NotificationId
|
||||
AND [UserId] = @UserId
|
||||
END
|
||||
@@ -0,0 +1,40 @@
|
||||
CREATE PROCEDURE [dbo].[Notification_Create]
|
||||
@Id UNIQUEIDENTIFIER OUTPUT,
|
||||
@Priority TINYINT,
|
||||
@Global BIT,
|
||||
@ClientType TINYINT,
|
||||
@UserId UNIQUEIDENTIFIER,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@Title NVARCHAR(256),
|
||||
@Body NVARCHAR(MAX),
|
||||
@CreationDate DATETIME2(7),
|
||||
@RevisionDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
INSERT INTO [dbo].[Notification] (
|
||||
[Id],
|
||||
[Priority],
|
||||
[Global],
|
||||
[ClientType],
|
||||
[UserId],
|
||||
[OrganizationId],
|
||||
[Title],
|
||||
[Body],
|
||||
[CreationDate],
|
||||
[RevisionDate]
|
||||
)
|
||||
VALUES (
|
||||
@Id,
|
||||
@Priority,
|
||||
@Global,
|
||||
@ClientType,
|
||||
@UserId,
|
||||
@OrganizationId,
|
||||
@Title,
|
||||
@Body,
|
||||
@CreationDate,
|
||||
@RevisionDate
|
||||
)
|
||||
END
|
||||
@@ -0,0 +1,10 @@
|
||||
CREATE PROCEDURE [dbo].[Notification_ReadById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT *
|
||||
FROM [dbo].[NotificationView]
|
||||
WHERE [Id] = @Id
|
||||
END
|
||||
@@ -0,0 +1,34 @@
|
||||
CREATE PROCEDURE [dbo].[Notification_ReadByUserIdAndStatus]
|
||||
@UserId UNIQUEIDENTIFIER,
|
||||
@ClientType TINYINT,
|
||||
@Read BIT,
|
||||
@Deleted BIT
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT n.*
|
||||
FROM [dbo].[NotificationView] n
|
||||
LEFT JOIN [dbo].[OrganizationUserView] ou ON n.[OrganizationId] = ou.[OrganizationId]
|
||||
AND ou.[UserId] = @UserId
|
||||
LEFT JOIN [dbo].[NotificationStatusView] ns ON n.[Id] = ns.[NotificationId]
|
||||
AND ns.[UserId] = @UserId
|
||||
WHERE [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 (ns.[NotificationId] IS NOT NULL
|
||||
AND ((@Read IS NULL
|
||||
OR IIF((@Read = 1 AND ns.[ReadDate] IS NOT NULL) OR
|
||||
(@Read = 0 AND ns.[ReadDate] IS NULL),
|
||||
1, 0) = 1)
|
||||
OR (@Deleted IS NULL
|
||||
OR IIF((@Deleted = 1 AND ns.[DeletedDate] IS NOT NULL) OR
|
||||
(@Deleted = 0 AND ns.[DeletedDate] IS NULL),
|
||||
1, 0) = 1))))
|
||||
ORDER BY [Priority] DESC, n.[CreationDate] DESC
|
||||
END
|
||||
@@ -0,0 +1,27 @@
|
||||
CREATE PROCEDURE [dbo].[Notification_Update]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@Priority TINYINT,
|
||||
@Global BIT,
|
||||
@ClientType TINYINT,
|
||||
@UserId UNIQUEIDENTIFIER,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@Title NVARCHAR(256),
|
||||
@Body NVARCHAR(MAX),
|
||||
@CreationDate DATETIME2(7),
|
||||
@RevisionDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
UPDATE [dbo].[Notification]
|
||||
SET [Priority] = @Priority,
|
||||
[Global] = @Global,
|
||||
[ClientType] = @ClientType,
|
||||
[UserId] = @UserId,
|
||||
[OrganizationId] = @OrganizationId,
|
||||
[Title] = @Title,
|
||||
[Body] = @Body,
|
||||
[CreationDate] = @CreationDate,
|
||||
[RevisionDate] = @RevisionDate
|
||||
WHERE [Id] = @Id
|
||||
END
|
||||
32
src/Sql/NotificationCenter/dbo/Tables/Notification.sql
Normal file
32
src/Sql/NotificationCenter/dbo/Tables/Notification.sql
Normal file
@@ -0,0 +1,32 @@
|
||||
CREATE TABLE [dbo].[Notification]
|
||||
(
|
||||
[Id] UNIQUEIDENTIFIER NOT NULL,
|
||||
[Priority] TINYINT NOT NULL,
|
||||
[Global] BIT NOT NULL,
|
||||
[ClientType] TINYINT NOT NULL,
|
||||
[UserId] UNIQUEIDENTIFIER NULL,
|
||||
[OrganizationId] UNIQUEIDENTIFIER NULL,
|
||||
[Title] NVARCHAR (256) NULL,
|
||||
[Body] NVARCHAR (MAX) NULL,
|
||||
[CreationDate] DATETIME2 (7) NOT NULL,
|
||||
[RevisionDate] DATETIME2 (7) NOT NULL,
|
||||
CONSTRAINT [PK_Notification] PRIMARY KEY CLUSTERED ([Id] ASC),
|
||||
CONSTRAINT [FK_Notification_Organization] FOREIGN KEY ([OrganizationId]) REFERENCES [dbo].[Organization] ([Id]),
|
||||
CONSTRAINT [FK_Notification_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
|
||||
);
|
||||
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_Notification_Priority_CreationDate_ClientType_Global_UserId_OrganizationId]
|
||||
ON [dbo].[Notification]([Priority] DESC, [CreationDate] DESC, [ClientType], [Global], [UserId], [OrganizationId]);
|
||||
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_Notification_UserId]
|
||||
ON [dbo].[Notification]([UserId] ASC) WHERE UserId IS NOT NULL;
|
||||
|
||||
|
||||
GO
|
||||
CREATE NONCLUSTERED INDEX [IX_Notification_OrganizationId]
|
||||
ON [dbo].[Notification]([OrganizationId] ASC) WHERE OrganizationId IS NOT NULL;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
CREATE TABLE [dbo].[NotificationStatus]
|
||||
(
|
||||
[NotificationId] UNIQUEIDENTIFIER NOT NULL,
|
||||
[UserId] UNIQUEIDENTIFIER NOT NULL,
|
||||
[ReadDate] DATETIME2 (7) NULL,
|
||||
[DeletedDate] DATETIME2 (7) NULL,
|
||||
CONSTRAINT [PK_NotificationStatus] PRIMARY KEY CLUSTERED ([NotificationId] ASC, [UserId] ASC),
|
||||
CONSTRAINT [FK_NotificationStatus_User] FOREIGN KEY ([UserId]) REFERENCES [dbo].[User] ([Id])
|
||||
);
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE VIEW [dbo].[NotificationStatusView]
|
||||
AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[NotificationStatus]
|
||||
@@ -0,0 +1,6 @@
|
||||
CREATE VIEW [dbo].[NotificationView]
|
||||
AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[Notification]
|
||||
Reference in New Issue
Block a user