mirror of
https://github.com/bitwarden/server
synced 2025-12-11 22:03:38 +00:00
[PM-20041] Deleting Notifications when Task is completed (#5896)
* mark all notifications associated with a security task as deleted when the task is completed * fix spelling * formatting * refactor "Active" to "NonDeleted" * refactor "Active" to "NonDeleted" for stored procedure * only send notifications per user for each notification * move notification status updates into the DB layer to save on multiple queries and insertions from the C# * Only return UserIds from db layer * omit userId from `MarkTaskAsCompletedCommand` query. The userId from the notification will be used * update UserIds * consistency in comments regarding `taskId` and `UserId`
This commit is contained in:
@@ -74,4 +74,53 @@ public class NotificationRepository : Repository<Core.NotificationCenter.Entitie
|
||||
ContinuationToken = results.Count < pageOptions.PageSize ? null : (pageNumber + 1).ToString()
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Guid>> MarkNotificationsAsDeletedByTask(Guid taskId)
|
||||
{
|
||||
await using var scope = ServiceScopeFactory.CreateAsyncScope();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var notifications = await dbContext.Notifications
|
||||
.Where(n => n.TaskId == taskId)
|
||||
.ToListAsync();
|
||||
|
||||
var notificationIds = notifications.Select(n => n.Id).ToList();
|
||||
|
||||
var statuses = await dbContext.Set<NotificationStatus>()
|
||||
.Where(ns => notificationIds.Contains(ns.NotificationId))
|
||||
.ToListAsync();
|
||||
|
||||
var now = DateTime.UtcNow;
|
||||
|
||||
// Update existing statuses and add missing ones
|
||||
foreach (var notification in notifications)
|
||||
{
|
||||
var status = statuses.FirstOrDefault(s => s.NotificationId == notification.Id);
|
||||
if (status != null)
|
||||
{
|
||||
if (status.DeletedDate == null)
|
||||
{
|
||||
status.DeletedDate = now;
|
||||
}
|
||||
}
|
||||
else if (notification.UserId.HasValue)
|
||||
{
|
||||
dbContext.Set<NotificationStatus>().Add(new NotificationStatus
|
||||
{
|
||||
NotificationId = notification.Id,
|
||||
UserId = (Guid)notification.UserId,
|
||||
DeletedDate = now
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
var userIds = notifications
|
||||
.Select(n => n.UserId)
|
||||
.Where(u => u.HasValue)
|
||||
.ToList();
|
||||
|
||||
return (IEnumerable<Guid>)userIds;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user