diff --git a/Controllers/APIController.cs b/Controllers/APIController.cs index e4e9a86..5619f19 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, No Vehicles Available or No Recipients Configured" }); + } + else if (operationResponses.All(x => x.Success)) + { + 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 = "Some emails sent, some 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))] 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 };