From 2ceb161ebb336866c248798c69511fb20ac6c119 Mon Sep 17 00:00:00 2001 From: Patrick <135162612+plavoie-BC@users.noreply.github.com> Date: Fri, 6 Mar 2026 14:52:55 -0800 Subject: [PATCH 1/2] AB#31165 - Refactor CHES email object construction and field handling --- .../EmailNotificationManager.cs | 38 +++++++++++++++---- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs b/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs index 65eacb335..f4c6f7555 100644 --- a/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs +++ b/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs @@ -117,7 +117,8 @@ public async Task SendEmailAsync(string emailTo, string bod } // Send the email using the CHES client service - var emailObject = await GetEmailObjectAsync(emailTo, body, subject, emailFrom, emailBodyType, emailTemplateName, emailCC, emailBCC); + var emailObject = await GetEmailObjectAsync(emailTo, body, subject, emailFrom, emailBodyType, emailTemplateName, emailCC, emailBCC, true); + var response = await chesClientService.SendAsync(emailObject); // Assuming SendAsync returns a HttpResponseMessage or equivalent: @@ -222,7 +223,8 @@ public async Task BuildEmailObjectWithAttachmentsAsync(EmailLog emailLo emailLog.BodyType, emailLog.TemplateName, emailLog.CC, - emailLog.BCC); + emailLog.BCC, + true); // Retrieve attachments from S3 var attachments = await emailAttachmentService.GetAttachmentsAsync(emailLog.Id); @@ -261,7 +263,8 @@ protected virtual async Task GetEmailObjectAsync( string? emailBodyType, string? emailTemplateName, string? emailCC = null, - string? emailBCC = null) + string? emailBCC = null, + bool excludeTemplate = false) { var toList = emailTo.ParseEmailList() ?? []; var ccList = emailCC.ParseEmailList(); @@ -274,28 +277,47 @@ protected virtual async Task GetEmailObjectAsync( emailObjectDictionary["body"] = body; emailObjectDictionary["bodyType"] = emailBodyType ?? "text"; - emailObjectDictionary["cc"] = ccList; - emailObjectDictionary["bcc"] = bccList; emailObjectDictionary["encoding"] = "utf-8"; emailObjectDictionary["from"] = emailFrom ?? defaultFromAddress ?? "NoReply@gov.bc.ca"; emailObjectDictionary["priority"] = "normal"; emailObjectDictionary["subject"] = subject; emailObjectDictionary["tag"] = "tag"; emailObjectDictionary["to"] = toList; - emailObjectDictionary["templateName"] = emailTemplateName; + + // Only include cc/bcc when provided CHES API expects arrays, not null. + if (ccList != null) + { + emailObjectDictionary["cc"] = ccList; + } + if (bccList != null) + { + emailObjectDictionary["bcc"] = bccList; + } + + // templateName is not part of the CHES MessageObject schema + // store it on the EmailLog but don't send it to the API. + if (!excludeTemplate) + { + emailObjectDictionary["templateName"] = emailTemplateName; + } return emailObject; } protected virtual EmailLog UpdateMappedEmailLog(EmailLog emailLog, dynamic emailDynamicObject) { + var dict = (IDictionary)emailDynamicObject; emailLog.Body = emailDynamicObject.body; emailLog.Subject = emailDynamicObject.subject; emailLog.BodyType = emailDynamicObject.bodyType; emailLog.FromAddress = emailDynamicObject.from; emailLog.ToAddress = string.Join(",", emailDynamicObject.to); - emailLog.CC = emailDynamicObject.cc != null ? string.Join(",", (IEnumerable)emailDynamicObject.cc) : string.Empty; - emailLog.BCC = emailDynamicObject.bcc != null ? string.Join(",", (IEnumerable)emailDynamicObject.bcc) : string.Empty; + emailLog.CC = dict.TryGetValue("cc", out var cc) && cc is IEnumerable ccList + ? string.Join(",", ccList) + : string.Empty; + emailLog.BCC = dict.TryGetValue("bcc", out var bcc) && bcc is IEnumerable bccList + ? string.Join(",", bccList) + : string.Empty; emailLog.TemplateName = emailDynamicObject.templateName; return emailLog; } From 3edb94d05d08f996aad8b7ec003f3673248392f7 Mon Sep 17 00:00:00 2001 From: Patrick <135162612+plavoie-BC@users.noreply.github.com> Date: Fri, 6 Mar 2026 15:01:07 -0800 Subject: [PATCH 2/2] AB#31165 - Clarify excludeTemplate naming --- .../EmailNotificaions/EmailNotificationManager.cs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs b/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs index f4c6f7555..eb031554d 100644 --- a/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs +++ b/applications/Unity.GrantManager/modules/Unity.Notifications/src/Unity.Notifications.Application/EmailNotificaions/EmailNotificationManager.cs @@ -117,7 +117,8 @@ public async Task SendEmailAsync(string emailTo, string bod } // Send the email using the CHES client service - var emailObject = await GetEmailObjectAsync(emailTo, body, subject, emailFrom, emailBodyType, emailTemplateName, emailCC, emailBCC, true); + var emailObject = await GetEmailObjectAsync( + emailTo, body, subject, emailFrom, emailBodyType, emailTemplateName, emailCC, emailBCC, excludeTemplate: true); var response = await chesClientService.SendAsync(emailObject); @@ -224,7 +225,7 @@ public async Task BuildEmailObjectWithAttachmentsAsync(EmailLog emailLo emailLog.TemplateName, emailLog.CC, emailLog.BCC, - true); + excludeTemplate: true); // Retrieve attachments from S3 var attachments = await emailAttachmentService.GetAttachmentsAsync(emailLog.Id);