Пример #1
0
        private async Task <bool> UpdateStoreIfAlreadyExistsInOtherAffiliateProgram(
            IList <Store> existingStores,
            IList <AffiliateStoreMatch> existingMatches,
            AffiliateStore affiliateStore)
        {
            if (existingStores == null || !existingStores.Any())
            {
                return(false);
            }
            if (existingMatches == null || !existingMatches.Any())
            {
                return(false);
            }

            var storeToUpdate = existingStores.FirstOrDefault(store => store.IsMatchableName(affiliateStore.Name));

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

            var newMatch = AffiliateStoreMatch.Create(storeToUpdate, affiliateStore);
            await _matchesRepository.SaveAsync(newMatch);

            UpdateStoreProperties(storeToUpdate, affiliateStore, existingMatches);
            await _storeRepository.SaveAsync(storeToUpdate);

            return(true);
        }
Пример #2
0
        public async Task ProcessUnifiedStore(AffiliateStore affiliateStore)
        {
            if (affiliateStore == null)
            {
                throw new ArgumentNullException(nameof(affiliateStore));
            }

            var matchedIds = await _matchesRepository.GetAllAsync();

            var stores = await _storeRepository.GetAllAsync();

            var updated = await UpdateStoreIfAlreadyExistsInTheSameAffiliateProgram(stores, matchedIds, affiliateStore);

            if (updated)
            {
                return;
            }

            updated = await UpdateStoreIfAlreadyExistsInOtherAffiliateProgram(stores, matchedIds, affiliateStore);

            if (updated)
            {
                return;
            }

            await CreateStoreAndMatch(affiliateStore);
        }
Пример #3
0
        public static AffiliateStoreMatch Create(Store store, AffiliateStore affiliateStore)
        {
            var match = affiliateStore.GetAdvertiseId();

            match.AdvertiseStoreId = store.StoreId;
            return(match);
        }
Пример #4
0
        private async Task CreateStoreAndMatch(AffiliateStore affiliateStore)
        {
            var storeToCreate = Store.Create();

            UpdateStoreProperties(storeToCreate, affiliateStore);
            await _storeRepository.SaveAsync(storeToCreate);

            var newMatch = AffiliateStoreMatch.Create(storeToCreate, affiliateStore);
            await _matchesRepository.SaveAsync(newMatch);
        }
Пример #5
0
 public static AffiliateStoreMatch GetAdvertiseId(this AffiliateStore affiliateStore)
 {
     return(new AffiliateStoreMatch
     {
         Id = Guid.NewGuid(),
         AffiliateProgram = affiliateStore.AffiliateProgram,
         AffiliateStoreId = affiliateStore.StoreId,
         CouponsCount = affiliateStore.CouponsCount,
     });
 }
Пример #6
0
 protected bool Equals(AffiliateStore other)
 {
     return(StoreId == other.StoreId &&
            Name == other.Name &&
            AffiliateProgram == other.AffiliateProgram &&
            Description == other.Description &&
            FriendlyName == other.FriendlyName &&
            Equals(ImageUrl, other.ImageUrl) &&
            Equals(StoreUrl, other.StoreUrl) &&
            CouponsCount == other.CouponsCount);
 }
Пример #7
0
 private static void UpdateStoreProperties(Store store, AffiliateStore affiliateStore, IList <AffiliateStoreMatch> allMatches = null)
 {
     store.Name         = affiliateStore.Name;
     store.FriendlyName = affiliateStore.FriendlyName;
     store.Description  = string.IsNullOrWhiteSpace(affiliateStore.Description) && !string.IsNullOrWhiteSpace(store.Description) ? store.Description : affiliateStore.Description;
     store.ImageUrl     = affiliateStore.ImageUrl;
     store.StoreUrl     = affiliateStore.StoreUrl;
     store.ChangedDate  = affiliateStore.ChangedDate;
     if (allMatches == null || !allMatches.Any())
     {
         store.CouponsCount = affiliateStore.CouponsCount;
     }
     else
     {
         store.CouponsCount = allMatches.Where(m => m.AdvertiseStoreId == store.StoreId).Sum(m => m.CouponsCount);
     }
 }
Пример #8
0
        private async Task <bool> UpdateStoreIfAlreadyExistsInTheSameAffiliateProgram(
            IList <Store> existingStores,
            IList <AffiliateStoreMatch> existingMatches,
            AffiliateStore affiliateStore)
        {
            if (existingStores == null || !existingStores.Any())
            {
                return(false);
            }
            if (existingMatches == null || !existingMatches.Any())
            {
                return(false);
            }

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

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

            var foundStore = existingStores.FirstOrDefault(store => store.StoreId == previousMatch.AdvertiseStoreId);

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

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

            // Update store
            UpdateStoreProperties(foundStore, affiliateStore, existingMatches);
            await _storeRepository.SaveAsync(foundStore);

            return(true);
        }
Пример #9
0
 public void CancelUnifiedStore(AffiliateStore affiliateStore)
 {
 }