Skip to content

Commit

Permalink
Merge pull request #1187 from cabinetoffice/bugfix/dp-1097-exclusions…
Browse files Browse the repository at this point in the history
…-not-showing

DP-1097 New exclusions not showing
  • Loading branch information
andymantell authored Jan 24, 2025
2 parents 2c1e491 + 87bb066 commit 97ddc6e
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using CO.CDP.AwsServices;
using CO.CDP.Forms.WebApi.Model;
using CO.CDP.Forms.WebApi.Tests.AutoMapper;
using CO.CDP.Forms.WebApi.UseCase;
Expand Down Expand Up @@ -462,6 +461,45 @@ public async Task Execute_ShouldCopySharedConsentWithExistingAnswersWhileUpdatin
)));
}

[Fact]
public async Task Execute_ShouldMarkExistingAnswerSetToDelete_WhenSameSectionTypeAnswerSetWithoutFurtherQuestionsExemptedIsAdded()
{
var organisation = GivenOrganisationExists(organisationId: Guid.NewGuid());
var section = GivenFormSectionExists(sectionId: Guid.NewGuid());
var sharedConsent = GivenSharedConsent(organisation, section.Form, state: Submitted);
var answerSetOld = GivenAnswerSet(sharedConsent: sharedConsent, section: section, answers: [], furtherQuestionsExempted: true);
answerSetOld.Id = 1;

_repository.Setup(r => r.GetSharedConsentWithAnswersAsync(section.Form.Guid, organisation.Guid))
.ReturnsAsync(sharedConsent);

var question = GivenFormQuestion(section: section, type: Text);

var updateFormSectionAnswers = new UpdateFormSectionAnswers
{
Answers = [new() { Id = Guid.NewGuid(), QuestionId = question.Guid, TextValue = "My new answer" },]
};

var command = (
formId: section.Form.Guid,
sectionId: section.Guid,
answerSetId: Guid.NewGuid(),
organisationId: organisation.Guid,
updateFormSectionAnswers);

await UseCase.Execute(command);

_repository.Verify(r => r.SaveSharedConsentAsync(It.Is<Persistence.SharedConsent>(sc =>
sc.Guid != sharedConsent.Guid &&
sc.SubmissionState == Draft &&
sc.AnswerSets.Count == 2 &&
sc.AnswerSets.First().Id == 0 &&
sc.AnswerSets.First().FurtherQuestionsExempted == true &&
sc.AnswerSets.First().Deleted == true &&
sc.AnswerSets.ElementAt(1).Answers.ElementAt(0).TextValue == "My new answer"
)));
}

[Fact]
public async Task Execute_ShouldReturnExistingAnswerSet_WhenFurtherQuestionsExemptedIsTrue()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using AutoMapper;
using CO.CDP.AwsServices;
using CO.CDP.Forms.WebApi.Model;
using CO.CDP.OrganisationInformation.Persistence;
using Persistence = CO.CDP.OrganisationInformation.Persistence.Forms;
Expand Down Expand Up @@ -28,12 +27,34 @@ public async Task<bool> Execute((Guid formId, Guid sectionId, Guid answerSetId,
var (updatedSharedConsent, mappedAnswerSetId, updatedAnswers) =
MapSharedConsent(sharedConsent, answerSetId, updateFormSectionAnswers.Answers ?? []);

await UpdateOrAddAnswers(mappedAnswerSetId, updatedAnswers, section, updatedSharedConsent, updateFormSectionAnswers.FurtherQuestionsExempted);
UpdateOrAddAnswers(mappedAnswerSetId, updatedAnswers, section, updatedSharedConsent, updateFormSectionAnswers.FurtherQuestionsExempted);
UpdateAnswerSets(updatedSharedConsent);
await formRepository.SaveSharedConsentAsync(updatedSharedConsent);

return true;
}

private static void UpdateAnswerSets(Persistence.SharedConsent updatedSharedConsent)
{
foreach (var answerSets in updatedSharedConsent.AnswerSets.GroupBy(s => s.SectionId))
{
var activeAnswerSets = answerSets.Where(a => !a.Deleted);

var answerSetsWithFurtherQuestionsExempted =
activeAnswerSets.Where(a => a.FurtherQuestionsExempted == true);

var answerSetsWithoutFurtherQuestionsExemptedCount =
activeAnswerSets.Count(a => a.FurtherQuestionsExempted == false);

if (answerSetsWithoutFurtherQuestionsExemptedCount > 0 && answerSetsWithFurtherQuestionsExempted.Any())
{
foreach (var answetSetWithFurtherQuestionsExempted in answerSetsWithFurtherQuestionsExempted)
{
answetSetWithFurtherQuestionsExempted.Deleted = true;
}
}
}
}

private static (Persistence.SharedConsent, Guid, List<FormAnswer>) MapSharedConsent(
Persistence.SharedConsent sharedConsent, Guid answerSetId, List<FormAnswer> answers)
{
Expand All @@ -59,7 +80,7 @@ private static (Persistence.SharedConsent, Guid, List<FormAnswer>) MapSharedCons
);
}

