mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 22:23:12 +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
127 lines
4.9 KiB
C#
127 lines
4.9 KiB
C#
using Data.SeaHavenIndustries;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Interfaces;
|
|
|
|
namespace SeaHaven.DataServices.Implementation
|
|
{
|
|
public class SyncDataService : ISyncDataService
|
|
{
|
|
private readonly ApplicationDbContext _context;
|
|
|
|
public SyncDataService(ApplicationDbContext context)
|
|
{
|
|
_context = context;
|
|
}
|
|
|
|
public async Task<string?> GetLastInternalWONumberAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await _context.workOrders
|
|
.Where(w => w.InternalWONumber != null && w.InternalWONumber != "")
|
|
.OrderByDescending(w => w.InternalWONumber)
|
|
.Select(w => w.InternalWONumber)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<WorkOrder?> GetWorkOrderByExternalIdAsync(string externalId, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.workOrders
|
|
.FirstOrDefaultAsync(w => w.ExternalWorkOrderId == externalId, cancellationToken);
|
|
}
|
|
|
|
public void EnqueueWorkOrder(WorkOrder workOrder)
|
|
{
|
|
_context.workOrders.Add(workOrder);
|
|
}
|
|
|
|
public void EnqueueComment(Comments comment)
|
|
{
|
|
_context.Comments.Add(comment);
|
|
}
|
|
|
|
public async Task SaveChangesAsync(CancellationToken cancellationToken)
|
|
{
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<int?> ResolveLocationAsync(string? siteCode, string? building, string? address, CancellationToken cancellationToken)
|
|
{
|
|
if (string.IsNullOrWhiteSpace(siteCode) && string.IsNullOrWhiteSpace(building))
|
|
return null;
|
|
|
|
var matchCode = siteCode ?? building;
|
|
var existing = await _context.Locations
|
|
.FirstOrDefaultAsync(l => l.Name == matchCode || l.Title == matchCode, cancellationToken);
|
|
|
|
if (existing != null)
|
|
return existing.Id;
|
|
|
|
var location = new Locations
|
|
{
|
|
Name = matchCode,
|
|
Title = building,
|
|
Address1 = address,
|
|
Status = "Active"
|
|
};
|
|
|
|
_context.Locations.Add(location);
|
|
await _context.SaveChangesAsync(cancellationToken);
|
|
|
|
return location.Id;
|
|
}
|
|
|
|
public async Task<int> GetLocationCountAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Locations.CountAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<bool> CommentExistsByExternalIdAsync(string externalId, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Comments
|
|
.AnyAsync(c => c.ExternalCommentId == externalId, cancellationToken);
|
|
}
|
|
|
|
public async Task<WorkOrder?> GetWorkOrderByInternalNumberAsync(string internalNumber, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.workOrders
|
|
.FirstOrDefaultAsync(w => w.InternalWONumber == internalNumber, cancellationToken);
|
|
}
|
|
|
|
public async Task<Dispatch?> GetDispatchByNumberAndWorkOrderIdAsync(string dispatchNumber, int workOrderId, CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Dispatches
|
|
.FirstOrDefaultAsync(d => d.DispatchNumber == dispatchNumber && d.WorkOrderId == workOrderId, cancellationToken);
|
|
}
|
|
|
|
public async Task<List<WorkOrder>> GetWorkOrdersMissingInternalNumberAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await _context.workOrders
|
|
.Where(w => w.InternalWONumber == null || w.InternalWONumber == "")
|
|
.OrderBy(w => w.Id)
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<List<Dispatch>> GetDispatchesMissingNumberAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Dispatches
|
|
.Where(d => d.DispatchNumber == null || d.DispatchNumber == "")
|
|
.OrderBy(d => d.Id)
|
|
.ToListAsync(cancellationToken);
|
|
}
|
|
|
|
public async Task<int> ExecuteBackfillCommentTypesAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Database.ExecuteSqlRawAsync(
|
|
"UPDATE Comments SET CommentType = 'customer' WHERE ExternalCommentId IS NOT NULL AND (CommentType IS NULL OR CommentType = '')",
|
|
cancellationToken);
|
|
}
|
|
|
|
public async Task<string?> GetLastDispatchNumberAsync(CancellationToken cancellationToken)
|
|
{
|
|
return await _context.Dispatches
|
|
.Where(d => d.DispatchNumber != null && d.DispatchNumber != "")
|
|
.OrderByDescending(d => d.DispatchNumber)
|
|
.Select(d => d.DispatchNumber)
|
|
.FirstOrDefaultAsync(cancellationToken);
|
|
}
|
|
}
|
|
}
|