public void SaveAnswersAsyncNoAnswersDocIdValidate(CuratedExperience curatedExperiencedata, dynamic expectedData, Guid answersDocId, CuratedExperienceAnswersViewModel viewModelAnswer, CuratedExperienceAnswers curatedExperienceAnswers, dynamic CuratedExperienceAnswersSchema)
        {
            //arrange
            var dbResponse = dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId);

            dbResponse.ReturnsForAnyArgs <CuratedExperienceAnswers>(curatedExperienceAnswers);

            Document       updatedDocument = new Document();
            JsonTextReader reader          = new JsonTextReader(new StringReader(CuratedExperienceAnswersSchema));

            updatedDocument.LoadFrom(reader);

            dbService.UpdateItemAsync <CuratedExperienceAnswersViewModel>(
                Arg.Any <string>(),
                Arg.Any <CuratedExperienceAnswersViewModel>(),
                Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument);

            dbService.CreateItemAsync <CuratedExperienceAnswers>(
                Arg.Any <CuratedExperienceAnswers>(),
                Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument);

            //act
            var response     = curatedExperience.SaveAnswersAsync(viewModelAnswer, curatedExperiencedata);
            var actualResult = JsonConvert.SerializeObject(response.Result);

            //assert
            Assert.Contains(expectedData, actualResult);
        }
        private CuratedExperienceComponent RemainingQuestions(CuratedExperience curatedExperience, Guid buttonId)
        {
            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())
                {
                    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(currentButton.Destination))
            {
                if (curatedExperience.Components.Where(x => x.Name == currentButton.Destination).Any())
                {
                    destinationComponent = curatedExperience.Components.Where(x => x.Name == currentButton.Destination).FirstOrDefault();
                }
            }

            return(destinationComponent.ComponentId == default(Guid) ? null : destinationComponent);
        }
        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);
        }
        public void GetCuratedExperienceAsyncValidate(CuratedExperience curatedExperiencedata, dynamic expectedData, Guid id)
        {
            //arrange
            var dbResponse = dbService.GetItemAsync <CuratedExperience>(id.ToString(), dbSettings.CuratedExperiencesCollectionId);

            dbResponse.ReturnsForAnyArgs <CuratedExperience>(curatedExperiencedata);
            var response = curatedExperience.GetCuratedExperienceAsync(id);

            //act
            var actualResult   = JsonConvert.SerializeObject(response.Result);
            var expectedResult = JsonConvert.SerializeObject(expectedData);

            //assert
            Assert.Equal(expectedResult, actualResult);
        }
        public void GetNextComponentAsyncValidate(CuratedExperience curatedExperiencedata, CuratedExperienceComponentViewModel expectedData, Guid answersDocId, CuratedExperienceAnswersViewModel component, CuratedExperienceAnswers curatedExperienceAnswers)
        {
            //arrange
            var dbResponse = dbService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId);

            dbResponse.ReturnsForAnyArgs <CuratedExperienceAnswers>(curatedExperienceAnswers);

            //act
            var response       = curatedExperience.GetNextComponentAsync(curatedExperiencedata, component);
            var actualResult   = JsonConvert.SerializeObject(response.Result);
            var expectedResult = JsonConvert.SerializeObject(expectedData);

            //assert
            Assert.Equal(expectedResult, actualResult);
        }
        public async Task <PersonalizedPlanViewModel> GeneratePersonalizedPlanAsync(CuratedExperience curatedExperience, Guid answersDocId)
        {
            var a2jPersonalizedPlan = await dynamicQueries.FindItemWhereAsync <JObject>(cosmosDbSettings.A2JAuthorDocsCollectionId, Constants.Id,
                                                                                        curatedExperience.A2jPersonalizedPlanId.ToString());

            var userAnswers = await backendDatabaseService.GetItemAsync <CuratedExperienceAnswers>(answersDocId.ToString(),
                                                                                                   cosmosDbSettings.GuidedAssistantAnswersCollectionId);

            if (a2jPersonalizedPlan == null || userAnswers == null || userAnswers.AnswersDocId == default(Guid))
            {
                return(null);
            }

            return(await personalizedPlanViewModelMapper.MapViewModel(personalizedPlanEngine.Build(a2jPersonalizedPlan, userAnswers)));
        }
Exemplo n.º 7
0
        public CuratedExperience ConvertA2JAuthorToCuratedExperience(JObject a2jSchema, bool isFromAdminImport = false)
        {
            var             curatedExperience = new CuratedExperience();
            var             a2jProperties     = a2jSchema.Properties();
            GuidedAssistant resource          = null;

            curatedExperience.CuratedExperienceId = Guid.NewGuid();
            curatedExperience.Title = a2jProperties.GetValue("title");

            if (!isFromAdminImport)
            {
                resource = MapResourceProperties(a2jProperties, curatedExperience.CuratedExperienceId);
                dbService.CreateItemAsync(resource, dbSettings.ResourcesCollectionId);
            }

            var pages = ((JObject)a2jProperties.Where(x => x.Name == "pages").FirstOrDefault()?.Value).Properties();

            foreach (var page in pages)
            {
                var pageProperties   = ((JObject)page.FirstOrDefault()).Properties();
                var componentFields  = GetFields(pageProperties);
                var componentButtons = GetButtons(pageProperties);
                var componentCodes   = GetCodes(pageProperties);

                curatedExperience.Components.Add(new CuratedExperienceComponent
                {
                    ComponentId = Guid.NewGuid(),
                    Name        = pageProperties.GetValue("name"),
                    Help        = pageProperties.GetValue("help").RemoveHtmlTags(),
                    Learn       = pageProperties.GetValue("learn").RemoveHtmlTags(),
                    Text        = pageProperties.GetValue("text").RemoveHtmlTags(keepTextFormatingTags: true, keepHyperlinkTags: true).RemoveCustomA2JFunctions(),
                    Fields      = componentFields,
                    Buttons     = componentButtons,
                    Code        = componentCodes
                });
            }

            // find out the first question in the list and add it to top (A2J Author doesn't order the pages in the json array, it depends on
            // an additional xml file to arrange the questions!)
            var firstQuestion      = curatedExperience.Components.Where(x => x.Name == a2jProperties.GetValue("firstPage")).First();
            var firstQuestionIndex = curatedExperience.Components.IndexOf(firstQuestion);

            curatedExperience.Components.RemoveAt(firstQuestionIndex);
            curatedExperience.Components.Insert(0, firstQuestion);

            dbService.CreateItemAsync(curatedExperience, dbSettings.CuratedExperiencesCollectionId);
            return(curatedExperience);
        }
        public void GeneratePersonalizedPlanAsyncValidate(CuratedExperience curatedExperience, CuratedExperienceAnswers expectedData, JObject personalizedPlan)
        {
            //arrange
            var a2jPersonalizedPlan = dynamicQueries.FindItemWhereAsync <JObject>(dbSettings.A2JAuthorDocsCollectionId, Constants.Id, curatedExperience.A2jPersonalizedPlanId.ToString());

            a2jPersonalizedPlan.ReturnsForAnyArgs <JObject>(personalizedPlan);

            var dbResponse = dbService.GetItemAsync <dynamic>(answersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId);
            var response   = personalizedPlanBusinessLogic.GeneratePersonalizedPlanAsync(curatedExperience, answersDocId);

            //act
            var actualResult   = JsonConvert.SerializeObject(response.Result);
            var expectedResult = JsonConvert.SerializeObject(expectedData);

            //assert
            Assert.Equal("null", actualResult);
        }
        public async Task <CuratedExperienceComponentViewModel> GetComponent(CuratedExperience curatedExperience, Guid componentId)
        {
            var dbComponent = new CuratedExperienceComponent();
            var answers     = new CuratedExperienceAnswers();

            if (componentId == default(Guid))
            {
                answers.AnswersDocId        = Guid.NewGuid();
                answers.CuratedExperienceId = curatedExperience.CuratedExperienceId;
                await dbService.CreateItemAsync(answers, dbSettings.GuidedAssistantAnswersCollectionId);

                dbComponent = curatedExperience.Components.First();
            }
            else
            {
                dbComponent = curatedExperience.Components.Where(x => x.ComponentId == componentId).FirstOrDefault();
            }

            return(MapComponentViewModel(curatedExperience, dbComponent, answers.AnswersDocId));
        }
        public void GetComponentValidateNewAnswerDoc(CuratedExperience curatedExperiencedata, CuratedExperienceComponentViewModel expectedData, Guid id, dynamic curateExperienceAnswers)
        {
            //arrange
            Document       updatedDocument = new Document();
            JsonTextReader reader          = new JsonTextReader(new StringReader(curateExperienceAnswers));

            updatedDocument.LoadFrom(reader);

            dbService.CreateItemAsync <CuratedExperienceComponentViewModel>(
                Arg.Any <CuratedExperienceComponentViewModel>(),
                Arg.Any <string>()).ReturnsForAnyArgs <Document>(updatedDocument);

            //act
            var response       = curatedExperience.GetComponent(curatedExperiencedata, id);
            var actualResult   = JsonConvert.SerializeObject(response.Result);
            var expectedResult = JsonConvert.SerializeObject(expectedData);

            //assert
            Assert.DoesNotContain(id.ToString(), actualResult);
        }
        private CuratedExperienceComponentViewModel MapComponentViewModel(CuratedExperience curatedExperience, CuratedExperienceComponent dbComponent, Guid answersDocId)
        {
            if (dbComponent == null || dbComponent.ComponentId == default(Guid))
            {
                return(null);
            }
            var remainingQuestions = CalculateRemainingQuestions(curatedExperience, dbComponent);

            return(new CuratedExperienceComponentViewModel
            {
                CuratedExperienceId = curatedExperience.CuratedExperienceId,
                AnswersDocId = answersDocId,
                QuestionsRemaining = remainingQuestions,
                ComponentId = dbComponent.ComponentId,
                Name = dbComponent.Name,
                Text = dbComponent.Text,
                Help = dbComponent.Help,
                Learn = dbComponent.Learn,
                Tags = dbComponent.Tags,
                Buttons = dbComponent.Buttons,
                Fields = dbComponent.Fields,
            });
        }
        private int CalculateRemainingQuestions(CuratedExperience curatedExperience, CuratedExperienceComponent currentComponent)
        {
            // start calculating routes based on the current component location in the json tree.
            var indexOfCurrentComponent = curatedExperience.Components.FindIndex(x => x.ComponentId == currentComponent.ComponentId);

            // every curated experience has one or more components; every component has one or more buttons; every button has one or more destinations.
            var possibleRoutes = new List <List <CuratedExperienceComponent> >();

            foreach (var component in curatedExperience.Components.Skip(indexOfCurrentComponent))
            {
                for (int i = 0; i < component.Buttons.Count; i++)
                {
                    if (possibleRoutes.Count <= i)
                    {
                        possibleRoutes.Add(new List <CuratedExperienceComponent>());
                    }

                    possibleRoutes[i].AddIfNotNull(RemainingQuestions(curatedExperience, component.Buttons[i].Id));
                }
            }

            // return the longest possible route
            return(possibleRoutes.OrderByDescending(x => x.Count).First().Count);
        }
        public async Task <Document> SaveAnswersAsync(CuratedExperienceAnswersViewModel viewModelAnswer, CuratedExperience curatedExperience)
        {
            var dbAnswers       = MapCuratedExperienceViewModel(viewModelAnswer, curatedExperience);
            var savedAnswersDoc = await dbService.GetItemAsync <CuratedExperienceAnswers>(viewModelAnswer.AnswersDocId.ToString(), dbSettings.GuidedAssistantAnswersCollectionId);

            if (savedAnswersDoc == null || savedAnswersDoc.AnswersDocId == default(Guid))
            {
                return(await dbService.CreateItemAsync(dbAnswers, dbSettings.GuidedAssistantAnswersCollectionId));
            }

            savedAnswersDoc.ButtonComponents.AddRange(dbAnswers.ButtonComponents);
            savedAnswersDoc.FieldComponents.AddRange(dbAnswers.FieldComponents);

            return(await dbService.UpdateItemAsync(viewModelAnswer.AnswersDocId.ToString(), savedAnswersDoc, dbSettings.GuidedAssistantAnswersCollectionId));
        }
 public async Task <CuratedExperienceComponentViewModel> GetNextComponentAsync(CuratedExperience curatedExperience, CuratedExperienceAnswersViewModel component)
 {
     return(MapComponentViewModel(curatedExperience,
                                  await FindDestinationComponentAsync(curatedExperience, component.ButtonId, component.AnswersDocId), component.AnswersDocId));
 }
        public CuratedExperienceAnswers MapCuratedExperienceViewModel(CuratedExperienceAnswersViewModel viewModelAnswer, CuratedExperience curatedExperience)
        {
            var buttonComponent = new CuratedExperienceComponent();

            foreach (var component in curatedExperience.Components)
            {
                if (component.Buttons.Where(x => x.Id == viewModelAnswer.ButtonId).Any())
                {
                    var button = component.Buttons.Where(x => x.Id == viewModelAnswer.ButtonId).FirstOrDefault();
                    buttonComponent = curatedExperience.Components.Where(x => x.Buttons.Contains(button)).FirstOrDefault();
                }
            }

            var userSelectedButtons = new List <ButtonComponent>();

            userSelectedButtons.Add(new ButtonComponent
            {
                ButtonId = viewModelAnswer.ButtonId,
                Name     = buttonComponent.Buttons.Where(x => x.Id == viewModelAnswer.ButtonId).FirstOrDefault().Name,
                Value    = buttonComponent.Buttons.Where(x => x.Id == viewModelAnswer.ButtonId).FirstOrDefault().Value,

                CodeBefore = buttonComponent.Code.CodeBefore,
                CodeAfter  = buttonComponent.Code.CodeAfter
            });

            var userSelectedFields = new List <FieldComponent>();

            foreach (var answerField in viewModelAnswer.Fields)
            {
                var fieldComponent = new CuratedExperienceComponent();
                foreach (var component in curatedExperience.Components)
                {
                    if (answerField != null && component.Fields.Where(x => x.Id == Guid.Parse(answerField.FieldId)).Any())
                    {
                        var selectedField = component.Fields.Where(x => x.Id == Guid.Parse(answerField.FieldId)).FirstOrDefault();
                        fieldComponent = curatedExperience.Components.Where(x => x.Fields.Contains(selectedField)).FirstOrDefault();
                        userSelectedFields.Add(new FieldComponent
                        {
                            CodeBefore = userSelectedButtons.Any(x => x.CodeBefore == fieldComponent.Code.CodeBefore) ? string.Empty : fieldComponent.Code.CodeBefore,
                            CodeAfter  = userSelectedButtons.Any(x => x.CodeAfter == fieldComponent.Code.CodeAfter) ? string.Empty : fieldComponent.Code.CodeAfter,
                            Fields     = new List <AnswerField>
                            {
                                new AnswerField
                                {
                                    FieldId = selectedField.Id,
                                    Text    = answerField.Value,
                                    Name    = selectedField.Name,
                                    Value   = selectedField.Value
                                }
                            }
                        });
                    }
                }
            }

            return(new CuratedExperienceAnswers
            {
                CuratedExperienceId = viewModelAnswer.CuratedExperienceId,
                AnswersDocId = viewModelAnswer.AnswersDocId,
                ButtonComponents = userSelectedButtons,
                FieldComponents = userSelectedFields
            });
        }