public async Task <CuratedExperienceComponent> FindDestinationComponentAsync(CuratedExperience curatedExperience, Guid buttonId, Guid answersDocId)
        {
            var allButtonsInCuratedExperience = curatedExperience.Components.Select(x => x.Buttons).ToList();
            var currentButton = new Button();

            foreach (var button in allButtonsInCuratedExperience)
            {
                if (button.Where(x => x.Id == buttonId).Any())
                {
                    //currentComponent = button.Where(x => x.Id == buttonId)
                    currentButton = button.Where(x => x.Id == buttonId).First();
                }
            }

            var destinationComponent         = new CuratedExperienceComponent();
            CuratedExperienceAnswers answers = new CuratedExperienceAnswers();
            var currentComponent             = curatedExperience.Components.Where(x => x.Buttons.Contains(currentButton)).FirstOrDefault();

            if (!string.IsNullOrWhiteSpace(currentComponent.Code.CodeAfter) && currentComponent.Code.CodeAfter.Contains(Tokens.GOTO))
            {
                answers = await dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId);

                // get the answers so far - done
                // get all the code in all the curated experience - to be done
                var currentComponentLogic = ExtractLogic(currentComponent, answers);
                if (currentComponentLogic != null)
                {
                    var parsedCode = parser.Parse(currentComponentLogic);
                    if (parsedCode.Any())
                    {
                        destinationComponent = curatedExperience.Components.Where(x => x.Name.Contains(parsedCode.Values.FirstOrDefault())).FirstOrDefault();
                    }
                }
            }
            else if (!string.IsNullOrWhiteSpace(currentButton.Destination))
            {
                if (curatedExperience.Components.Where(x => x.Name == currentButton.Destination).Any())
                {
                    destinationComponent = curatedExperience.Components.Where(x => x.Name == currentButton.Destination).FirstOrDefault();
                }
            }
            if (!string.IsNullOrWhiteSpace(destinationComponent.Code.CodeBefore) && destinationComponent.Code.CodeBefore.Contains(Tokens.GOTO))
            {
                if (answers.AnswersDocId == default(Guid))
                {
                    answers = await dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId);
                }
                var currentComponentLogic = ExtractLogic(destinationComponent, answers);
                if (currentComponentLogic != null)
                {
                    var parsedCode = parser.Parse(currentComponentLogic);
                    if (parsedCode.Any())
                    {
                        destinationComponent = curatedExperience.Components.Where(x => x.Name.Contains(parsedCode.Values.LastOrDefault())).FirstOrDefault();
                    }
                }
            }
            return(destinationComponent.ComponentId == default(Guid) ? null : destinationComponent);
        }
Example #2
0
        public UnprocessedPersonalizedPlan Build(JObject personalizedPlan, CuratedExperienceAnswers userAnswers)
        {
            var stepsInScope         = new List <JToken>();
            var evaluatedUserAnswers = parser.Parse(userAnswers);
            var a2jAnswers           = MapStringsToA2JAuthorValues(evaluatedUserAnswers);

            var root = personalizedPlan
                       .Properties()
                       .GetArrayValue("rootNode")
                       .FirstOrDefault();

            foreach (var child in root.GetValueAsArray <JArray>("children"))
            {
                var states = child.GetArrayValue("state");
                foreach (var state in states)
                {
                    foreach (var answer in a2jAnswers)
                    {
                        if (answer.Key == state.GetValue("leftOperand") && answer.Value == state.GetValue("operator"))
                        {
                            stepsInScope.Add(child);
                        }
                    }
                }
            }

            var unprocessedPlan = new UnprocessedPersonalizedPlan();

            unprocessedPlan.Id = Guid.NewGuid();


            var unprocessedTopic = new UnprocessedTopic();
            var cultureInfo      = Thread.CurrentThread.CurrentCulture;
            var textInfo         = cultureInfo.TextInfo;

            unprocessedTopic.Name = textInfo.ToTitleCase(personalizedPlan.Properties().GetValue("title"));

            foreach (var step in stepsInScope)
            {
                foreach (var childrenRoot in step.GetValueAsArray <JArray>("children"))
                {
                    var unprocessedStep = new UnprocessedStep();
                    foreach (var child in childrenRoot.GetValueAsArray <JObject>("rootNode").GetValueAsArray <JArray>("children"))
                    {
                        var state = child.GetArrayValue("state").FirstOrDefault();
                        unprocessedStep.Id = Guid.NewGuid();

                        if (!string.IsNullOrWhiteSpace(state.GetValue("title")))
                        {
                            unprocessedStep.Title = state.GetValue("title");
                            continue;
                        }
                        else
                        {
                            if (!string.IsNullOrWhiteSpace(state.GetValue("userContent")))
                            {
                                unprocessedStep.Description = state.GetValue("userContent").ExtractIdsRemoveCustomA2JTags().SanitizedHtml;
                                unprocessedStep.ResourceIds = state.GetValue("userContent").ExtractIdsRemoveCustomA2JTags().ResourceIds;
                            }
                        }
                        unprocessedTopic.UnprocessedSteps.Add(unprocessedStep);
                    }
                }
            }

            unprocessedPlan.UnprocessedTopics.Add(unprocessedTopic);
            return(unprocessedPlan);
        }