Ejemplo n.º 1
0
        public ActionResult BulkRequestValuation(BulkRequestValuationViewModel model)
        {
            if (model.File == null)
                return View(model);

            try
            {
                var invited = 0;
                var notInvited = new List<NotInvitedViewModel>();
                var errorWhenInvited = new List<ErrorInvitedViewModel>();

                var data = _bulkImporter.Execute(model.File.InputStream);

                Logger.Info("Sending bulk validation requests...");

                var emails = (from a in data
                              where !string.IsNullOrWhiteSpace(a.ISSN) && !string.IsNullOrWhiteSpace(a.AuthorEmail) && EmailValidator.IsValid(a.AuthorEmail)
                              let journal = journalRepository.FindByIssn(a.ISSN)
                              where journal != null
                              select new RequestValuationViewModel
                              {
                                  JournalId = journal.Id,
                                  JournalTitle = journal.Title,
                                  JournalISSN = journal.ISSN,
                                  EmailFrom = model.EmailFrom,
                                  EmailTo = a.AuthorEmail,
                                  RecipientName = a.AuthorName,
                                  EmailBody = $"Dear {a.AuthorName},\r\n\r\n{model.EmailBody.Replace("<<JournalTitle>>", journal.Title)}",
                                  EmailSubject = model.EmailSubject.Replace("<<JournalTitle>>", journal.Title).Replace("<<JournalISSN>>", journal.ISSN),
                                  IsKnownEmailAddress = IsKnownEmailAddress(a.AuthorEmail),
                                  HasKnownEmailDomain = HasKnownEmailDomain(a.AuthorEmail)
                              }).ToList();

                foreach (var item in emails.Select(e => new { Email = e.ToRequestValuationEmail(), Model = e }))
                {
                    // NOTE: Requirements have changed. Leo Waaijers requested that e-mail addresses with unknown domains could be invited to fill in a Score Card - Sergi Papaseit (2015-12-10)
                    try
                    {
                        item.Email.Url = FillEmailUrl(item.Model);
                        item.Email.Send();

                        Logger.Info($"Sent validation request to {item.Email.To}.");

                        invited++;
                    }
                    catch (Exception ex)
                    {
                        Logger.Error($"Error sending validation request to {item.Email.To}: {ex}.", ex.ToString());

                        errorWhenInvited.Add(new ErrorInvitedViewModel
                        {
                            ISSN = string.IsNullOrWhiteSpace(item.Model.JournalISSN) ? "Unknown" : item.Model.JournalISSN,
                            AuthorName = item.Model.RecipientName,
                            AuthorEmail = string.IsNullOrWhiteSpace(item.Model.EmailTo) ? "Unknown" : item.Model.EmailTo
                        });
                    }
                }

                notInvited.AddRange(
                    from a in data
                    where string.IsNullOrWhiteSpace(a.ISSN) || string.IsNullOrWhiteSpace(a.AuthorEmail) || !EmailValidator.IsValid(a.AuthorEmail)
                    select new NotInvitedViewModel
                    {
                        ISSN = string.IsNullOrWhiteSpace(a.ISSN) ? "Unknown" : a.ISSN,
                        AuthorName = a.AuthorName,
                        AuthorEmail = string.IsNullOrWhiteSpace(a.AuthorEmail) ? "Unknown" : a.AuthorEmail + " (invalid)"
                    });

                foreach (var notInvitedViewModel in notInvited)
                {
                    Logger.Info($"Skipped sending validation request to {notInvitedViewModel.AuthorEmail}.");
                }

                return View("BulkInviteSuccessful", new AuthorsInvitedViewModel
                {
                    AmountInvited = invited,
                    AuthorsNotInvited = notInvited,
                    AuthorsInvitedWithError = errorWhenInvited
                });
            }
            catch (ArgumentException invalidFileException)
            {
                ModelState.AddModelError("generalError", invalidFileException.Message);
            }
            catch (DbEntityValidationException)
            {
                //foreach (var eve in e.EntityValidationErrors)
                //{
                //    Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",eve.Entry.Entity.GetType().Name, eve.Entry.State);
                //    foreach (var ve in eve.ValidationErrors)
                //    {
                //        Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage);
                //    }
                //}
                //throw;
            }
            catch (FormatException fe)
            {
                ModelState.AddModelError("generalError", $"An error has ocurred: {fe.Message}");
            }
            catch (Exception exception)
            {
                ModelState.AddModelError("generalError", $"An error has ocurred: {exception.InnermostMessage()}");
            }

            return View(model);
        }
Ejemplo n.º 2
0
        public ActionResult BulkRequestValuation()
        {
            var regex = new Regex(@"Dear Sir/Madam,\s*[\r\n]*", RegexOptions.Compiled);
            var model = new BulkRequestValuationViewModel
            {
                EmailBody = regex.Replace(Resources.RequestValuation.Body, ""),
                EmailSubject = Resources.RequestValuation.Subject
            };

            var currentUser = UserProfileRepository.Find(Authentication.CurrentUserId);

            if (currentUser != null)
                model.EmailFrom = currentUser.Email;

            return View(model);
        }