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;
            }
        }
        private async Task <long?> GetTopicIdAsync(long?orderId)
        {
            long?topicId;

            if (orderId.HasValue)
            {
                var topicReference = CreateTopicReference.FromOrderId(orderId.Value);
                topicId = await CommunicationMicroService.AllocateTopicAsync(topicReference, null);
            }
            else
            {
                topicId = null;
            }

            return(topicId);
        }
        public async Task <long> SendOutboundMessageAsync(string userId, string subject, string text, long?replyToMessageId, long?orderId)
        {
            using var log = BeginFunction(nameof(MessageAdminService), nameof(SendOutboundMessageAsync), userId, subject, text, replyToMessageId, orderId);
            try
            {
                await Assert(SecurityPolicy.IsPrivileged).ConfigureAwait(false);

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

                long?topicId;
                if (orderId.HasValue)
                {
                    var topicReference = CreateTopicReference.FromOrderId(orderId.Value);
                    topicId = await CommunicationMicroService.AllocateTopicAsync(topicReference, null);
                }
                else
                {
                    topicId = null;
                }

                var messageId = await CommunicationMicroService.SendMessageToParticipantAsync(participantId, subject, text, replyToMessageId, topicId);

                //using var ctx = QuiltContextFactory.Create();

                //var dbAspNetUser = await ctx.AspNetUsers.Where(r => r.Id == userId).SingleAsync().ConfigureAwait(false);


                //Message dbReplyToMessage;
                //if (replyToMessageId != null)
                //{
                //    dbReplyToMessage = await ctx.Messages.Where(r => r.MessageId == replyToMessageId.Value).SingleAsync().ConfigureAwait(false);
                //    var participantUserId = ParseUserId.FromParticipantReference(dbReplyToMessage.Participant.ParticipantReference);
                //    if (participantUserId != userId)
                //    {
                //        throw new InvalidOperationException(string.Format("Reply to message user ID mismatch (user ID = {0}, message user ID = {1}). ", userId, participantUserId));
                //    }

                //    if (dbReplyToMessage.AcknowledgementDateTimeUtc == null)
                //    {
                //        dbReplyToMessage.AcknowledgementDateTimeUtc = GetUtcNow();
                //    }

                //    if (topicId == null)
                //    {
                //        // Propagate order ID from original message.
                //        //
                //        topicId = dbReplyToMessage.TopicId;
                //    }
                //    else
                //    {
                //        if (dbReplyToMessage.TopicId != null)
                //        {
                //            if (dbReplyToMessage.TopicId != orderId)
                //            {
                //                throw new InvalidOperationException(string.Format("Reply to message topic ID mismatch (topic ID = {0}, message topic ID = {1}). ", topicId, dbReplyToMessage.TopicId));
                //            }
                //        }
                //    }
                //}
                //else
                //{
                //    dbReplyToMessage = null;
                //}

                //var participantId = await CommunicationMicroService.AllocateParticipantAsync(participantReference).ConfigureAwait(false);

                //var dbMessage = new Message()
                //{
                //    ParticipantId = participantId,
                //    ConversationId = dbReplyToMessage != null ? dbReplyToMessage.ConversationId : Guid.NewGuid(),
                //    SendReceiveCode = SendReceiveCodes.ToUser,
                //    Subject = subject,
                //    Text = text,
                //    Email = dbAspNetUser.Email,
                //    TopicId = topicId,
                //    CreateDateTimeUtc = GetUtcNow()
                //};
                //_ = ctx.Messages.Add(dbMessage);

                //// Prepend order information.
                ////
                //string orderNumber;
                //if (orderId != null)
                //{
                //    var dbOrder = await ctx.Orders.Where(r => r.OrderId == orderId.Value).SingleAsync().ConfigureAwait(false);
                //    orderNumber = dbOrder.OrderNumber;

                //    var url = Constants.WebsiteFullUrl + "/Order/Index/" + orderId.Value.ToString();

                //    text = "Order " + dbOrder.OrderNumber + ": " + url + System.Environment.NewLine + System.Environment.NewLine + text;
                //}
                //else
                //{
                //    orderNumber = null;
                //}

                //var formatter = new OutboundMessageEmailFormatter(subject, text, orderId, orderNumber);

                //var dbEmailRequest = new EmailRequest()
                //{
                //    EmailRequestStatusTypeCodeNavigation = ctx.EmailRequestStatusType(EmailRequestStatusTypes.Posted),
                //    SenderEmail = Constants.DoNotReplyEmail,
                //    SenderEmailName = Constants.DoNotReplyEmailName,
                //    RecipientEmail = dbAspNetUser.Email,
                //    RecipientEmailName = dbAspNetUser.EmailName(),
                //    RecipientParticipantId = participantId,
                //    Subject = formatter.GetSubject(),
                //    BodyText = formatter.GetText(),
                //    BodyHtml = formatter.GetHtml(),
                //    BodyType = EmailBodyTypes.Message,
                //    CreateDateTimeUtc = GetUtcNow(),
                //    StatusDateTimeUtc = GetUtcNow()
                //};
                //_ = ctx.EmailRequests.Add(dbEmailRequest);

                //var dbMessageEmailRequest = new MessageEmailRequest()
                //{
                //    Message = dbMessage,
                //    EmailRequest = dbEmailRequest
                //};
                //_ = ctx.MessageEmailRequests.Add(dbMessageEmailRequest);

                //_ = await ctx.SaveChangesAsync().ConfigureAwait(false);

                //var result = dbMessage.MessageId;

                var result = messageId;

                log.Result(result);

                return(result);
            }
            catch (Exception ex)
            {
                log.Exception(ex);
                throw;
            }
        }