private async Task ExtractAnswers(Guid applicationId, List <SubmittedApplicationAnswer> answers, string questionId, string questionTag) { try { _logger.LogInformation($"Extract Partnership answers for application {applicationId}"); var answersbyQuestionTag = await ExtractAnswersByQuestionTag(applicationId, questionTag, questionId); if (answersbyQuestionTag == null) { return; } var tabularData = JsonConvert.DeserializeObject <TabularData>(answersbyQuestionTag.Value); var question = new Question { QuestionId = questionId, Input = new Input { Type = TabularDataType } }; var tabularAnswers = TabularDataMapper.GetAnswers(applicationId, YourOrganisation, WhosInControl, PageIdPartnershipAddPartners, question, tabularData); answers.AddRange(tabularAnswers); } catch (Exception ex) { _logger.LogError(ex, $"Unable to extracted {questionTag} answers for Application: {applicationId}"); throw; } }
public void GetAnswers_Returns_expected_result() { var expectedItemCount = _tabularData.DataRows.Count * _tabularData.HeadingTitles.Count; var result = TabularDataMapper.GetAnswers(_applicationId, _sequenceNumber, _sectionNumber, _pageId, _question, _tabularData); CollectionAssert.IsNotEmpty(result); Assert.AreEqual(expectedItemCount, result.Count); for (int row = 0; row < _tabularData.DataRows.Count; row++) { for (int column = 0; column < _tabularData.HeadingTitles.Count; column++) { var answer = result[column + (row * _tabularData.DataRows.Count)]; Assert.AreEqual(_applicationId, answer.ApplicationId); Assert.AreEqual(_sequenceNumber, answer.SequenceNumber); Assert.AreEqual(_sectionNumber, answer.SectionNumber); Assert.AreEqual(_pageId, answer.PageId); Assert.AreEqual(_question.QuestionId, answer.QuestionId); Assert.AreEqual(_question.Input.Type, answer.QuestionType); Assert.AreEqual(_tabularData.DataRows[row].Columns[column], answer.Answer); Assert.AreEqual(_tabularData.HeadingTitles[column], answer.ColumnHeading); Assert.AreEqual(row, answer.RowNumber); Assert.AreEqual(column, answer.ColumnNumber); } } }
public void GetAnswers_when_TabularData_empty_Returns_empty() { _tabularData = new TabularData(); var result = TabularDataMapper.GetAnswers(_applicationId, _pageId, _question, _tabularData); CollectionAssert.IsEmpty(result); }
public void GetAnswers_when_Question_null_Returns_empty() { _question = null; var result = TabularDataMapper.GetAnswers(_applicationId, _pageId, _question, _tabularData); CollectionAssert.IsEmpty(result); }
private static List <SubmittedApplicationAnswer> ExtractQuestionAnswers(Guid applicationId, string pageId, Question question, ICollection <Answer> answers) { var submittedQuestionAnswers = new List <SubmittedApplicationAnswer>(); var questionId = question.QuestionId; // Note: RoATP only has a single answer per question var questionAnswer = answers?.FirstOrDefault(ans => ans.QuestionId == questionId && !string.IsNullOrWhiteSpace(ans.Value)); if (questionAnswer != null) { switch (question.Input.Type.ToUpper()) { case "TABULARDATA": var tabularData = JsonConvert.DeserializeObject <TabularData>(questionAnswer.Value); var tabularAnswers = TabularDataMapper.GetAnswers(applicationId, pageId, question, tabularData); submittedQuestionAnswers.AddRange(tabularAnswers); break; case "CHECKBOXLIST": case "COMPLEXCHECKBOXLIST": var checkboxAnswers = CheckBoxListMapper.GetAnswers(applicationId, pageId, question, questionAnswer.Value); submittedQuestionAnswers.AddRange(checkboxAnswers); break; default: var submittedAnswer = SubmittedAnswerMapper.GetAnswer(applicationId, pageId, question, questionAnswer.Value); submittedQuestionAnswers.Add(submittedAnswer); break; } // We have to do similar for extracting any matching further question if (question.Input.Options != null) { var submittedFurtherQuestionAnswers = new List <SubmittedApplicationAnswer>(); var submittedValues = submittedQuestionAnswers.Where(sqa => sqa.QuestionId == questionId).Select(ans => ans.Answer); foreach (var option in question.Input.Options.Where(opt => opt.FurtherQuestions != null)) { // Check that option was selected if (submittedValues.Contains(option.Value)) { foreach (var furtherQuestion in option.FurtherQuestions) { var furtherQuestionAnswers = ExtractQuestionAnswers(applicationId, pageId, furtherQuestion, answers); submittedFurtherQuestionAnswers.AddRange(furtherQuestionAnswers); } } } submittedQuestionAnswers.AddRange(submittedFurtherQuestionAnswers); } } return(submittedQuestionAnswers); }