1
0
mirror of https://github.com/bitwarden/server synced 2025-12-31 23:53:17 +00:00
Files
server/src/Core/Vault/Commands/MarkTaskAsCompletedCommand.cs
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

58 lines
2.0 KiB
C#

using Bit.Core.Context;
using Bit.Core.Exceptions;
using Bit.Core.Utilities;
using Bit.Core.Vault.Authorization.SecurityTasks;
using Bit.Core.Vault.Commands.Interfaces;
using Bit.Core.Vault.Enums;
using Bit.Core.Vault.Repositories;
using Microsoft.AspNetCore.Authorization;
namespace Bit.Core.Vault.Commands;
public class MarkTaskAsCompletedCommand : IMarkTaskAsCompleteCommand
{
private readonly ISecurityTaskRepository _securityTaskRepository;
private readonly IAuthorizationService _authorizationService;
private readonly ICurrentContext _currentContext;
private readonly IMarkNotificationsForTaskAsDeletedCommand _markNotificationsForTaskAsDeletedAsync;
public MarkTaskAsCompletedCommand(
ISecurityTaskRepository securityTaskRepository,
IAuthorizationService authorizationService,
ICurrentContext currentContext,
IMarkNotificationsForTaskAsDeletedCommand markNotificationsForTaskAsDeletedAsync)
{
_securityTaskRepository = securityTaskRepository;
_authorizationService = authorizationService;
_currentContext = currentContext;
_markNotificationsForTaskAsDeletedAsync = markNotificationsForTaskAsDeletedAsync;
}
/// <inheritdoc />
public async Task CompleteAsync(Guid taskId)
{
if (!_currentContext.UserId.HasValue)
{
throw new NotFoundException();
}
var task = await _securityTaskRepository.GetByIdAsync(taskId);
if (task is null)
{
throw new NotFoundException();
}
await _authorizationService.AuthorizeOrThrowAsync(_currentContext.HttpContext.User, task,
SecurityTaskOperations.Update);
task.Status = SecurityTaskStatus.Completed;
task.RevisionDate = DateTime.UtcNow;
await _securityTaskRepository.ReplaceAsync(task);
// Mark all notifications related to this task as deleted
await _markNotificationsForTaskAsDeletedAsync.MarkAsDeletedAsync(taskId);
}
}