Exemplo n.º 1
0
        public async void ProcessOrderQueueMessage(int orderID)
        {
            try
            {
                OrderDataAccess _orderAccess = new OrderDataAccess(_iconfiguration);

                DatabaseResponse orderMqResponse = new DatabaseResponse();

                orderMqResponse = await _messageQueueDataAccess.GetOrderMessageQueueBody(orderID);

                OrderQM orderDetails = new OrderQM();

                string topicName = string.Empty;

                string pushResult = string.Empty;

                if (orderMqResponse != null && orderMqResponse.Results != null)
                {
                    orderDetails = (OrderQM)orderMqResponse.Results;

                    DatabaseResponse OrderCountResponse = await _orderAccess.GetCustomerOrderCount(orderDetails.customerID);

                    try
                    {
                        Dictionary <string, string> attribute = new Dictionary <string, string>();

                        topicName = ConfigHelper.GetValueByKey(ConfigKey.SNS_Topic_ChangeRequest.GetDescription(), _iconfiguration).Results.ToString().Trim();

                        if (string.IsNullOrWhiteSpace(topicName))
                        {
                            throw new NullReferenceException("topicName is null for Order (" + orderID + ") for RemoveVAS Request Service API");
                        }

                        attribute.Add(EventTypeString.EventType, ((OrderCount)OrderCountResponse.Results).SuccessfulOrders == 1 ? Core.Enums.RequestType.NewCustomer.GetDescription() : Core.Enums.RequestType.NewService.GetDescription());

                        pushResult = await _messageQueueDataAccess.PublishMessageToMessageQueue(topicName, orderDetails, attribute);

                        if (pushResult.Trim().ToUpper() == "OK")
                        {
                            MessageQueueRequest queueRequest = new MessageQueueRequest
                            {
                                Source           = CheckOutType.Orders.ToString(),
                                NumberOfRetries  = 1,
                                SNSTopic         = topicName,
                                CreatedOn        = DateTime.Now,
                                LastTriedOn      = DateTime.Now,
                                PublishedOn      = DateTime.Now,
                                MessageAttribute = ((OrderCount)OrderCountResponse.Results).SuccessfulOrders == 1 ? Core.Enums.RequestType.NewCustomer.GetDescription() : Core.Enums.RequestType.NewService.GetDescription(),
                                MessageBody      = JsonConvert.SerializeObject(orderDetails),
                                Status           = 1
                            };
                            await _messageQueueDataAccess.InsertMessageInMessageQueueRequest(queueRequest);
                        }
                        else
                        {
                            MessageQueueRequest queueRequest = new MessageQueueRequest
                            {
                                Source                                = CheckOutType.Orders.ToString(),
                                NumberOfRetries                       = 1,
                                SNSTopic                              = topicName,
                                CreatedOn                             = DateTime.Now,
                                LastTriedOn                           = DateTime.Now,
                                PublishedOn                           = DateTime.Now,
                                MessageAttribute                      = ((OrderCount)OrderCountResponse.Results).SuccessfulOrders == 1 ? Core.Enums.RequestType.NewCustomer.GetDescription() : Core.Enums.RequestType.NewService.GetDescription(),
                                MessageBody                           = orderDetails != null?JsonConvert.SerializeObject(orderDetails) : null,
                                                               Status = 0
                            };
                            await _messageQueueDataAccess.InsertMessageInMessageQueueRequest(queueRequest);
                        }
                    }
                    catch (Exception ex)
                    {
                        LogInfo.Error(new ExceptionHelper().GetLogString(ex, ErrorLevel.Critical));

                        MessageQueueRequestException queueRequest = new MessageQueueRequestException
                        {
                            Source           = CheckOutType.Orders.ToString(),
                            NumberOfRetries  = 1,
                            SNSTopic         = topicName,
                            CreatedOn        = DateTime.Now,
                            LastTriedOn      = DateTime.Now,
                            PublishedOn      = DateTime.Now,
                            MessageAttribute = ((OrderCount)OrderCountResponse.Results).SuccessfulOrders == 1 ? Core.Enums.RequestType.NewCustomer.GetDescription() : Core.Enums.RequestType.NewService.GetDescription(),
                            MessageBody      = JsonConvert.SerializeObject(orderDetails),
                            Status           = 0
                        };
                        await _messageQueueDataAccess.InsertMessageInMessageQueueRequestException(queueRequest);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }