private List <Data.EF.Users.User> GetUsersToNotify(Data.EF.Actions.Action action) { List <int> ids; using (var session = actionsSessionFactory.CreateContext()) { ids = session.Notifications.Where(n => n.Action.ObjectId == action.ObjectId).Select(n => n.UserId).ToList(); ids = ids.Except(session.Actions.Where(a => a.ObjectId == action.ObjectId).Select(a => a.UserId).ToList()).ToList(); } using (var session = usersSessionFactory.CreateContext()) { return(session.Users.Where(u => ids.Contains(u.Id) && u.UserEmails.Any(e => e.SendMail && e.IsEmailConfirmed)).ToList()); } }
public void SendObjectCreatedNotification(ObjectCreatedNotification notification, Data.EF.Actions.Action action, NotificationTypes type, string absoluteObjectLink) { using (var userSession = usersSessionFactory.CreateContext()) { var users = GetUsersToNotify(action); if (users.Any()) { string subject = action.Subject; using (var session = noSqlSessionFactory()) { if (type == NotificationTypes.IdeaCreated) { var idea = session.GetAll <Data.MongoDB.Idea>().SingleOrDefault(i => i.Id == action.ObjectId); if (idea != null) { subject = idea.Subject; idea.IsMailSent = true; session.Update(idea); } } else if (type == NotificationTypes.IssueCreated) { var issue = session.GetAll <Data.MongoDB.Issue>().SingleOrDefault(i => i.Id == action.ObjectId); if (issue != null) { subject = issue.Subject; issue.IsMailSent = true; session.Update(issue); } } } notification.ObjectSubject = subject; notification.ObjectLink = absoluteObjectLink; var not = GetNotification((int)type); notification.MessageTemplate = not.Message; notification.Subject = not.Subject + ": " + subject; logger.Information(string.Format("Sending emails for {0} users", users.Count)); foreach ( var user in users.SelectMany(u => u.UserEmails.Where(e => e.IsEmailConfirmed && e.SendMail))) { notification.To = user.Email; notification.ToDisplayName = user.User.FirstName + " " + user.User.LastName; notification.Execute(); } } } }