This commit is contained in:
2022-11-21 01:04:15 +00:00
parent a9e8acf24b
commit b9f80c6143

View File

@@ -231,7 +231,8 @@ namespace AyaNova.Api.Controllers
/// <summary>
/// Send direct SMTP message notification so single object / address
/// Note: adds to queue, not instantly sent
/// Server notification settings must be set and active
/// Currently supported types are Customer, HeadOffice, Vendor, User
/// </summary>
/// <returns>Accepted on success or error</returns>
[HttpPost("direct-smtp")]
@@ -270,13 +271,62 @@ namespace AyaNova.Api.Controllers
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;
{
var o = await ct.Customer.AsNoTracking().Where(x => x.Id == notifyDirectSMTP.ObjectId).Select(x => new { x.Name, x.EmailAddress, x.Active }).FirstOrDefaultAsync();
if (o == null)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "ObjectId"));
}
if (string.IsNullOrWhiteSpace(o.EmailAddress))
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "EmailAddress", $"Customer {o.Name} doesn't have an email address no where to send this"));
if (o.Active == false)
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "Active", $"Customer {o.Name} is not active, only active customers can be emailed directly"));
notifyDirectSMTP.ToAddress = o.EmailAddress;
}
break;
case AyaType.HeadOffice:
{
var o = await ct.HeadOffice.AsNoTracking().Where(x => x.Id == notifyDirectSMTP.ObjectId).Select(x => new { x.Name, x.EmailAddress, x.Active }).FirstOrDefaultAsync();
if (o == null)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "ObjectId"));
}
if (string.IsNullOrWhiteSpace(o.EmailAddress))
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "EmailAddress", $"HeadOffice {o.Name} doesn't have an email address no where to send this"));
if (o.Active == false)
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "Active", $"HeadOffice {o.Name} is not active, only active head offices can be emailed directly"));
notifyDirectSMTP.ToAddress = o.EmailAddress;
}
break;
case AyaType.Vendor:
{
var o = await ct.Vendor.AsNoTracking().Where(x => x.Id == notifyDirectSMTP.ObjectId).Select(x => new { x.Name, x.EmailAddress, x.Active }).FirstOrDefaultAsync();
if (o == null)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "ObjectId"));
}
if (string.IsNullOrWhiteSpace(o.EmailAddress))
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "EmailAddress", $"Vendor {o.Name} doesn't have an email address no where to send this"));
if (o.Active == false)
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "Active", $"Vendor {o.Name} is not active, only active Vendors can be emailed directly"));
notifyDirectSMTP.ToAddress = o.EmailAddress;
}
break;
case AyaType.User:
{
var o = await ct.User.Include(z => z.UserOptions).AsNoTracking().Where(x => x.Id == notifyDirectSMTP.ObjectId).Select(x => new { x.Name, x.UserOptions.EmailAddress, x.Active }).FirstOrDefaultAsync();
if (o == null)
{
return BadRequest(new ApiErrorResponse(ApiErrorCode.NOT_FOUND, "ObjectId"));
}
if (string.IsNullOrWhiteSpace(o.EmailAddress))
return BadRequest(new ApiErrorResponse(ApiErrorCode.VALIDATION_MISSING_PROPERTY, "EmailAddress", $"Vendor {o.Name} doesn't have an email address no where to send this"));
if (o.Active == false)
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "Active", $"Vendor {o.Name} is not active, only active Vendors can be emailed directly"));
notifyDirectSMTP.ToAddress = o.EmailAddress;
}
break;
default:
return BadRequest(new ApiErrorResponse(ApiErrorCode.INVALID_OPERATION, "AType", "Specified Type not supported for 'on request' smtp"));
@@ -287,7 +337,7 @@ namespace AyaNova.Api.Controllers
IMailer m = AyaNova.Util.ServiceProviderProvider.Mailer;
try
{
await m.SendEmailAsync(notifyDirectSMTP.ToAddress, notifyDirectSMTP.Subject, notifyDirectSMTP.TextBody, ServerGlobalOpsSettingsCache.Notify,null,null,notifyDirectSMTP.HTMLBody);
await m.SendEmailAsync(notifyDirectSMTP.ToAddress, notifyDirectSMTP.Subject, notifyDirectSMTP.TextBody, ServerGlobalOpsSettingsCache.Notify, null, null, notifyDirectSMTP.HTMLBody);
}
catch (Exception ex)
{