Example #1
0
        protected List <Recipient> LoadRecipients(NotificationType notificationType, Customer customer, bool dsrDSMOnly = false)
        {
            if (customer == null)
            {
                return(new List <Recipient>());
            }

            UserProfileReturn users = GetUsers(customer, dsrDSMOnly);

            if (users.UserProfiles.Count == 0)
            {
                return(new List <Recipient>());
            }


            List <UserMessagingPreference> userDefaultMessagingPreferences = // list of each user's default prefs
                                                                             userMessagingPreferenceRepository.ReadByUserIdsAndNotificationType(users.UserProfiles.Select(u => u.UserId), notificationType, true).ToList();
            List <UserMessagingPreference> customerMessagingPreferences =    // list of customer's user specific pref
                                                                          userMessagingPreferenceRepository.ReadByCustomerAndNotificationType(customer.CustomerNumber, customer.CustomerBranch, notificationType).ToList();

            List <UserMessagingPreference> ump = new List <UserMessagingPreference>();

            ump.AddRange(userDefaultMessagingPreferences);
            ump.AddRange(customerMessagingPreferences);
            string prefs = string.Empty;

            foreach (var u in ump)
            {
                prefs += u.Channel + u.UserId.ToString("B") + u.NotificationType + "; ";
            }

            log.WriteInformationLog
                (String.Format(
                    "notification prefs: {0}, profiles count: {1}, profiles: {2}, userDefaultMessagingPreferences: {3}, customerMessagingPreferences: {4}",
                    prefs,
                    users.UserProfiles.Count,
                    JsonConvert.SerializeObject(users.UserProfiles.Select(p => new {
                UserId       = p.UserId,
                EmailAddress = p.EmailAddress
            }).ToList()),
                    userDefaultMessagingPreferences,
                    customerMessagingPreferences));

            List <Recipient> recipients = new List <Recipient>();

            foreach (UserProfile userProfile in users.UserProfiles)
            {
                if (userDefaultMessagingPreferences != null)
                {
                    // first, check for customer specific prefs
                    List <UserMessagingPreference> prefsToUse = customerMessagingPreferences.Where(
                        p => p.UserId == userProfile.UserId).ToList(); // check for customer specific prefs first
                    if (prefsToUse == null || prefsToUse.Count() == 0) // then check for defaults
                    {
                        prefsToUse = userDefaultMessagingPreferences.Where(p => p.UserId == userProfile.UserId).ToList();
                    }

                    foreach (var pref in prefsToUse)
                    {
                        if (pref.Channel == Channel.Email)
                        {
                            recipients.Add(new Recipient()
                            {
                                ProviderEndpoint = userProfile.EmailAddress,
                                Channel          = Channel.Email,
                                UserId           = userProfile.UserId,
                                UserEmail        = userProfile.EmailAddress,
                                CustomerNumber   = customer.CustomerNumber
                            });
                        }
                        else if (pref.Channel == Channel.MobilePush)
                        {
                            // lookup any and all mobile devices
                            foreach (var device in userPushNotificationDeviceRepository.ReadUserDevices(userProfile.UserId))
                            {
                                if (device.Enabled != false)
                                {
                                    recipients.Add(new Recipient()
                                    {
                                        ProviderEndpoint = device.ProviderEndpointId,
                                        DeviceOS         = device.DeviceOS,
                                        Channel          = Channel.MobilePush,
                                        UserId           = userProfile.UserId,
                                        UserEmail        = userProfile.EmailAddress,
                                        DeviceId         = device.DeviceId,
                                        CustomerNumber   = customer.CustomerNumber
                                    });
                                }
                            }
                        }
                        else if (pref.Channel == Channel.Web)
                        {
                            recipients.Add(new Recipient()
                            {
                                UserId         = userProfile.UserId,
                                CustomerNumber = customer.CustomerNumber,
                                Channel        = Channel.Web,
                                UserEmail      = userProfile.EmailAddress
                            });
                        }
                    }
                }
            }

            Dictionary <string, Recipient> dict = new Dictionary <string, Recipient>();

            foreach (Recipient rec in recipients)
            {
                string dupkey = rec.UserId + "_" + rec.CustomerNumber + "_" + rec.Channel + "_" + rec.ProviderEndpoint;
                if (dict.Keys.Contains(dupkey, StringComparer.CurrentCultureIgnoreCase) == false)
                {
                    dict.Add(dupkey, rec);
                }
            }

            return(dict.Values.ToList());
        }
 private void GetUserMessagingPreferences(NotificationType notificationType, Customer customer, UserProfileReturn users, out List <UserMessagingPreference> userDefaultMessagingPreferences, out List <UserMessagingPreference> customerMessagingPreferences, out List <UserMessagingPreference> ump)
 {
     userDefaultMessagingPreferences = _userMessagingPreferenceRepository.ReadByUserIdsAndNotificationType(users.UserProfiles.Select(u => u.UserId), notificationType, true).ToList();
     customerMessagingPreferences    = _userMessagingPreferenceRepository.ReadByCustomerAndNotificationType(customer.CustomerNumber, customer.CustomerBranch, notificationType).ToList();
     ump = new List <UserMessagingPreference>();
 }