// create model for Create view public NotificationCreateVM CreateUpdateModel(string incidentNumber, NotificationCreateVM model = null) { var lastestNotification = _context.Notification.Where(n => n.IncidentNumber == incidentNumber) .OrderByDescending(n => n.SentDateTime).FirstOrDefault(); int headingLength = lastestNotification.NotificationHeading.IndexOf(" (Edit"); if (model == null) { model = new NotificationCreateVM() { IncidentNumber = incidentNumber, StartDateTime = lastestNotification.StartDateTime, EndDateTime = lastestNotification.EndDateTime, LevelOfImpactID = lastestNotification.LevelOfImpactID, NotificationTypeID = lastestNotification.NotificationTypeID, PriorityID = lastestNotification.PriorityID, StatusID = lastestNotification.StatusID, NotificationDescription = lastestNotification.NotificationDescription, NotificationHeading = lastestNotification.NotificationHeading = headingLength == -1 ? lastestNotification.NotificationHeading : lastestNotification.NotificationHeading.Substring(0, headingLength) }; model.ServerReferenceIDs = lastestNotification.Servers.Select(s => s.ReferenceID).ToArray(); model.ApplicationReferenceIDs = lastestNotification.Applications.Select(a => a.ReferenceID).ToArray(); } model = CreateAddModel(model); return(model); }
public static string NotificationSMS(NotificationCreateVM model) { ApplicationDbContext _context = new ApplicationDbContext(); string levelOfImpact = _context.LevelOfImpact.FirstOrDefault(l => l.LevelOfImpactID == model.LevelOfImpactID).LevelName; string status = _context.Status.FirstOrDefault(s => s.StatusID == model.StatusID).StatusName; string template = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Service/templates/NotificationSMSTemplate.txt")); string message = template.Replace("{Subject}", model.NotificationHeading) .Replace("{IncidentNumber}", model.IncidentNumber) .Replace("{LevelOfImpact}", levelOfImpact) .Replace("{Status}", status) .Replace("{Url}", "http://" + HttpContext.Current.Request.Url.Authority + SUB_DIRECTORY + "Notification/DetailsThread/" + model.IncidentNumber); return(message); }
// create model for CreateThread view public NotificationCreateVM CreateAddModel(NotificationCreateVM model = null) { if (model == null) { model = new NotificationCreateVM() { IncidentNumber = Guid.NewGuid().ToString(), StartDateTime = DateTime.Now }; } model.ApplicationList = _slRepo.GetApplicationListByServer(model.ServerReferenceIDs); model.ServerList = _slRepo.GetServerList(); model.NotificationTypeList = _slRepo.GetTypeList(); model.LevelOfImpactList = _slRepo.GetImpactLevelList(); model.StatusList = _slRepo.GetStatusList(Key.STATUS_TYPE_NOTIFICATION); model.PriorityList = _slRepo.GetPriorityList(); return(model); }
// get the phone numbers of the users with sms as preference recieve method public List <PhoneNumber> GetPhoneNumbers(NotificationCreateVM notification) { var servers = _context.Server.Where(s => notification.ServerReferenceIDs.Contains(s.ReferenceID)); var apps = _context.Application.Where(a => notification.ApplicationReferenceIDs.Contains(a.ReferenceID)); var priorityValue = _context.Notification.Where(n => notification.PriorityID == n.Priority.PriorityID) .Select(n => n.Priority.PriorityValue) .FirstOrDefault(); // Get recievers List <string> receivers; if (apps.Count() == 0) { receivers = servers.SelectMany( s => s.Applications.SelectMany( a => a.UserDetails.Where( u => u.SendMethod.SendMethodName == Key.SEND_METHOD_SMS || u.SendMethod.SendMethodName == Key.SEND_METHOD_EMAIL_AND_SMS) .Select(u => u.MobilePhone))).ToList(); } else { receivers = apps.SelectMany( a => a.UserDetails.Where( u => u.SendMethod.SendMethodName == Key.SEND_METHOD_SMS || u.SendMethod.SendMethodName == Key.SEND_METHOD_EMAIL_AND_SMS) .Select(u => u.MobilePhone)).ToList(); } receivers = receivers.Distinct().ToList(); // Get phone numbers List <PhoneNumber> phoneNumbers = new List <PhoneNumber>(); foreach (var phoneNumber in receivers) { phoneNumbers.Add(new PhoneNumber(phoneNumber)); } return(phoneNumbers); }
public static string NotificationEmail(NotificationCreateVM model) { ApplicationDbContext _context = new ApplicationDbContext(); // get application id and other information that is about to be sent var appsId = _context.Application.Where(a => model.ApplicationReferenceIDs.Contains(a.ReferenceID)).Select(app => app.ApplicationName); string levelOfImpact = _context.LevelOfImpact.FirstOrDefault(l => l.LevelOfImpactID == model.LevelOfImpactID).LevelName; string status = _context.Status.FirstOrDefault(s => s.StatusID == model.StatusID).StatusName; // loop through the list of applications if user has more than one application string apps = ""; foreach (var app in appsId) { apps = app.ToString(); } // calculate the duration the application / server were down for TimeSpan?duration = (model.EndDateTime - model.StartDateTime); // load the html template and read the content to be replaced later. string path = File.ReadAllText(HttpContext.Current.Server.MapPath("~/Service/templates/NotificationEmailTemplate.html")); string message = ""; message = path.Replace("{Subject}", model.NotificationHeading + ", " + apps) .Replace("{Description}", model.NotificationDescription) .Replace("{IncidentNumber}", model.IncidentNumber) .Replace("{ServiceAffected}", apps) .Replace("{ServiceImpact}", levelOfImpact) .Replace("{CurrentState}", status) .Replace("{StartTime}", model.StartDateTime == null ? DateTime.Now.ToString() : model.StartDateTime.ToString()) .Replace("{EndTime}", model.EndDateTime == null ? "Not available at this time" : model.EndDateTime.ToString()) .Replace("{Duration}", model.StartDateTime == null || model.EndDateTime == null ? "Not available at this time" : duration.ToString()) .Replace("{URL}", "http://" + HttpContext.Current.Request.Url.Authority + SUB_DIRECTORY + "Notification/DetailsThread/" + model.IncidentNumber); return(message); }
public async Task <ActionResult> CreateThread(NotificationCreateVM model) { string result = ""; if (ModelState.IsValid) { // give this model a new incident # model.IncidentNumber = _nRepo.NewIncidentNumber(model.NotificationTypeID); // add notification to the database bool success = _nRepo.CreateNotification(model, out result); if (success) { // send email and sms after notification has been created await NotificationService.SendEmail(_nRepo.CreateMails(model)); await NotificationService.SendSMS(_nRepo.GetPhoneNumbers(model), TemplateService.NotificationSMS(model)); TempData["SuccessMsg"] = result; return(RedirectToAction("Index")); } } else { TempData["ErrorMsg"] = "Cannot add Notification, model not valid."; } // recreate model for the view model = _nRepo.CreateAddModel(model); // if notification returns null, redirect to notification index if (model == null) { TempData["ErrorMsg"] = "Cannot create new thread at the moment"; return(RedirectToAction("Index")); } TempData["ErrorMsg"] = result; return(View(model)); }
public async Task <ActionResult> Create(NotificationCreateVM model) { string result = ""; if (ModelState.IsValid) { // add notification to the database bool success = _nRepo.CreateNotification(model, out result); if (success) { // send email and sms after notification has been created await NotificationService.SendEmail(_nRepo.CreateMails(model)); await NotificationService.SendSMS(_nRepo.GetPhoneNumbers(model), TemplateService.NotificationSMS(model)); TempData["SuccessMsg"] = result; return(RedirectToAction("DetailsThread", new { id = model.IncidentNumber })); } } else { TempData["ErrorMsg"] = "Cannot update Notification, model not valid."; } // recreate model for the view model = _nRepo.CreateUpdateModel(model.IncidentNumber, model); // if notification returns null, redirect to thread view if (model == null) { TempData["ErrorMsg"] = "Cannot create new notification at the moment"; return(RedirectToAction("DetailsThread", new { id = model.IncidentNumber })); } TempData["ErrorMsg"] = result; return(View(model)); }
// create mails using a template body for users with email as preference recieve method public List <MailMessage> CreateMails(NotificationCreateVM notification) { try { var servers = _context.Server.Where(s => notification.ServerReferenceIDs.Contains(s.ReferenceID)); var apps = _context.Application.Where(a => notification.ApplicationReferenceIDs.Contains(a.ReferenceID)); var priorityValue = _context.Notification.Where(n => notification.PriorityID == n.Priority.PriorityID) .Select(n => n.Priority.PriorityValue) .FirstOrDefault(); // Get recievers List <string> receivers; if (apps.Count() == 0) { receivers = servers.SelectMany( s => s.Applications.SelectMany( a => a.UserDetails.Where( u => u.SendMethod.SendMethodName == Key.SEND_METHOD_EMAIL || u.SendMethod.SendMethodName == Key.SEND_METHOD_EMAIL_AND_SMS) .Select(u => u.User.Email))).ToList(); } else { receivers = apps.SelectMany( a => a.UserDetails.Where( u => u.SendMethod.SendMethodName == Key.SEND_METHOD_EMAIL || u.SendMethod.SendMethodName == Key.SEND_METHOD_EMAIL_AND_SMS) .Select(u => u.User.Email)).ToList(); } receivers = receivers.Distinct().ToList(); List <MailMessage> mails = new List <MailMessage>(); foreach (string receiver in receivers) { // check if user have confirmed their email UserManager <ApplicationUser> manager = new UserManager <ApplicationUser>(new UserStore <ApplicationUser>(_context)); ApplicationUser user = manager.FindByEmail(receiver); if (user != null) { if (user.EmailConfirmed) { //create the mail message MailMessage mail = new MailMessage(); //set the addresses mail.From = new MailAddress("*****@*****.**"); mail.To.Add(receiver); //set the content mail.Subject = notification.NotificationHeading; mail.Body = TemplateService.NotificationEmail(notification); mail.IsBodyHtml = true; switch (priorityValue) { case Key.PRIORITY_VALUE_HIGH: mail.Priority = MailPriority.High; break; case Key.PRIORITY_VALUE_NORMAL: mail.Priority = MailPriority.Normal; break; case Key.PRIORITY_VALUE_LOW: mail.Priority = MailPriority.Low; break; default: mail.Priority = MailPriority.Normal; break; } mails.Add(mail); } } } return(mails); } catch (Exception) { return(null); } }
// create new notification. this is also used for updating a thread with new notification public bool CreateNotification(NotificationCreateVM notification, out string msg) { try { var userId = HttpContext.Current.User.Identity.GetUserId(); var sendMethodId = _context.UserDetail .Where(a => a.UserID == userId) .FirstOrDefault().SendMethodID; //TO DO: check if it's by server or by Application if (notification.ServerReferenceIDs == null) { msg = "Must choose a Server"; return(false); } if (notification.EndDateTime != null) { var invalidEndTime = DateTime.Compare((DateTime)notification.StartDateTime, (DateTime)notification.EndDateTime) >= 0; if (invalidEndTime) { msg = "End time cannot be ealier than start time."; return(false); } } if (notification.ApplicationReferenceIDs == null) { notification.ApplicationReferenceIDs = new string[0]; } var servers = _context.Server.Where(s => notification.ServerReferenceIDs.Contains(s.ReferenceID)); var apps = _context.Application.Where(a => notification.ApplicationReferenceIDs.Contains(a.ReferenceID)); var priorityValue = _context.Notification.Where(n => notification.PriorityID == n.Priority.PriorityID) .Select(n => n.Priority.PriorityValue) .FirstOrDefault(); Notification newNotification = new Notification() { LevelOfImpactID = notification.LevelOfImpactID, NotificationTypeID = notification.NotificationTypeID, NotificationHeading = notification.NotificationHeading, NotificationDescription = notification.NotificationDescription, StatusID = notification.StatusID, PriorityID = notification.PriorityID, UserID = userId, //TO DO: discuss how referenceID is generated ReferenceID = Guid.NewGuid().ToString(), IncidentNumber = notification.IncidentNumber, //TO DO: convert input time to UTC time SentDateTime = DateTime.Now, StartDateTime = notification.StartDateTime, EndDateTime = notification.EndDateTime, }; newNotification.Servers = servers.ToList(); newNotification.Applications = apps.ToList(); _context.Notification.Add(newNotification); _context.SaveChanges(); msg = "Notification Sent"; return(true); } catch (Exception) { msg = "Notification not created"; return(false); } }