1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 09:32:48 -05:00
Files
bitwarden/util/Migrator/DbScripts/2025-05-30_00_Notification_MarkAsDeletedByTask.sql
Nick Krantz 69b7600eab [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`
2025-06-27 16:04:47 -05:00

36 lines
1.1 KiB
Transact-SQL

CREATE OR ALTER PROCEDURE [dbo].[Notification_MarkAsDeletedByTask]
@TaskId UNIQUEIDENTIFIER
AS
BEGIN
SET NOCOUNT ON;
-- Collect UserIds as they are altered
DECLARE @UserIdsForAlteredNotifications TABLE (
UserId UNIQUEIDENTIFIER
);
-- Update existing NotificationStatus as deleted
UPDATE ns
SET ns.DeletedDate = GETUTCDATE()
OUTPUT inserted.UserId INTO @UserIdsForAlteredNotifications
FROM NotificationStatus ns
INNER JOIN Notification n ON ns.NotificationId = n.Id
WHERE n.TaskId = @TaskId
AND ns.DeletedDate IS NULL;
-- Insert NotificationStatus records for notifications that don't have one yet
INSERT INTO NotificationStatus (NotificationId, UserId, DeletedDate)
OUTPUT inserted.UserId INTO @UserIdsForAlteredNotifications
SELECT n.Id, n.UserId, GETUTCDATE()
FROM Notification n
LEFT JOIN NotificationStatus ns
ON n.Id = ns.NotificationId
WHERE n.TaskId = @TaskId
AND ns.NotificationId IS NULL;
-- Return the UserIds associated with the altered notifications
SELECT u.UserId
FROM @UserIdsForAlteredNotifications u;
END
GO