110 lines
5.8 KiB
C#
110 lines
5.8 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Application;
|
|
using DigitalData.Core.Contracts.Application;
|
|
using DigitalData.Core.Contracts.CultureServices;
|
|
using EnvelopeGenerator.Application.Contracts;
|
|
using EnvelopeGenerator.Application.DTOs;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Infrastructure.Contracts;
|
|
|
|
namespace EnvelopeGenerator.Application.Services
|
|
{
|
|
public class EnvelopeReceiverService : BasicCRUDService<IEnvelopeReceiverRepository, EnvelopeReceiverDto, EnvelopeReceiver, int>, IEnvelopeReceiverService
|
|
{
|
|
public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IKeyTranslationService translationService, IMapper mapper)
|
|
: base(repository, translationService, mapper)
|
|
{
|
|
}
|
|
|
|
public async Task<IServiceResult<IEnumerable<EnvelopeReceiverDto>>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true)
|
|
{
|
|
var env_rcvs = await _repository.ReadBySignatureAsync(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
return Successful(_mapper.MapOrThrow<IEnumerable<EnvelopeReceiverDto>>(env_rcvs));
|
|
}
|
|
|
|
public async Task<IServiceResult<IEnumerable<EnvelopeReceiverDto>>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false)
|
|
{
|
|
var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
return Successful(_mapper.MapOrThrow<IEnumerable<EnvelopeReceiverDto>>(env_rcvs));
|
|
}
|
|
|
|
public async Task<IServiceResult<EnvelopeReceiverDto>> ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true)
|
|
{
|
|
var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
if (env_rcv is null)
|
|
return Failed<EnvelopeReceiverDto>()
|
|
.WithClientMessageKey(MessageKey.EnvelopeReceiverNotFound);
|
|
|
|
return Successful(_mapper.MapOrThrow<EnvelopeReceiverDto>(env_rcv));
|
|
}
|
|
|
|
public async Task<IServiceResult<EnvelopeReceiverDto>> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Failed<EnvelopeReceiverDto>()
|
|
.WithClientMessageKey(MessageKey.WrongEnvelopeReceiverId2Client)
|
|
.WithWarningMessage((uuid, signature).ToTitle())
|
|
.WithWarningMessageKey(MessageKey.WrongEnvelopeReceiverId2Logger)
|
|
.WithWarningMessageKey(MessageKey.PossibleSecurityBreach)
|
|
.WithFlag(Flag.PossibleSecurityBreach);
|
|
|
|
return await ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
}
|
|
|
|
public async Task<IServiceResult<bool>> VerifyAccessCodeAsync(string uuid, string signature, string accessCode)
|
|
{
|
|
var er = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature);
|
|
|
|
if (er is null)
|
|
return Failed<bool>()
|
|
.WithClientMessageKey(MessageKey.EnvelopeOrReceiverNonexists)
|
|
.WithWarningMessage((uuid, signature).ToTitle())
|
|
.WithWarningMessageKey(MessageKey.EnvelopeOrReceiverNonexists)
|
|
.WithWarningMessageKey(MessageKey.PossibleDataIntegrityIssue)
|
|
.WithFlag(MessageKey.PossibleDataIntegrityIssue);
|
|
|
|
var actualAccessCode = er.AccessCode;
|
|
|
|
if (actualAccessCode is null)
|
|
return Failed<bool>()
|
|
.WithClientMessageKey(MessageKey.AccessCodeNull2Client)
|
|
.WithCriticalMessage((uuid, signature).ToTitle())
|
|
.WithCriticalMessageKey(MessageKey.AccessCodeNull2Logger)
|
|
.WithCriticalMessageKey(MessageKey.DataIntegrityIssue)
|
|
.WithFlag(Flag.DataIntegrityIssue);
|
|
|
|
else if(accessCode != actualAccessCode)
|
|
return Successful(false).WithClientMessageKey(MessageKey.WrongAccessCode);
|
|
else
|
|
return Successful(true);
|
|
}
|
|
|
|
public async Task<IServiceResult<bool>> VerifyAccessCodeAsync(string envelopeReceiverId, string accessCode)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Failed<bool>()
|
|
.WithClientMessageKey(MessageKey.WrongEnvelopeReceiverId2Client)
|
|
.WithCriticalMessageKey(MessageKey.WrongEnvelopeReceiverId2Logger)
|
|
.WithCriticalMessageKey(MessageKey.SecurityBreach)
|
|
.WithCriticalMessage("Attempt to verify access code detected. Such actions are generally not initiated by well-intentioned users. Potential security breach suspected. Immediate investigation required.")
|
|
.WithFlag(Flag.SecurityBreach);
|
|
|
|
return await VerifyAccessCodeAsync(uuid: uuid, signature: signature, accessCode: accessCode);
|
|
}
|
|
|
|
public async Task<IServiceResult<bool>> IsExisting(string envelopeReceiverId)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Failed(false).WithFlag(EnvelopeFlag.NonDecodableEnvelopeReceiverId);
|
|
|
|
int count = await _repository.CountAsync(uuid:uuid, signature:signature);
|
|
return Successful(count > 0);
|
|
}
|
|
}
|
|
} |