protected virtual async Task <IEnumerable <Coupon> > FindValidCouponsAsync(ICollection <string> couponCodes, string userId)
        {
            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 = await _couponService.SearchCouponsAsync(new CouponSearchCriteria { Codes = couponCodes, PromotionId = Id });

                    foreach (var coupon in coupons.Results.OrderBy(x => x.TotalUsesCount))
                    {
                        var couponIsValid = true;
                        if (coupon.ExpirationDate != null)
                        {
                            couponIsValid = coupon.ExpirationDate > DateTime.UtcNow;
                        }
                        if (couponIsValid && coupon.MaxUsesNumber > 0)
                        {
                            var usage = await _usageService.SearchUsagesAsync(new PromotionUsageSearchCriteria { PromotionId = Id, CouponCode = coupon.Code, Take = 0 });

                            couponIsValid = usage.TotalCount < coupon.MaxUsesNumber;
                        }
                        if (couponIsValid && coupon.MaxUsesPerUser > 0 && !string.IsNullOrWhiteSpace(userId))
                        {
                            var usage = await _usageService.SearchUsagesAsync(new PromotionUsageSearchCriteria { PromotionId = Id, CouponCode = coupon.Code, UserId = userId, Take = int.MaxValue });

                            couponIsValid = usage.TotalCount < coupon.MaxUsesPerUser;
                        }
                        if (couponIsValid)
                        {
                            result.Add(coupon);
                        }
                    }
                }
            }
            return(result);
        }
        public async Task DoExportAsync(Stream outStream, Action <ExportImportProgressInfo> progressCallback,
                                        ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);

            using (var sw = new StreamWriter(outStream))
                using (var writer = new JsonTextWriter(sw))
                {
                    await writer.WriteStartObjectAsync();

                    progressInfo.Description = "Promotions exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Promotions");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _promotionSearchService.SearchPromotionsAsync(new PromotionSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } promotions have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content folders exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentFolders");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _dynamicContentSearchService.SearchFoldersAsync(new DynamicContentFolderSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content folders have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content items exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentItems");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _dynamicContentSearchService.SearchContentItemsAsync(new DynamicContentItemSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content items have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content places exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentPlaces");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _dynamicContentSearchService.SearchContentPlacesAsync(new DynamicContentPlaceSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content places have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content publications exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentPublications");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _dynamicContentSearchService.SearchContentPublicationsAsync(new DynamicContentPublicationSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content publications have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Coupons exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Coupons");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _couponService.SearchCouponsAsync(new CouponSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } coupons have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Usages exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Usages");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _promotionUsageService.SearchUsagesAsync(new PromotionUsageSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } usages have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    await writer.WriteEndObjectAsync();

                    await writer.FlushAsync();
                }
        }
Ejemplo n.º 3
0
        public async Task DoExportAsync(Stream outStream, Action <ExportImportProgressInfo> progressCallback,
                                        ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);

            using (var sw = new StreamWriter(outStream))
                using (var writer = new JsonTextWriter(sw))
                {
                    await writer.WriteStartObjectAsync();

                    progressInfo.Description = "Promotions exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Promotions");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <Promotion>) await _promotionSearchService.SearchPromotionsAsync(new PromotionSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } promotions have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content folders exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentFolders");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult        = AbstractTypeFactory <DynamicContentFolderSearchResult> .TryCreateInstance();
                        var result              = await LoadFoldersRecursiveAsync(null);
                        searchResult.Results    = result;
                        searchResult.TotalCount = result.Count;
                        return((GenericSearchResult <DynamicContentFolder>)searchResult);
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content folders have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content items exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentItems");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <DynamicContentItem>) await _dynamicContentSearchService.SearchContentItemsAsync(new DynamicContentItemSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content items have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content places exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentPlaces");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <DynamicContentPlace>) await _dynamicContentSearchService.SearchContentPlacesAsync(new DynamicContentPlaceSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content places have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Dynamic content publications exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("DynamicContentPublications");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _dynamicContentSearchService.SearchContentPublicationsAsync(new DynamicContentPublicationSearchCriteria {
                            Skip = skip, Take = take
                        });
                        return((GenericSearchResult <DynamicContentPublication>)searchResult);
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } dynamic content publications have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Coupons exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Coupons");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _couponService.SearchCouponsAsync(new CouponSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } coupons have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    progressInfo.Description = "Usages exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Usages");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, (skip, take) => _promotionUsageService.SearchUsagesAsync(new PromotionUsageSearchCriteria {
                        Skip = skip, Take = take
                    }), (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } usages have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    await writer.WriteEndObjectAsync();

                    await writer.FlushAsync();
                }
        }