Beispiel #1
0
        protected virtual IEnumerable <Coupon> FindValidCoupons(ICollection <string> couponCodes)
        {
            var result = new List <Coupon>();

            if (!couponCodes.IsNullOrEmpty())
            {
                //Remove empty codes from input list
                couponCodes = couponCodes.Where(x => !string.IsNullOrEmpty(x)).ToList();
                if (!couponCodes.IsNullOrEmpty())
                {
                    var coupons = _couponService.SearchCoupons(new CouponSearchCriteria {
                        Codes = couponCodes, PromotionId = Id
                    }).Results.OrderBy(x => x.TotalUsesCount);
                    foreach (var coupon in coupons)
                    {
                        var couponIsValid = true;
                        if (coupon.ExpirationDate != null)
                        {
                            couponIsValid = coupon.ExpirationDate > DateTime.UtcNow;
                        }
                        if (couponIsValid && coupon.MaxUsesNumber > 0)
                        {
                            couponIsValid = _usageService.SearchUsages(new PromotionUsageSearchCriteria {
                                PromotionId = Id, CouponCode = coupon.Code, Take = 0
                            }).TotalCount <= coupon.MaxUsesNumber;
                        }
                        if (couponIsValid)
                        {
                            result.Add(coupon);
                        }
                    }
                }
            }
            return(result);
        }
Beispiel #2
0
        private void RecordUsages(string objectId, IEnumerable <PromotionUsage> oldUsages, IEnumerable <PromotionUsage> newUsages)
        {
            var toAddUsages    = newUsages.Except(oldUsages, EqualityComparer);
            var toRemoveUsages = oldUsages.Except(newUsages, EqualityComparer);

            if (!toAddUsages.IsNullOrEmpty())
            {
                _usageService.SaveUsages(toAddUsages.ToArray());
            }
            if (!toRemoveUsages.IsNullOrEmpty())
            {
                var alreadyExistUsages = _usageService.SearchUsages(new PromotionUsageSearchCriteria {
                    ObjectId = objectId
                }).Results;
                _usageService.DeleteUsages(alreadyExistUsages.Intersect(toRemoveUsages, EqualityComparer).Select(x => x.Id).ToArray());
            }
        }
        private BackupObject GetBackupObject(Action <ExportImportProgressInfo> progressCallback)
        {
            var result       = new BackupObject();
            var progressInfo = new ExportImportProgressInfo {
                Description = "Search promotions..."
            };

            progressCallback(progressInfo);
            var allPromotions = _promotionSearchService.SearchPromotions(new Domain.Marketing.Model.Promotions.Search.PromotionSearchCriteria
            {
                Take = int.MaxValue
            }).Results;

            progressInfo.Description = String.Format("{0} promotions loading...", allPromotions.Count());
            progressCallback(progressInfo);
            result.Promotions = _promotionService.GetPromotionsByIds(allPromotions.Select(x => x.Id).ToArray());

            progressInfo.Description = "Search dynamic content objects...";
            progressCallback(progressInfo);

            progressInfo.Description = String.Format("Loading folders...");
            progressCallback(progressInfo);
            result.ContentFolders = LoadFoldersRecursive(null);

            progressInfo.Description = String.Format("Loading places...");
            progressCallback(progressInfo);
            result.ContentPlaces = _dynamicContentSearchService.SearchContentPlaces(new DynamicContentPlaceSearchCriteria {
                Take = int.MaxValue
            }).Results.ToList();

            progressInfo.Description = String.Format("Loading contents...");
            progressCallback(progressInfo);
            result.ContentItems = _dynamicContentSearchService.SearchContentItems(new DynamicContentItemSearchCriteria {
                Take = int.MaxValue
            }).Results.ToList();

            progressInfo.Description = String.Format("Loading publications...");
            progressCallback(progressInfo);
            result.ContentPublications = _dynamicContentSearchService.SearchContentPublications(new DynamicContentPublicationSearchCriteria {
                Take = int.MaxValue
            }).Results.ToList();

            progressInfo.Description = String.Format("Loading coupons...");
            progressCallback(progressInfo);
            var couponsTotal = _couponService.SearchCoupons(new CouponSearchCriteria {
                Take = 0
            }).TotalCount;
            var pageSize = 500;

            Paginate(couponsTotal, pageSize, (x) =>
            {
                progressInfo.Description = String.Format($"Loading coupons: {Math.Min(x * pageSize, couponsTotal)} of {couponsTotal} loaded");
                progressCallback(progressInfo);
                result.Coupons.AddRange(_couponService.SearchCoupons(new CouponSearchCriteria {
                    Skip = (x - 1) * pageSize, Take = pageSize
                }).Results);
            });

            progressInfo.Description = String.Format("Loading usages...");
            progressCallback(progressInfo);
            var usagesTotal = _usageService.SearchUsages(new PromotionUsageSearchCriteria {
                Take = 0
            }).TotalCount;

            Paginate(usagesTotal, pageSize, (x) =>
            {
                progressInfo.Description = String.Format($"Loading usages: {Math.Min(x * pageSize, usagesTotal)} of {usagesTotal} loaded");
                progressCallback(progressInfo);
                result.Usages.AddRange(_usageService.SearchUsages(new PromotionUsageSearchCriteria {
                    Skip = (x - 1) * pageSize, Take = pageSize
                }).Results);
            });

            return(result);
        }
 protected virtual GenericSearchResult <PromotionUsage> LoadPromotionUsages(PromotionUsageSearchCriteria criteria)
 {
     return(_usageService.SearchUsages(criteria));
 }