Skip to content

Commit

Permalink
Merge pull request #719 from cabinetoffice/feature/dp-447-pdf-share-m…
Browse files Browse the repository at this point in the history
…y-info

BE - Exclusions - Share my information - PDF - Exclusions
  • Loading branch information
dbgoaco authored Oct 8, 2024
2 parents 89fe468 + e9339ad commit d05346c
Show file tree
Hide file tree
Showing 9 changed files with 146 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.DataSharing.WebApi.Tests;
using CO.CDP.OrganisationInformation.Persistence;
using FluentAssertions;
using Moq;
Expand Down Expand Up @@ -75,4 +76,23 @@ public async Task ShouldMapOrganisationToBasicInformation()
result.BasicInformation.EmailAddress.Should().Be(organisation.ContactPoints.FirstOrDefault()?.Email);
result.BasicInformation.LegalForm.Should().NotBeNull();
}

[Fact]
public async Task ShouldMapFormAnswerSetsForPdf()
{
var org = CreateOrganisation();
var sharedConsent = EntityFactory.GetSharedConsent(org.Id, org.Guid, Guid.NewGuid());
sharedConsent.Organisation = org;

_shareCodeRepository.Setup(r => r.GetByShareCode("ABC-123")).ReturnsAsync(sharedConsent);

var result = await DataService.GetSharedSupplierInformationAsync("ABC-123");

result.FormAnswerSetForPdfs.Should().ContainSingle();

var exclusionsSection = result.FormAnswerSetForPdfs.First(fa => fa.SectionType == CO.CDP.OrganisationInformation.Persistence.Forms.FormSectionType.Exclusions);
exclusionsSection.QuestionAnswers.Should().Contain(qa => qa.Item1 == "Were your accounts audited?" && qa.Item2 == "yes");
exclusionsSection.QuestionAnswers.Should().Contain(qa => qa.Item1 == "What is the financial year end date for the information you uploaded?" && qa.Item2 == DateTime.Today.ToString("dd-MM-yyyy"));
exclusionsSection.QuestionAnswers.Should().Contain(qa => qa.Item1 == "Upload your accounts" && qa.Item2 == "No");
}
}
21 changes: 19 additions & 2 deletions Services/CO.CDP.DataSharing.WebApi.Tests/DataSharingFactory.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Amazon.CloudWatch;
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.OrganisationInformation;
using CO.CDP.OrganisationInformation.Persistence;
Expand Down Expand Up @@ -25,13 +26,13 @@ public static CO.CDP.OrganisationInformation.Persistence.Forms.SharedConsent Cre
Organisation = theOrganisation,
FormId = 1,
Form = null!,
AnswerSets = null!,
AnswerSets = [],
SubmissionState = default,
SubmittedAt = null,
FormVersionId = string.Empty,
ShareCode = shareCode ?? "valid-sharecode",
CreatedOn = DateTimeOffset.UtcNow,
UpdatedOn = DateTimeOffset.UtcNow
UpdatedOn = DateTimeOffset.UtcNow,
};
}

Expand Down Expand Up @@ -150,6 +151,22 @@ public static BasicInformation CreateMockBasicInformation()
}
};
}

public static List<FormAnswerSetForPdf> CreateMockFormAnswerSetForPdfs()
{
return
[
new FormAnswerSetForPdf() {
QuestionAnswers = [
new Tuple<string, string>("Did this exclusion happen in the UK? ", "Yes"),
new Tuple<string, string>("Enter an email address ", "[email protected]")
],
SectionName = "Exclusions",
SectionType = OrganisationInformation.Persistence.Forms.FormSectionType.Exclusions
}
];
}

