1
0
mirror of https://github.com/bitwarden/server.git synced 2025-06-07 19:50:32 -05:00

Only return UserIds from db layer

This commit is contained in:
Nick Krantz 2025-05-31 20:15:20 -05:00
parent 928bad3d0f
commit 8d40d66e63
No known key found for this signature in database
GPG Key ID: FF670021ABCAB82E
6 changed files with 28 additions and 25 deletions

View File

@ -39,7 +39,7 @@ public interface INotificationRepository : IRepository<Notification, Guid>
/// <param name="taskId">The unique identifier of the task.</param>
/// <param name="userId">User Id</param>
/// <returns>
/// 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.
/// </returns>
Task<IEnumerable<Notification>> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId);
Task<IEnumerable<Guid>> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId);
}

View File

@ -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);
}
}
}

View File

@ -57,11 +57,11 @@ public class NotificationRepository : Repository<Notification, Guid>, INotificat
};
}
public async Task<IEnumerable<Notification>> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId)
public async Task<IEnumerable<Guid>> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId)
{
await using var connection = new SqlConnection(ConnectionString);
var results = await connection.QueryAsync<Notification>(
var results = await connection.QueryAsync<Guid>(
"[dbo].[Notification_MarkAsDeletedByTask]",
new
{

View File

@ -75,7 +75,7 @@ public class NotificationRepository : Repository<Core.NotificationCenter.Entitie
};
}
public async Task<IEnumerable<Core.NotificationCenter.Entities.Notification>> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId)
public async Task<IEnumerable<Guid>> MarkNotificationsAsDeletedByTask(Guid taskId, Guid userId)
{
await using var scope = ServiceScopeFactory.CreateAsyncScope();
var dbContext = GetDatabaseContext(scope);
@ -116,6 +116,11 @@ public class NotificationRepository : Repository<Core.NotificationCenter.Entitie
await dbContext.SaveChangesAsync();
return notifications;
var userIds = notifications
.Select(n => n.UserId)
.Where(u => u.HasValue)
.ToList();
return (IEnumerable<Guid>)userIds;
}
}

View File

@ -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

View File

@ -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