public EmailNotificatorTests(ITestOutputHelper output)
        {
            _output = output;
            var senderAddress           = "*****@*****.**";
            var senderName              = "notif-test";
            var mailhogSmtpServerConfig = new SmtpServerConfig()
            {
                Host          = "localhost",
                Port          = 25,
                SenderName    = senderName,
                SenderAddress = senderAddress
            };
            var mailSettingsConfig = new MailSettingsConfig()
            {
                Smtp = new MailSettingsConfig.SmtpConfig()
                {
                    Servers = new List <SmtpServerConfig>()
                    {
                        mailhogSmtpServerConfig
                    }
                }
            };

            var toList  = new List <string>();
            var subject = "Test subject";
            var body    = "Test body";

            _emailMessage = new EmailMessage(toList, subject, body);

            var smtpClientFactory  = new SmtpClientWithoutSslFactory();
            var mailMessageFactory = new MailMessageFactory();

            _notificator = new EmailNotificator(smtpClientFactory, mailSettingsConfig, mailMessageFactory);
        }
        public EmailNotificatorTests()
        {
            _smtpClientFactory  = Substitute.For <ISmtpClientFactory>();
            _mailSettingsConfig = MailSettingsConfigFake.Generate();

            _mailMessageFactory = Substitute.For <IMessageFactory <MailMessageCreateRequest, MailMessage> >();
            _notificator        = new EmailNotificator(_smtpClientFactory, _mailSettingsConfig, _mailMessageFactory);

            var recipients = new List <string>()
            {
                _faker.Internet.Email(),
            };

            _emailMessage = new EmailMessage("from", recipients, "subject", "body");

            var smtpServerConfig1 = SmtpServerConfigFake.Generate();
            var smtpServerConfig2 = SmtpServerConfigFake.Generate();
            var smtpServerConfig3 = SmtpServerConfigFake.Generate();

            _mailSettingsConfig.Smtp.Servers = new List <SmtpServerConfig>()
            {
                smtpServerConfig1, smtpServerConfig2, smtpServerConfig3
            };

            _smtpClient1 = Substitute.ForPartsOf <FakeSmtpClient>(smtpServerConfig1.Host);
            _smtpClient2 = Substitute.ForPartsOf <FakeSmtpClient>(smtpServerConfig2.Host);
            _smtpClient3 = Substitute.ForPartsOf <FakeSmtpClient>(smtpServerConfig3.Host);

            _smtpClientFactory.Create(smtpServerConfig1).Returns(_smtpClient1);
            _smtpClientFactory.Create(smtpServerConfig2).Returns(_smtpClient2);
            _smtpClientFactory.Create(smtpServerConfig3).Returns(_smtpClient3);
        }
Example #3
0
        private async Task NotifyStudents(IDatabase database)
        {
            try
            {
                logger.LogInformation("Notification started.");



                database.NotificationRuleService.GetAll().ToList().ForEach(rule => {
                    var to   = DateTime.Today;
                    var from = to.AddDays(rule.AttendancePeriod * -1);

                    var studentIds = database.StudentService.GetAll()
                                     .Where(st => st.StudyProgrammeId == rule.StudyProgrammeId &&
                                            st.Language == rule.Language && st.LearningForm == rule.LearningForm)
                                     .Select(s => s.Id).ToList();

                    studentIds.ForEach(studentId =>
                    {
                        var attendances = database.StudentAttendanceService.GetAllByStudentId(studentId)
                                          .Where(a => a.Date >= from && a.Date <= to);

                        if (attendances.Any())
                        {
                            var procent = 100d * attendances.Sum(a => a.RealAttendance) / attendances.Sum(a => a.NecessaryAttendance);
                            if (procent <= rule.AttendanceProcent)
                            {
                                var student = database.StudentService.Get(studentId);
                                // Notify
                                var message = InsertDataIntoMessage(rule.Message, student, procent);

                                NotificationResult result = new NotificationResult();
                                switch (rule.NotificationMethod)
                                {
                                case BusinessLogic.Entities.Enumerations.NotificationMethod.Email:
                                    {
                                        var emails = new List <string>();
                                        if (!string.IsNullOrEmpty(student.Email1))
                                        {
                                            emails.Add(student.Email1);
                                        }
                                        if (!string.IsNullOrEmpty(student.Email2))
                                        {
                                            emails.Add(student.Email2);
                                        }
                                        if (emails.Any())
                                        {
                                            result = EmailNotificator.Notify(emails.ToArray(), message);
                                        }
                                        break;
                                    }

                                case BusinessLogic.Entities.Enumerations.NotificationMethod.SMS:
                                    {
                                        var emails = new List <string>();
                                        if (!string.IsNullOrEmpty(student.Phone1))
                                        {
                                            emails.Add(student.Phone1);
                                        }
                                        if (!string.IsNullOrEmpty(student.Phone2))
                                        {
                                            emails.Add(student.Phone2);
                                        }
                                        if (emails.Any())
                                        {
                                            result = SmsNotificator.Notify(emails.ToArray(), message);
                                        }
                                        break;
                                    }
                                }

                                database.NotificationHistoryService.Add(new NotificationHistory
                                {
                                    Id           = 0,
                                    StudentId    = studentId,
                                    Message      = message,
                                    SendingTime  = DateTime.UtcNow,
                                    Status       = result.NotificationStatus,
                                    ErrorMessage = result.ErrorMessage
                                });
                            }
                        }
                    });
                });

                logger.LogInformation("Notification ended.");
            }
            catch (Exception exception)
            {
                logger.LogError(exception, exception.Message);
            }
        }