public static List<ConnectedPersonInformation> CreateMockConnectedPersonInformation()
{
return
Expand Down
4 changes: 2 additions & 2 deletions Services/CO.CDP.DataSharing.WebApi.Tests/EntityFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ private static PersistenceForms.FormSection GivenSection(Guid sectionId, Persist
Form = form,
Questions = new List<PersistenceForms.FormQuestion>(),
Title = "Test Section",
Type = PersistenceForms.FormSectionType.Standard,
Type = PersistenceForms.FormSectionType.Declaration,
AllowsMultipleAnswerSets = true,
CheckFurtherQuestionsExempted = false,
DisplayOrder = 1,
Expand Down Expand Up @@ -406,7 +406,7 @@ private static PersistenceForms.FormSection GivenSection(Guid sectionId, Persist
{
Guid = Guid.NewGuid(),
Title = string.Empty,
Type = PersistenceForms.FormSectionType.Standard,
Type = PersistenceForms.FormSectionType.Exclusions,
FormId = form.Id,
Form = form,
Questions = new List<PersistenceForms.FormQuestion>(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public void GenerateBasicInformationPdf_ShouldGeneratePdfWithAllInformation()
OrganisationId = Guid.NewGuid(),
BasicInformation = DataSharingFactory.CreateMockBasicInformation(),
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation(),
FormAnswerSetForPdfs = DataSharingFactory.CreateMockFormAnswerSetForPdfs()
};

var pdfBytes = _pdfGenerator.GenerateBasicInformationPdf(supplierInformation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ public async Task Execute_ShouldReturnPdfBytes_WhenShareCodeExists()
{
OrganisationId = organisationId,
BasicInformation = DataSharingFactory.CreateMockBasicInformation(),
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation()
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation(),
FormAnswerSetForPdfs = DataSharingFactory.CreateMockFormAnswerSetForPdfs()
};

var pdfBytes = new byte[] { 1, 2, 3 };
Expand Down Expand Up @@ -56,7 +57,8 @@ public async Task Execute_ShouldCallDataServiceAndPdfGenerator_WhenShareCodeExis
{
OrganisationId = organisationId,
BasicInformation = DataSharingFactory.CreateMockBasicInformation(),
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation()
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation(),
FormAnswerSetForPdfs = DataSharingFactory.CreateMockFormAnswerSetForPdfs()
};

var pdfBytes = new byte[] { 1, 2, 3 };
Expand Down Expand Up @@ -86,7 +88,8 @@ public async Task Execute_WhenDoesNotHaveAccessToOrganisation_ThrowsUserUnauthor
{
OrganisationId = organisationId,
BasicInformation = DataSharingFactory.CreateMockBasicInformation(),
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation()
ConnectedPersonInformation = DataSharingFactory.CreateMockConnectedPersonInformation(),
FormAnswerSetForPdfs = DataSharingFactory.CreateMockFormAnswerSetForPdfs()
});

_claimService.Setup(cs => cs.HaveAccessToOrganisation(organisationId, invalidscope))
Expand Down
68 changes: 64 additions & 4 deletions Services/CO.CDP.DataSharing.WebApi/DataService/DataService.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using CO.CDP.DataSharing.WebApi.Model;
using CO.CDP.OrganisationInformation.Persistence;
using CO.CDP.OrganisationInformation;
using CO.CDP.OrganisationInformation.Persistence;
using static CO.CDP.OrganisationInformation.Persistence.ConnectedEntity;
using Address = CO.CDP.OrganisationInformation.Address;
using ConnectedIndividualTrust = CO.CDP.DataSharing.WebApi.Model.ConnectedIndividualTrust;
Expand All @@ -14,17 +14,77 @@ public async Task<SharedSupplierInformation> GetSharedSupplierInformationAsync(s
{
var sharedConsent = await shareCodeRepository.GetByShareCode(shareCode)
?? throw new ShareCodeNotFoundException(Constants.ShareCodeNotFoundExceptionMessage);

var allFormSectionsExceptDeclaractions = sharedConsent.AnswerSets.Where(a =>
a.Section.Type != OrganisationInformation.Persistence.Forms.FormSectionType.Declaration);
var connectedEntities = await connectedEntityRepository.FindByOrganisation(sharedConsent.Organisation.Guid);

return new SharedSupplierInformation
{
OrganisationId = sharedConsent.Organisation.Guid,
BasicInformation = MapToBasicInformation(sharedConsent.Organisation),
ConnectedPersonInformation = MapToConnectedPersonInformation(connectedEntities)
ConnectedPersonInformation = MapToConnectedPersonInformation(connectedEntities),
FormAnswerSetForPdfs = MapFormAnswerSetsForPdf(allFormSectionsExceptDeclaractions)
};
}

public static IEnumerable<FormAnswerSetForPdf> MapFormAnswerSetsForPdf(
IEnumerable<OrganisationInformation.Persistence.Forms.FormAnswerSet> answerSets)
{
var pdfAnswerSets = new List<FormAnswerSetForPdf>();

foreach (var answerSet in answerSets)
{
var pdfAnswerSet = new FormAnswerSetForPdf() {
SectionName = answerSet.Section.Title,
SectionType = answerSet.Section.Type,
QuestionAnswers = []
};

pdfAnswerSets.Add(pdfAnswerSet);

foreach (var answer in answerSet.Answers)
{
switch (answer.Question.Type)
{
case OrganisationInformation.Persistence.Forms.FormQuestionType.YesOrNo:
{
pdfAnswerSet.QuestionAnswers.Add(new Tuple<string, string>($"{answer.Question.Title}",
answer.OptionValue ?? "No"));
break;
}
case OrganisationInformation.Persistence.Forms.FormQuestionType.Date:
{
pdfAnswerSet.QuestionAnswers.Add(new Tuple<string, string>($"{answer.Question.Title}",
answer.DateValue?.ToString("dd-MM-yyyy") ?? "Not specified"));
break;
}
case OrganisationInformation.Persistence.Forms.FormQuestionType.Url:
{
pdfAnswerSet.QuestionAnswers.Add(new Tuple<string, string>($"{answer.Question.Title}",
answer.TextValue ?? ""));
break;
}
case OrganisationInformation.Persistence.Forms.FormQuestionType.FileUpload:
{
pdfAnswerSet.QuestionAnswers.Add(new Tuple<string, string>($"{answer.Question.Title}",
answer.TextValue ?? "No"));
break;
}
case OrganisationInformation.Persistence.Forms.FormQuestionType.Text:
case OrganisationInformation.Persistence.Forms.FormQuestionType.MultiLine:
{
pdfAnswerSet.QuestionAnswers.Add(new Tuple<string, string>($"{answer.Question.Title}:",
answer.TextValue ?? ""));
break;
}
}

}
}

return pdfAnswerSets;
}

public static BasicInformation MapToBasicInformation(Organisation organisation)
{
var supplierInfo = organisation.SupplierInfo
Expand Down
10 changes: 10 additions & 0 deletions Services/CO.CDP.DataSharing.WebApi/Model/FormAnswerSetForPdf.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using CO.CDP.OrganisationInformation.Persistence.Forms;

namespace CO.CDP.DataSharing.WebApi.Model;

public record FormAnswerSetForPdf
{
public required string SectionName { get; init; }
public required FormSectionType SectionType { get; init; }
public required List<Tuple<string, string>> QuestionAnswers { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ public record SharedSupplierInformation
public required Guid OrganisationId { get; init; }
public required BasicInformation BasicInformation { get; init; }
public required List<ConnectedPersonInformation> ConnectedPersonInformation { get; init; }
public required IEnumerable<FormAnswerSetForPdf> FormAnswerSetForPdfs { get; init; }
}
23 changes: 23 additions & 0 deletions Services/CO.CDP.DataSharing.WebApi/PdfGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public byte[] GenerateBasicInformationPdf(SharedSupplierInformation supplierInfo
{
AddBasicInformationSection(col, basicInformation);
AddConnectedPersonInformationSection(col, connectedPersonInformation);
AddFormSections(col, supplierInformation.FormAnswerSetForPdfs);
});
page.Footer().AlignRight().Text(x =>
Expand All @@ -46,6 +47,28 @@ public byte[] GenerateBasicInformationPdf(SharedSupplierInformation supplierInfo
}
}

private void AddFormSections(ColumnDescriptor col, IEnumerable<FormAnswerSetForPdf> formAnswerSet)
{
foreach (var answerSet in formAnswerSet)
{
col.Item().Text(answerSet.SectionName).Bold().FontSize(14);
col.Item().PaddingBottom(10);

if (answerSet.QuestionAnswers != null)
{
foreach (var qa in answerSet.QuestionAnswers)
{
col.Item().Element(container => AddTwoColumnRow(container, qa.Item1, qa.Item2));
}

col.Item().PaddingBottom(10);
}

col.Item().LineHorizontal(1);
col.Item().PaddingBottom(10);
}
}

private void AddBasicInformationSection(ColumnDescriptor col, BasicInformation basicInformation)
{
col.Item().Text("Basic Information").Bold().FontSize(14);
Expand Down

0 comments on commit d05346c

Please sign in to comment.