108 lines
5.6 KiB
C#
108 lines
5.6 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Application;
|
|
using DigitalData.Core.DTO;
|
|
using EnvelopeGenerator.Application.Contracts;
|
|
using EnvelopeGenerator.Application.DTOs;
|
|
using EnvelopeGenerator.Application.Resources;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Infrastructure.Contracts;
|
|
using Microsoft.Extensions.Localization;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace EnvelopeGenerator.Application.Services
|
|
{
|
|
public class EnvelopeReceiverService : BasicCRUDService<IEnvelopeReceiverRepository, EnvelopeReceiverDto, EnvelopeReceiver, int>, IEnvelopeReceiverService
|
|
{
|
|
public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper)
|
|
: base(repository, localizer, mapper)
|
|
{
|
|
}
|
|
|
|
public async Task<DataResult<IEnumerable<EnvelopeReceiverDto>>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true)
|
|
{
|
|
var env_rcvs = await _repository.ReadBySignatureAsync(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
return Result.Success(_mapper.MapOrThrow<IEnumerable<EnvelopeReceiverDto>>(env_rcvs));
|
|
}
|
|
|
|
public async Task<DataResult<IEnumerable<EnvelopeReceiverDto>>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false)
|
|
{
|
|
var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
return Result.Success(_mapper.MapOrThrow<IEnumerable<EnvelopeReceiverDto>>(env_rcvs));
|
|
}
|
|
|
|
public async Task<DataResult<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 Result.Fail<EnvelopeReceiverDto>()
|
|
.Message(Key.EnvelopeReceiverNotFound);
|
|
|
|
return Result.Success(_mapper.MapOrThrow<EnvelopeReceiverDto>(env_rcv));
|
|
}
|
|
|
|
public async Task<DataResult<EnvelopeReceiverDto>> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Result.Fail<EnvelopeReceiverDto>()
|
|
.Message(_localizer[Key.WrongEnvelopeReceiverId])
|
|
.Notice(LogLevel.Warning, (uuid, signature).ToTitle())
|
|
.Notice(LogLevel.Warning, EnvelopeFlag.WrongEnvelopeReceiverId)
|
|
.Notice(LogLevel.Warning, Flag.PossibleSecurityBreach);
|
|
|
|
return await ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
}
|
|
|
|
public async Task<DataResult<bool>> VerifyAccessCodeAsync(string uuid, string signature, string accessCode)
|
|
{
|
|
var er = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature);
|
|
|
|
if (er is null)
|
|
return Result.Fail<bool>()
|
|
.Message(_localizer[Key.EnvelopeOrReceiverNonexists])
|
|
.Notice(LogLevel.Warning, (uuid, signature).ToTitle())
|
|
.Notice(LogLevel.Warning, EnvelopeFlag.EnvelopeOrReceiverNonexists)
|
|
.Notice(LogLevel.Warning, Flag.PossibleDataIntegrityIssue);
|
|
|
|
var actualAccessCode = er.AccessCode;
|
|
|
|
if (actualAccessCode is null)
|
|
return Result.Fail<bool>()
|
|
.Message(_localizer[Key.AccessCodeNull])
|
|
.Notice(LogLevel.Critical, (uuid, signature).ToTitle())
|
|
.Notice(LogLevel.Critical, EnvelopeFlag.AccessCodeNull)
|
|
.Notice(LogLevel.Critical, Flag.DataIntegrityIssue);
|
|
|
|
else if (accessCode != actualAccessCode)
|
|
return Result.Success(false).Message(_localizer[Key.WrongAccessCode]);
|
|
else
|
|
return Result.Success(true);
|
|
}
|
|
|
|
public async Task<DataResult<bool>> VerifyAccessCodeAsync(string envelopeReceiverId, string accessCode)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Result.Fail<bool>()
|
|
.Message(Key.WrongEnvelopeReceiverId)
|
|
.Notice(LogLevel.Critical, EnvelopeFlag.WrongEnvelopeReceiverId)
|
|
.Notice(LogLevel.Critical, Flag.SecurityBreach)
|
|
.Notice(LogLevel.Critical, "Attempt to verify access code detected. Such actions are generally not initiated by well-intentioned users. Potential security breach suspected. Immediate investigation required.");
|
|
|
|
return await VerifyAccessCodeAsync(uuid: uuid, signature: signature, accessCode: accessCode);
|
|
}
|
|
|
|
public async Task<DataResult<bool>> IsExisting(string envelopeReceiverId)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Result.Fail<bool>().Notice(LogLevel.Warning, EnvelopeFlag.NonDecodableEnvelopeReceiverId, "In IsExisting(string envelopeReceiverId)");
|
|
|
|
int count = await _repository.CountAsync(uuid:uuid, signature:signature);
|
|
return Result.Success(count > 0);
|
|
}
|
|
}
|
|
} |