From d97637e8268040888a40dbe2ef50306f771d6310 Mon Sep 17 00:00:00 2001 From: Jonathon Warwick Date: Mon, 6 Nov 2023 14:40:59 +0000 Subject: [PATCH 1/3] Parsing form answers individually to avoid subject from being blank if one question doesnt match --- src/TagParsers/FormAnswerTagParser.cs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/TagParsers/FormAnswerTagParser.cs b/src/TagParsers/FormAnswerTagParser.cs index d1a21072f..771d69378 100644 --- a/src/TagParsers/FormAnswerTagParser.cs +++ b/src/TagParsers/FormAnswerTagParser.cs @@ -34,7 +34,24 @@ public async Task Parse(Page page, FormAnswers formAnswers) public string ParseString(string content, FormAnswers formAnswers) { var answersDictionary = formAnswers.Pages?.SelectMany(x => x.Answers).ToDictionary(x => x.QuestionId, x => x.Response); - var updatedContent = Parse(content, answersDictionary, Regex); + var updatedContent = content; + var matches = Regex.Matches(content); + + foreach (Match match in matches) + { + var questionId = $"{{{{{match.Value}}}}}"; + + try + { + var replacedContent = Parse(questionId, answersDictionary, Regex); + updatedContent = updatedContent.Replace(questionId, replacedContent); + } + catch (Exception) + { + updatedContent = updatedContent.Replace(questionId, string.Empty); + } + } + return updatedContent; } } From 59b6322afe515b9166809bcd933ffbfb7f488eab Mon Sep 17 00:00:00 2001 From: Jonathon Warwick Date: Mon, 6 Nov 2023 14:50:33 +0000 Subject: [PATCH 2/3] removed try catch from email submit service as it has been put into tag parser instead --- .../EmailSubmitService/EmailSubmitService.cs | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/src/Services/EmailSubmitService/EmailSubmitService.cs b/src/Services/EmailSubmitService/EmailSubmitService.cs index c37567cdc..74af68900 100644 --- a/src/Services/EmailSubmitService/EmailSubmitService.cs +++ b/src/Services/EmailSubmitService/EmailSubmitService.cs @@ -71,19 +71,11 @@ public async Task EmailSubmission(MappingEntity data, string form, strin }); var email = await _emailHelper.GetEmailInformation(form); - var parsedSubjectInformation = new EmailConfiguration(); - try - { - var subjectInformation = JsonConvert.SerializeObject(email); - subjectInformation = _tagParsers.Aggregate(subjectInformation, (current, tagParser) => tagParser.ParseString(current, data.FormAnswers)); - parsedSubjectInformation = JsonConvert.DeserializeObject(subjectInformation); - } - catch (Exception ex) - { - parsedSubjectInformation.Subject = Regex.Replace(email.Subject, "{{.+}}", ""); - _logger.LogInformation($"{nameof(EmailSubmitService)}::{nameof(EmailSubmission)}: '{email.Subject}' QuestionID contains a null value.", ex); - } + var subjectInformation = JsonConvert.SerializeObject(email); + subjectInformation = _tagParsers.Aggregate(subjectInformation, (current, tagParser) => tagParser.ParseString(current, data.FormAnswers)); + var parsedSubjectInformation = JsonConvert.DeserializeObject(subjectInformation); + var subject = !string.IsNullOrEmpty(parsedSubjectInformation.Subject) ? parsedSubjectInformation.Subject From 30e96d69d3228eae391edfd82e717df5d8638623 Mon Sep 17 00:00:00 2001 From: Jonathon Warwick Date: Mon, 6 Nov 2023 15:48:12 +0000 Subject: [PATCH 3/3] reverted removal of trycatch to fix test --- .../EmailSubmitService/EmailSubmitService.cs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/Services/EmailSubmitService/EmailSubmitService.cs b/src/Services/EmailSubmitService/EmailSubmitService.cs index 74af68900..c37567cdc 100644 --- a/src/Services/EmailSubmitService/EmailSubmitService.cs +++ b/src/Services/EmailSubmitService/EmailSubmitService.cs @@ -71,11 +71,19 @@ public async Task EmailSubmission(MappingEntity data, string form, strin }); var email = await _emailHelper.GetEmailInformation(form); + var parsedSubjectInformation = new EmailConfiguration(); - var subjectInformation = JsonConvert.SerializeObject(email); - subjectInformation = _tagParsers.Aggregate(subjectInformation, (current, tagParser) => tagParser.ParseString(current, data.FormAnswers)); - var parsedSubjectInformation = JsonConvert.DeserializeObject(subjectInformation); - + try + { + var subjectInformation = JsonConvert.SerializeObject(email); + subjectInformation = _tagParsers.Aggregate(subjectInformation, (current, tagParser) => tagParser.ParseString(current, data.FormAnswers)); + parsedSubjectInformation = JsonConvert.DeserializeObject(subjectInformation); + } + catch (Exception ex) + { + parsedSubjectInformation.Subject = Regex.Replace(email.Subject, "{{.+}}", ""); + _logger.LogInformation($"{nameof(EmailSubmitService)}::{nameof(EmailSubmission)}: '{email.Subject}' QuestionID contains a null value.", ex); + } var subject = !string.IsNullOrEmpty(parsedSubjectInformation.Subject) ? parsedSubjectInformation.Subject