public async Task <int> ResendSmsMessageAsync() { var dateTime = _dateTimeProvider.OffsetNow.AddMinutes(-1); var messages = _repository.GetAll() .Where(x => x.SentDateTime == null && x.RetriedCount < 3) .Where(x => (x.RetriedCount == 0 && x.CreatedDateTime < dateTime) || (x.RetriedCount != 0 && x.UpdatedDateTime < dateTime)) .ToList(); if (messages.Any()) { foreach (var sms in messages) { await _smsMessageCreatedEventSender.SendAsync(new SmsMessageCreatedEvent { Id = sms.Id }); sms.RetriedCount++; await _repository.AddOrUpdateAsync(sms); await _repository.UnitOfWork.SaveChangesAsync(); } } else { _logger.LogInformation("No SMS to resend."); } return(messages.Count); }