public void PollforEventResultReadyNotification()
        {
            var notificationTypes = _notificationTypeRepository.GetAll();

            var notificationIsActive = notificationTypes.Any(nt => (nt.NotificationTypeAlias == NotificationTypeAlias.EventResultReadyNotification) && nt.IsActive);

            if (!notificationIsActive)
            {
                return;
            }

            var eventIds = _eventCustomerResultRepository.GetEventIdsForEventResultReadyNotiFication().ToArray();

            if (eventIds.Count() == 0)
            {
                return;
            }

            var createdByOrgRoleUserId = _organizationRoleUserRepository.GetOrganizationRoleUserIdsForRole((long)Roles.FranchisorAdmin).First();

            foreach (var eventId in eventIds)
            {
                try
                {
                    var notificationSent = _eventNotificationRepository.GetByEventId(eventId, NotificationTypeAlias.EventResultReadyNotification);
                    if (notificationSent != null)
                    {
                        continue;
                    }

                    var hospitalPartnerId = _hospitalPartnerRepository.GetHospitalPartnerIdForEvent(eventId);
                    if (hospitalPartnerId >= 1)
                    {
                        var partnerReleaseSinged = _eventCustomerResultRepository.CheckAnyCustomerResultsReadyAndSignedPartnerRelease(eventId);
                        if (partnerReleaseSinged)
                        {
                            var allResultsReady = _eventCustomerResultRepository.CheckAllCustomerResultsReadyForEvent(eventId);

                            if (!allResultsReady)
                            {
                                _logger.Info(string.Format("All Results are not ready For Event Id {0}", eventId));
                            }

                            var orgRoleUsers        = _organizationRoleUserRepository.GetOrganizationRoleUsersByOrganizationId(hospitalPartnerId);
                            var hospitalFacilityIds = _hospitalFacilityRepository.GetSelectedHospitalFacilityIdForEvent(eventId);
                            if (hospitalFacilityIds != null && hospitalFacilityIds.Any())
                            {
                                foreach (var hospitalFacilityId in hospitalFacilityIds)
                                {
                                    var hospitalFacilityOrgRoleUsers = _organizationRoleUserRepository.GetOrganizationRoleUsersByOrganizationId(hospitalFacilityId);
                                    if (hospitalFacilityOrgRoleUsers != null && hospitalFacilityOrgRoleUsers.Any())
                                    {
                                        orgRoleUsers = orgRoleUsers.Concat(hospitalFacilityOrgRoleUsers);
                                    }
                                }
                            }
                            var userIds = orgRoleUsers.Select(oru => oru.UserId).ToList();

                            var users             = _userRepository.GetUsers(userIds);
                            var eventHostViewData = _eventService.GetById(eventId);

                            foreach (var user in users)
                            {
                                if (user.Email == null || string.IsNullOrEmpty(user.Email.ToString()))
                                {
                                    continue;
                                }

                                var model         = _emailNotificationModelsFactory.GetEventResultReadyNotificationViewModel(user, eventHostViewData);
                                var notifications = _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.EventResultReadyNotification, EmailTemplateAlias.EventResultReadyNotification, model, new string[] { user.Email.ToString() }, user.Id, createdByOrgRoleUserId, "Event Result Ready Notification");
                                if (notifications != null && notifications.Any())
                                {
                                    var eventNotification = new EventNotification {
                                        EventId = eventId, NotificationId = notifications.First().Id
                                    };
                                    _eventNotificationRepository.Save(eventNotification);
                                }
                            }
                        }
                    }

                    //var account = _corporateAccountRepository.GetbyEventId(eventId);
                    //if (account != null && account.Id > 0 && account.SendEventResultReadyNotification)
                    //{
                    //    var orgRoleUsers = _organizationRoleUserRepository.GetOrganizationRoleUsersByOrganizationIdAndRoleId(account.Id, (long)Roles.CorporateAccountCoordinator);

                    //    var userIds = orgRoleUsers.Select(oru => oru.UserId).ToList();

                    //    var users = _userRepository.GetUsers(userIds);
                    //    var eventHostViewData = _eventService.GetById(eventId);

                    //    foreach (var user in users)
                    //    {
                    //        if (user.Email == null || string.IsNullOrEmpty(user.Email.ToString()))
                    //            continue;

                    //        var model = _emailNotificationModelsFactory.GetEventResultReadyNotificationViewModel(user, eventHostViewData);
                    //        var notifications = _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.EventResultReadyNotification, EmailTemplateAlias.EventResultReadyNotification, model, new string[] { user.Email.ToString() }, user.Id, createdByOrgRoleUserId, "Event Result Ready Notification");
                    //        if (notifications != null && notifications.Any())
                    //        {
                    //            var eventNotification = new EventNotification { EventId = eventId, NotificationId = notifications.First().Id };
                    //            _eventNotificationRepository.Save(eventNotification);
                    //        }
                    //    }
                    //}
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Event Result Ready Notification Error For Event Id {0} \nMessage:{1} \nStackTrace: {2}", eventId, ex.Message, ex.StackTrace));
                }
            }
        }
