Merge pull request #614 from hargata/Hargata/517
Add Default Reminder Email Recipient
This commit is contained in:
@@ -666,6 +666,7 @@ namespace CarCareTracker.Controllers
|
|||||||
{
|
{
|
||||||
var vehicles = _dataAccess.GetVehicles();
|
var vehicles = _dataAccess.GetVehicles();
|
||||||
List<OperationResponse> operationResponses = new List<OperationResponse>();
|
List<OperationResponse> operationResponses = new List<OperationResponse>();
|
||||||
|
var defaultEmailAddress = _config.GetDefaultReminderEmail();
|
||||||
foreach(Vehicle vehicle in vehicles)
|
foreach(Vehicle vehicle in vehicles)
|
||||||
{
|
{
|
||||||
var vehicleId = vehicle.Id;
|
var vehicleId = vehicle.Id;
|
||||||
@@ -681,6 +682,10 @@ namespace CarCareTracker.Controllers
|
|||||||
//get list of recipients.
|
//get list of recipients.
|
||||||
var userIds = _userAccessDataAccess.GetUserAccessByVehicleId(vehicleId).Select(x => x.Id.UserId);
|
var userIds = _userAccessDataAccess.GetUserAccessByVehicleId(vehicleId).Select(x => x.Id.UserId);
|
||||||
List<string> emailRecipients = new List<string>();
|
List<string> emailRecipients = new List<string>();
|
||||||
|
if (!string.IsNullOrWhiteSpace(defaultEmailAddress))
|
||||||
|
{
|
||||||
|
emailRecipients.Add(defaultEmailAddress);
|
||||||
|
}
|
||||||
foreach (int userId in userIds)
|
foreach (int userId in userIds)
|
||||||
{
|
{
|
||||||
var userData = _userRecordDataAccess.GetUserRecordById(userId);
|
var userData = _userRecordDataAccess.GetUserRecordById(userId);
|
||||||
@@ -693,15 +698,19 @@ namespace CarCareTracker.Controllers
|
|||||||
var result = _mailHelper.NotifyUserForReminders(vehicle, emailRecipients, results);
|
var result = _mailHelper.NotifyUserForReminders(vehicle, emailRecipients, results);
|
||||||
operationResponses.Add(result);
|
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))
|
} 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
|
} 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))]
|
[Authorize(Roles = nameof(UserData.IsRootUser))]
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ namespace CarCareTracker.Helper
|
|||||||
bool AuthenticateRootUser(string username, string password);
|
bool AuthenticateRootUser(string username, string password);
|
||||||
string GetWebHookUrl();
|
string GetWebHookUrl();
|
||||||
string GetMOTD();
|
string GetMOTD();
|
||||||
|
string GetDefaultReminderEmail();
|
||||||
string GetLogoUrl();
|
string GetLogoUrl();
|
||||||
string GetServerLanguage();
|
string GetServerLanguage();
|
||||||
bool GetServerEnableShopSupplies();
|
bool GetServerEnableShopSupplies();
|
||||||
@@ -52,6 +53,15 @@ namespace CarCareTracker.Helper
|
|||||||
}
|
}
|
||||||
return motd;
|
return motd;
|
||||||
}
|
}
|
||||||
|
public string GetDefaultReminderEmail()
|
||||||
|
{
|
||||||
|
var defaultEmail = _config["DEFAULT_REMINDER_EMAIL"];
|
||||||
|
if (string.IsNullOrWhiteSpace(defaultEmail))
|
||||||
|
{
|
||||||
|
defaultEmail = "";
|
||||||
|
}
|
||||||
|
return defaultEmail;
|
||||||
|
}
|
||||||
public OpenIDConfig GetOpenIDConfig()
|
public OpenIDConfig GetOpenIDConfig()
|
||||||
{
|
{
|
||||||
OpenIDConfig openIdConfig = _config.GetSection("OpenIDConfig").Get<OpenIDConfig>() ?? new OpenIDConfig();
|
OpenIDConfig openIdConfig = _config.GetSection("OpenIDConfig").Get<OpenIDConfig>() ?? new OpenIDConfig();
|
||||||
|
|||||||
@@ -113,14 +113,20 @@ namespace CarCareTracker.Helper
|
|||||||
string tableBody = "";
|
string tableBody = "";
|
||||||
foreach(ReminderRecordViewModel reminder in reminders)
|
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 += $"<tr class='{reminder.Urgency}'><td>{StaticHelper.GetTitleCaseReminderUrgency(reminder.Urgency)}</td><td>{reminder.Description}</td><td>{dueOn}</td></tr>";
|
tableBody += $"<tr class='{reminder.Urgency}'><td>{StaticHelper.GetTitleCaseReminderUrgency(reminder.Urgency)}</td><td>{reminder.Description}</td><td>{dueOn}</td></tr>";
|
||||||
}
|
}
|
||||||
emailBody = emailBody.Replace("{TableBody}", tableBody);
|
emailBody = emailBody.Replace("{TableBody}", tableBody);
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
SendEmail(emailAddresses, emailSubject, emailBody);
|
var result = SendEmail(emailAddresses, emailSubject, emailBody);
|
||||||
|
if (result)
|
||||||
|
{
|
||||||
return new OperationResponse { Success = true, Message = "Email Sent!" };
|
return new OperationResponse { Success = true, Message = "Email Sent!" };
|
||||||
|
} else
|
||||||
|
{
|
||||||
|
return new OperationResponse { Success = false, Message = StaticHelper.GenericErrorMessage };
|
||||||
|
}
|
||||||
} catch (Exception ex)
|
} catch (Exception ex)
|
||||||
{
|
{
|
||||||
return new OperationResponse { Success = false, Message = ex.Message };
|
return new OperationResponse { Success = false, Message = ex.Message };
|
||||||
|
|||||||
Reference in New Issue
Block a user