Ejemplo n.º 1
0
        /// <summary>
        /// Integration event handler which starts the create order process
        /// </summary>
        /// <param name="event">
        /// Integration event message which is sent by the
        /// basket.api once it has successfully process the
        /// order items.
        /// </param>
        /// <returns></returns>
        public async Task Handle(BasketCheckoutAcceptedIntegrationEvent @event)
        {
            var serviceModel = new CheckoutBasketServiceModel
            {
                ClientId             = @event.ClientId,
                ClientName           = @event.ClientName,
                SellerId             = @event.SellerId,
                BasketId             = @event.Basket?.Id,
                BillingAddressId     = @event.BillingAddressId,
                BillingCity          = @event.BillingCity,
                BillingCompany       = @event.BillingCompany,
                BillingCountryCode   = @event.BillingCountryCode,
                BillingFirstName     = @event.BillingFirstName,
                BillingLastName      = @event.BillingLastName,
                BillingPhone         = @event.BillingPhone,
                BillingPhonePrefix   = @event.BillingPhonePrefix,
                BillingPostCode      = @event.BillingPostCode,
                BillingRegion        = @event.BillingRegion,
                BillingStreet        = @event.BillingStreet,
                ShippingAddressId    = @event.ShippingAddressId,
                ShippingCity         = @event.ShippingCity,
                ShippingCompany      = @event.ShippingCompany,
                ShippingCountryCode  = @event.ShippingCountryCode,
                ShippingFirstName    = @event.ShippingFirstName,
                ShippingLastName     = @event.ShippingLastName,
                ShippingPhone        = @event.ShippingPhone,
                ShippingPhonePrefix  = @event.ShippingPhonePrefix,
                ShippingPostCode     = @event.ShippingPostCode,
                ShippingRegion       = @event.ShippingRegion,
                ShippingStreet       = @event.ShippingStreet,
                ExpectedDeliveryDate = @event.ExpectedDeliveryDate,
                ExternalReference    = @event.ExternalReference,
                MoreInfo             = @event.MoreInfo,
                IpAddress            = @event.IpAddress,
                Items = @event.Basket?.Items?.Select(x => new CheckoutBasketItemServiceModel
                {
                    ProductId            = x.ProductId,
                    ProductSku           = x.ProductSku,
                    ProductName          = x.ProductName,
                    PictureUrl           = x.PictureUrl,
                    Quantity             = x.Quantity,
                    ExternalReference    = x.ExternalReference,
                    ExpectedDeliveryFrom = x.DeliveryFrom,
                    ExpectedDeliveryTo   = x.DeliveryTo,
                    MoreInfo             = x.MoreInfo
                })
            };

            var validator = new CheckoutBasketServiceModelValidator();

            var validationResult = await validator.ValidateAsync(serviceModel);

            if (validationResult.IsValid)
            {
                await this.ordersService.CheckoutAsync(serviceModel);
            }
        }
Ejemplo n.º 2
0
        public async Task <IActionResult> BasketCheckoutPost(BasketCheckoutRequestModel request)
        {
            var sellerClaim   = this.User.Claims.FirstOrDefault(x => x.Type == AccountConstants.Claims.OrganisationIdClaim);
            var isSellerClaim = this.User.Claims.FirstOrDefault(x => x.Type == JwtClaimTypes.Role && x.Value == AccountConstants.Roles.Seller);
            var serviceModel  = new CheckoutBasketServiceModel
            {
                BasketId             = request.BasketId,
                IsSeller             = isSellerClaim != null,
                ClientId             = request.ClientId,
                ClientName           = request.ClientName,
                BillingAddressId     = request.BillingAddressId,
                BillingCity          = request.BillingCity,
                BillingCompany       = request.BillingCompany,
                BillingCountryCode   = request.BillingCountryCode,
                BillingFirstName     = request.BillingFirstName,
                BillingLastName      = request.BillingLastName,
                BillingPhone         = request.BillingPhone,
                BillingPhonePrefix   = request.BillingPhonePrefix,
                BillingPostCode      = request.BillingPostCode,
                BillingRegion        = request.BillingRegion,
                BillingStreet        = request.BillingStreet,
                ShippingAddressId    = request.ShippingAddressId,
                ShippingCity         = request.ShippingCity,
                ShippingCompany      = request.ShippingCompany,
                ShippingCountryCode  = request.ShippingCountryCode,
                ShippingFirstName    = request.ShippingFirstName,
                ShippingLastName     = request.ShippingLastName,
                ShippingPhone        = request.ShippingPhone,
                ShippingPhonePrefix  = request.ShippingPhonePrefix,
                ShippingPostCode     = request.ShippingPostCode,
                ShippingRegion       = request.ShippingRegion,
                ShippingStreet       = request.ShippingStreet,
                ExpectedDeliveryDate = request.ExpectedDeliveryDate,
                MoreInfo             = request.MoreInfo,
                Language             = CultureInfo.CurrentCulture.Name,
                Username             = this.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Email)?.Value,
                OrganisationId       = GuidHelper.ParseNullable(sellerClaim?.Value)
            };

            var validator = new CheckoutBasketServiceModelValidator();

            var validationResult = await validator.ValidateAsync(serviceModel);

            if (validationResult.IsValid)
            {
                await this.basketService.CheckoutAsync(serviceModel);

                return(this.StatusCode((int)HttpStatusCode.Accepted));
            }

            throw new CustomException(string.Join(ErrorConstants.ErrorMessagesSeparator, validationResult.Errors.Select(x => x.ErrorMessage)), (int)HttpStatusCode.UnprocessableEntity);
        }
