public async Task HandleShipmentEventAsync(MFulfillment_ShipmentEvent eventData)
        {
            using var log = BeginFunction(nameof(FulfillmentEventMicroService), nameof(HandleShipmentEventAsync), eventData);
            try
            {
                using var ctx = CreateQuiltContext();

                switch (eventData.EventType)
                {
                case MFulfillment_ShipmentEventTypes.Process:
                {
                    var fulfillableIds = new HashSet <long>();
                    foreach (var shipmentRequestId in eventData.ShipmentRequestIds)
                    {
                        var mShipmentRequest = await FullfillmentMicroService.GetShipmentRequestAsync(shipmentRequestId).ConfigureAwait(false);

                        foreach (var mShipmentRequestItem in mShipmentRequest.ShipmentRequestItems)
                        {
                            var mFulfillable = await FullfillmentMicroService.GetFulfillableByItemAsync(mShipmentRequestItem.FulfillableItemId).ConfigureAwait(false);

                            var fulfillableId = mFulfillable.FulfillableId;
                            if (fulfillableIds.Contains(fulfillableId))
                            {
                                _ = fulfillableIds.Add(fulfillableId);

                                if (TryParseOrderId.FromFulfillableReference(mFulfillable.FulfillableReference, out var orderId))
                                {
                                    var mOrder = await OrderMicroService.GetOrderAsync(orderId).ConfigureAwait(false);

                                    var userId = ParseUserId.FromOrdererReference(mOrder.OrdererReference);

                                    var participantReference = CreateParticipantReference.FromUserId(userId);
                                    var participantId        = await CommunicationMicroService.AllocateParticipantAsync(participantReference).ConfigureAwait(false);

                                    var topicReference = CreateTopicReference.FromOrderId(orderId);
                                    var topicId        = await CommunicationMicroService.AllocateTopicAsync(topicReference, null).ConfigureAwait(false);

                                    await CommunicationMicroService.SendNotification(participantId, NotificationTypeCodes.OrderShipped, topicId).ConfigureAwait(false);
                                }
                            }
                        }
                    }
                }
                break;
                }

                await Task.CompletedTask.ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }
예제 #2
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}");
                    }
                }
            }
        }