Exemplo n.º 1
0
        public async Task When_Company_Has_Children_Should_Call_SaveChanges_For_All_Child_Company_Catalogs()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 2,
                ProductItemId    = 4,
                CompanyId        = 1,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = false,
            };

            var company = _companiesDbSet.FirstOrDefault(c => c.Id == model.CompanyId);
            var descendantsAffectedByUpdate = GetCompanyDescendantsAffectedByUpdate(company, model.CatalogId);

            await _sut.AddProductItemToCatalog(model);

            //starting from 1, because it needs to call save changes for the parent
            var numOfTimesSaveChangesShouldBeCalled  = 1;
            var numOfCompanyCatalogsAffectedByUpdate = 0;

            descendantsAffectedByUpdate.ForEach(c =>
            {
                c.CompanyCatalogs.Where(cc => cc.CatalogType == CatalogType.MyCatalog).ToList().ForEach(cc =>
                {
                    numOfCompanyCatalogsAffectedByUpdate++;
                });
            });

            numOfTimesSaveChangesShouldBeCalled += numOfCompanyCatalogsAffectedByUpdate;

            _dbContextMock.Verify(x => x.SaveChangesAsync(), Times.Exactly(numOfTimesSaveChangesShouldBeCalled));
        }
Exemplo n.º 2
0
        public async Task When_Company_Has_Parent_And_FixedRetailPrice_Is_True_For_Product_In_Assigned_Catalog_Should_Set_RetailPrice_To_RetailPrice_From_AssignedCatalog()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 11,
                ProductItemId    = 1,
                CompanyId        = 9,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = false,
            };

            var company                  = _companiesDbSet.FirstOrDefault(c => c.Id == model.CompanyId);
            var assignedCatalog          = company?.CompanyCatalogs.FirstOrDefault(cc => cc.CatalogType == CatalogType.Assigned)?.Catalog;
            var expectedRetailPriceValue = assignedCatalog?.CatalogProductItems.FirstOrDefault(cp => cp.ProductItemId == model.ProductItemId)?.RetailPrice;

            var editedCatalog = company?.CompanyCatalogs.FirstOrDefault(cc => cc.CatalogId == model.CatalogId)?.Catalog;

            await _sut.AddProductItemToCatalog(model);

            var catalogProductItem = editedCatalog?.CatalogProductItems.FirstOrDefault(cp => cp.ProductItemId == model.ProductItemId);
            var retailPrice        = catalogProductItem?.RetailPrice;

            retailPrice.Should().Be(expectedRetailPriceValue);
        }
Exemplo n.º 3
0
        public async Task When_Company_Has_Children_Should_Add_CatalogProductItem_To_Childrens_CompanyCatalogs()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 2,
                ProductItemId    = 4,
                CompanyId        = 1,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = false,
            };

            var company = _companiesDbSet.FirstOrDefault(c => c.Id == model.CompanyId);
            var descendantsAffectedByUpdate = GetCompanyDescendantsAffectedByUpdate(company, model.CatalogId);

            await _sut.AddProductItemToCatalog(model);

            descendantsAffectedByUpdate.ForEach(c =>
            {
                c.CompanyCatalogs.Where(cc => cc.CatalogType == CatalogType.MyCatalog).ToList().ForEach(cc =>
                {
                    var catalogProductItem =
                        cc.Catalog.CatalogProductItems.FirstOrDefault(p => p.ProductItemId == model.ProductItemId);

                    catalogProductItem.Should().NotBeNull();
                });
            });
        }
Exemplo n.º 4
0
        public void When_CompanyCatalog_With_CatalogId_Is_Not_In_CompanyCatalogs_Should_Throw_NonExistingCompanyCatalogException()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 7,
                ProductItemId    = 3,
                CompanyId        = 5,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = true,
            };

            Func <Task> act = async() => { await _sut.AddProductItemToCatalog(model); };

            act.Should().Throw <NoProductInProductCatalogException>();
        }
Exemplo n.º 5
0
        public void When_Company_Has_No_Assigned_Catalog_Should_Throw_NoAssignedCatalogException()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 2,
                ProductItemId    = 1,
                CompanyId        = 7,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = true,
            };

            Func <Task> act = async() => { await _sut.AddProductItemToCatalog(model); };

            act.Should().Throw <NoAssignedCatalogException>();
        }
Exemplo n.º 6
0
        public void When_Company_Does_Not_Exist_Should_Throw_CompanyNotFoundException()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 2,
                ProductItemId    = 1,
                CompanyId        = 20,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = true,
            };

            Func <Task> act = async() => { await _sut.AddProductItemToCatalog(model); };

            act.Should().Throw <CompanyNotFoundException>();
        }
Exemplo n.º 7
0
        public async Task When_Company_Has_No_Parent_Should_Set_RetailPrice_To_Value_From_Model()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 2,
                ProductItemId    = 4,
                CompanyId        = 1,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = false,
            };

            var company = _companiesDbSet.FirstOrDefault(c => c.Id == model.CompanyId);
            var catalog = company?.CompanyCatalogs.FirstOrDefault(cc => cc.CatalogId == model.CatalogId)?.Catalog;

            await _sut.AddProductItemToCatalog(model);

            var catalogProductItem = catalog?.CatalogProductItems.FirstOrDefault(cp => cp.ProductItemId == model.ProductItemId);
            var retailPrice        = catalogProductItem?.RetailPrice;

            retailPrice.Should().Be(model.RetailPrice);
        }
