Example #1
0
        public void SendNotificationsEmailToUser(string userId, int companyId)
        {
            var notificationSettings = GetNotificationSettings(userId);
            var company = _companyRepository.GetById(companyId);

            if (notificationSettings?.NotificationIteration != NotificationIteration.WithoutNotification)
            {
                var processedNotifications = _processedNotificationRepository.GetAll().Where(c => c.UserId == userId)?.OrderByDescending(c => c.DateTime)?.ToList();
                var lastNotificationDate   = processedNotifications.Count > 0 ? processedNotifications?.Select(c => c.DateTime)?.FirstOrDefault() : null;
                var allUsersNotifications  = GetAllEmailNotifications(companyId, userId, notificationSettings);
                if (allUsersNotifications.Count > 0)
                {
                    var notificationEmailBody = GetNotificationEmailBody(allUsersNotifications);
                    if (lastNotificationDate.HasValue)
                    {
                        if (notificationSettings.NotificationIteration == NotificationIteration.OnesADay &&
                            DateTimeHelper.GetDaysCountFromDateToDate(DateTime.Now, lastNotificationDate.Value) == 1)
                        {
                            SendEmail(notificationEmailBody, company.Email);
                            logging.Add($"Notes were sent to the user {userId} at {DateTime.Now}. One day iteration.");
                            ProcessNotifications(userId, allUsersNotifications.Select(n => n.Id));
                        }
                        else if (notificationSettings.NotificationIteration == NotificationIteration.OnesAWeek &&
                                 DateTimeHelper.GetDaysCountFromDateToDate(DateTime.Now, lastNotificationDate.Value) == 7)
                        {
                            SendEmail(notificationEmailBody, company.Email);
                            logging.Add($"Notes were sent to the user {userId} at {DateTime.Now}. Week iteration.");
                            ProcessNotifications(userId, allUsersNotifications.Select(n => n.Id));
                        }
                    }
                    else
                    {
                        SendEmail(notificationEmailBody, company.Email);
                        logging.Add($"Notes were sent to the user {userId} at {DateTime.Now} at first time");
                        ProcessNotifications(userId, allUsersNotifications.Select(n => n.Id));
                    }
                }
            }
        }
Example #2
0
        public async Task <ActionResult> RunScheduled()
        {
            await RunNotificationEmails();

            lock (obj)
            {
                DatabaseLogging logging = new DatabaseLogging();
                DateTime        now     = DateTimeHelper.GetCurrentDateTimeByTimeZone(Constants.CurrentTimeZoneId);

                logging.Add($"Run Scheduled emails. Today is {now.DayOfWeek}. Time: {now.Hour}:{now.Minute}. Full: {now}");

                if (now.DayOfWeek == DayOfWeek.Saturday || now.DayOfWeek == DayOfWeek.Sunday)
                {
                    logging.Add("We don't send emails today " + now.DayOfWeek + " " + now);
                    return(Content("We don't send emails today " + now.DayOfWeek + " " + now));
                }

                if (now.Hour != 9)
                {
                    logging.Add("time is not correct " + now.Hour + " Full: " + now);
                    return(Content("time is not correct " + now.Hour + " Full: " + now));
                }

                ScheduledEmailRepository emailRepository = new ScheduledEmailRepository();
                IEmailService            emailService    = new GoogleEmailService();
#if DEBUG
                emailService = new SendGridEmailService();
#endif
                var scheduled = emailRepository.GetScheduled().ToList();
                logging.Add($"Schedule time is fine. Got {scheduled.Count} emails to sent");

                foreach (var email in scheduled)
                {
                    try
                    {
                        emailService.SendIntroMail(email.CompanyId, email.Subject, email.Body, email.Email);
                        emailRepository.UpdateStatus(email.Id, EmailStatus.Sent);
                        logging.Add($"Email {email.Id} is sent");
                    }
                    catch (Exception e)
                    {
                        emailRepository.UpdateStatus(email.Id, EmailStatus.Failed);
                        logging.Add($"Email {email.Id} is failed. Error: {e.Message}");
                    }
                }

                logging.Add($"***Scheduled emails are done***");
            }
            return(Content("OK"));
        }