public async Task <CommandHandlingResult> Handle(SendSmsCommand command, IEventPublisher eventPublisher) { var message = await _smsRepository.GetAsync(command.Id); var msg = new { Phone = command.Phone.SanitizePhone(), command.Id, command.Provider, command.CountryCode }; if (message == null) { _log.WriteInfo(nameof(SendSmsCommand), msg, $"Sms message with messageId = {command.Id} not found"); return(CommandHandlingResult.Ok()); } if (message.IsExpired(_smsSettings.SmsRetryTimeout)) { await _smsRepository.DeleteAsync(message.Id, message.MessageId); _log.WriteInfo(nameof(SendSmsCommand), msg, "Sms message expired and has been deleted"); return(CommandHandlingResult.Ok()); } var sender = _smsSenderFactory.GetSender(command.Provider); _log.WriteInfo(nameof(SendSmsCommand), msg, "Sending sms"); try { string messageId = await sender.SendSmsAsync(command.Phone, command.Message, command.CountryCode); if (!string.IsNullOrEmpty(messageId)) { await _smsRepository.SetMessageIdAsync(messageId, command.Id); _log.WriteInfo(nameof(SendSmsCommand), new { command.Id, MessageId = messageId }, "Message has been sent"); } else { await _smsRepository.DeleteAsync(command.Id, messageId); _log.WriteInfo(nameof(SendSmsCommand), new { command.Id }, "Sms message has been deleted"); } } catch (Exception) { await _smsProviderInfoRepository.AddAsync(command.Provider, command.CountryCode, SmsDeliveryStatus.Failed); return(CommandHandlingResult.Fail(_smsSettings.SmsSendDelay)); } return(CommandHandlingResult.Ok()); }