Ejemplo n.º 3
0
        public async Task CheckoutAsync(CheckoutBasketServiceModel checkoutBasketServiceModel)
        {
            var basket = await this.basketRepository.GetBasketAsync(checkoutBasketServiceModel.BasketId.Value);

            if (basket == null)
            {
                throw new ArgumentNullException();
            }

            var message = new BasketCheckoutAcceptedIntegrationEvent
            {
                Language             = checkoutBasketServiceModel.Language,
                OrganisationId       = checkoutBasketServiceModel.OrganisationId,
                Username             = checkoutBasketServiceModel.Username,
                ClientId             = checkoutBasketServiceModel.ClientId,
                SellerId             = checkoutBasketServiceModel.OrganisationId,
                ClientName           = checkoutBasketServiceModel.ClientName,
                BillingAddressId     = checkoutBasketServiceModel.BillingAddressId,
                BillingCity          = checkoutBasketServiceModel.BillingCity,
                BillingCompany       = checkoutBasketServiceModel.BillingCompany,
                BillingCountryCode   = checkoutBasketServiceModel.BillingCountryCode,
                BillingFirstName     = checkoutBasketServiceModel.BillingFirstName,
                BillingLastName      = checkoutBasketServiceModel.BillingLastName,
                BillingPhone         = checkoutBasketServiceModel.BillingPhone,
                BillingPhonePrefix   = checkoutBasketServiceModel.BillingPhonePrefix,
                BillingPostCode      = checkoutBasketServiceModel.BillingPostCode,
                BillingRegion        = checkoutBasketServiceModel.BillingRegion,
                BillingStreet        = checkoutBasketServiceModel.BillingStreet,
                ShippingAddressId    = checkoutBasketServiceModel.ShippingAddressId,
                ShippingCity         = checkoutBasketServiceModel.ShippingCity,
                ShippingCompany      = checkoutBasketServiceModel.ShippingCompany,
                ShippingCountryCode  = checkoutBasketServiceModel.ShippingCountryCode,
                ShippingFirstName    = checkoutBasketServiceModel.ShippingFirstName,
                ShippingLastName     = checkoutBasketServiceModel.ShippingLastName,
                ShippingPhone        = checkoutBasketServiceModel.ShippingPhone,
                ShippingPhonePrefix  = checkoutBasketServiceModel.ShippingPhonePrefix,
                ShippingPostCode     = checkoutBasketServiceModel.ShippingPostCode,
                ShippingRegion       = checkoutBasketServiceModel.ShippingRegion,
                ShippingStreet       = checkoutBasketServiceModel.ShippingStreet,
                ExternalReference    = checkoutBasketServiceModel.ExternalReference,
                ExpectedDeliveryDate = checkoutBasketServiceModel.ExpectedDeliveryDate,
                MoreInfo             = checkoutBasketServiceModel.MoreInfo,
                Basket = new BasketEventModel
                {
                    Id    = basket.Id,
                    Items = basket.Items.Select(x => new BasketItemEventModel
                    {
                        ProductId         = x.ProductId,
                        ProductSku        = x.ProductSku,
                        ProductName       = x.ProductName,
                        PictureUrl        = x.PictureUrl,
                        Quantity          = x.Quantity,
                        ExternalReference = x.ExternalReference,
                        DeliveryFrom      = x.DeliveryFrom,
                        DeliveryTo        = x.DeliveryTo,
                        MoreInfo          = x.MoreInfo
                    })
                }
            };

            var itemGroups = basket.Items.OrEmptyIfNull().GroupBy(g => g.ProductId);
            var item       = new List <BasketCheckoutProductEventModel>();

            foreach (var group in itemGroups)
            {
                item.Add(new BasketCheckoutProductEventModel
                {
                    ProductId      = group.FirstOrDefault().ProductId,
                    BookedQuantity = (int)-group.Sum(x => x.Quantity)
                });
            }

            var bookedItems = new BasketCheckoutProductsIntegrationEvent
            {
                Items = item.Select(x => new BasketCheckoutProductEventModel
                {
                    ProductId      = x.ProductId,
                    BookedQuantity = x.BookedQuantity,
                })
            };

            this.eventBus.Publish(bookedItems);
            this.eventBus.Publish(message);
        }
