Beispiel #1
0
        private async Task <bool> UpdateCategoryIfAlreadyExistsInOtherAffiliateProgram(
            IList <Category> existingCategorys,
            IList <AffiliateCategoryMatch> existingMatches,
            AffiliateCategory affiliateCategory)
        {
            if (existingCategorys == null || !existingCategorys.Any())
            {
                return(false);
            }
            if (existingMatches == null || !existingMatches.Any())
            {
                return(false);
            }

            var categoryToUpdate = existingCategorys.FirstOrDefault(category => category.IsMatchableName(affiliateCategory.Name));

            if (categoryToUpdate == null)
            {
                return(false);
            }

            var newMatch = AffiliateCategoryMatch.Create(categoryToUpdate, affiliateCategory);
            await _matchesRepository.SaveAsync(newMatch);

            UpdateCategoryProperties(categoryToUpdate, affiliateCategory, existingMatches);
            await _categoryRepository.SaveAsync(categoryToUpdate);

            return(true);
        }
Beispiel #2
0
        public static AffiliateCategoryMatch Create(Category category, AffiliateCategory affiliateCategory)
        {
            var match = affiliateCategory.GetAdvertiseId();

            match.AdvertiseCategoryId = category.CategoryId;
            return(match);
        }
Beispiel #3
0
        public async Task ProcessUnifiedCategory(AffiliateCategory affiliateCategory)
        {
            if (affiliateCategory == null)
            {
                throw new ArgumentNullException(nameof(affiliateCategory));
            }

            var matchedIds = await _matchesRepository.GetAllAsync();

            var categories = await _categoryRepository.GetAllAsync();

            var updated = await UpdateCategoryIfAlreadyExistsInTheSameAffiliateProgram(categories, matchedIds, affiliateCategory);

            if (updated)
            {
                return;
            }

            updated = await UpdateCategoryIfAlreadyExistsInOtherAffiliateProgram(categories, matchedIds, affiliateCategory);

            if (updated)
            {
                return;
            }

            await CreateCategoryAndMatch(affiliateCategory);
        }
Beispiel #4
0
 public static AffiliateCategoryMatch GetAdvertiseId(this AffiliateCategory category)
 {
     return(new AffiliateCategoryMatch
     {
         Id = Guid.NewGuid(),
         AffiliateProgram = category.AffiliateProgram,
         AffiliateCategoryId = category.CategoryId,
         CouponsCount = category.CouponsCount,
     });
 }
Beispiel #5
0
        private async Task CreateCategoryAndMatch(AffiliateCategory affiliateCategory)
        {
            var categoryToCreate = Category.Create();

            UpdateCategoryProperties(categoryToCreate, affiliateCategory);
            await _categoryRepository.SaveAsync(categoryToCreate);

            var newMatch = AffiliateCategoryMatch.Create(categoryToCreate, affiliateCategory);
            await _matchesRepository.SaveAsync(newMatch);
        }
Beispiel #6
0
        private static void UpdateCategoryProperties(Category category, AffiliateCategory affiliateCategory, IList <AffiliateCategoryMatch> allMatches = null)
        {
            var advertiseId = affiliateCategory.GetAdvertiseId();

            category.Name         = affiliateCategory.Name;
            category.FriendlyName = affiliateCategory.FriendlyName;
            category.CategoryUrl  = affiliateCategory.CategoryUrl;
            category.ChangedDate  = affiliateCategory.ChangedDate;
            if (allMatches == null || !allMatches.Any())
            {
                category.CouponsCount = affiliateCategory.CouponsCount;
            }
            else
            {
                category.CouponsCount = allMatches.Where(m => m.AdvertiseCategoryId == category.CategoryId).Sum(m => m.CouponsCount);
            }
        }
Beispiel #7
0
        private async Task <IList <AffiliateCategory> > GetAllCategoriesAsync()
        {
            var categories = new List <AffiliateCategory>();
            var stores     = await _storeRepository.GetAllAsync();

            Parallel.ForEach(stores, new ParallelOptions {
                MaxDegreeOfParallelism = 20
            }, store =>
            {
                var programRespnse = _programRepository.GetProgramAsync(store.StoreId.ToString()).ConfigureAwait(false).GetAwaiter().GetResult();
                var programs       = programRespnse?.Programs;
                if (programs == null)
                {
                    return;
                }

                foreach (var program in programs)
                {
                    var zanoxCategories = program?.Categories;
                    if (zanoxCategories == null)
                    {
                        continue;
                    }
                    foreach (var wrapper in zanoxCategories)
                    {
                        var zanoxSubCategories = wrapper.Category;
                        foreach (var zanoxCategory in zanoxSubCategories)
                        {
                            var category = new AffiliateCategory
                            {
                                CategoryId       = zanoxCategory.Id,
                                AffiliateProgram = Affiliates.Zanox,
                                Name             = zanoxCategory.Name,
                                FriendlyName     = zanoxCategory.Name.ToFriendlyName()
                            };
                            categories.Add(category);
                        }
                    }
                }
            });
            return(categories.GroupBy(c => c.CategoryId).Select(g => g.FirstOrDefault()).ToList());
        }
Beispiel #8
0
        private async Task <bool> UpdateCategoryIfAlreadyExistsInTheSameAffiliateProgram(
            IList <Category> existingCategorys,
            IList <AffiliateCategoryMatch> existingMatches,
            AffiliateCategory affiliateCategory)
        {
            if (existingCategorys == null || !existingCategorys.Any())
            {
                return(false);
            }
            if (existingMatches == null || !existingMatches.Any())
            {
                return(false);
            }

            var advertiseId   = affiliateCategory.GetAdvertiseId();
            var previousMatch = existingMatches.FirstOrDefault(id => id.Matched(advertiseId));

            if (previousMatch == null)
            {
                return(false);
            }

            var foundCategory = existingCategorys.FirstOrDefault(category => category.CategoryId == previousMatch.AdvertiseCategoryId);

            if (foundCategory == null)
            {
                return(false);
            }

            // Update match
            previousMatch.Update(advertiseId);
            await _matchesRepository.SaveAsync(previousMatch);

            // Update category
            UpdateCategoryProperties(foundCategory, affiliateCategory, existingMatches);
            await _categoryRepository.SaveAsync(foundCategory);

            return(true);
        }
Beispiel #9
0
 protected bool Equals(AffiliateCategory other)
 {
     return(CategoryId == other.CategoryId && Name == other.Name && AffiliateProgram == other.AffiliateProgram && FriendlyName == other.FriendlyName && Equals(CategoryUrl, other.CategoryUrl) && CouponsCount == other.CouponsCount);
 }
Beispiel #10
0
 public void CancelUnifiedCategory(AffiliateCategory affiliateCategory)
 {
 }