mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-02 10:53:23 +00:00
* 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
108 lines
4.1 KiB
C#
108 lines
4.1 KiB
C#
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Http;
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
namespace Api.SeaHavenIndustries.Infrastructure
|
|
{
|
|
public class FileStorageAdapter : IFileStoragePort
|
|
{
|
|
private readonly IWebHostEnvironment _webHostEnvironment;
|
|
private readonly IHttpContextAccessor _httpContext;
|
|
|
|
public FileStorageAdapter(IWebHostEnvironment webHostEnvironment, IHttpContextAccessor httpContext)
|
|
{
|
|
_webHostEnvironment = webHostEnvironment;
|
|
_httpContext = httpContext;
|
|
}
|
|
|
|
public async Task<string> SaveFileAsync(IFormFile file)
|
|
{
|
|
var fileName = Path.GetFileName(file.FileName);
|
|
if (string.IsNullOrWhiteSpace(fileName))
|
|
throw new InvalidDataException("The uploaded file must have a valid name.");
|
|
|
|
var uniqueFileName = $"{Guid.NewGuid()}_{fileName}";
|
|
var uploadPath = Path.Combine("Assets", "Documents");
|
|
var webRoot = _webHostEnvironment.WebRootPath
|
|
?? Path.Combine(_webHostEnvironment.ContentRootPath, "wwwroot");
|
|
var fullPath = Path.Combine(webRoot, uploadPath, uniqueFileName);
|
|
|
|
Directory.CreateDirectory(Path.GetDirectoryName(fullPath)!);
|
|
|
|
using (var stream = System.IO.File.Create(fullPath))
|
|
{
|
|
await file.CopyToAsync(stream);
|
|
}
|
|
|
|
var request = _httpContext.HttpContext?.Request
|
|
?? throw new InvalidOperationException("An active HTTP request is required to build the file URL.");
|
|
var domain = $"{request.Scheme}://{request.Host}";
|
|
return $"{domain}/{uploadPath.Replace("\\", "/")}/{uniqueFileName}";
|
|
}
|
|
}
|
|
|
|
public class DispatchEmailAdapter : IDispatchEmailPort
|
|
{
|
|
private readonly Helper.SendMessage _sendMessage;
|
|
|
|
public DispatchEmailAdapter(Helper.SendMessage sendMessage)
|
|
{
|
|
_sendMessage = sendMessage;
|
|
}
|
|
|
|
public async Task<bool> SendDispatchEmailAsync(string emailTo, string subject, string htmlBody, string? replyTo)
|
|
{
|
|
return await _sendMessage.SendDispatchEmail(emailTo, subject, htmlBody, replyTo);
|
|
}
|
|
}
|
|
|
|
public class VendorTokenAdapter : IVendorTokenPort
|
|
{
|
|
private readonly IVendorPortalTokenService _tokenService;
|
|
|
|
public VendorTokenAdapter(IVendorPortalTokenService tokenService)
|
|
{
|
|
_tokenService = tokenService;
|
|
}
|
|
|
|
public async Task<string> GetOrCreateActiveTokenAsync(int vendorId)
|
|
{
|
|
var token = await _tokenService.GetOrCreateActiveTokenAsync(
|
|
vendorId, CancellationToken.None);
|
|
return token.Token;
|
|
}
|
|
}
|
|
|
|
public class VendorDocumentStorageAdapter : IVendorDocumentStoragePort
|
|
{
|
|
private readonly IWebHostEnvironment _environment;
|
|
|
|
public VendorDocumentStorageAdapter(IWebHostEnvironment environment)
|
|
{
|
|
_environment = environment;
|
|
}
|
|
|
|
public async Task SaveAsync(int vendorId, int dispatchId, string storedFileName, Stream content, CancellationToken cancellationToken)
|
|
{
|
|
var path = Helper.VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, vendorId, dispatchId, storedFileName);
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
await using var file = File.Create(path);
|
|
await content.CopyToAsync(file, cancellationToken);
|
|
}
|
|
|
|
public Stream OpenRead(int vendorId, int dispatchId, string storedFileName)
|
|
{
|
|
var path = Helper.VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, vendorId, dispatchId, storedFileName);
|
|
return File.OpenRead(path);
|
|
}
|
|
|
|
public void Delete(int vendorId, int dispatchId, string storedFileName)
|
|
{
|
|
var path = Helper.VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, vendorId, dispatchId, storedFileName);
|
|
if (File.Exists(path))
|
|
{
|
|
File.Delete(path);
|
|
}
|
|
}
|
|
}
|
|
}
|