Ejemplo n.º 4
0
        public async Task CheckoutAsync(CheckoutBasketServiceModel serviceModel)
        {
            var order = new Order
            {
                OrderStateId         = OrderStatesConstants.NewId,
                OrderStatusId        = OrderStatusesConstants.NewId,
                ClientId             = serviceModel.ClientId,
                ClientName           = serviceModel.ClientName,
                SellerId             = serviceModel.SellerId,
                BillingAddressId     = serviceModel.BillingAddressId,
                BillingCity          = serviceModel.BillingCity,
                BillingCompany       = serviceModel.BillingCompany,
                BillingCountryCode   = serviceModel.BillingCountryCode,
                BillingFirstName     = serviceModel.BillingFirstName,
                BillingLastName      = serviceModel.BillingLastName,
                BillingPhone         = serviceModel.BillingPhone,
                BillingPhonePrefix   = serviceModel.BillingPhonePrefix,
                BillingPostCode      = serviceModel.BillingPostCode,
                BillingRegion        = serviceModel.BillingRegion,
                BillingStreet        = serviceModel.BillingStreet,
                ShippingAddressId    = serviceModel.ShippingAddressId,
                ShippingCity         = serviceModel.ShippingCity,
                ShippingCompany      = serviceModel.ShippingCompany,
                ShippingCountryCode  = serviceModel.ShippingCountryCode,
                ShippingFirstName    = serviceModel.ShippingFirstName,
                ShippingLastName     = serviceModel.ShippingLastName,
                ShippingPhone        = serviceModel.ShippingPhone,
                ShippingPhonePrefix  = serviceModel.ShippingPhonePrefix,
                ShippingPostCode     = serviceModel.ShippingPostCode,
                ShippingRegion       = serviceModel.ShippingRegion,
                ShippingStreet       = serviceModel.ShippingStreet,
                ExternalReference    = serviceModel.ExternalReference,
                ExpectedDeliveryDate = serviceModel.ExpectedDeliveryDate,
                MoreInfo             = serviceModel.MoreInfo,
                IpAddress            = serviceModel.IpAddress
            };

            this.context.Orders.Add(order.FillCommonProperties());

            foreach (var basketItem in serviceModel.Items)
            {
                var orderItem = new OrderItem
                {
                    OrderId              = order.Id,
                    ProductId            = basketItem.ProductId.Value,
                    ProductSku           = basketItem.ProductSku,
                    ProductName          = basketItem.ProductName,
                    PictureUrl           = basketItem.PictureUrl,
                    Quantity             = basketItem.Quantity,
                    ExternalReference    = basketItem.ExternalReference,
                    ExpectedDeliveryFrom = basketItem.ExpectedDeliveryFrom,
                    ExpectedDeliveryTo   = basketItem.ExpectedDeliveryTo,
                    MoreInfo             = basketItem.MoreInfo
                };

                this.context.OrderItems.Add(orderItem.FillCommonProperties());
            }

            await this.context.SaveChangesAsync();

            var message = new OrderStartedIntegrationEvent
            {
                BasketId = serviceModel.BasketId
            };

            this.eventBus.Publish(message);
        }