Example #1
0
        /// <summary>
        /// You may customized what is returned here based on number od existing orders or date or get number from external system
        /// </summary>
        /// <param name="order"></param>
        /// <param name="context"></param>
        /// <param name="findEntitiesInListCommand"></param>
        /// <returns></returns>
        private string GetCustomOrderNumber(Order order, CommercePipelineExecutionContext context, FindEntitiesInListCommand findEntitiesInListCommand)
        {
            //Get Contact Component which contains customer information
            var contactComponent = order.GetComponent <ContactComponent>();

            try
            {
                int orderCounts = 0;
                // get all existing orders.
                IEnumerable <Order> orders = (IEnumerable <Order>)findEntitiesInListCommand.Process <Order>(context.CommerceContext, CommerceEntity.ListName <Order>(), 0, int.MaxValue).Result.Items;

                // use the info you have to generate an appropriate order number. You may also use the data you have to call an external system.
                // in this instance we will just return the number of existing orders incremented by 1
                // Return order count and increment by 1 as the new order number.
                if (orders.Any())
                {
                    // Total orders
                    orderCount = orders.Count();
                }
                return((orderCount + 1).ToString());
            }
            catch (Exception)
            {
                // return a random guid if ther was an isssue retriving existing orders or all else failed.
                return(Guid.NewGuid().ToString("B"));
            }
        }
Example #2
0
        public async override Task <Customer> Run(Customer customer, CommercePipelineExecutionContext context)
        {
            var policy = context.GetPolicy <LoyaltyPointsPolicy>();

            var summary = customer.GetComponent <LoyaltySummary>();

            if (policy.IsCustomerProcessed(customer))
            {
                return(customer);
            }
            summary.LastProcessedDate = new DateTimeOffset?(DateTimeOffset.UtcNow);

            string listName = string.Format(context.GetPolicy <KnownOrderListsPolicy>().CustomerOrders, customer.Id);

            CommerceList <Order> orders = await _findEntitiesInListCommand.Process <Order>(context.CommerceContext, listName, 0, int.MaxValue);

            foreach (var order in orders.Items)
            {
                int pointsFromOrder = await _processOrderPipeline.Run(order, context);

                summary.TotalPoints += pointsFromOrder;
            }

            await _persistEntityPipeline.Run(new PersistEntityArgument(customer), context);

            return(customer);
        }
        /// <summary>
        /// Retrieve all entities of given type
        /// </summary>
        /// <param name="context">context</param>
        /// <returns>List of all composer templates</returns>
        public EntityCollectionModel ExportCommerceEntities <T>(CommerceContext context) where T : CommerceEntity
        {
            CommerceList <T>      commerceList = _findEntitiesInListCommand.Process <T>(context, CommerceEntity.ListName <T>(), 0, int.MaxValue).Result;
            List <CommerceEntity> entityList;

            if (commerceList == null)
            {
                entityList = null;
            }
            else
            {
                entityList = commerceList?.Items?.Cast <CommerceEntity>().ToList();
            }
            if (entityList == null)
            {
                entityList = new List <CommerceEntity>();
            }

            return(new EntityCollectionModel
            {
                EntityType = typeof(T),
                Entities = entityList
            });
        }
Example #4
0
        public bool Evaluate(IRuleExecutionContext context)
        {
            var commerceContext         = context.Fact <CommerceContext>();
            var cart                    = commerceContext?.GetObject <Cart>();
            var contextContactComponent = cart?.GetComponent <ContactComponent>();

            if (cart == null || string.IsNullOrWhiteSpace(contextContactComponent?.ShopperId))
            {
                return(false);
            }

            var listName        = string.Format(commerceContext.GetPolicy <KnownOrderListsPolicy>().CustomerOrders, contextContactComponent.ShopperId);
            var customersOrders = _findEntitiesInListCommand.Process <Order>(commerceContext, listName, 0, int.MaxValue).Result.Items.ToList();

            return(cart.Lines.Any(cartLine => customersOrders.Any(order => order.Lines.Any(ProductExistsInOrderAndCart(cartLine)))));
        }
Example #5
0
        public async Task <List <Order> > Resolve(CommerceContext commerceContext)
        {
            if (commerceContext == null || !commerceContext.CurrentUserIsRegistered())
            {
                return(null);
            }

            string listName = string.Format(CultureInfo.InvariantCulture,
                                            commerceContext.GetPolicy <KnownOrderListsPolicy>().CustomerOrders,
                                            commerceContext.CurrentCustomerId());

            CommerceList <Order> result =
                await findEntitiesInListCommand.Process <Order>(commerceContext, listName, 0, int.MaxValue);

            return(result?.Items);
        }
Example #6
0
        /// <summary>
        /// /
        /// </summary>
        /// <param name="context"></param>
        /// <param name="findEntitiesInListCommand"></param>
        /// <returns></returns>
        private static string GenerateNewCustomerNumber(CommercePipelineExecutionContext context, FindEntitiesInListCommand findEntitiesInListCommand)
        {
            // get all existing customers.
            var customers = (IEnumerable <Customer>)findEntitiesInListCommand.Process <Customer>(context.CommerceContext, CommerceEntity.ListName <Customer>(), 0, int.MaxValue).Result.Items;

            // Total existing customers
            var customerCount = customers.Count();

            if (!customers.Any())
            {
                return("customer1");
            }

            // use the info you have to generate an appropriate customer number. You may also use the data you have to call an external system.
            // in this instance we will just return the number of existing customers incremented by 1
            // Return customer count and increment by 1 as the new customer number.

            var nextOrderNumber = customerCount + 1;

            return("customer" + nextOrderNumber.ToString());
        }
Example #7
0
        /// <summary>
        /// Gets all Composer Templates
        /// </summary>
        /// <param name="context">context</param>
        /// <returns>List of all composer templates</returns>
        public List <ComposerTemplate> GetAllComposerTemplates(CommerceContext context)
        {
            CommerceList <ComposerTemplate> commerceList = _findEntitiesInListCommand.Process <ComposerTemplate>(context, CommerceEntity.ListName <ComposerTemplate>(), 0, int.MaxValue).Result;
            List <ComposerTemplate>         composerTemplateList;

            if (commerceList == null)
            {
                composerTemplateList = null;
            }
            else
            {
                List <ComposerTemplate> items = commerceList.Items;
                composerTemplateList = items != null?items.ToList() : null;
            }
            if (composerTemplateList == null)
            {
                composerTemplateList = new List <ComposerTemplate>();
            }

            return(composerTemplateList);
        }
Example #8
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);
        }