Example #1
0
        public void PollForTestUpsell()
        {
            var notificationTypes = _notificationTypeRepository.GetAll();

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

            if (!notificationIsActive)
            {
                return;
            }

            var eventCustomers = _eventCustomerRepository.GetEventCustomersForTestUpsellNotification(1).ToArray();

            if (!eventCustomers.Any())
            {
                return;
            }
            eventCustomers = eventCustomers.OrderBy(ec => ec.EventId).ToArray();

            long eventId = 0;
            IEnumerable <EventTest> eventTests = null;

            foreach (var eventCustomer in eventCustomers)
            {
                try
                {
                    if (eventId != eventCustomer.EventId)
                    {
                        eventId    = eventCustomer.EventId;
                        eventTests = _eventTestRepository.GetTestsForEvent(eventId);
                        if (eventTests != null && eventTests.Any())
                        {
                            eventTests = eventTests.Where(et => et.Test.ShowInAlaCarte).Select(et => et).ToArray();
                        }
                    }

                    if (!eventTests.Any())
                    {
                        return;
                    }
                    var order = _orderRepository.GetOrder(eventCustomer.CustomerId, eventId);

                    var purchasedEventTestIds = new List <long>();
                    var packagePurchased      = _eventPackageRepository.GetPackageForOrder(order.Id);
                    if (packagePurchased != null)
                    {
                        purchasedEventTestIds.AddRange(packagePurchased.Tests.Select(t => t.Id).ToList());
                    }

                    var testPurchased = _eventTestRepository.GetTestsForOrder(order.Id);
                    if (testPurchased != null && testPurchased.Any())
                    {
                        purchasedEventTestIds.AddRange(testPurchased.Select(t => t.Id));
                    }

                    var sendNotification = false;
                    IEnumerable <EventTest> notpurchasedEventTests = null;
                    if (purchasedEventTestIds == null || !purchasedEventTestIds.Any())
                    {
                        notpurchasedEventTests = eventTests;
                        sendNotification       = true;
                    }
                    else
                    {
                        notpurchasedEventTests = eventTests.Where(et => !purchasedEventTestIds.Contains(et.Id)).Select(et => et).ToArray();
                        if (notpurchasedEventTests != null && notpurchasedEventTests.Any())
                        {
                            sendNotification = true;
                        }
                    }

                    if (sendNotification)
                    {
                        var customer = _customerRepository.GetCustomer(eventCustomer.CustomerId);
                        var tests    = notpurchasedEventTests.Select(et => et.Test).ToArray();
                        var testUpsellNotificationModel = _emailNotificationModelsFactory.GetTestUpsellNotificationModel(customer, tests);
                        _notifier.NotifySubscribersViaEmail(NotificationTypeAlias.TestUpsellNotification, EmailTemplateAlias.TestUpsellNotification, testUpsellNotificationModel, customer.Id, customer.CustomerId, "TestUpsellNotification");
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(string.Format("Test Upsell Notification Error For Customer Id : {0} and Event Id {1} \nMessage:{2} \nStackTrace: {3}", eventCustomer.CustomerId, eventCustomer.EventId, ex.Message, ex.StackTrace));
                }
            }
        }