319 lines
14 KiB
C#
319 lines
14 KiB
C#
using AutoMapper;
|
|
using DigitalData.Core.Application;
|
|
using DigitalData.Core.Abstraction.Application.DTO;
|
|
using EnvelopeGenerator.Application.Resources;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using Microsoft.Extensions.Localization;
|
|
using Microsoft.Extensions.Logging;
|
|
using EnvelopeGenerator.Application.Common.Extensions;
|
|
using EnvelopeGenerator.Application.Envelopes.Queries;
|
|
using EnvelopeGenerator.Application.Receivers.Queries;
|
|
using EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiver;
|
|
using EnvelopeGenerator.Application.Common.Dto.Messaging;
|
|
using EnvelopeGenerator.Application.Common;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Repositories;
|
|
using EnvelopeGenerator.Application.Common.Interfaces.Services;
|
|
|
|
namespace EnvelopeGenerator.Application.Services;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Obsolete("Use MediatR")]
|
|
public class EnvelopeReceiverService : BasicCRUDService<IEnvelopeReceiverRepository, EnvelopeReceiverDto, EnvelopeReceiver, (int Envelope, int Receiver)>, IEnvelopeReceiverService
|
|
{
|
|
private readonly IStringLocalizer<Resource> _localizer;
|
|
|
|
private readonly ISmsSender _smsSender;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
/// <param name="localizer"></param>
|
|
/// <param name="mapper"></param>
|
|
/// <param name="smsSender"></param>
|
|
public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IStringLocalizer<Resource> localizer, IMapper mapper, ISmsSender smsSender)
|
|
: base(repository, mapper)
|
|
{
|
|
_localizer = localizer;
|
|
_smsSender = smsSender;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="signature"></param>
|
|
/// <param name="withEnvelope"></param>
|
|
/// <param name="withReceiver"></param>
|
|
/// <param name="readOnly"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<IEnumerable<EnvelopeReceiverDto>>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true, bool readOnly = true)
|
|
{
|
|
var env_rcvs = await _repository.ReadBySignatureAsync(signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
|
|
return Result.Success(_mapper.Map<IEnumerable<EnvelopeReceiverDto>>(env_rcvs));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="uuid"></param>
|
|
/// <param name="withEnvelope"></param>
|
|
/// <param name="withReceiver"></param>
|
|
/// <param name="readOnly"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<IEnumerable<EnvelopeReceiverDto>>> ReadByUuidAsync(string uuid, bool withEnvelope = true, bool withReceiver = false, bool readOnly = true)
|
|
{
|
|
var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
|
|
return Result.Success(_mapper.Map<IEnumerable<EnvelopeReceiverDto>>(env_rcvs));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="uuid"></param>
|
|
/// <param name="withEnvelope"></param>
|
|
/// <param name="withReceiver"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<IEnumerable<string?>>> ReadAccessCodeByUuidAsync(string uuid, bool withEnvelope = false, bool withReceiver = true)
|
|
{
|
|
var env_rcvs = await _repository.ReadByUuidAsync(uuid: uuid, withEnvelope: withEnvelope, withReceiver: withReceiver);
|
|
if( env_rcvs is null || !env_rcvs.Any())
|
|
return Result.Fail<IEnumerable<string?>>()
|
|
.Message(_localizer.EnvelopeReceiverNotFound());
|
|
return Result.Success(env_rcvs.Select(er => er.AccessCode) ?? Enumerable.Empty<string?>());
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="uuid"></param>
|
|
/// <param name="signature"></param>
|
|
/// <param name="withEnvelope"></param>
|
|
/// <param name="withReceiver"></param>
|
|
/// <param name="readOnly"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<EnvelopeReceiverDto>> ReadByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
|
|
{
|
|
var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
|
|
if (env_rcv is null)
|
|
return Result.Fail<EnvelopeReceiverDto>()
|
|
.Message("EnvelopeReceiverNotFound");
|
|
|
|
return Result.Success(_mapper.Map<EnvelopeReceiverDto>(env_rcv));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="uuid"></param>
|
|
/// <param name="signature"></param>
|
|
/// <param name="withEnvelope"></param>
|
|
/// <param name="withReceiver"></param>
|
|
/// <param name="readOnly"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<EnvelopeReceiverSecretDto>> ReadWithSecretByUuidSignatureAsync(string uuid, string signature, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
|
|
{
|
|
var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: withEnvelope, withReceiver: withReceiver, readOnly: readOnly);
|
|
if (env_rcv is null)
|
|
return Result.Fail<EnvelopeReceiverSecretDto>()
|
|
.Message("EnvelopeReceiverNotFound");
|
|
|
|
return Result.Success(_mapper.Map<EnvelopeReceiverSecretDto>(env_rcv));
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeReceiverId"></param>
|
|
/// <param name="withEnvelope"></param>
|
|
/// <param name="withReceiver"></param>
|
|
/// <param name="readOnly"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<EnvelopeReceiverDto>> ReadByEnvelopeReceiverIdAsync(string envelopeReceiverId, bool withEnvelope = true, bool withReceiver = true, bool readOnly = true)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Result.Fail<EnvelopeReceiverDto>()
|
|
.Message(_localizer.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, readOnly: readOnly);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="uuid"></param>
|
|
/// <param name="signature"></param>
|
|
/// <param name="accessCode"></param>
|
|
/// <returns></returns>
|
|
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.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.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.WrongAccessCode());
|
|
else
|
|
return Result.Success(true);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeReceiverId"></param>
|
|
/// <param name="accessCode"></param>
|
|
/// <returns></returns>
|
|
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("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);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeReceiverId"></param>
|
|
/// <returns></returns>
|
|
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);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeId"></param>
|
|
/// <param name="receiverId"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<string>> ReadAccessCodeByIdAsync(int envelopeId, int receiverId)
|
|
{
|
|
var code = await _repository.ReadAccessCodeByIdAsync(envelopeId: envelopeId, receiverId: receiverId);
|
|
return code is null ?
|
|
Result.Fail<string>().Notice(LogLevel.Error, Flag.DataIntegrityIssue, $"Access code is null. Envelope ID is {envelopeId} and receiver ID {receiverId}")
|
|
: Result.Success(code);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="username"></param>
|
|
/// <param name="min_status"></param>
|
|
/// <param name="max_status"></param>
|
|
/// <param name="envelopeQuery"></param>
|
|
/// <param name="receiverQuery"></param>
|
|
/// <param name="ignore_statuses"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<IEnumerable<EnvelopeReceiverDto>>> ReadByUsernameAsync(string username, EnvelopeStatus? min_status = null, EnvelopeStatus? max_status = null, ReadEnvelopeQuery? envelopeQuery = null, ReadReceiverQuery? receiverQuery = null, params EnvelopeStatus[] ignore_statuses)
|
|
{
|
|
var er_list = await _repository.ReadByUsernameAsync(username: username, min_status: min_status, max_status: max_status, ignore_statuses: ignore_statuses);
|
|
|
|
if(envelopeQuery?.Id is int eId)
|
|
er_list = er_list.Where(er => er.EnvelopeId == eId);
|
|
|
|
if (envelopeQuery?.Uuid is string uuid)
|
|
er_list = er_list.Where(er => er.Envelope?.Uuid == uuid);
|
|
|
|
if (envelopeQuery?.Status?.Include?.FirstOrDefault() is EnvelopeStatus status)
|
|
er_list = er_list.Where(er => er.Envelope?.Status == status);
|
|
|
|
if(receiverQuery?.Id is int id)
|
|
er_list = er_list.Where(er => er.Receiver?.Id == id);
|
|
|
|
if (receiverQuery?.Signature is string signature)
|
|
er_list = er_list.Where(er => er.Receiver?.Signature == signature);
|
|
|
|
var dto_list = _mapper.Map<IEnumerable<EnvelopeReceiverDto>>(er_list);
|
|
return Result.Success(dto_list);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="mail"></param>
|
|
/// <param name="id"></param>
|
|
/// <param name="signature"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<string>> ReadLastUsedReceiverNameByMailAsync(string? mail = null, int? id = null, string? signature = null)
|
|
{
|
|
var er = await _repository.ReadLastByReceiverAsync(mail, id, signature);
|
|
return er is null ? Result.Fail<string>().Notice(LogLevel.None, Flag.NotFound) : Result.Success(er.Name);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="envelopeReceiverId"></param>
|
|
/// <param name="message"></param>
|
|
/// <returns></returns>
|
|
public async Task<DataResult<SmsResponse>> SendSmsAsync(string envelopeReceiverId, string message)
|
|
{
|
|
(string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId();
|
|
|
|
if (uuid is null || signature is null)
|
|
return Result.Fail<SmsResponse>()
|
|
.Message(_localizer.WrongEnvelopeReceiverId())
|
|
.Notice(LogLevel.Warning, (uuid, signature).ToTitle())
|
|
.Notice(LogLevel.Warning, EnvelopeFlag.WrongEnvelopeReceiverId)
|
|
.Notice(LogLevel.Warning, Flag.PossibleSecurityBreach);
|
|
|
|
var env_rcv = await _repository.ReadByUuidSignatureAsync(uuid: uuid, signature: signature, withEnvelope: false, withReceiver: false);
|
|
if (env_rcv is null)
|
|
return Result.Fail<SmsResponse>()
|
|
.Message("EnvelopeReceiverNotFound");
|
|
|
|
if (env_rcv.PhoneNumber is null)
|
|
return Result.Fail<SmsResponse>()
|
|
.Message("PhoneNumberNonexists")
|
|
.Notice(LogLevel.Error, Flag.NotFound, $"An attempt was made to send sms to the user whose phone number is null. Envelope recipient ID is {envelopeReceiverId}, UUID is {uuid} and signature is {signature}.");
|
|
|
|
var res = await _smsSender.SendSmsAsync(recipient: env_rcv.PhoneNumber, message: message);
|
|
|
|
return Result.Success(res);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="uuid"></param>
|
|
/// <returns></returns>
|
|
/// <exception cref="NotImplementedException"></exception>
|
|
public Task<DataResult<IEnumerable<EnvelopeReceiverSecretDto>>> ReadWithSecretByUuidAsync(string uuid)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
} |