Exemplo n.º 1
0
        public async Task SendEmailMessageAsync(Guid id)
        {
            var emailMessage = _repository.GetAll().FirstOrDefault(x => x.Id == id);

            if (emailMessage != null && !emailMessage.SentDateTime.HasValue)
            {
                try
                {
                    await _emailNotification.SendAsync(new DTOs.EmailMessageDTO
                    {
                        From    = emailMessage.From,
                        Tos     = emailMessage.Tos,
                        CCs     = emailMessage.CCs,
                        BCCs    = emailMessage.BCCs,
                        Subject = emailMessage.Subject,
                        Body    = emailMessage.Body,
                    });

                    _repository.UpdateSent(emailMessage.Id);
                }
                catch (Exception ex)
                {
                    _repository.UpdateFailed(emailMessage.Id, Environment.NewLine + Environment.NewLine + ex.ToString());
                }
            }
        }
Exemplo n.º 2
0
        public async Task SendEmailMessageAsync(Guid id)
        {
            var emailMessage = _repository.GetAll().FirstOrDefault(x => x.Id == id);

            if (emailMessage != null && !emailMessage.SentDateTime.HasValue)
            {
                try
                {
                    await _emailNotification.SendAsync(emailMessage);

                    _repository.UpdateSent(emailMessage.Id);
                }
                catch (Exception ex)
                {
                    _repository.UpdateFailed(emailMessage.Id, Environment.NewLine + Environment.NewLine + ex.ToString());
                }
            }
        }
