From 6c3fa21cc560a6dcb309958ee5a76248d5383413 Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Thu, 12 Sep 2024 09:43:52 -0600 Subject: [PATCH 1/2] Added environment variable to configure default reminder email recipient. Fixed bug with due date in email body and improved logging and error catching when sending emails. --- Controllers/APIController.cs | 15 ++++++++++++--- Helper/ConfigHelper.cs | 10 ++++++++++ Helper/MailHelper.cs | 12 +++++++++--- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index e4e9a86..c529cab 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -666,6 +666,7 @@ namespace CarCareTracker.Controllers { var vehicles = _dataAccess.GetVehicles(); List operationResponses = new List(); + var defaultEmailAddress = _config.GetDefaultReminderEmail(); foreach(Vehicle vehicle in vehicles) { var vehicleId = vehicle.Id; @@ -681,6 +682,10 @@ namespace CarCareTracker.Controllers //get list of recipients. var userIds = _userAccessDataAccess.GetUserAccessByVehicleId(vehicleId).Select(x => x.Id.UserId); List emailRecipients = new List(); + if (!string.IsNullOrWhiteSpace(defaultEmailAddress)) + { + emailRecipients.Add(defaultEmailAddress); + } foreach (int userId in userIds) { var userData = _userRecordDataAccess.GetUserRecordById(userId); @@ -693,15 +698,19 @@ namespace CarCareTracker.Controllers var result = _mailHelper.NotifyUserForReminders(vehicle, emailRecipients, results); operationResponses.Add(result); } - if (operationResponses.All(x => x.Success)) + if (!operationResponses.Any()) { - return Json(new OperationResponse { Success = true, Message = "Emails sent" }); + return Json(new OperationResponse { Success = false, Message = "No Emails sent either because there are no vehicles or no recipients" }); + } + else if (operationResponses.All(x => x.Success)) + { + return Json(new OperationResponse { Success = true, Message = $"{operationResponses.Count()} Emails sent" }); } else if (operationResponses.All(x => !x.Success)) { return Json(new OperationResponse { Success = false, Message = "All emails failed, check SMTP settings" }); } else { - return Json(new OperationResponse { Success = true, Message = "Some emails sent, some failed, check recipient settings" }); + return Json(new OperationResponse { Success = true, Message = $"{operationResponses.Count(x=>x.Success)} Emails sent, {operationResponses.Count(x => !x.Success)} failed, check recipient settings" }); } } [Authorize(Roles = nameof(UserData.IsRootUser))] diff --git a/Helper/ConfigHelper.cs b/Helper/ConfigHelper.cs index b6c1844..1a434ab 100644 --- a/Helper/ConfigHelper.cs +++ b/Helper/ConfigHelper.cs @@ -14,6 +14,7 @@ namespace CarCareTracker.Helper bool AuthenticateRootUser(string username, string password); string GetWebHookUrl(); string GetMOTD(); + string GetDefaultReminderEmail(); string GetLogoUrl(); string GetServerLanguage(); bool GetServerEnableShopSupplies(); @@ -52,6 +53,15 @@ namespace CarCareTracker.Helper } return motd; } + public string GetDefaultReminderEmail() + { + var defaultEmail = _config["DEFAULT_REMINDER_EMAIL"]; + if (string.IsNullOrWhiteSpace(defaultEmail)) + { + defaultEmail = ""; + } + return defaultEmail; + } public OpenIDConfig GetOpenIDConfig() { OpenIDConfig openIdConfig = _config.GetSection("OpenIDConfig").Get() ?? new OpenIDConfig(); diff --git a/Helper/MailHelper.cs b/Helper/MailHelper.cs index f1dc6fe..acab09d 100644 --- a/Helper/MailHelper.cs +++ b/Helper/MailHelper.cs @@ -113,14 +113,20 @@ namespace CarCareTracker.Helper string tableBody = ""; foreach(ReminderRecordViewModel reminder in reminders) { - var dueOn = reminder.Metric == ReminderMetric.Both ? $"{reminder.Date} or {reminder.Mileage}" : reminder.Metric == ReminderMetric.Date ? $"{reminder.Date.ToShortDateString()}" : $"{reminder.Mileage}"; + var dueOn = reminder.Metric == ReminderMetric.Both ? $"{reminder.Date.ToShortDateString()} or {reminder.Mileage}" : reminder.Metric == ReminderMetric.Date ? $"{reminder.Date.ToShortDateString()}" : $"{reminder.Mileage}"; tableBody += $"{StaticHelper.GetTitleCaseReminderUrgency(reminder.Urgency)}{reminder.Description}{dueOn}"; } emailBody = emailBody.Replace("{TableBody}", tableBody); try { - SendEmail(emailAddresses, emailSubject, emailBody); - return new OperationResponse { Success = true, Message = "Email Sent!" }; + var result = SendEmail(emailAddresses, emailSubject, emailBody); + if (result) + { + return new OperationResponse { Success = true, Message = "Email Sent!" }; + } else + { + return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage }; + } } catch (Exception ex) { return new OperationResponse { Success = false, Message = ex.Message }; From 2db01aa4adfdd7c6416b5f75e524453695df1f2d Mon Sep 17 00:00:00 2001 From: "DESKTOP-T0O5CDB\\DESK-555BD" Date: Thu, 12 Sep 2024 10:00:39 -0600 Subject: [PATCH 2/2] reworded response message --- Controllers/APIController.cs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index c529cab..5619f19 100644 --- a/Controllers/APIController.cs +++ b/Controllers/APIController.cs @@ -700,17 +700,17 @@ namespace CarCareTracker.Controllers } if (!operationResponses.Any()) { - return Json(new OperationResponse { Success = false, Message = "No Emails sent either because there are no vehicles or no recipients" }); + return Json(new OperationResponse { Success = false, Message = "No Emails Sent, No Vehicles Available or No Recipients Configured" }); } else if (operationResponses.All(x => x.Success)) { - return Json(new OperationResponse { Success = true, Message = $"{operationResponses.Count()} Emails sent" }); + return Json(new OperationResponse { Success = true, Message = $"Emails Sent({operationResponses.Count()})" }); } else if (operationResponses.All(x => !x.Success)) { - return Json(new OperationResponse { Success = false, Message = "All emails failed, check SMTP settings" }); + return Json(new OperationResponse { Success = false, Message = $"All Emails Failed({operationResponses.Count()}), Check SMTP Settings" }); } else { - return Json(new OperationResponse { Success = true, Message = $"{operationResponses.Count(x=>x.Success)} Emails sent, {operationResponses.Count(x => !x.Success)} failed, check recipient settings" }); + return Json(new OperationResponse { Success = true, Message = $"Emails Sent({operationResponses.Count(x => x.Success)}), Emails Failed({operationResponses.Count(x => !x.Success)}), Check Recipient Settings" }); } } [Authorize(Roles = nameof(UserData.IsRootUser))]