mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-02 07:23:21 +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
78 lines
3.4 KiB
C#
78 lines
3.4 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace Api.SeaHavenIndustries.Helper
|
|
{
|
|
public class VendorDocumentScanWorker : BackgroundService
|
|
{
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
private readonly IWebHostEnvironment _environment;
|
|
private readonly ILogger<VendorDocumentScanWorker> _logger;
|
|
|
|
public VendorDocumentScanWorker(
|
|
IServiceScopeFactory scopeFactory,
|
|
IWebHostEnvironment environment,
|
|
ILogger<VendorDocumentScanWorker> logger)
|
|
{
|
|
_scopeFactory = scopeFactory;
|
|
_environment = environment;
|
|
_logger = logger;
|
|
}
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
await ScanPendingAsync(stoppingToken);
|
|
await Task.Delay(TimeSpan.FromSeconds(30), stoppingToken);
|
|
}
|
|
}
|
|
|
|
private async Task ScanPendingAsync(CancellationToken cancellationToken)
|
|
{
|
|
using var scope = _scopeFactory.CreateScope();
|
|
var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
|
|
var scanner = scope.ServiceProvider.GetRequiredService<IDocumentScanner>();
|
|
var pending = await db.VendorCompletionDocuments
|
|
.Where(document => document.ScanStatus == "Pending")
|
|
.OrderBy(document => document.CreatedDate)
|
|
.Take(20)
|
|
.ToListAsync(cancellationToken);
|
|
|
|
foreach (var document in pending)
|
|
{
|
|
var path = VendorDocumentStorage.ResolvePath(_environment.ContentRootPath, document);
|
|
var result = await scanner.ScanAsync(path, cancellationToken);
|
|
if (result == DocumentScanResult.Unavailable)
|
|
{
|
|
_logger.LogWarning("Vendor document scan service unavailable; document {DocumentId} remains quarantined", document.Id);
|
|
continue;
|
|
}
|
|
|
|
document.ScanStatus = result == DocumentScanResult.Passed ? "Passed" : "Rejected";
|
|
document.ReviewStatus = result == DocumentScanResult.Passed ? "Processing" : "Rejected";
|
|
document.RejectionReason = result == DocumentScanResult.Infected
|
|
? "The upload failed malware scanning."
|
|
: document.RejectionReason;
|
|
document.ScannedAt = DateTime.UtcNow;
|
|
document.LastModificationTime = DateTime.UtcNow;
|
|
await db.SaveChangesAsync(cancellationToken);
|
|
|
|
if (result == DocumentScanResult.Infected && File.Exists(path))
|
|
File.Delete(path);
|
|
}
|
|
}
|
|
}
|
|
|
|
public static class VendorDocumentStorage
|
|
{
|
|
public static string Root(string contentRoot) =>
|
|
Path.Combine(contentRoot, "App_Data", "vendor-documents");
|
|
|
|
public static string ResolvePath(string contentRoot, int vendorId, int dispatchId, string storedFileName) =>
|
|
Path.Combine(Root(contentRoot), vendorId.ToString(), dispatchId.ToString(), storedFileName);
|
|
|
|
public static string ResolvePath(string contentRoot, VendorCompletionDocument document) =>
|
|
ResolvePath(contentRoot, document.VendorId, document.DispatchId, document.StoredFileName);
|
|
}
|
|
}
|