Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(submit-email): allow for file uploads to be attached to email #886

Merged
merged 15 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/Builders/EmailBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ private static MimeMessage BuildMessage(EmailMessage emailMessage)
}
};

if(emailMessage.FileUploads is not null)
{
foreach(var file in emailMessage.FileUploads)
{
var attachment = new MimePart("application", file.TrustedOriginalFileName.Split('.').Last().ToLower())
{
Content = new MimeContent(new MemoryStream(Convert.FromBase64String(file.Content.Replace("\"", "")))),
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
FileName = file.TrustedOriginalFileName,
IsAttachment = true
};

multipart.Add(attachment);
}
}

if (emailMessage.Attachment is not null)
{
var attachment = new MimePart("application", "pdf")
Expand Down
28 changes: 25 additions & 3 deletions src/Models/EmailMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ public class EmailMessage

public byte[] Attachment { get; }

public string AttachmentName { get; }
public List<StockportGovUK.NetStandard.Gateways.Models.FileManagement.File> FileUploads { get; set; }

public string AttachmentName { get; set; }

public EmailMessage() { }

Expand All @@ -33,14 +35,34 @@ public EmailMessage(string subject, string body, string fromEmail, string toEmai
ToEmail = toEmail;
}

public EmailMessage(string subject, string body, string fromEmail, string toEmail, byte[] attachment, string attachmntName)
public EmailMessage(string subject, string body, string fromEmail, string toEmail, List<StockportGovUK.NetStandard.Gateways.Models.FileManagement.File> fileUploads)
{
Subject = subject;
Body = body;
FromEmail = fromEmail;
ToEmail = toEmail;
FileUploads = fileUploads;
}

public EmailMessage(string subject, string body, string fromEmail, string toEmail, byte[] attachment, string attachmentName)
{
Subject = subject;
Body = body;
FromEmail = fromEmail;
ToEmail = toEmail;
Attachment = attachment;
AttachmentName = attachmentName;
}

public EmailMessage(string subject, string body, string fromEmail, string toEmail, byte[] attachment, string attachmentName, List<StockportGovUK.NetStandard.Gateways.Models.FileManagement.File> fileUploads)
{
Subject = subject;
Body = body;
FromEmail = fromEmail;
ToEmail = toEmail;
Attachment = attachment;
AttachmentName = attachmntName;
AttachmentName = attachmentName;
FileUploads = fileUploads;
}
}
}
100 changes: 74 additions & 26 deletions src/Services/EmailSubmitService/EmailSubmitService.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
using System.Text.RegularExpressions;
using form_builder.Configuration;
using form_builder.Constants;
using form_builder.Enum;
using form_builder.Helpers.EmailHelpers;
using form_builder.Helpers.PageHelpers;
using form_builder.Helpers.Session;
using form_builder.Mappers;
using form_builder.Models;
using form_builder.Models.Elements;
using form_builder.Providers.EmailProvider;
using form_builder.Providers.ReferenceNumbers;
using form_builder.Services.DocumentService;
Expand All @@ -14,11 +17,14 @@
using form_builder.TagParsers;
using Newtonsoft.Json;

using File = StockportGovUK.NetStandard.Gateways.Models.FileManagement.File;

