/// <summary> /// Assigns a Notification to the user if it isn't assigned already. /// </summary> /// <param name="patient">The patient to assign the notification to</param> /// <param name="notificationType">The type of notification </param> /// <param name="notificationData">The notification data to add the notification to</param> private static void AssignNotification(Patient patient, NotificationType notificationType, Dictionary <string, NotificationData> notificationData) { foreach (User u in patient.ProxyUserPatientMap.Select(u => u.User)) { NotifcationManager.AssignNotification(u, notificationType, notificationData); } if (string.IsNullOrWhiteSpace(patient.Email)) { return; } if (notificationData.ContainsKey(patient.Email) && notificationData[patient.Email].NotificationTarget.GetType() != typeof(Patient)) { return; } if (!notificationData.ContainsKey(patient.Email)) { NotificationData data = new NotificationData() { NotificationTarget = patient, Notifications = new List <NotificationType>() }; notificationData.Add(patient.Email, data); } if (!notificationData[patient.Email].Notifications.Contains(notificationType)) { notificationData[patient.Email].Notifications.Add(notificationType); } }
/// <summary> /// Runs the notification checker and sends outstanding notifications and reminders /// </summary> public static void SendNotifications() { AccessHandlerManager ahm = new AccessHandlerManager(); List <Notification> notifications = ahm.NotificationHandler.GetNotifications(); Dictionary <string, NotificationData> notificationData = new Dictionary <string, NotificationData>(); foreach (Notification n in notifications) { switch (n.NotificationType) { case NotificationType.RegistrationComplete: Task <User> t = ahm.UserAccessHandler.FindByIdAsync(n.NotificationObjectId); t.Wait(); User user = t.Result; NotifcationManager.AssignNotification(user, n.NotificationType, notificationData); n.NotificationSendTime = DateTime.Now; n.NotificationCompleted = true; ahm.NotificationHandler.UpdateNotification(n); break; case NotificationType.NewQuestionnaire: QuestionnaireUserResponseGroup group = ahm.QuestionnaireAccessHandler.GetSmallQuestionnaireUserResponseGroupById(int.Parse(n.NotificationObjectId)); Patient patient = ahm.UserAccessHandler.FindPatient(group.Patient.Id); if (group != null && !group.Completed && group.Patient.ProxyUserPatientMap.Any(m => m.User.EmailConfirmed) && (n.NotificationSendTime == null || n.NotificationSendTime < DateTime.Now.AddHours(-4))) { NotifcationManager.AssignNotification(patient, n.NotificationType, notificationData); n.NotificationSendTime = DateTime.Now; n.NotificationCompleted = true; ahm.NotificationHandler.UpdateNotification(n); } else { n.NotificationCompleted = true; ahm.NotificationHandler.UpdateNotification(n); } break; } } TextParser parser = new TextParser(ahm); foreach (string email in notificationData.Keys) { NotificationData data = notificationData[email]; StringBuilder textBuilder = new StringBuilder(); StringBuilder htmlBuilder = new StringBuilder(); ReplaceableObjectKeys objectKey = data.NotificationTarget.GetType() == typeof(User) ? ReplaceableObjectKeys.User : ReplaceableObjectKeys.Patient; TextDefinition start = parser.ParseMessage("NotificationStart", new Dictionary <ReplaceableObjectKeys, object>() { { objectKey, data.NotificationTarget } }); TextDefinition end = parser.ParseMessage("NotificationEnd", new Dictionary <ReplaceableObjectKeys, object>() { { objectKey, data.NotificationTarget } }); textBuilder.Append(start.Text); htmlBuilder.Append(start.Html); foreach (NotificationType t in data.Notifications) { TextDefinition td; td = parser.ParseMessage(t.ToString(), new Dictionary <ReplaceableObjectKeys, object>() { { objectKey, data.NotificationTarget } }); textBuilder.Append(td.Text); htmlBuilder.Append(td.Html); /* * switch (t) * { * case NotificationType.RegistrationComplete: * //textBuilder.AppendLine(Text) * td = parser.ParseMessage(NotificationType.RegistrationComplete.ToString(), null); * textBuilder.Append(td.Text); * htmlBuilder.Append(td.Html); * break; * case NotificationType.NewQuestionnaire: * td = parser.ParseMessage(NotificationType.RegistrationComplete.ToString(), null); * textBuilder.Append(td.Text); * htmlBuilder.Append(td.Html); * break; * }*/ } textBuilder.Append(end.Text); htmlBuilder.Append(end.Html); SmtpMailClient.SendMail(email, "Replay Notification", textBuilder.ToString(), htmlBuilder.ToString()); } }