Пример #1
0
        public IActionResult New(int networkEquipmentId)
        {
            var replyData = InitUserNetworks();

            var response = new IncidentChatReply {
                EquipmentId          = networkEquipmentId,
                QuestionIDs          = replyData.QuestionIDs,
                CurrentUserIncidents = _getUserIncidents()
            };

            if (networkEquipmentId == 0)
            {
                response.ReplyContent = replyData.ChatText;
                return(View(response));
            }

            var netEquip = _dbContext.UserNetworkEquipments.FirstOrDefault(e => e.UserNetworkEquipmentId == networkEquipmentId);

            replyData = InitIncidentTypes();

            if (netEquip == null)
            {
                response.ReplyContent = replyData.ChatText;
                return(View(response));
            }

            response.ReplyContent = replyData.ChatText;
            response.QuestionIDs  = replyData.QuestionIDs;
            response.Stage        = 3;

            return(View(response));
        }
Пример #2
0
        public IActionResult New()
        {
            var replyData = InitUserNetworks();

            var response = new IncidentChatReply
            {
                ReplyContent         = replyData.ChatText,
                QuestionIDs          = replyData.QuestionIDs,
                CurrentUserIncidents = _getUserIncidents()
            };

            return(View(response));
        }
Пример #3
0
        public IActionResult Post(IncidentChatMessage chatMessage)
        //public IActionResult Post([FromBody] JsonElement body)
        {
            //var chatMessage = JsonConvert.DeserializeObject<IncidentChatMessage>(body.GetRawText());

            ChatReplyRawContent replyData;

            _logger.LogInformation("Chat Message Received!", chatMessage);
            var chatReply = new IncidentChatReply();

            chatReply.IncidentId       = chatMessage.IncidentId;
            chatReply.QuestionId       = chatMessage.QuestionId;
            chatReply.EquipmentId      = chatMessage.EquipmentId;
            chatReply.NetworkId        = chatMessage.NetworkId;
            chatReply.AnswerId         = chatMessage.AnswerId;
            chatReply.SessionCompleted = chatMessage.SessionCompleted;

            if (chatMessage.SessionCompleted)
            {
                chatReply.ReplyContent = "The chat session has ended!";
                return(Ok(chatReply));
            }

            int replyNumber;

            if (!int.TryParse(chatMessage.MessageContent, out replyNumber))
            {
                chatReply.IsError      = true;
                chatReply.ErrorMessage = "Invalid entry, please retart incident!";
                chatReply.ReplyContent = "Invalid entry, please retart incident!!!";

                return(Ok(chatReply));
            }
            var id = chatMessage.QuestionIDs.FirstOrDefault(i => i.Key == replyNumber.ToString()).Value;

            replyNumber = int.Parse(id);

            //var iDs = chatMessage.QuestionIDs;

            if (chatMessage.Stage == 2)
            {
                replyData              = InitUserNetworkEquipments(replyNumber);
                chatReply.NetworkId    = replyNumber;
                chatReply.ReplyContent = replyData.ChatText;
                chatReply.QuestionIDs  = replyData.QuestionIDs;

                return(Ok(chatReply));
            }

            if (chatMessage.Stage == 3)
            {
                replyData              = InitIncidentTypes();
                chatReply.EquipmentId  = replyNumber;
                chatReply.ReplyContent = replyData.ChatText;
                chatReply.QuestionIDs  = replyData.QuestionIDs;

                return(Ok(chatReply));
            }

            Incident incident;

            if (chatMessage.Stage == 4)
            {
                incident = new Incident
                {
                    IncidentTypeId         = replyNumber,
                    UserNetworkEquipmentId = chatMessage.EquipmentId,
                    IncidentDate           = DateTime.UtcNow
                };
                _dbContext.Incidents.Add(incident);
                _dbContext.SaveChanges();

                chatReply.IncidentId   = incident.IncidentId;
                chatMessage.IncidentId = incident.IncidentId;

                //return Ok(chatReply);
            }
            else
            {
                incident = _dbContext.Incidents.FirstOrDefault(i => i.IncidentId == chatMessage.IncidentId);
            }


            if (chatReply.IncidentId == 0)
            {
                //Restart CHat Request
                replyData              = InitUserNetworkEquipments(replyNumber);
                chatReply.NetworkId    = replyNumber;
                chatReply.ReplyContent = replyData.ChatText;
                chatReply.QuestionIDs  = replyData.QuestionIDs;

                return(Ok(chatReply));
            }

            Question       nextQuestion     = new Question();
            QuestionAnswer lastAnswer       = new QuestionAnswer();
            var            sessionCompleted = false;

            //var currentIncident = _dbContext.Incidents.FirstOrDefault(i=>i.IncidentId == chatMessage.IncidentId);

            //var incidentQuestions = _dbContext.IncudentTypeQuestions.Where(i => i.IncidentId == incident.IncidentId).ToList();

            if (chatMessage.QuestionId != 0)
            {
                var incidentTypeQuestion = new IncidentTypeQuestion {
                    AnswerId   = replyNumber,
                    IncidentId = incident.IncidentId,
                    QuestionId = chatMessage.QuestionId
                };
                _dbContext.IncidentTypeQuestions.Add(incidentTypeQuestion);
                _dbContext.SaveChanges();
            }

            if (!_dbContext.IncidentTypeQuestions.Any(i => i.IncidentId == incident.IncidentId))
            {
                nextQuestion = _dbContext.Questions.FirstOrDefault(q => q.IncidentTypeId == incident.IncidentTypeId);
            }
            else
            {
                var incidentQuestions = _dbContext.IncidentTypeQuestions.Where(i => i.IncidentId == incident.IncidentId).ToArray();
                lastAnswer = _dbContext.QuestionAnswers.FirstOrDefault(a => a.QuestionAnswerId == incidentQuestions[incidentQuestions.Length - 1].AnswerId);

                if (lastAnswer.NextQuestionId == 0)
                {
                    sessionCompleted = true;
                }
                else
                {
                    nextQuestion = _dbContext.Questions.FirstOrDefault(q => q.QuestionId == lastAnswer.NextQuestionId);
                }
            }

            if (sessionCompleted)
            {
                if (!string.IsNullOrWhiteSpace(lastAnswer?.Troubleshooting))
                {
                    chatReply.ReplyContent = "<strong>Troubleshooting:</strong><br />" + _getTroubleshootText(lastAnswer.Troubleshooting);
                }

                if (!string.IsNullOrWhiteSpace(lastAnswer?.Mitigation))
                {
                    //chatReply.ReplyContent += _getTroubleshootText(lastAnswer.Mitigation);
                    chatReply.ReplyContent += _getFinalMessage(incident);
                }


                chatReply.ReplyContent += "<br /><br />The chat session has ended!";

                chatReply.SessionCompleted = true;
                return(Ok(chatReply));
            }

            chatReply.QuestionId   = nextQuestion.QuestionId;
            replyData              = InitChatQuestionAnswer(nextQuestion);
            chatReply.ReplyContent = replyData.ChatText;
            chatReply.QuestionIDs  = replyData.QuestionIDs;


            _logger.LogInformation("Chat Reply Sent!", chatReply);

            return(Ok(chatReply));
        }