//TODO: Make a NotificationService to centralize and encapsulate notification methods/logic public void SendFriendRequestNotifications() { // get friend requests that haven't had a notification sent var friendRequests = _customerFriendRepository.Table .Where(x => x.Confirmed == false && x.NotificationCount == 0) .GroupBy(x => x.ToCustomerId) .Select(g => new { CustomerId = g.Key, FriendRequestCount = g.Count() }) .ToList(); foreach (var friendRequest in friendRequests) { var customer = _customerService.GetCustomerById(friendRequest.CustomerId); if (customer == null) { continue; } _mobSocialMessageService.SendFriendRequestNotification(customer, friendRequest.FriendRequestCount, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); UpdateFriendRequestNotificationCounts(friendRequest.CustomerId); } // Send a reminder a week later. Get friend requests that have had one notification since last week var weekUnconfirmedFriendRequests = _customerFriendRepository.Table .Where(x => x.Confirmed == false && x.NotificationCount == 1) .Where(x => DbFunctions.AddDays(x.LastNotificationDate, 7) < DateTime.Now) .GroupBy(x => x.ToCustomerId) .Select(g => new { CustomerId = g.Key, FriendRequestCount = g.Count() }) .ToList(); foreach (var weekUnconfirmedFriendRequest in weekUnconfirmedFriendRequests) { var customer = _customerService.GetCustomerById(weekUnconfirmedFriendRequest.CustomerId); _mobSocialMessageService.SendPendingFriendRequestNotification(customer, weekUnconfirmedFriendRequest.FriendRequestCount, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); UpdateFriendRequestNotificationCounts(weekUnconfirmedFriendRequest.CustomerId); } // Send pending friend request reminder each month var monthlyUnconfirmedFriendRequests = _customerFriendRepository.Table .Where(x => x.Confirmed == false && x.NotificationCount > 1) .Where(x => DbFunctions.AddMonths(x.LastNotificationDate, 1) < DateTime.Now) .GroupBy(x => x.ToCustomerId) .Select(g => new { CustomerId = g.Key, FriendRequestCount = g.Count() }) .ToList(); foreach (var monthlyUnconfirmedFriendRequest in monthlyUnconfirmedFriendRequests) { var customer = _customerService.GetCustomerById(monthlyUnconfirmedFriendRequest.CustomerId); _mobSocialMessageService.SendPendingFriendRequestNotification(customer, monthlyUnconfirmedFriendRequest.FriendRequestCount, _workContext.WorkingLanguage.Id, _storeContext.CurrentStore.Id); UpdateFriendRequestNotificationCounts(monthlyUnconfirmedFriendRequest.CustomerId); } }