diff --git a/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs b/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs index 75a897a2..0004238e 100644 --- a/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs +++ b/EnvelopeGenerator.Application/Contracts/IEnvelopeReceiverService.cs @@ -1,7 +1,7 @@ using DigitalData.Core.Abstractions.Application; using DigitalData.Core.DTO; using EnvelopeGenerator.Application.DTOs.EnvelopeReceiver; -using EnvelopeGenerator.Application.DTOs.Receiver; +using EnvelopeGenerator.Application.DTOs.Messaging; using EnvelopeGenerator.Domain.Entities; namespace EnvelopeGenerator.Application.Contracts @@ -30,5 +30,7 @@ namespace EnvelopeGenerator.Application.Contracts Task>> ReadByUsernameAsync(string username, int? min_status = null, int? max_status = null, params int[] ignore_statuses); Task> ReadLastUsedReceiverNameByMail(string mail); + + Task> SendSmsAsync(string envelopeReceiverId, string message); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Key.cs b/EnvelopeGenerator.Application/Key.cs index 4509d0ea..52f35d19 100644 --- a/EnvelopeGenerator.Application/Key.cs +++ b/EnvelopeGenerator.Application/Key.cs @@ -14,6 +14,7 @@ public static readonly string PossibleSecurityBreach = nameof(PossibleSecurityBreach); public static readonly string WrongEnvelopeReceiverId = nameof(WrongEnvelopeReceiverId); public static readonly string EnvelopeOrReceiverNonexists = nameof(EnvelopeOrReceiverNonexists); + public static readonly string PhoneNumberNonexists = nameof(PhoneNumberNonexists); public static readonly string Default = nameof(Default); } } \ No newline at end of file diff --git a/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs b/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs index 12b5ea11..0ab6eb7c 100644 --- a/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs +++ b/EnvelopeGenerator.Application/Services/EnvelopeReceiverService.cs @@ -9,6 +9,7 @@ using EnvelopeGenerator.Infrastructure.Contracts; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using EnvelopeGenerator.Extensions; +using EnvelopeGenerator.Application.DTOs.Messaging; namespace EnvelopeGenerator.Application.Services { @@ -16,10 +17,13 @@ namespace EnvelopeGenerator.Application.Services { private readonly IStringLocalizer _localizer; - public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IStringLocalizer localizer, IMapper mapper) + private readonly IMessagingService _messagingService; + + public EnvelopeReceiverService(IEnvelopeReceiverRepository repository, IStringLocalizer localizer, IMapper mapper, IMessagingService messagingService) : base(repository, mapper) { _localizer = localizer; + _messagingService = messagingService; } public async Task>> ReadBySignatureAsync(string signature, bool withEnvelope = false, bool withReceiver = true) @@ -135,5 +139,31 @@ namespace EnvelopeGenerator.Application.Services var er = await _repository.ReadLastByReceiver(mail); return er is null ? Result.Fail().Notice(LogLevel.None, Flag.NotFound) : Result.Success(er.Name); } + + public async Task> SendSmsAsync(string envelopeReceiverId, string message) + { + (string? uuid, string? signature) = envelopeReceiverId.DecodeEnvelopeReceiverId(); + + if (uuid is null || signature is null) + return Result.Fail() + .Message(_localizer[Key.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() + .Message(Key.EnvelopeReceiverNotFound); + + if (env_rcv.PhoneNumber is null) + return Result.Fail() + .Message(Key.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 _messagingService.SendSmsAsync(recipient: env_rcv.PhoneNumber, message: message); + + return Result.Success(res); + } } } \ No newline at end of file