shoc-backend/Api.SeaHavenIndustries/Helper/SendMessage.cs
Alexandre Brandizzi 7d245eb717
refactor: enforce backend boundaries and optimize dispatch (#30)
* refactor(api): enforce service and data-service boundaries

* refactor(api): complete feature service boundaries

* refactor(identity): enforce service and data boundaries

* refactor(vendors): enforce service and data boundaries

* refactor(workorders): enforce service and data boundaries

* refactor(backend): enforce architecture and optimize dispatch

* style(backend): format changed architecture files

* fix(architecture): address backend review follow-ups

* fix(backend): sanitize exception disclosure in changed API endpoints

Replace raw exception-message disclosure (ex.Message) returned to API
callers with a stable sanitized public message plus correlated structured
internal logging, across the endpoints changed in this PR.

- Add SanitizedErrors helper: logs the original exception at Error with a
  generated correlation id and returns a stable public message referencing
  it so support can trace without exposing internals.
- Inject ILogger<T> into the 14 changed controllers and route every
  ex.Message/dbex.Message disclosure through the helper, preserving status
  codes, response shapes, and business data (e.g. OpenWorkOrders).
- Leave FluentValidation (vex.Errors) and existing fixed-message catches
  untouched; out-of-scope controllers (Account/Contact/Employee/Asset/
  PMSchedule) are unchanged.
- Add focused tests proving internal exception text is not returned and
  that Error logging carrying the original exception is invoked.

* fix(architecture): abstract job run state access

* style: format board update service

* test: use collection assertion idiom
2026-07-24 17:35:34 -03:00

127 lines
5.2 KiB
C#

using SeaHaven.Services.Interfaces;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace Api.SeaHavenIndustries.Helper
{
public class SendMessage : IEmailSender
{
private IConfiguration _configuration;
//string keysapi = "";
public SendMessage(IConfiguration configuration)
{
_configuration = configuration;
//keysapi = _configuration.GetValue<string>("SendGrid:ApiKey");
}
public async Task<bool> SendEMail(string emailTo, string subject, string body)
{
try
{
//MailMessage mail = new MailMessage();
//mail.From = new MailAddress("infoesquall@codevoir.com"); //IMPORTANT: This must be same as your smtp authentication address.
//mail.To.Add(emailTo);
//mail.Subject = subject;
//mail.Body = body;
//mail.IsBodyHtml = true;
//SmtpClient smtp = new SmtpClient("mail.codevoir.com");
//NetworkCredential Credentials = new NetworkCredential("<smtp-user>", "<smtp-password>");
//smtp.UseDefaultCredentials = false;
//smtp.Credentials = Credentials;
//smtp.Port = 8889; //25 alternative port number is 8889
//smtp.EnableSsl = false;
//smtp.Send(mail);
//var apiKey = Environment.GetEnvironmentVariable("<SENDGRID_API_KEY>\r\n");
//var apiKey1 = Environment.GetEnvironmentVariable("<SENDGRID_API_KEY>");
// var apiKey2 = Environment.GetEnvironmentVariable("SENDGRID_API_KEY");
var apiKey = _configuration.GetValue<string>("SendGrid:ApiKey");
//var apiKey = "<SENDGRID_API_KEY>";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("tech@seahavenind.com", "Sea haven Industries");
//var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress(emailTo, "");
var apiKey3 = Environment.GetEnvironmentVariable("SendGrid:ApiKey");
var plainTextContent = "";
var htmlContent = body;
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, htmlContent);
var response = await client.SendEmailAsync(msg);
var dd = response.Body.ReadAsStringAsync();
return true;
}
catch (Exception ex)
{
return false;
}
}
public async Task<bool> SendEmailAsync(string emailTo, string subject, string htmlBody)
=> await SendEMail(emailTo, subject, htmlBody);
public async Task<bool> SendEMailAttachment(string emailTo, string subject, byte[] pdfBytes)
{
try
{
var apiKey = _configuration.GetValue<string>("SendGrid:ApiKey");
//var apiKey = "<SENDGRID_API_KEY>";
var client = new SendGridClient(apiKey);
var from = new EmailAddress("tech@seahavenind.com", "Sea haven Industries");
//var subject = "Sending with SendGrid is Fun";
var to = new EmailAddress(emailTo, "");
var apiKey3 = Environment.GetEnvironmentVariable("SendGrid:ApiKey");
var plainTextContent = "Please find the attached PDF";
var htmlContent = "";
var msg = MailHelper.CreateSingleEmail(from, to, subject, plainTextContent, null);
// Specify the content type for the attachment
var attachment = new SendGrid.Helpers.Mail.Attachment
{
Content = Convert.ToBase64String(pdfBytes),
Filename = "Workorder.pdf",
Type = "application/pdf", // Use the correct MIME type for PDF
Disposition = "attachment",
ContentId = "Attachment" // Optional: ContentId for inline attachments
};
msg.Attachments = new List<SendGrid.Helpers.Mail.Attachment> { attachment };
var response = await client.SendEmailAsync(msg);
var dd = response.Body.ReadAsStringAsync();
return true;
}
catch (Exception ex)
{
return false;
}
}
public async Task<bool> SendDispatchEmail(string emailTo, string subject, string htmlBody, string? replyTo)
{
try
{
var apiKey = _configuration.GetValue<string>("SendGrid:ApiKey");
var client = new SendGridClient(apiKey);
var from = new EmailAddress("tech@seahavenind.com", "Sea Haven Industries");
var to = new EmailAddress(emailTo, "");
var msg = MailHelper.CreateSingleEmail(from, to, subject, "", htmlBody);
if (!string.IsNullOrWhiteSpace(replyTo))
msg.SetReplyTo(new EmailAddress(replyTo));
var response = await client.SendEmailAsync(msg);
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}