namespace form_builder.Services.EmailSubmitService
{
public class EmailSubmitService : IEmailSubmitService
{
private readonly IMappingService _mappingService;
private readonly IElementMapper _elementMapper;
private readonly IEmailHelper _emailHelper;
private readonly ISessionHelper _sessionHelper;
private readonly IEmailProvider _emailProvider;
Expand All @@ -31,6 +37,7 @@ public class EmailSubmitService : IEmailSubmitService
public EmailSubmitService(
IMappingService mappingService,
IEmailHelper emailHelper,
IElementMapper elementMapper,
ISessionHelper sessionHelper,
IPageHelper pageHelper,
IEmailProvider emailProvider,
Expand All @@ -42,18 +49,21 @@ ILogger<EmailSubmitService> logger
{
_mappingService = mappingService;
_emailHelper = emailHelper;
_elementMapper = elementMapper;
_pageHelper = pageHelper;
_sessionHelper = sessionHelper;
_emailProvider = emailProvider;
_referenceNumberProvider = referenceNumberProvider;
_documentSummaryService = documentSummaryService;
_tagParsers = tagParsers;
_logger= logger;
_logger = logger;
}

public async Task<string> EmailSubmission(MappingEntity data, string form, string sessionGuid)
{
var reference = string.Empty;
List<File> files = new();
List<File> fileUploads = new();

if (data.BaseForm.GenerateReferenceNumber)
{
Expand All @@ -63,12 +73,12 @@ public async Task<string> EmailSubmission(MappingEntity data, string form, strin
}

var doc = _documentSummaryService.GenerateDocument(
new DocumentSummaryEntity
{
DocumentType = EDocumentType.Html,
PreviousAnswers = data.FormAnswers,
FormSchema = data.BaseForm
});
new DocumentSummaryEntity
{
DocumentType = EDocumentType.Html,
PreviousAnswers = data.FormAnswers,
FormSchema = data.BaseForm
});

var email = await _emailHelper.GetEmailInformation(form);
var parsedSubjectInformation = new EmailConfiguration();
Expand All @@ -94,29 +104,67 @@ public async Task<string> EmailSubmission(MappingEntity data, string form, strin
: $"<p>{email.Body}</p>";

var emailMessage = new EmailMessage(
subject,
body,
email.Sender,
string.Join(",", email.Recipient)
);
subject,
body,
email.Sender,
string.Join(",", email.Recipient)
);

bool isFileUpload = data.FormAnswers.AllAnswers
.Any(_ => _.QuestionId.Contains(FileUploadConstants.SUFFIX));

if (isFileUpload)
{
var fileElements = data.BaseForm.Pages
.SelectMany(_ => _.Elements)
.Where(_ => _.Type.Equals(EElementType.FileUpload) || _.Type.Equals(EElementType.MultipleFileUpload));

foreach (var element in fileElements)
{
files = (List<File>)_elementMapper.GetAnswerValue(element, data.FormAnswers).Result;
foreach (File file in files ?? new List<File>())
fileUploads.Add(file);
}

if (fileUploads.Any())
{
emailMessage = new EmailMessage(
subject,
body,
email.Sender,
string.Join(",", email.Recipient),
fileUploads
);
}
}

if (email.AttachPdf)
{
var pdfdoc = _documentSummaryService.GenerateDocument(
new DocumentSummaryEntity
{
DocumentType = EDocumentType.Pdf,
PreviousAnswers = data.FormAnswers,
FormSchema = data.BaseForm
});

emailMessage = new EmailMessage(
subject,
body,
email.Sender,
string.Join(",", email.Recipient),
pdfdoc.Result,
$"{data.FormAnswers.CaseReference}_data.pdf"
new DocumentSummaryEntity
{
DocumentType = EDocumentType.Pdf,
PreviousAnswers = data.FormAnswers,
FormSchema = data.BaseForm
});

emailMessage = fileUploads.Any() ?
emailMessage = new EmailMessage(
subject,
body,
email.Sender,
string.Join(",", email.Recipient),
pdfdoc.Result,
$"{data.FormAnswers.CaseReference}_data.pdf",
fileUploads
)
: emailMessage = new EmailMessage(
subject,
body,
email.Sender,
string.Join(",", email.Recipient),
pdfdoc.Result,
$"{data.FormAnswers.CaseReference}_data.pdf"
);
}

Expand Down
83 changes: 61 additions & 22 deletions tests/unit-tests/UnitTests/Services/EmailSubmitServiceTests.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
using Amazon.Runtime.Internal.Util;
using form_builder.Builders;
using form_builder.Builders;
using form_builder.Configuration;
using form_builder.Constants;
using form_builder.Enum;
using form_builder.Helpers.EmailHelpers;
using form_builder.Helpers.PageHelpers;
using form_builder.Helpers.Session;
using form_builder.Mappers;
using form_builder.Models;
using form_builder.Models.Elements;
using form_builder.Providers.EmailProvider;
using form_builder.Providers.ReferenceNumbers;
using form_builder.Services.DocumentService;
Expand All @@ -26,6 +28,7 @@ public class EmailSubmitServiceTests
{
private readonly Mock<IMappingService> _mappingService = new();
private readonly Mock<IEmailHelper> _mockEmailHelper = new();
private readonly Mock<IElementMapper> _mockElementMapper = new();
private readonly Mock<ISessionHelper> _sessionHelper = new();
private readonly Mock<IEmailProvider> _emailProvider = new();
private readonly Mock<IDocumentSummaryService> _documentSummaryService = new();
Expand All @@ -42,7 +45,6 @@ public EmailSubmitServiceTests()
.Setup(_ => _.ParseString(It.IsAny<string>(), It.IsAny<FormAnswers>()))
.Returns("{'AttachPdf':true,'Body':null,'FormName':['jw-roast-preference'],'Recipient':['jonathon.warwick@stockport.gov.uk'],'Sender':'noreply@stockport.gov.uk','Subject':'JW Roast Preference: {{QUESTION:firrstName}} {{QUESTION:favouriteyFood}}'}");


var tagParserItems = new List<ITagParser> { _tagParser.Object };

_mockTagParsers
Expand All @@ -65,10 +67,23 @@ public EmailSubmitServiceTests()
.Setup(_ => _.GetReference(It.IsAny<string>(), 8))
.Returns("12345678");

_emailProvider
.Setup(_ => _.SendEmail(It.IsAny<EmailMessage>()))
.ReturnsAsync(HttpStatusCode.OK);

_mockEmailHelper
.Setup(_ => _.GetEmailInformation(It.IsAny<string>()))
.ReturnsAsync(new EmailConfiguration
{
FormName = new List<string> { "form" },
Subject = "test",
Recipient = new List<string> { "google" }
});

_emailSubmitService = new EmailSubmitService(
_mappingService.Object,
_mockEmailHelper.Object,
_mockElementMapper.Object,
_sessionHelper.Object,
_pageHelper.Object,
_emailProvider.Object,
Expand All @@ -82,24 +97,6 @@ public EmailSubmitServiceTests()
[Fact]
public async Task Submit_ShouldCallMapping_And_SubmitService()
{
// Arrange
_emailProvider
.Setup(_ => _.SendEmail(It.IsAny<EmailMessage>()))
.ReturnsAsync(System.Net.HttpStatusCode.OK);

_mockEmailHelper
.Setup(_ => _.GetEmailInformation(It.IsAny<string>()))
.ReturnsAsync(new EmailConfiguration
{
FormName = new List<string> { "form" },
Subject = "test",
Recipient = new List<string> { "google" }
});

_referenceNumberProvider
.Setup(_ => _.GetReference(It.IsAny<string>(), 8))
.Returns("12345678");

// Arrange
var element = new ElementBuilder()
.WithType(EElementType.H1)
Expand Down Expand Up @@ -159,9 +156,51 @@ public async Task Submit_ShouldSubmitAndEmail_ThrowExceptionAndRemoveVariable()
.Throws(new Exception());

// Act & Assert
await _emailSubmitService.EmailSubmission(new MappingEntity { BaseForm = new FormSchema(), FormAnswers=new FormAnswers() }, "form", "sessionGuid");
await _emailSubmitService.EmailSubmission(new MappingEntity { BaseForm = new FormSchema(), FormAnswers = new FormAnswers() }, "form", "sessionGuid");
Assert.Equal(expectedSubject, actualSubject);
}

[Fact]
public async Task Submit_ShouldCallElementMapper_ToGetListOf_FileUploads()
{
// Arrange
var emailMessage = new EmailMessage("subject", "body", "email@stockport.gov.uk", "email@stockport.gov.uk");

var fileUploadElement = new ElementBuilder()
.WithLabel("File upload")
.WithQuestionId("questionIDOne")
.WithType(EElementType.FileUpload)
.Build();

var data = new MappingEntity
{
FormAnswers = new FormAnswers
{
Path = "page-one",
FormName = "form",
Pages = new List<PageAnswers>
{
new()
{
Answers = new List<Answers>
{
new() { QuestionId = $"questionIDOne{FileUploadConstants.SUFFIX}", Response = new List<FileUploadModel>() },
new() { QuestionId = $"questionIDTwo{FileUploadConstants.SUFFIX}", Response = new List<FileUploadModel>() }
}
}
}
},
BaseForm = new FormSchema
{
Pages = new List<Page>
{
new() { Elements = new List<IElement> { fileUploadElement } }
}
}
};

await _emailSubmitService.EmailSubmission(data, "form", "sessionGuid");
_mockElementMapper.Verify(_ => _.GetAnswerValue(It.IsAny<IElement>(), It.IsAny<FormAnswers>()), Times.Once);
}
}
}
Loading