using Bit.Api.Tools.Models.Request;
using Bit.Core.Entities;
using Bit.Core.Exceptions;
using Bit.Core.Tools.Entities;
using Bit.Core.Tools.Repositories;
using Bit.Core.Tools.Services;
namespace Bit.Api.KeyManagement.Validators;
///
/// Send implementation for
///
public class SendRotationValidator : IRotationValidator, IReadOnlyList>
{
private readonly ISendAuthorizationService _sendAuthorizationService;
private readonly ISendRepository _sendRepository;
///
/// Instantiates a new
///
/// Enables conversion of to
/// Retrieves all user s
public SendRotationValidator(ISendAuthorizationService sendAuthorizationService, ISendRepository sendRepository)
{
_sendAuthorizationService = sendAuthorizationService;
_sendRepository = sendRepository;
}
public async Task> ValidateAsync(User user, IEnumerable sends)
{
var result = new List();
var existingSends = await _sendRepository.GetManyByUserIdAsync(user.Id);
if (existingSends == null || existingSends.Count == 0)
{
return result;
}
foreach (var existing in existingSends)
{
var send = sends.FirstOrDefault(c => c.Id == existing.Id);
if (send == null)
{
throw new BadRequestException("All existing sends must be included in the rotation.");
}
result.Add(send.ToSend(existing, _sendAuthorizationService));
}
return result;
}
}