Exemplo n.º 3
0
        public async Task <IActionResult> OnPostAsync()
        {
            var emailSent = await _emailNotification.SendAsync(
                $"[CryptoDev-Mail] {ContactViewModel.Subject}",
                $"{ContactViewModel.Email} pisze: {ContactViewModel.Message}",
                $"{ContactViewModel.Email} pisze: {ContactViewModel.Message}",
                new string[] { "*****@*****.**" }
                );

            if (emailSent.IsSuccess)
            {
                _logger.LogInformation($"{emailSent.Message}");

                return(RedirectToPage("/ThankYou", "MailSent"));
            }
            else
            {
                _logger.LogError($"Error: {emailSent.Message} for user: {ContactViewModel.Email}.");

                return(RedirectToPage("/Error", "UserError", new { message = emailSent.Message }));
            }
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Index(ContactViewModel model)
        {
            var emailSent = await _emailNotification.SendAsync(
                $"[CryptoDev-Mail] {model.Subject}",
                $"{model.UserEmail} pisze: {model.EmailContent}",
                $"{model.UserEmail} pisze: {model.EmailContent}",
                new string[] { "*****@*****.**" }
                );

            if (emailSent.IsSuccess)
            {
                _logger.LogInformation($"{emailSent.Message}");

                return(RedirectToAction("Student", "Home", new { userGuid = model.UserGuid }));
            }
            else
            {
                _logger.LogError($"Error: {emailSent.Message} for user: {model.UserEmail}.");

                return(View(new ContactViewModel {
                    Message = emailSent.Message
                }));
            }
        }
        public async Task <int> SendEmailMessagesAsync()
        {
            var deplayedTimes = new[]
            {
                TimeSpan.FromMinutes(1),
                TimeSpan.FromMinutes(2),
                TimeSpan.FromMinutes(3),
                TimeSpan.FromMinutes(5),
                TimeSpan.FromMinutes(8),
                TimeSpan.FromMinutes(13),
                TimeSpan.FromMinutes(21),
                TimeSpan.FromMinutes(34),
                TimeSpan.FromMinutes(55),
                TimeSpan.FromMinutes(89),
            };

            var dateTime            = _dateTimeProvider.OffsetNow;
            var defaultAttemptCount = 5;

            var messages = _repository.GetAll()
                           .Where(x => x.SentDateTime == null)
                           .Where(x => x.ExpiredDateTime == null || x.ExpiredDateTime > dateTime)
                           .Where(x => (x.MaxAttemptCount == 0 && x.AttemptCount < defaultAttemptCount) || x.AttemptCount < x.MaxAttemptCount)
                           .Where(x => x.NextAttemptDateTime == null || x.NextAttemptDateTime <= dateTime)
                           .ToList();

            if (messages.Any())
            {
                foreach (var email in messages)
                {
                    string log = Environment.NewLine + Environment.NewLine
                                 + $"[{_dateTimeProvider.OffsetNow.ToString(CultureInfo.InvariantCulture)}] ";
                    try
                    {
                        await _emailNotification.SendAsync(new DTOs.EmailMessageDTO
                        {
                            From    = email.From,
                            Tos     = email.Tos,
                            CCs     = email.CCs,
                            BCCs    = email.BCCs,
                            Subject = email.Subject,
                            Body    = email.Body,
                        });

                        email.SentDateTime = _dateTimeProvider.OffsetNow;
                        email.Log         += log + "Succeed.";
                    }
                    catch (Exception ex)
                    {
                        email.Log += log + ex.ToString();
                        email.NextAttemptDateTime = _dateTimeProvider.OffsetNow + deplayedTimes[email.AttemptCount];
                    }

                    email.AttemptCount   += 1;
                    email.Log             = email.Log.Trim();
                    email.UpdatedDateTime = _dateTimeProvider.OffsetNow;

                    if (email.MaxAttemptCount == 0)
                    {
                        email.MaxAttemptCount = defaultAttemptCount;
                    }

                    await _repository.UnitOfWork.SaveChangesAsync();
                }
            }
            else
            {
                _logger.LogInformation("No email to send.");
            }

            return(messages.Count);
        }
Exemplo n.º 6
0
        public async Task OnPost()
        {
            var callback = await RequestExt.GetObject <PaymentViewModel>(Request.Body);

            if (!(callback is null))
            {
                var added = await _dataRepository.CreateOrderHistoryAsync(
                    new Order
                {
                    OrderMnemonic = callback.Data.Id,
                    Status        = callback.Data.Status,
                    Email         = callback.Data.BuyerFields.BuyerEmail,
                    BtcPrice      = callback.Data.BtcPaid,
                    Rate          = callback.Data.Rate.ToString()
                });

                if (added == 1)
                {
                    var msg = $"Order {callback.Data.Id} ({callback.Data.Status}) added for {callback.Data.BuyerFields.BuyerEmail}";

                    _logger.LogInformation(msg);

                    _pushoverNotification.Send("GDI Order (new)", $"{msg}");

                    if (callback.Data.Status == "paid")
                    {
                        var user = await _dataRepository.GetUserAsync(callback.Data.BuyerFields.BuyerEmail);

                        if (!(user is null))
                        {
                            var addUserToCourse = await _dataRepository.AddUserToCourseAsync(
                                new UserCourse
                            {
                                UserId   = user.UserId,
                                CourseId = 1     // GDI only now
                            });

                            _pushoverNotification.Send("GDI Order (paid)", $"{msg}");

                            if (addUserToCourse == 1)
                            {
                                var link = $"https://app.cryptodev.tv/student/{user.Guid}";

                                var email = await _emailNotification.SendAsync(
                                    "GitHub dla Inwestorów już dostępny dla Ciebie",
                                    $"Dziękuje za zakup. Kurs dostępny tutaj: {link}",
                                    $"Dziękuje za zakup. Kurs dostępny <a href='{link}'>tutaj</a>",
                                    new string[] { user.Email });

                                if (email.IsSuccess)
                                {
                                    _pushoverNotification.Send("GDI Order (sent)", $"{msg}");
                                }
                                else
                                {
                                    _logger.LogWarning($"{email.Message}");

                                    _pushoverNotification.Send("GDI Error (mail)", $"{email.Message}");
                                }
                            }
                        }
                        else
                        {
                            var err1 = $"User {callback.Data.BuyerFields.BuyerEmail} not assigned for course";

                            _logger.LogWarning($"{err1}");

                            _pushoverNotification.Send("GDI Error (assigned)", $"{err1}");
                        }
                    }
                }
        public async Task <int> SendEmailMessagesAsync()
        {
            var circuit = _circuitBreakerManager.GetCircuitBreaker("EmailService", TimeSpan.FromMinutes(1));

            circuit.EnsureOkStatus();

            var deplayedTimes = new[]
            {
                TimeSpan.FromMinutes(1),
                TimeSpan.FromMinutes(2),
                TimeSpan.FromMinutes(3),
                TimeSpan.FromMinutes(5),
                TimeSpan.FromMinutes(8),
                TimeSpan.FromMinutes(13),
                TimeSpan.FromMinutes(21),
                TimeSpan.FromMinutes(34),
                TimeSpan.FromMinutes(55),
                TimeSpan.FromMinutes(89),
            };

            var dateTime            = _dateTimeProvider.OffsetNow;
            var defaultAttemptCount = 5;

            var messages = _repository.GetAll()
                           .Where(x => x.SentDateTime == null)
                           .Where(x => x.ExpiredDateTime == null || x.ExpiredDateTime > dateTime)
                           .Where(x => (x.MaxAttemptCount == 0 && x.AttemptCount < defaultAttemptCount) || x.AttemptCount < x.MaxAttemptCount)
                           .Where(x => x.NextAttemptDateTime == null || x.NextAttemptDateTime <= dateTime)
                           .ToList();

            if (messages.Any())
            {
                foreach (var email in messages)
                {
                    string log = Environment.NewLine + Environment.NewLine
                                 + $"[{_dateTimeProvider.OffsetNow.ToString(CultureInfo.InvariantCulture)}] ";
                    try
                    {
                        await _emailNotification.SendAsync(email);

                        email.SentDateTime = _dateTimeProvider.OffsetNow;
                        email.Log         += log + "Succeed.";

                        _circuitBreakerManager.LogSuccess(circuit);
                    }
                    catch (Exception ex)
                    {
                        email.Log += log + ex.ToString();
                        email.NextAttemptDateTime = _dateTimeProvider.OffsetNow + deplayedTimes[email.AttemptCount];

                        _circuitBreakerManager.LogFailure(circuit, 5, TimeSpan.FromMinutes(5));
                    }

                    email.AttemptCount   += 1;
                    email.Log             = email.Log.Trim();
                    email.UpdatedDateTime = _dateTimeProvider.OffsetNow;

                    if (email.MaxAttemptCount == 0)
                    {
                        email.MaxAttemptCount = defaultAttemptCount;
                    }

                    await _repository.UnitOfWork.SaveChangesAsync();

                    circuit.EnsureOkStatus();
                }
            }
            else
            {
                _logger.LogInformation("No email to send.");
            }

            return(messages.Count);
        }