Esempio n. 1
0
 /// <summary>
 /// Sends an email using SMTP mail
 /// </summary>
 /// <param name="to">The address to send to</param>
 /// <param name="subject">The subject of the email</param>
 /// <param name="text">The text to send</param>
 /// <param name="html">The html to send</param>
 public static void SendMail(string to, string subject, string text, string html)
 {
     SmtpMailClient.SendMail(new string[] { to }, null, null, subject, text, html);
 }
Esempio n. 2
0
        /// <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());
            }
        }