Exemple #2
0
        public void PollforEventResultReadyNotification()
        {
            var notificationTypes = _notificationTypeRepository.GetAll();

            var notificationIsActive = notificationTypes.Any(nt => (nt.NotificationTypeAlias == NotificationTypeAlias.EventResultReadyNotification) && nt.IsActive);

            if (!notificationIsActive)
            {
                return;
            }

            if (!_cutofDate.HasValue)
            {
                _logger.Info("Please provide cut of date to sent Corporate Event Result Ready Notification");
                return;
            }

            var eventIds = _eventCustomerResultRepository.GetEventIdsForCorporateAccountEventResultReadyNotiFication(_cutofDate.Value).ToArray();

            if (eventIds.Count() == 0)
            {
                _logger.Info("No Corporate Event found for sending Result Ready notification");
                return;
            }

            var createdByOrgRoleUserId = _organizationRoleUserRepository.GetOrganizationRoleUserIdsForRole((long)Roles.FranchisorAdmin).First();

            foreach (var eventId in eventIds)
            {
                try
                {
                    var notificationSent = _eventNotificationRepository.GetByEventId(eventId, NotificationTypeAlias.EventResultReadyNotification);
                    if (notificationSent != null && notificationSent.IsForCorporateAccount)
                    {
                        continue;
                    }

                    var account = _corporateAccountRepository.GetbyEventId(eventId);
                    if (account != null && account.SendEventResultReadyNotification)
                    {
                        var orgRoleUsers = _organizationRoleUserRepository.GetOrganizationRoleUsersByOrganizationIdAndRoleId(account.Id, (long)Roles.CorporateAccountCoordinator);

                        if (!orgRoleUsers.IsNullOrEmpty())
                        {
                            var userIds = orgRoleUsers.Select(oru => oru.UserId).ToList();

                            var users             = _userRepository.GetUsers(userIds);
                            var eventHostViewData = _eventService.GetById(eventId);

                            foreach (var user in users)
                            {
                                if (user.Email == null || string.IsNullOrEmpty(user.Email.ToString()))
                                {
                                    continue;
                                }

                                var model =
                                    _emailNotificationModelsFactory.GetEventResultReadyNotificationViewModel(user,
                                                                                                             eventHostViewData);
                                var notifications =
                                    _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.EventResultReadyNotification, EmailTemplateAlias.EventResultReadyNotification, model, new string[] { user.Email.ToString() }, user.Id, createdByOrgRoleUserId,
                                                                        "Event Result Ready Notification");

                                if (notifications != null && notifications.Any())
                                {
                                    var eventNotification = new EventNotification
                                    {
                                        EventId               = eventId,
                                        NotificationId        = notifications.First().Id,
                                        IsForCorporateAccount = true
                                    };
                                    _eventNotificationRepository.SaveForCorporateAccount(eventNotification);
                                }
                            }
                        }
                        else
                        {
                            _logger.Info(string.Format("No coordinator Attached to Event For Account {0}  is Off", account.Id));
                        }
                    }
                    else
                    {
                        _logger.Info(string.Format("Send Event Result Ready Notification For Account {0}  is Off", account.Id));
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Corporat Event Result Ready Notification Error For Event Id {0} \nMessage:{1} \nStackTrace: {2}", eventId, ex.Message, ex.StackTrace));
                }
            }
        }