Exemplo n.º 1
0
        public async Task DisassociateParentCategories(SellableItem sellableItem, CommerceContext context)
        {
            var parentCategorySitecoreIds = sellableItem?.ParentCategoryList?.Split('|');

            if (parentCategorySitecoreIds == null || parentCategorySitecoreIds.Length == 0)
            {
                return;
            }

            var categoryList = await _getManagedListCommand.Process(context, CommerceEntity.ListName <SellableItem>()).ConfigureAwait(false);

            var allCategories = categoryList?.Items?.Cast <Category>();

            if (allCategories != null)
            {
                foreach (var parentCategorySitecoreId in parentCategorySitecoreIds)
                {
                    var parentCategory = allCategories.FirstOrDefault(c => c.SitecoreId == parentCategorySitecoreId);
                    if (parentCategory != null)
                    {
                        await _deleteRelationshipCommand.Process(context, parentCategory.Id, sellableItem.Id, "CategoryToSellableItem");
                    }
                }
            }
        }
Exemplo n.º 2
0
        public async Task DisassociateParentCategories(string catalogName, Category category, CommerceContext context)
        {
            var catalogCommerceId = $"{CommerceEntity.IdPrefix<Catalog>()}{catalogName}";
            await _deleteRelationshipCommand.Process(context, catalogCommerceId, category.Id, "CategoryToCategory");

            var parentCategorySitecoreIds = category?.ParentCategoryList?.Split('|');

            if (parentCategorySitecoreIds == null || parentCategorySitecoreIds.Length == 0)
            {
                return;
            }

            var categoryList = await _getManagedListCommand.Process(context, CommerceEntity.ListName <Category>()).ConfigureAwait(false);

            var allCategories = categoryList?.Items?.Cast <Category>();

            if (allCategories != null)
            {
                foreach (var parentCategorySitecoreId in parentCategorySitecoreIds)
                {
                    var parentCategory = allCategories.FirstOrDefault(c => c.SitecoreId == parentCategorySitecoreId);
                    if (parentCategory != null)
                    {
                        await _deleteRelationshipCommand.Process(context, parentCategory.Id, category.Id, "CategoryToCategory");
                    }
                }
            }
        }
Exemplo n.º 3
0
        private async Task <ManagedList> EnsureList(CommercePipelineExecutionContext context, string listName)
        {
            ManagedList list = await _getManagedListCommand.Process(context.CommerceContext, listName);

            if (list == null)
            {
                context.Logger.LogDebug($"{this.Name}: List {listName} not found. Creating it.");
                list = await _createManagedListCommand.Process(context.CommerceContext, listName);
            }

            return(list);
        }
Exemplo n.º 4
0
        public override async Task <IssueCouponArgument> Run(IssueCouponArgument arg,
                                                             CommercePipelineExecutionContext context)
        {
            var policy = context.GetPolicy <LoyaltyPointsPolicy>();

            var summary = arg.Customer.GetComponent <LoyaltySummary>();

            if (summary.TotalPoints - summary.AppliedPoints < policy.PointsForCoupon)
            {
                return(arg);
            }

            ManagedList couponList =
                await _getManagedListCommand.Process(context.CommerceContext, Constants.AvailableCouponsList);

            if (couponList == null)
            {
                return(arg);
            }

            var commerceCommand = new CommerceCommand(_serviceProvider);
            await commerceCommand.PerformTransaction(context.CommerceContext,
                                                     async() =>
            {
                int couponsToIssue = ((summary.TotalPoints - summary.AppliedPoints) / policy.PointsForCoupon);

                CommerceList <Coupon> list =
                    await _findEntitiesInListCommand.Process <Coupon>(context.CommerceContext,
                                                                      Constants.AvailableCouponsList, 0, couponsToIssue);

                if (list.Items.Count < couponsToIssue)
                {
                    context.Abort(
                        $"{Name}: Unable to provision required {couponsToIssue} coupons. Examine List {Constants.AvailableCouponsList} and CreateCouponsMinion process.",
                        context);
                    return;
                }

                List <string> couponCodes = list.Items.Select(coupon => coupon.Code).ToList();
                List <string> entityIds   = list.Items.Select(coupon => coupon.Id).ToList();

                var result = await _removeListEntitiesCommand.Process(context.CommerceContext,
                                                                      Constants.AvailableCouponsList, entityIds);
                if (!result.Success)
                {
                    context.Abort($"{Name}: Unable to remove coupon(s) from List {Constants.AvailableCouponsList}",
                                  context);
                    return;
                }

                summary.AppliedPoints += couponsToIssue *policy.PointsForCoupon;

                summary.CouponEntities.AddRange(entityIds);

                arg.CouponEntities.AddRange(entityIds);
                arg.Coupons.AddRange(couponCodes);

                await _persistEntityPipeline.Run(new PersistEntityArgument(arg.Customer), context);
            });

            return(arg);
        }