예제 #1
0
        public async Task GetByIdsAsync_GetThenSavePromotionUsage_ReturnCachedPromotionUsage()
        {
            //Arrange
            var id = Guid.NewGuid().ToString();
            var newPromotionUsage = new PromotionUsage {
                Id = id
            };
            var newPromotionUsageEntity = AbstractTypeFactory <PromotionUsageEntity> .TryCreateInstance().FromModel(newPromotionUsage, new PrimaryKeyResolvingMap());

            var service = GetPromotionUsageServiceImplWithPlatformMemoryCache();

            _repositoryMock.Setup(x => x.Add(newPromotionUsageEntity))
            .Callback(() =>
            {
                _repositoryMock.Setup(o => o.GetMarketingUsagesByIdsAsync(new[] { id }))
                .ReturnsAsync(new[] { newPromotionUsageEntity });
            });

            //Act
            var nullPromotionUsage = await service.GetByIdsAsync(new[] { id });

            await service.SaveUsagesAsync(new[] { newPromotionUsage });

            var promotionUsage = await service.GetByIdsAsync(new[] { id });

            //Assert
            Assert.NotEqual(nullPromotionUsage, promotionUsage);
        }
        private PromotionUsage UpdatePromotionUsage(ICollection <PromotionUsage> currentUsages, Discount discount)
        {
            var usage = currentUsages.FirstOrDefault(x => x.PromotionId == discount.PromotionId);

            if (usage != null)
            {
                usage.Status    = (int)UsageStatus;
                usage.UsageDate = DateTime.UtcNow;
            }
            else
            {
                usage = new PromotionUsage
                {
                    CouponCode   = discount.DiscountCode,
                    MemberId     = CustomerSessionService.CustomerSession.CustomerId,
                    OrderGroupId = CurrentOrderGroup.OrderGroupId,
                    PromotionId  = discount.PromotionId,
                    Status       = (int)UsageStatus,
                    UsageDate    = DateTime.UtcNow
                };

                //Need to add here too to avoid duplicates
                currentUsages.Add(usage);

                MarketingRepository.Add(usage);
            }

            return(usage);
        }
        public void RegisterToUsePromotion(IPromotionEvaluationContext context, Promotion promotion)
        {
            var promotionUsage = new PromotionUsage
            {
                CouponCode   = context.CouponCode,
                MemberId     = context.CustomerId,
                PromotionId  = promotion.PromotionId,
                OrderGroupId = ((OrderGroup)context.ContextObject).OrderGroupId
            };

            _marketingRepository.Add(promotionUsage);

            _marketingRepository.UnitOfWork.Commit();
        }