case 4310
This commit is contained in:
@@ -11,6 +11,7 @@ using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using AyaNova.Util;
|
||||
|
||||
namespace AyaNova.Api.Controllers
|
||||
{
|
||||
@@ -227,6 +228,93 @@ namespace AyaNova.Api.Controllers
|
||||
[Required]
|
||||
public List<long> Users { get; set; }
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Send direct SMTP message notification so single object / address
|
||||
/// Note: adds to queue, not instantly sent
|
||||
/// </summary>
|
||||
/// <returns>NoContent on success or error</returns>
|
||||
[HttpPost("direct-smtp")]
|
||||
public async Task<IActionResult> SendNotifySmtpDirectMessage([FromBody] NotifyDirectSMTP notifyDirectSMTP)
|
||||
{
|
||||
if (serverState.IsClosed)
|
||||
return StatusCode(503, new ApiErrorResponse(serverState.ApiErrorCode, null, serverState.Reason));
|
||||
|
||||
if (!ModelState.IsValid)
|
||||
return BadRequest(new ApiErrorResponse(ModelState));
|
||||
|
||||
|
||||
//validate
|
||||
|
||||
if (string.IsNullOrWhiteSpace(notifyDirectSMTP.ToAddress))
|
||||
{
|
||||
//We need to fetch the address from the object type and id
|
||||
//if no id then can skip the rest here
|
||||
if (notifyDirectSMTP.ObjectId == 0)
|
||||
{
|
||||
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "ObjectId", "No address or object id specified, no where to send this"));
|
||||
}
|
||||
//get the address
|
||||
switch (notifyDirectSMTP.AType)
|
||||
{
|
||||
case AyaType.NoType:
|
||||
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "ToAddress", "No address or object type and id specified no where to send this"));
|
||||
case AyaType.Customer:
|
||||
var CustomerInfo = await ct.Customer.AsNoTracking().Where(x => x.Id == notifyDirectSMTP.ObjectId).Select(x => new { x.Name, x.EmailAddress, x.Active }).FirstAsync();
|
||||
if (string.IsNullOrWhiteSpace(CustomerInfo.EmailAddress))
|
||||
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "EmailAddress", $"Customer {CustomerInfo.Name} doesn't have an email address no where to send this"));
|
||||
if (CustomerInfo.Active == false)
|
||||
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "Active", $"Customer {CustomerInfo.Name} is not active, only active customers can be emailed directly"));
|
||||
notifyDirectSMTP.ToAddress = CustomerInfo.EmailAddress;
|
||||
break;
|
||||
default:
|
||||
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "AType", "Specified Type not supported for 'on request' smtp"));
|
||||
|
||||
}
|
||||
}
|
||||
var UserId = UserIdFromContext.Id(HttpContext.Items);
|
||||
|
||||
IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer;
|
||||
try
|
||||
{
|
||||
if (!ServerGlobalOpsSettingsCache.Notify.SmtpDeliveryActive)
|
||||
{
|
||||
await NotifyEventHelper.AddGeneralNotifyEvent(NotifyEventType.GeneralNotification,
|
||||
$"Email notifications are set to OFF at server, unable to send 'on request' type SMTP notification subject:{notifyDirectSMTP.Subject}",
|
||||
"Error",
|
||||
null,
|
||||
UserId);
|
||||
log.LogInformation($"** WARNING: SMTP notification is currently set to Active=False; unable to send 'on request' type SMTP notification subject:{notifyDirectSMTP.Subject} **");
|
||||
|
||||
}
|
||||
else
|
||||
await m.SendEmailAsync(notifyDirectSMTP.ToAddress, "Test from Notification system", "This is a test to confirm notification system is working", ServerGlobalOpsSettingsCache.Notify);
|
||||
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
await NotifyEventHelper.AddOpsProblemEvent("SMTP direct message failed", ex);
|
||||
return StatusCode(500, new ApiErrorResponse(ApiErrorCode.API_SERVER_ERROR, null, ExceptionUtil.ExtractAllExceptionMessages(ex)));
|
||||
}
|
||||
|
||||
return NoContent();
|
||||
}
|
||||
|
||||
public class NotifyDirectSMTP
|
||||
{
|
||||
public NotifyDirectSMTP()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public long ObjectId { get; set; } = 0;
|
||||
public AyaType AType { get; set; } = AyaType.NoType;
|
||||
public string ToAddress { get; set; }
|
||||
public string Subject { get; set; }
|
||||
public string TextBody { get; set; }
|
||||
public string HTMLBody { get; set; }
|
||||
}
|
||||
|
||||
//------------
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user