Esempio n. 1
0
        private MFulfillment_AllocateFulfillable RandomFulfillable()
        {
            var fulfillableReference = CreateFulfillableReference.FromTimestamp(GetUniqueNow());
            var utcNow = DateTime.UtcNow;
            var name   = $"Fulfillable {utcNow.Year - 2000:00}-{utcNow.DayOfYear:000}-{utcNow.Hour * utcNow.Minute:0000}.{utcNow.Millisecond:000}";

            var items = new List <MFulfillment_AllocateFulfillableItem>();
            int max   = Random.Next(9) + 1;

            for (int idx = 0; idx < max; ++idx)
            {
                items.Add(RandomFulfillableItem());
            }

            return(new MFulfillment_AllocateFulfillable()
            {
                Name = name,
                ShippingAddress = new MCommon_Address()
                {
                    Name = "RICHARD TODD",
                    AddressLine1 = "17340 W 156 TER",
                    City = "OLATHE",
                    StateCode = "KS",
                    PostalCode = "66062",
                    CountryCode = "US"
                },
                FulfillableReference = fulfillableReference,
                FulfillableItems = items
            });
        }
Esempio n. 2
0
        public async Task <AOrder_Order> GetOrderAsync(long orderId)
        {
            using var log = BeginFunction(nameof(OrderAdminService), nameof(GetOrderAsync), orderId);
            try
            {
                await Assert(SecurityPolicy.IsPrivileged).ConfigureAwait(false);

                var mOrder = await OrderMicroService.GetOrderAsync(orderId).ConfigureAwait(false);

                var mTransactions = await OrderMicroService.GetOrderTransactionSummariesAsync(orderId, null, null);

                var mEvents = await OrderMicroService.GetOrderEventLogSummariesAsync(orderId, null, null);

                var fulfillableReference = CreateFulfillableReference.FromOrderId(orderId);
                var fulfillableId        = await FulfillmentMicroService.LookupFulfillableAsync(fulfillableReference);

                var mFulfillable = fulfillableId != null
                    ? await FulfillmentMicroService.GetFulfillableAsync(fulfillableId.Value).ConfigureAwait(false)
                    : null;

                var fundableReference = CreateFundableReference.FromOrderId(orderId);
                var fundableId        = await FundingMicroService.LookupFundableAsync(fundableReference);

                var mFundable = fundableId != null
                    ? await FundingMicroService.GetFundableAsync(fundableId.Value).ConfigureAwait(false)
                    : null;

                var mUser = TryParseUserId.FromOrdererReference(mOrder.OrdererReference, out string userId)
                    ? await UserMicroService.GetUserAsync(userId)
                    : null;

                var result = Create.AOrder_Order(mOrder, mTransactions, mEvents, mFulfillable, mFundable, mUser);

                log.Result(result);

                return(result);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }
Esempio n. 3
0
        private async Task HandleFulfillmentRequiredEventAsync(MOrder_OrderEvent eventData)
        {
            var fulfillableReference = CreateFulfillableReference.FromOrderId(eventData.OrderId);

            var existingFulfillableId = await FullfillmentMicroService.LookupFulfillableAsync(fulfillableReference);

            long fulfillableId;

            if (!existingFulfillableId.HasValue)
            {
                var allocateFulfillable = new MFulfillment_AllocateFulfillable()
                {
                    FulfillableReference = fulfillableReference,
                    Name             = $"Order {eventData.OrderNumber}",
                    ShippingAddress  = eventData.ShippingAddress,
                    FulfillableItems = new List <MFulfillment_AllocateFulfillableItem>()
                };
                foreach (var fulfillmentEventItem in eventData.OrderEventItems)
                {
                    var fulfillableItemReference = CreateFulfillableItemReference.FromOrderItemId(fulfillmentEventItem.OrderItemId);
                    var allocateFulfillableItem  = new MFulfillment_AllocateFulfillableItem()
                    {
                        FulfillableItemReference = fulfillableItemReference,
                        Description               = fulfillmentEventItem.Description,
                        ConsumableReference       = fulfillmentEventItem.ConsumableReference,
                        FulfillableItemComponents = new List <MFulfillment_AllocateFulfillableItemComponent>()
                    };
                    foreach (var fulfillmentEventItemComponent in fulfillmentEventItem.OrderEventItemComponents)
                    {
                        var allocateFulfillableItemComponent = new MFulfillment_AllocateFulfillableItemComponent()
                        {
                            Description         = fulfillmentEventItemComponent.Description,
                            ConsumableReference = fulfillmentEventItemComponent.ConsumableReference,
                            Quantity            = fulfillmentEventItemComponent.Quantity
                        };
                        allocateFulfillableItem.FulfillableItemComponents.Add(allocateFulfillableItemComponent);
                    }
                    allocateFulfillable.FulfillableItems.Add(allocateFulfillableItem);
                }

                var allocateFulfillableResponse = await FullfillmentMicroService.AllocateFulfillableAsync(allocateFulfillable);

                fulfillableId = allocateFulfillableResponse.FulfillableId;
            }
            else
            {
                fulfillableId = existingFulfillableId.Value;
            }
            //LogMessage($"Fullfillable ID = {fulfillableId}.");

            foreach (var fulfillmentEventItem in eventData.OrderEventItems)
            {
                if (fulfillmentEventItem.RequiredQuantity != 0)
                {
                    var fulfillableItemReference = CreateFulfillableItemReference.FromOrderItemId(fulfillmentEventItem.OrderItemId);
                    var fulfillableItemId        = await FullfillmentMicroService.LookupFulfillableItemAsync(fulfillableItemReference);

                    if (fulfillableItemId.HasValue)
                    {
                        await FullfillmentMicroService.SetFulfillmentRequestQuantity(fulfillableItemId.Value, fulfillmentEventItem.RequiredQuantity, eventData.UnitOfWork).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new InvalidOperationException($"Fulfillable item not found for reference {fulfillableItemReference}");
                    }
                }
            }
        }
Esempio n. 4
0
        public async Task CreateAndShipOrder()
        {
            var services = ServiceScope.ServiceProvider;
            var logger   = services.GetService <ILogger <OrderTest> >();

            var unitOfWork = CreateUnitOfWork.Timestamp(GetUniqueNow());

            // Get the user ID.
            //
            string userId;
            {
                var identityUser = await UserManager.FindByNameAsync("*****@*****.**");

                userId = identityUser.Id;
                logger.LogInformation("User ID = {0}", userId);
            }

            // Get the orderer ID.
            //
            long ordererId;
            {
                var ordererReference = CreateOrdererReference.FromTimestamp(GetUniqueNow());
                ordererId = await OrderMicroService.AllocateOrdererAsync(ordererReference);

                logger.LogInformation("Orderer ID = {0}", ordererId);
            }

            // Get the funder ID.
            //
            long funderId;
            {
                var funderReference = CreateFunderReference.FromTimestamp(GetUniqueNow());
                funderId = await FundingMicroService.AllocateFunderAsync(funderReference);

                logger.LogInformation("Funder ID = {0}", funderId);
            }

            // Create the design.
            //
            Guid designId;
            {
                var designData = Factory.CreateDesign();
                designId = await DesignAjaxService.SaveDesignAsync(userId, designData);

                logger.LogInformation($"Design ID = {designId}");
            }

            // Create the project.
            //
            string projectId;
            {
                projectId = await ProjectUserService.CreateProjectAsync(userId, ProjectUserService.ProjectType_Kit, "Test Project", designId);

                logger.LogInformation($"Project ID = {projectId}");
            }

            // Create the orderable ID.
            //
            long orderableId;
            {
                var projectSnapshotId = await ProjectMicroService.GetCurrentSnapshotIdAsync(Guid.Parse(projectId));

                var mProjectSnapshotDetail = await ProjectMicroService.GetProjectSnapshotAsync(projectSnapshotId);

                var mAllocateOrderable             = MicroDataFactory.MOrder_AllocateOrderable(mProjectSnapshotDetail);
                var mAllocateOrderableResponseData = await OrderMicroService.AllocateOrderableAsync(mAllocateOrderable);

                orderableId = mAllocateOrderableResponseData.OrderableId;
                logger.LogInformation($"Orderable ID = {orderableId}");
            }

            var shippingAddress = new MCommon_Address()
            {
                Name         = "RICH TODD",
                AddressLine1 = "17340 W 156 TER",
                City         = "OLATHE",
                StateCode    = "KS",
                PostalCode   = "66062",
                CountryCode  = "US"
            };

            // Create the order.
            //
            long orderId;

            {
                // Add item to cart.
                //
                _ = await OrderMicroService.AddCartItemAsync(ordererId, orderableId, 2);

                // Update shipping address.
                //
                _ = await OrderMicroService.UpdateShippingAddressAsync(ordererId, shippingAddress);

                // Submit order.
                //
                orderId = await OrderMicroService.SubmitCartAsync(ordererId);

                logger.LogInformation($"Order ID = {orderId}");
            }

            _ = await EventProcessorMicroService.ProcessPendingEvents();

            // Create fundable for order.
            //
            long fundableId;
            {
                var fundableReference = CreateFundableReference.FromOrderId(orderId);
                fundableId = await FundingMicroService.AllocateFundableAsync(fundableReference);

                logger.LogInformation($"Fundable ID = {fundableId}");
            }

            // Retrieve the fundable detail.
            //
            MFunding_Fundable fundableDetail;

            {
                fundableDetail = await FundingMicroService.GetFundableAsync(fundableId);

                logger.LogInformation($"Fundable Detail = {fundableDetail}");
            }

            // Post a receipt for the order.
            //
            {
                var fundsRequiredDelta = fundableDetail.FundsRequiredTotal - fundableDetail.FundsReceived;
                await FundingMicroService.SetFundsReceivedAsync(funderId, fundableDetail.FundableReference, fundableDetail.FundsRequiredTotal, unitOfWork.Next());

                //await fundingService.TransferFundsAsync(funderId, fundableId, fundsRequiredDelta);
                logger.LogInformation($"{fundsRequiredDelta} funds applied.");
            }

            _ = await EventProcessorMicroService.ProcessPendingEvents();

            // Lookup fulfillable.
            //
            long fulfillableId;
            {
                var fulfillableReference = CreateFulfillableReference.FromOrderId(orderId);
                fulfillableId = (await FulfillmentMicroService.LookupFulfillableAsync(fulfillableReference)).Value;
                await FulfillmentMicroService.SetFulfillableShippingAddress(fulfillableId, shippingAddress);

                logger.LogInformation($"Fulfillable ID = {fulfillableId}");
            }

            // Lookup pending shipment request.
            //
            long?shipmentRequestId;
            {
                shipmentRequestId = await FulfillmentMicroService.GetPendingShipmentRequestAsync(fulfillableId);

                logger.LogInformation($"Shipment Request ID = {shipmentRequestId}");
            }

            // Open it.
            //
            {
                await FulfillmentMicroService.OpenShipmentRequestAsync(shipmentRequestId.Value);
            }

            // Lookup shipment request.
            //
            MFulfillment_ShipmentRequest shipmentRequestDetail;
            {
                shipmentRequestDetail = await FulfillmentMicroService.GetShipmentRequestAsync(shipmentRequestId.Value);

                logger.LogInformation($"Shipment Request Detail = {shipmentRequestDetail}");
            }

            // Create shipment.
            //
            long shipmentId;

            {
                var items = new List <MFulfillment_CreateShipmentItem>();
                foreach (var shipmentRequestItemDetail in shipmentRequestDetail.ShipmentRequestItems)
                {
                    items.Add(new MFulfillment_CreateShipmentItem()
                    {
                        ShipmentRequestItemId = shipmentRequestItemDetail.ShipmentRequestItemId,
                        Quantity = shipmentRequestItemDetail.Quantity
                    });
                }

                var data = new MFulfillment_CreateShipment()
                {
                    ShippingVendorId    = ShippingVendorIds.Usps,
                    ShipmentDateTimeUtc = Locale.GetUtcNow(),
                    TrackingCode        = "123123",
                    CreateShipmentItems = items
                };

                shipmentId = await FulfillmentMicroService.CreateShipmentAsync(data);

                await FulfillmentMicroService.PostShipmentAsync(shipmentId);

                await FulfillmentMicroService.ProcessShipmentAsync(shipmentId);

                logger.LogInformation($"Shipment ID = {shipmentId}");
            }

            _ = await EventProcessorMicroService.ProcessPendingEvents();
        }