public ActionResult AddUnsaved(UnsavedContactViewModel model) { if (!ModelState.IsValid) { return(View("unsavedContacts", model)); } var contacts = model.Contacts; foreach (var c in contacts) { var newContact = new Contact { FirstName = c.FirstName, LastName = c.LastName, Email = c.Email, PhoneNumber = c.PhoneNumber }; GetUserContacts(GetCurrentUserId()).Add(newContact); _db.SaveChanges(); } return(RedirectToAction("Index")); }
public ActionResult SendEventsInvitation([Bind(Include = "ParticipantsEmails,EventDate,EventId,SendRemainder,ReminderDate,ListDate")] InvitationViewModel model) { var id = model.EventId; if (!ModelState.IsValid) { return(View(ReturnInvitationModel(id))); } //ToDo: change list and remainder dates to swiss time to be used for sending email // get event from database var eventForInvitation = GetEvent(id); eventForInvitation.ListDate = // ReSharper disable once PossibleInvalidOperationException ConvertDateTime.ToSwissTimezone(TimeZoneInfo.ConvertTimeToUtc((DateTime)model.ListDate)); if (model.ReminderDate != null) { eventForInvitation.ReminderDate = ConvertDateTime.ToSwissTimezone(TimeZoneInfo.ConvertTimeToUtc((DateTime)model.ReminderDate)); Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("{ From Invitation controller sending invitation } Remainder date is on " + eventForInvitation.ReminderDate)); } /* * This is used to check time on the server * when this is deployed */ Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("{ From Invitation controller sending invitation } List date is on " + eventForInvitation.ListDate)); //Check if invitations can still be sent var notPassed = _service.EventHasNotPassed(eventForInvitation); if (!notPassed) { return(View("_CantInvite", eventForInvitation)); } var unsavedContacts = new UnsavedContactViewModel(); EmailInformation emailInfo = null; var allSaved = false; var contacts = new List <Contact>(); var emails = new List <EmailInformation>(); //loop through emails var emailList = model.ParticipantsEmails.Split(',').ToList(); foreach (var participantEmail in emailList) { var email = _service.RemoveBrackets(participantEmail); //save new participant SaveParticipantInDb(email, eventForInvitation); #region Create and send Email emailInfo = ComposeEmailInfo(eventForInvitation, email); emails.Add(emailInfo); //Send Invitation Email try { PostalEmailManager.SendEmail(emailInfo, new InvitationEmail()); //todo: this is to be removed before deployment for production Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("Email sent to " + emailInfo.ParticipantEmail)); if (model.SendRemainder) { var remainderDate = Service.GetRemanderDate(eventForInvitation); JobManager.ScheduleRemainderEmail(emails, remainderDate); //todo: this is to be removed before deployment for production Elmah.ErrorSignal.FromCurrentContext().Raise(new Exception("remainder is set at " + remainderDate)); } } catch (Exception exception) { Elmah.ErrorSignal.FromCurrentContext().Raise(exception); return(RedirectToAction("Error")); } #endregion #region after sending email, save unsaved contacts var contactEmails = _contactsController.GetUserContacts(UserId); allSaved = contactEmails.Any(c => c.Email == email); if (allSaved) { continue; } var contact = new Contact { Email = email }; contacts.Add(contact); unsavedContacts.Contacts = contacts; #endregion } #region Scheduling List email // start participant list summary scheduler var listDate = Service.GetListDate(eventForInvitation); JobManager.ScheduleParticipantListEmail(emailInfo, listDate); #endregion //redirect to details if all contacts are saved if (allSaved) { Response.Cookies.Add(new HttpCookie("successCookie", "Action is completed successfully")); return(RedirectToAction("Details", "Events", new { id })); } //Let user save his contacts return view with list of unsaved contacts unsavedContacts.EventId = eventForInvitation.Id; TempData["model"] = unsavedContacts; //Pass list to SaveEmails action return(RedirectToAction("SaveEmails")); }