mirror of
https://github.com/bitwarden/server.git
synced 2025-04-05 05:00:19 -05:00

* feat(innovation-archive): Controller Work - Added bulk endpoints for archive and feature flagged them. * feat(innovation-archive): Controller Work - Added in archive unarchive stored procedure. * test(innovation-archive): Controller Work - Got test logic working. --------- Co-authored-by: gbubemismith <gsmithwalter@gmail.com>
61 lines
2.1 KiB
C#
61 lines
2.1 KiB
C#
using Bit.Core.Enums;
|
|
using Bit.Core.Exceptions;
|
|
using Bit.Core.Platform.Push;
|
|
using Bit.Core.Services;
|
|
using Bit.Core.Vault.Entities;
|
|
using Bit.Core.Vault.Models.Data;
|
|
using Bit.Core.Vault.Repositories;
|
|
|
|
namespace Bit.Core.Vault.Commands.Interfaces;
|
|
|
|
public class UnarchiveCiphersCommand : IUnarchiveCiphersCommand
|
|
{
|
|
private readonly ICipherRepository _cipherRepository;
|
|
private readonly IEventService _eventService;
|
|
private readonly IPushNotificationService _pushService;
|
|
|
|
public UnarchiveCiphersCommand(
|
|
ICipherRepository cipherRepository,
|
|
IEventService eventService,
|
|
IPushNotificationService pushService
|
|
)
|
|
{
|
|
_cipherRepository = cipherRepository;
|
|
_eventService = eventService;
|
|
_pushService = pushService;
|
|
}
|
|
|
|
public async Task<ICollection<CipherOrganizationDetails>> UnarchiveManyAsync(IEnumerable<Guid> cipherIds, Guid unarchivingUserId)
|
|
{
|
|
var cipherIdEnumerable = cipherIds as Guid[] ?? cipherIds.ToArray();
|
|
if (cipherIds == null || cipherIdEnumerable.Length == 0)
|
|
{
|
|
throw new BadRequestException("No cipher ids provided.");
|
|
}
|
|
|
|
var cipherIdsSet = new HashSet<Guid>(cipherIdEnumerable);
|
|
|
|
var ciphers = await _cipherRepository.GetManyByUserIdAsync(unarchivingUserId);
|
|
var unarchivingCiphers = ciphers
|
|
.Where(c => cipherIdsSet.Contains(c.Id) && c.Edit)
|
|
.Select(CipherOrganizationDetails (c) => c).ToList();
|
|
|
|
var revisionDate = await _cipherRepository.UnarchiveAsync(unarchivingCiphers.Select(c => c.Id), unarchivingUserId);
|
|
|
|
var events = unarchivingCiphers.Select(c =>
|
|
{
|
|
c.RevisionDate = revisionDate;
|
|
c.ArchivedDate = null;
|
|
return new Tuple<Cipher, EventType, DateTime?>(c, EventType.Cipher_Unarchived, null);
|
|
});
|
|
foreach (var eventsBatch in events.Chunk(100))
|
|
{
|
|
await _eventService.LogCipherEventsAsync(eventsBatch);
|
|
}
|
|
|
|
await _pushService.PushSyncCiphersAsync(unarchivingUserId);
|
|
|
|
return unarchivingCiphers;
|
|
}
|
|
}
|