private async Task UpdateOrAddAnswers(
private void UpdateOrAddAnswers(
Guid answerSetId,
List<FormAnswer> answers,
Persistence.FormSection section,
Expand All @@ -71,7 +92,7 @@ private async Task UpdateOrAddAnswers(
ValidateQuestions(answers, questionDictionary);

var answerSet = sharedConsent.AnswerSets.FirstOrDefault(a => a.Guid == answerSetId)
?? await CreateAnswerSet(answerSetId, sharedConsent, section, furtherQuestionsExempted);
?? CreateAnswerSet(answerSetId, sharedConsent, section, furtherQuestionsExempted);

answerSet.Answers = MapAnswers(answers, questionDictionary, answerSet.Answers);
}
Expand Down Expand Up @@ -170,30 +191,12 @@ private Persistence.SharedConsent CreateSharedConsent(Organisation organisation,
};
}

private async Task<Persistence.FormAnswerSet> CreateAnswerSet(
private static Persistence.FormAnswerSet CreateAnswerSet(
Guid answerSetId,
Persistence.SharedConsent sharedConsent,
Persistence.FormSection section,
bool furtherQuestionsExempted)
{
var currentAnswers = await formRepository.GetFormAnswerSetsFromCurrentSharedConsentAsync(section.Guid, sharedConsent.Organisation.Guid);

if (currentAnswers != null)
{
var existingFurtherQuestionsExemptedAnswer = currentAnswers.FirstOrDefault(x => x.Deleted == false && x.FurtherQuestionsExempted == true);

if (existingFurtherQuestionsExemptedAnswer != null)
{
if (furtherQuestionsExempted == true)
{
return existingFurtherQuestionsExemptedAnswer;
}
else
{
existingFurtherQuestionsExemptedAnswer.Deleted = true;
}
}
}
var answerSet = new Persistence.FormAnswerSet
{
Guid = answerSetId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ public static FormAnswerSet GivenAnswerSet(
FormSection? section = null,
List<FormAnswer>? answers = null,
Guid? answerSetId = null,
bool deleted = false
bool deleted = false,
bool furtherQuestionsExempted = false
)
{
var theSection = section ?? GivenFormSection();
Expand All @@ -130,7 +131,7 @@ public static FormAnswerSet GivenAnswerSet(
Section = theSection,
Answers = answers ?? [],
Deleted = deleted,
FurtherQuestionsExempted = false
FurtherQuestionsExempted = furtherQuestionsExempted
};
sharedConsent.AnswerSets.Add(existingAnswerSet);
answers?.ForEach(a => a.FormAnswerSet = existingAnswerSet);
Expand Down

0 comments on commit 97ddc6e

Please sign in to comment.