Exemplo n.º 8
0
        public async Task When_CatalogProductItem_With_ProductItemId_Does_Not_Exist_In_Catalog_Should_Add_CatalogProductItem_To_Catalog()
        {
            var model = new CatalogProductModel
            {
                CatalogId        = 4,
                ProductItemId    = 4,
                CompanyId        = 2,
                ResellerPrice    = 10,
                RetailPrice      = 15,
                FixedRetailPrice = true,
            };

            var company            = _companiesDbSet.FirstOrDefault(c => c.Id == model.CompanyId);
            var catalog            = company?.CompanyCatalogs.FirstOrDefault(cc => cc.CatalogId == model.CatalogId)?.Catalog;
            var catalogProductItem = catalog?.CatalogProductItems.FirstOrDefault(cp => cp.ProductItemId == model.ProductItemId);

            catalogProductItem.Should().BeNull();

            await _sut.AddProductItemToCatalog(model);

            catalogProductItem = catalog?.CatalogProductItems.FirstOrDefault(cp => cp.ProductItemId == model.ProductItemId);

            catalogProductItem.Should().NotBeNull();
        }
        public async Task AddProductItemToCatalog(CatalogProductModel productCatalogModel)
        {
            var company = await _dbContext.Companies.Include(c => c.Parent).Include(c => c.MyCompanies.Select(mc => mc.CompanyCatalogs))
                          .Include(c => c.CompanyCatalogs.Select(cc => cc.Catalog.CatalogProductItems)).FirstOrDefaultAsync(c => c.Id == productCatalogModel.CompanyId);

            if (company == null)
            {
                throw new CompanyNotFoundException($"CompanyId: {productCatalogModel.CompanyId}");
            }

            var assignedCatalog = company.CompanyCatalogs.FirstOrDefault(c => c.CatalogType == CatalogType.Assigned);

            if (assignedCatalog == null)
            {
                throw new NoAssignedCatalogException($"CompanyId: {productCatalogModel.CompanyId}");
            }

            var assignedCatalogProductItem = assignedCatalog.Catalog.CatalogProductItems.FirstOrDefault(p => p.ProductItemId == productCatalogModel.ProductItemId);

            if (assignedCatalogProductItem == null)
            {
                throw new NoProductInProductCatalogException($"CompanyId: {productCatalogModel.CompanyId}, CatalogId: {assignedCatalog.Catalog.Id}, ProductId: {productCatalogModel.ProductItemId}");
            }

            var companyCatalog = company.CompanyCatalogs.FirstOrDefault(c => c.CatalogId == productCatalogModel.CatalogId && c.CatalogType == CatalogType.MyCatalog);

            if (companyCatalog == null)
            {
                throw new NonExistingCompanyCatalogException($"CompanyId: {company.Id}, CatalogI: {productCatalogModel.CatalogId}");
            }

            if (companyCatalog.Catalog.CatalogProductItems.All(p => p.ProductItemId != productCatalogModel.ProductItemId))
            {
                companyCatalog.Catalog.CatalogProductItems.Add(new CatalogProductItem
                {
                    CatalogId        = productCatalogModel.CatalogId,
                    ProductItemId    = productCatalogModel.ProductItemId,
                    ResellerPrice    = productCatalogModel.ResellerPrice,
                    RetailPrice      = company.Parent == null ? productCatalogModel.RetailPrice : assignedCatalogProductItem.FixedRetailPrice ? assignedCatalogProductItem.RetailPrice : productCatalogModel.RetailPrice,
                    FixedRetailPrice = company.Parent == null ? productCatalogModel.FixedRetailPrice : assignedCatalogProductItem.FixedRetailPrice,
                    Available        = true
                });
            }

            foreach (var myCompany in company.MyCompanies.Where(c => c.CompanyCatalogs.Any(cc => cc.CatalogType == CatalogType.Assigned && cc.CatalogId == productCatalogModel.CatalogId)))
            {
                foreach (var myCompanyCatalog in myCompany.CompanyCatalogs.Where(c => c.CatalogType == CatalogType.MyCatalog))
                {
                    await AddProductItemToCatalog(new CatalogProductModel
                    {
                        CompanyId        = myCompany.Id,
                        ProductItemId    = productCatalogModel.ProductItemId,
                        CatalogId        = myCompanyCatalog.CatalogId,
                        FixedRetailPrice = company.Parent == null ? productCatalogModel.FixedRetailPrice : assignedCatalogProductItem.FixedRetailPrice,
                        RetailPrice      = company.Parent == null ? productCatalogModel.RetailPrice : assignedCatalogProductItem.FixedRetailPrice ? assignedCatalogProductItem.RetailPrice : productCatalogModel.RetailPrice,
                        ResellerPrice    = _catalogUtilities.CalculateResellerPrice(productCatalogModel.RetailPrice, productCatalogModel.ResellerPrice)
                    });
                }
            }

            await _dbContext.SaveChangesAsync();
        }