From 8d40d66e63107b22a12036aeb287a715a6cad652 Mon Sep 17 00:00:00 2001 From: Nick Krantz Date: Sat, 31 May 2025 20:15:20 -0500 Subject: [PATCH] Only return UserIds from db layer --- .../Repositories/INotificationRepository.cs | 4 ++-- .../MarkNotificationsForTaskAsDeletedCommand.cs | 6 +++--- .../Repositories/NotificationRepository.cs | 4 ++-- .../Repositories/NotificationRepository.cs | 9 +++++++-- .../Notification_MarkAsDeletedByTask.sql | 15 +++++++-------- ...05-30_00_Notification_MarkAsDeletedByTask.sql} | 15 +++++++-------- 6 files changed, 28 insertions(+), 25 deletions(-) rename util/Migrator/DbScripts/{2025-05-29_02_Notification_MarkAsDeletedByTask.sql => 2025-05-30_00_Notification_MarkAsDeletedByTask.sql} (70%) diff --git a/src/Core/NotificationCenter/Repositories/INotificationRepository.cs b/src/Core/NotificationCenter/Repositories/INotificationRepository.cs index 0ae0542838..d52049e65b 100644 --- a/src/Core/NotificationCenter/Repositories/INotificationRepository.cs +++ b/src/Core/NotificationCenter/Repositories/INotificationRepository.cs @@ -39,7 +39,7 @@ public interface INotificationRepository : IRepository /// The unique identifier of the task. /// User Id /// - /// A collection of notifications associated with the task that are now marked as deleted. + /// A collection of users ids for the notifications that are now marked as deleted. /// - Task> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId); + Task> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId); } diff --git a/src/Core/Vault/Commands/MarkNotificationsForTaskAsDeletedCommand.cs b/src/Core/Vault/Commands/MarkNotificationsForTaskAsDeletedCommand.cs index 67bd7f2f12..2165bc3fe5 100644 --- a/src/Core/Vault/Commands/MarkNotificationsForTaskAsDeletedCommand.cs +++ b/src/Core/Vault/Commands/MarkNotificationsForTaskAsDeletedCommand.cs @@ -20,13 +20,13 @@ public class MarkNotificationsForTaskAsDeletedCommand : IMarkNotificationsForTas public async Task MarkAsDeletedAsync(Guid taskId, Guid userId) { - var notifications = await _notificationRepository.MarkNotificationsAsDeletedByTask(taskId, userId); + var userIds = await _notificationRepository.MarkNotificationsAsDeletedByTask(taskId, userId); // For each user associated with the notifications, send a push notification so local tasks can be updated. - var uniqueUserIds = notifications.Select(n => n.UserId).Where(u => u.HasValue).Distinct(); + var uniqueUserIds = userIds.Distinct(); foreach (var id in uniqueUserIds) { - await _pushNotificationService.PushPendingSecurityTasksAsync((Guid)id); + await _pushNotificationService.PushPendingSecurityTasksAsync(id); } } } diff --git a/src/Infrastructure.Dapper/NotificationCenter/Repositories/NotificationRepository.cs b/src/Infrastructure.Dapper/NotificationCenter/Repositories/NotificationRepository.cs index 0c67ede40a..8739198586 100644 --- a/src/Infrastructure.Dapper/NotificationCenter/Repositories/NotificationRepository.cs +++ b/src/Infrastructure.Dapper/NotificationCenter/Repositories/NotificationRepository.cs @@ -57,11 +57,11 @@ public class NotificationRepository : Repository, INotificat }; } - public async Task> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId) + public async Task> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId) { await using var connection = new SqlConnection(ConnectionString); - var results = await connection.QueryAsync( + var results = await connection.QueryAsync( "[dbo].[Notification_MarkAsDeletedByTask]", new { diff --git a/src/Infrastructure.EntityFramework/NotificationCenter/Repositories/NotificationRepository.cs b/src/Infrastructure.EntityFramework/NotificationCenter/Repositories/NotificationRepository.cs index 06d287382e..6729a6f1fc 100644 --- a/src/Infrastructure.EntityFramework/NotificationCenter/Repositories/NotificationRepository.cs +++ b/src/Infrastructure.EntityFramework/NotificationCenter/Repositories/NotificationRepository.cs @@ -75,7 +75,7 @@ public class NotificationRepository : Repository> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId) + public async Task> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId) { await using var scope = ServiceScopeFactory.CreateAsyncScope(); var dbContext = GetDatabaseContext(scope); @@ -116,6 +116,11 @@ public class NotificationRepository : Repository n.UserId) + .Where(u => u.HasValue) + .ToList(); + + return (IEnumerable)userIds; } } diff --git a/src/Sql/NotificationCenter/dbo/Stored Procedures/Notification_MarkAsDeletedByTask.sql b/src/Sql/NotificationCenter/dbo/Stored Procedures/Notification_MarkAsDeletedByTask.sql index 2216dcc666..6badf6821d 100644 --- a/src/Sql/NotificationCenter/dbo/Stored Procedures/Notification_MarkAsDeletedByTask.sql +++ b/src/Sql/NotificationCenter/dbo/Stored Procedures/Notification_MarkAsDeletedByTask.sql @@ -6,14 +6,14 @@ BEGIN SET NOCOUNT ON; -- Collect NotificationIds as they are altered - DECLARE @AlteredNotifications TABLE ( - NotificationId UNIQUEIDENTIFIER + DECLARE @UserIdsForAlteredNotifications TABLE ( + UserId UNIQUEIDENTIFIER ); -- Update existing NotificationStatus as deleted UPDATE ns SET ns.DeletedDate = GETUTCDATE() - OUTPUT inserted.NotificationId INTO @AlteredNotifications + OUTPUT inserted.UserId INTO @UserIdsForAlteredNotifications FROM NotificationStatus ns INNER JOIN Notification n ON ns.NotificationId = n.Id WHERE n.TaskId = @TaskId @@ -22,7 +22,7 @@ BEGIN -- Insert NotificationStatus records for notifications that don't have one yet INSERT INTO NotificationStatus (NotificationId, UserId, DeletedDate) - OUTPUT inserted.NotificationId INTO @AlteredNotifications + OUTPUT inserted.UserId INTO @UserIdsForAlteredNotifications SELECT n.Id, @UserId, GETUTCDATE() FROM Notification n LEFT JOIN NotificationStatus ns @@ -30,9 +30,8 @@ BEGIN WHERE n.TaskId = @TaskId AND ns.NotificationId IS NULL; - -- Return all notifications that have been altered - SELECT n.* - FROM Notification n - INNER JOIN @AlteredNotifications a ON n.Id = a.NotificationId; + -- Return the user ids associated with the altered notifications + SELECT u.UserId + FROM @UserIdsForAlteredNotifications u; END GO diff --git a/util/Migrator/DbScripts/2025-05-29_02_Notification_MarkAsDeletedByTask.sql b/util/Migrator/DbScripts/2025-05-30_00_Notification_MarkAsDeletedByTask.sql similarity index 70% rename from util/Migrator/DbScripts/2025-05-29_02_Notification_MarkAsDeletedByTask.sql rename to util/Migrator/DbScripts/2025-05-30_00_Notification_MarkAsDeletedByTask.sql index 27f332baa8..fcf758605b 100644 --- a/util/Migrator/DbScripts/2025-05-29_02_Notification_MarkAsDeletedByTask.sql +++ b/util/Migrator/DbScripts/2025-05-30_00_Notification_MarkAsDeletedByTask.sql @@ -6,14 +6,14 @@ BEGIN SET NOCOUNT ON; -- Collect NotificationIds as they are altered - DECLARE @AlteredNotifications TABLE ( - NotificationId UNIQUEIDENTIFIER + DECLARE @UserIdsForAlteredNotifications TABLE ( + UserId UNIQUEIDENTIFIER ); -- Update existing NotificationStatus as deleted UPDATE ns SET ns.DeletedDate = GETUTCDATE() - OUTPUT inserted.NotificationId INTO @AlteredNotifications + OUTPUT inserted.UserId INTO @UserIdsForAlteredNotifications FROM NotificationStatus ns INNER JOIN Notification n ON ns.NotificationId = n.Id WHERE n.TaskId = @TaskId @@ -22,7 +22,7 @@ BEGIN -- Insert NotificationStatus records for notifications that don't have one yet INSERT INTO NotificationStatus (NotificationId, UserId, DeletedDate) - OUTPUT inserted.NotificationId INTO @AlteredNotifications + OUTPUT inserted.UserId INTO @UserIdsForAlteredNotifications SELECT n.Id, @UserId, GETUTCDATE() FROM Notification n LEFT JOIN NotificationStatus ns @@ -30,9 +30,8 @@ BEGIN WHERE n.TaskId = @TaskId AND ns.NotificationId IS NULL; - -- Return all notifications that have been altered - SELECT n.* - FROM Notification n - INNER JOIN @AlteredNotifications a ON n.Id = a.NotificationId; + -- Return the user ids associated with the altered notifications + SELECT u.UserId + FROM @UserIdsForAlteredNotifications u; END GO