Exemplo n.º 1
0
        public override async Task <Customer> Run(Customer arg, CommercePipelineExecutionContext context)
        {
            var details = arg.GetComponent <CustomerDetailsComponent>().View.ChildViews.FirstOrDefault() as EntityView;

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

            string mainId = details.GetPropertyValue("MainAccountId").ToString();

            if (mainId == null)
            {
                return(arg);
            }
            var mainAccount = await _getCustomerPipeline.Run(new GetCustomerArgument(mainId, string.Empty), context);

            var reference = mainAccount.HasComponent <StructuredAccountOwnerComponent>()
                                    ? mainAccount.GetComponent <StructuredAccountOwnerComponent>()
                            .Subaccounts.FirstOrDefault(x => x.EntityTarget.Equals(arg.Id, StringComparison.OrdinalIgnoreCase)) : null;

            if (reference != null)
            {
                var subaccounts = mainAccount.GetComponent <StructuredAccountOwnerComponent>();
                subaccounts.Subaccounts.Remove(reference);
                await _persistEntityPipeline.Run(new PersistEntityArgument(mainAccount), context);
            }

            return(arg);
        }
        public override async Task <Customer> Run(Customer arg, CommercePipelineExecutionContext context)
        {
            Condition.Requires <Customer>(arg).IsNotNull <Customer>("The customer can not be null");
            Condition.Requires <string>(arg.UserName).IsNotNullOrEmpty("The customer user name can not be null");

            string mainAccountId = GetMainAccountId(context);

            if (string.IsNullOrEmpty(mainAccountId))
            {
                return(arg);
            }

            var mainAccountCustomer = await _getCustomerPipeline.Run(new GetCustomerArgument(mainAccountId, string.Empty), context);

            StructuredAccountOwnerComponent structuredAccountOwnerComponent = mainAccountCustomer.GetComponent <StructuredAccountOwnerComponent>();

            structuredAccountOwnerComponent.Subaccounts.Add(new EntityReference(arg.Id, arg.FriendlyId));

            var resulttt = await _persistEntityPipeline.Run(new PersistEntityArgument(mainAccountCustomer), context);

            return(arg);
        }