Exemplo n.º 1
0
        public ProductOperationStatus GetProduct(int productId, int optionId)
        {
            //TODO: give option of using Examine to get nodes
            var operationStatus = new ProductOperationStatus();
            try
            {
                var product = new Product();
                //get the info from Umbraco
                Node productNode = new Node(productId);
                Node optionNode = (optionId == 0) ? new Node(productId) : new Node(optionId);

                product.ProductId = productId;

                IProductTypeSetting productTypeSetting =
                    _config.GetConfiguration().ProductTypeSettings[productNode.NodeTypeAlias];

                product.OrderIndex = (productTypeSetting != null)
                                         ? productTypeSetting.OrderIndex
                                         : 1;

                product.Title = (productTypeSetting != null)
                                    ? UmbracoHelper.InsertPropertyAliases(productNode, productTypeSetting.TitleProperty)
                                    : UmbracoHelper.InsertPropertyAliases(productNode,
                                                                          _config.GetConfiguration()
                                                                                 .ProductTitleProperty);

                product.ProductUrl = (productNode.template > 0) ? productNode.NiceUrl : "";

                product.ProductExternalId = (productTypeSetting != null)
                                                ? UmbracoHelper.GetPropertyAlias(productNode,
                                                                                 productTypeSetting.ExternalIdProperty)
                                                : UmbracoHelper.GetPropertyAlias(productNode,
                                                                                 _config.GetConfiguration()
                                                                                        .ProductExternalIdProperty);

                string locationConfig = (productTypeSetting != null)
                                            ? productTypeSetting.LocationHandler
                                            : _config.GetConfiguration().LocationHandler;

                var locationHandler = _locationHandlerFactory.GetLocationHandler(locationConfig);

                string locationCode = (productTypeSetting != null)
                                          ? UmbracoHelper.GetPropertyAlias(productNode,
                                                                           productTypeSetting.LocationProperty)
                                          : "";

                product.LocationCode = locationCode;

                var location = locationHandler.GetLocation(locationCode);

                product.Location = location.Title;
                product.LocationEmail = location.Email;

                product.StartDate = (productTypeSetting != null)
                                        ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                 productTypeSetting
                                                                                     .StartDateProperty)
                                        : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                 _config.GetConfiguration()
                                                                                        .ProductStartDateProperty);

                product.FinishDate = (productTypeSetting != null)
                                         ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  productTypeSetting.FinishDateProperty)
                                         : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  _config.GetConfiguration()
                                                                                         .ProductFinishDateProperty);

                product.StartDateRange1 = (productTypeSetting != null)
                                        ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                 productTypeSetting
                                                                                     .StartDateRange1Property)
                                        : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                 _config.GetConfiguration()
                                                                                        .ProductStartDateRange1Property);

                product.FinishDateRange1 = (productTypeSetting != null)
                                         ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  productTypeSetting.FinishDateRange1Property)
                                         : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  _config.GetConfiguration()
                                                                                         .ProductFinishDateRange1Property);

                product.StartDateRange2 = (productTypeSetting != null)
                                        ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                 productTypeSetting
                                                                                     .StartDateRange2Property)
                                        : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                 _config.GetConfiguration()
                                                                                        .ProductStartDateRange2Property);

                product.FinishDateRange2 = (productTypeSetting != null)
                                         ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  productTypeSetting.FinishDateRange2Property)
                                         : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  _config.GetConfiguration()
                                                                                         .ProductFinishDateRange2Property);

                product.ProductType = (productTypeSetting != null)
                                          ? productTypeSetting.ProductType
                                          : _config.GetConfiguration().ProductType;

                product.ProductTypeCode = productNode.NodeTypeAlias;

                product.Category = (productTypeSetting != null)
                                       ? UmbracoHelper.GetPropertyAlias(productNode, productTypeSetting.CategoryProperty)
                                       : UmbracoHelper.GetPropertyAlias(productNode,
                                                                        _config.GetConfiguration()
                                                                               .ProductCategoryProperty);

                product.VoucherCategory = (productTypeSetting != null)
                                              ? UmbracoHelper.GetPropertyAlias(productNode,
                                                                               productTypeSetting
                                                                                   .VoucherCategoryProperty)
                                              : UmbracoHelper.GetPropertyAlias(productNode,
                                                                               _config.GetConfiguration()
                                                                                      .ProductVoucherCategoryProperty);

                product.Code = (productTypeSetting != null)
                                   ? UmbracoHelper.GetPropertyAlias(productNode, productTypeSetting.CodeProperty)
                                   : UmbracoHelper.GetPropertyAlias(productNode,
                                                                    _config.GetConfiguration().ProductCodeProperty);

                product.Status = (productTypeSetting != null)
                                     ? UmbracoHelper.GetPropertyAlias(productNode, productTypeSetting.StatusProperty)
                                     : UmbracoHelper.GetPropertyAlias(productNode,
                                                                      _config.GetConfiguration().ProductStatusProperty);

                product.EcommerceDisabled = (productTypeSetting != null)
                                                ? UmbracoHelper.GetBooleanPropertyAlias(productNode,
                                                                                        productTypeSetting
                                                                                            .EcommerceDisabledProperty, false)
                                                : UmbracoHelper.GetBooleanPropertyAlias(productNode,
                                                                                        _config.GetConfiguration()
                                                                                               .ProductEcommerceDisabledProperty,
                                                                                        false);

                product.DepositOptionDisabled = (productTypeSetting != null)
                                                ? UmbracoHelper.GetBooleanPropertyAlias(productNode,
                                                                                        productTypeSetting
                                                                                            .DepositOptionDisabledProperty, false)
                                                : UmbracoHelper.GetBooleanPropertyAlias(productNode,
                                                                                        _config.GetConfiguration()
                                                                                               .ProductDepositOptionDisabledProperty,
                                                                                        false);

                product.DepositAmount = (productTypeSetting != null)
                                            ? productTypeSetting.DepositAmount
                                            : _config.GetConfiguration().ProductDepositAmount;

                product.MaximumQuantity = (productTypeSetting != null)
                                              ? productTypeSetting.MaximumQuantity
                                              : 0;

                product.OptionId = optionId;

                string priceAlias = (productTypeSetting != null)
                                        ? productTypeSetting.PriceProperty
                                        : _config.GetConfiguration().ProductPriceProperty;

                string earlyBirdPriceAlias = (productTypeSetting != null)
                        ? productTypeSetting.EarlyBirdPriceProperty
                        : _config.GetConfiguration().ProductEarlyBirdPriceProperty;

                decimal? price, earlyBirdPrice = null;
                decimal tempPrice, tempEarlyBirdPrice;

                if (optionId > 0)
                {
                    product.OptionTitle = (productTypeSetting != null)
                                              ? UmbracoHelper.InsertPropertyAliases(optionNode,
                                                                                    productTypeSetting
                                                                                        .OptionProperty)
                                              : UmbracoHelper.InsertPropertyAliases(optionNode,
                                                                                    _config.GetConfiguration()
                                                                                           .ProductOptionProperty);
                    product.OptionExternalId = (productTypeSetting != null)
                                                   ? UmbracoHelper.GetPropertyAlias(optionNode,
                                                                                    productTypeSetting
                                                                                        .OptionExternalIdProperty)
                                                   : UmbracoHelper.GetPropertyAlias(optionNode,
                                                                                    _config.GetConfiguration()
                                                                                           .ProductOptionExternalIdProperty);
                    price = decimal.TryParse(UmbracoHelper.GetPropertyAlias(optionNode, priceAlias), out tempPrice) ? tempPrice : (decimal?)null;
                    earlyBirdPrice=decimal.TryParse(UmbracoHelper.GetPropertyAlias(optionNode, earlyBirdPriceAlias), out tempEarlyBirdPrice) ? tempEarlyBirdPrice : (decimal?)null;

                }
                else
                {
                    price = decimal.TryParse(UmbracoHelper.GetPropertyAlias(productNode, priceAlias), out tempPrice) ? tempPrice : (decimal?)null;
                    earlyBirdPrice = decimal.TryParse(UmbracoHelper.GetPropertyAlias(productNode, earlyBirdPriceAlias),
                        out tempEarlyBirdPrice)
                        ? tempEarlyBirdPrice
                        : (decimal?) null;

                }
                product.Price = price;
                product.EarlyBirdPrice = earlyBirdPrice;
                product.BespokePricingHandler = (productTypeSetting != null)
                                                           ? productTypeSetting.BespokePricingHandler
                                                           : "";

                product.EarlyBirdPriceCutOffDate = (productTypeSetting != null)
                                         ? UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  productTypeSetting.EarlyBirdPriceCutOffDateProperty)
                                         : UmbracoHelper.GetDateTimePropertyAlias(productNode,
                                                                                  _config.GetConfiguration()
                                                                                         .ProductEarlyBirdPriceCutOffDateProperty);

                operationStatus.Product = product;
                operationStatus.Status = true;
            }
            catch (Exception e)
            {
                operationStatus = OperationStatusExceptionHelper<ProductOperationStatus>
                    .CreateFromException("An error has occurred retrieving the product", e);
            }

            return operationStatus;
        }
        public void Initialise()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType<ContactRepository>().As<IContactRepository>();
            builder.RegisterType<ContactSecondaryRepository>().As<IContactSecondaryRepository>();
            builder.RegisterType<DefaultContactSynchroniser>().As<IContactSynchroniser>();
            builder.RegisterType<ContactService>().As<IContactService>();
            builder.RegisterType<MailChimpEmailSubscriptionConnector>().As<IEmailSubscriptionConnector>();
            builder.RegisterType<OrderRepository>().As<IOrderRepository>();
            builder.RegisterType<DiscountHandlerFactory>().As<IDiscountHandlerFactory>();
            builder.RegisterType<EcommerceService>().As<IEcommerceService>();
            builder.RegisterType<ShippingHandlerFactory>().As<IShippingHandlerFactory>();
            builder.RegisterType<LocationHandlerFactory>().As<ILocationHandlerFactory>();
            builder.RegisterType<LocationHandler>().As<ILocationHandler>();
            builder.RegisterType<OrderQueueService>().As<IOrderQueueService>();
            builder.RegisterType<EmailSubscriptionsService>().As<IEmailSubscriptionsService>();
            builder.RegisterType<BespokePricingHandlerFactory>().As<IBespokePricingHandlerFactory>();
            builder.RegisterType<OrderQueue>().As<IOrderQueue>();
            builder.RegisterType<OrderCoordinator>().As<IOrderCoordinator>();
            builder.RegisterType<Database>()
                .WithParameter("connectionString",
                    ConfigurationManager.ConnectionStrings["umbracoDbDSN"].ConnectionString)
                .WithParameter("providerName", "System.Data.SqlClient");

            //mocking config service
            var configService = new Mock<IConfigurationService>();
            configService.Setup(x => x.GetConfiguration()).Returns(new CustomerPortalSection());

            var product1 = new Product
            {
                Title = "Test Publication",
                Code = "OP1",
                ProductId = 100,
                OptionId = 0,
                Price = 5,
                ProductType = "P",
                OrderIndex = 2
            };
            var product1Status = new ProductOperationStatus {Status = true, Product = product1};

            var course1 = new Product
            {
                Title = "Test Course 1",
                Location = "Flatford Mill",
                LocationCode = "FM",
                DepositAmount = 50,
                ProductId = 1000,
                OptionId = 2000,
                Price = 300,
                ProductType = "C",
                OptionTitle = "Sole Residency",
                OrderIndex = 1,
                Category = "F/1",
                VoucherCategory = ""
            };
            var course1Status = new ProductOperationStatus {Status = true, Product = course1};

            var course1WithoutOptions = new Product
            {
                Title = "Test Course 1",
                Location = "Flatford Mill",
                LocationCode = "FM",
                DepositAmount = 50,
                ProductId = 1000,
                ProductType = "C",
                OrderIndex = 1,
                Category = "F/1",
                VoucherCategory = ""
            };
            var course1WithoutOptionsStatus = new ProductOperationStatus {Status = true, Product = course1WithoutOptions};

            var course1Options = new List<ProductOption>();

            course1Options.Add(new ProductOption {OptionId = 2000,Name="Sole Residency",OptionPrice=300});
            course1Options.Add(new ProductOption { OptionId = 2001, Name = "Shared Room", OptionPrice = 250 });
            course1Options.Add(new ProductOption { OptionId = 2002, Name = "Non Resident", OptionPrice = 200 });
            var course1OptionsStatus = new ProductOptionOperationStatus {Status = true, ProductOptions = course1Options};

            var course2 = new Product
                {
                    Title = "Test Course 2",
                    Location= "Juniper Hall",
                    LocationCode = "JH",
                    DepositAmount = 50,
                    ProductId = 1001,
                    OptionId = 2001,
                    Price = 250,
                    ProductType = "C",
                    OptionTitle = "Shared room",
                    OrderIndex = 1,
                    Category = "H/1",
                    VoucherCategory = "TESTFILT1"
                };
            var course2Status = new ProductOperationStatus {Status = true, Product = course2};

            var courseBespoke1 = new Product
            {
                Title = "Test Bespoke Course 1",
                Location = "Juniper Hall",
                LocationCode = "JH",
                DepositAmount = 50,
                ProductId = 2000,
                OptionId = 0,
                Price = (decimal)16.50,
                ProductType = "C",
                OptionTitle = "Sole occupancy",
                OrderIndex = 3,
                Category = "F/10",
                VoucherCategory = ""
            };
            var courseBespoke1Status = new ProductOperationStatus { Status = true, Product = courseBespoke1 };

            var courseBespoke2 = new Product
            {
                Title = "Test Bespoke Course 1",
                Location = "Juniper Hall",
                StartDateRange1 = new DateTime(2014,08,01),
                FinishDateRange1 = new DateTime(2014, 08, 10),
                LocationCode = "JH",
                DepositAmount = 50,
                ProductId = 2001,
                OptionId = 0,
                Price = (decimal)16.50,
                ProductType = "C",
                OptionTitle = "Sole occupancy",
                OrderIndex = 3,
                Category = "F/10",
                VoucherCategory = ""
            };
            var courseBespoke2Status = new ProductOperationStatus { Status = true, Product = courseBespoke2 };

            var Voucher20PercentOff = new Voucher
            {
                Title = "Ten per cent off",
                VoucherId = 1,
                Percentage = 10,
                OrderIndex = 1
            };
            var Voucher20PercentOffStatus = new VoucherOperationStatus
            {
                Status = true,
                Voucher = Voucher20PercentOff
            };

            var Voucher20PercentOffFamilyCourses = new Voucher
                {
                    Title = "Twenty per cent off family courses",
                    VoucherId = 2,
                    Percentage = 20,
                    OrderIndex = 1,
                    ProductCategoryFilter = "F",
                };
            var Voucher20PercentOffFamilyCoursesStatus = new VoucherOperationStatus
                {
                    Status = true,
                    Voucher = Voucher20PercentOffFamilyCourses
                };

            var Voucher20PoundPerItemWhenSpent100Pounds = new Voucher
                {
                    Title = "Twenty pounds per item off when hundred pounds spent",
                    VoucherId = 3,
                    PerItemAmount = 20,
                    OrderIndex = 1,
                    MinimumPayment = 100
                };

            var Voucher20PoundPerItemWhenSpent100PoundsStatus = new VoucherOperationStatus
                {
                    Status = true,
                    Voucher = Voucher20PoundPerItemWhenSpent100Pounds
                };

            var Voucher20PerCentWhenSpent100Pounds = new Voucher
                {
                    Title = "Twenty pounds per cent off when hundred pounds spent",
                    VoucherId = 4,
                    PerItemAmount = 10,
                    OrderIndex = 2,
                    MinimumPayment = 100
                };

            var Voucher20PerCentWhenSpent100PoundsStatus = new VoucherOperationStatus
                {
                    Status = true,
                    Voucher = Voucher20PerCentWhenSpent100Pounds
                };

            var Voucher10PerCentOnSelectedCourses = new Voucher
            {
                Title = "Ten per cent off selected courses",
                VoucherId = 5,
                Percentage = 10,
                VoucherCategoryFilter = "TESTFILT1",
                OrderIndex = 1,
            };

            var Voucher10PerCentOnSelectedCoursesStatus = new VoucherOperationStatus
            {
                Status = true,
                Voucher = Voucher10PerCentOnSelectedCourses
            };

            var VoucherNotFoundStatus = new VoucherOperationStatus
            {
                Status = false,
                Message="Voucher not found"
            };

            //need to mock productRepository as tied to Umbraco nodes
            var productRepository = new Mock<IProductRepository>();
            productRepository.Setup(x => x.GetProduct(100, 0)).Returns(product1Status);
            productRepository.Setup(x => x.GetProduct(1000,0)).Returns(course1WithoutOptionsStatus);
            productRepository.Setup(x => x.GetProduct(1000, 2000)).Returns(course1Status);
            productRepository.Setup(x => x.GetProductOptions(1000)).Returns(course1OptionsStatus);
            productRepository.Setup(x => x.GetProduct(1001, 2001)).Returns(course2Status);
            productRepository.Setup(x => x.GetProduct(2000, 0)).Returns(courseBespoke1Status);
            productRepository.Setup(x => x.GetProduct(2001, 0)).Returns(courseBespoke2Status);

            //need to mock VoucherRepository as tied to Umbraco nodes
            var VoucherRepository = new Mock<IVoucherRepository>();
            VoucherRepository.Setup(x => x.GetVoucher("TEST1")).Returns(Voucher20PercentOffStatus);
            VoucherRepository.Setup(x => x.GetVoucher("TEST2")).Returns(Voucher20PercentOffFamilyCoursesStatus);
            VoucherRepository.Setup(x => x.GetVoucher("TEST3")).Returns(Voucher20PoundPerItemWhenSpent100PoundsStatus);
            VoucherRepository.Setup(x => x.GetVoucher("TEST4")).Returns(Voucher20PerCentWhenSpent100PoundsStatus);
            VoucherRepository.Setup(x => x.GetVoucher("TEST5")).Returns(Voucher10PerCentOnSelectedCoursesStatus);

            VoucherRepository.Setup(x => x.GetVoucher("TESTNOTFOUND")).Returns(VoucherNotFoundStatus);

            //need to mock IContactAuthenticationHandler as tied to HttpContext
            var contactAuthenticationHandler = new Mock<IContactAuthenticationHandler>();
            contactAuthenticationHandler.Setup(x => x.IsCurrentUserLoggedIn()).Returns(false);
            contactAuthenticationHandler.Setup(x => x.GetUserId()).Returns(Guid.NewGuid().ToString());

            builder.RegisterInstance(configService.Object).As<IConfigurationService>();
            builder.RegisterInstance(contactAuthenticationHandler.Object).As<IContactAuthenticationHandler>();
            builder.RegisterInstance(productRepository.Object).As<IProductRepository>();
            builder.RegisterInstance(VoucherRepository.Object).As<IVoucherRepository>();

            var container = builder.Build();

            _sut = container.Resolve<IEcommerceService>();
        }