/// <summary> /// Sends the email notifications. /// </summary> /// <param name="dateToConsider">The date to consider.</param> public static void SendEmailNotifications(DateTime dateToConsider) { // Handles the concurrent issues. (this class should not access concurrently) if (ProjectNotificationDetailsList.Count == 0) { using (StageBitzDB dataContext = new StageBitzDB()) { // Set code values. DailyEmailNotificationCodeId = Utils.GetCodeIdByCodeValue("UserEmailNotificationType", "DAILY"); WeeklyEmailNotificationCodeId = Utils.GetCodeIdByCodeValue("UserEmailNotificationType", "WEEKLY"); NoEmailNotificationCodeId = Utils.GetCodeIdByCodeValue("UserEmailNotificationType", "NOTATALL"); bool isWeeklyEmailSendDate = false; NotificationBL notificationBL = new NotificationBL(dataContext); ProjectBL projectBL = new ProjectBL(dataContext); DateTime endDate = dateToConsider; int dateDiff = 1; // If weekly email day set date diff as 7 days. if (dateToConsider.DayOfWeek == (DayOfWeek)Enum.Parse(typeof(DayOfWeek), Utils.GetSystemValue("EmailNotificationDayOfWeek"))) { dateDiff = 7; isWeeklyEmailSendDate = true; } // Get project notifications static list. ProjectNotificationDetailsList = notificationBL.GetUserEmailNotifications(dateToConsider, dateDiff); // Get all project ids with notifications. List <int> projectIds = ProjectNotificationDetailsList.Select(pnd => pnd.ProjectId).Distinct().ToList <int>(); // Get all user ids need to send email notifications. List <int> allUsers = projectBL.GetAllProjectUserAndCompanyUserIds(projectIds); // Send emails for all users. foreach (int userId in allUsers) { // Get project user infos List <ProjectUserInfo> projectUserInfoList = projectBL.GetProjectUserInfo(userId); // Get company admin user infos List <ProjectUserInfo> companyAdminProjectUserInfoList = projectBL.GetCompanyAdminProjectUserInfo(projectIds, userId); // remove duplicate project user infos from company admin project list. companyAdminProjectUserInfoList.RemoveAll(capui => projectUserInfoList.Exists(pui => (capui.ProjectId == pui.ProjectId && capui.UserId == pui.UserId))); if (projectUserInfoList.Count > 0 || companyAdminProjectUserInfoList.Count > 0) { ProjectUserInfo user = null; // Get first one. if (projectUserInfoList.Count > 0) { user = projectUserInfoList.FirstOrDefault(); } else if (companyAdminProjectUserInfoList.Count > 0) { user = companyAdminProjectUserInfoList.FirstOrDefault(); } if (user != null) { // Filter project user infos. List <ProjectUserInfo> projectUserInfos = GetFilteredProjectUserInfoForUser(user.UserId, user.ProjectEmailNotificationCodeId, projectUserInfoList, isWeeklyEmailSendDate, true); // Filter company admin user infos. List <ProjectUserInfo> companyAdminProjectUserInfos = GetFilteredProjectUserInfoForUser(user.UserId, user.CompanyEmailNotificationCodeId, companyAdminProjectUserInfoList, isWeeklyEmailSendDate, false); if (projectUserInfos.Count > 0 || companyAdminProjectUserInfos.Count > 0) { // Gets the email content. string emailText = GetEmailText(user, projectUserInfos, companyAdminProjectUserInfos); // Send the email. EmailSender.SendNotificationEmail(user.UserEmail, user.UserName, emailText); } } } } } // Clear static project notification details list. ProjectNotificationDetailsList.Clear(); } }