mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-09-30 06:03: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
104 lines
3.8 KiB
C#
104 lines
3.8 KiB
C#
using Data.SeaHavenIndustries;
|
|
using FluentAssertions;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using SeaHaven.DataServices.Implementation;
|
|
using Xunit;
|
|
|
|
namespace Api.SeaHavenIndustries.Tests;
|
|
|
|
public class WorkOrderDataServiceBatchingTests
|
|
{
|
|
private static ApplicationDbContext NewContext()
|
|
{
|
|
var options = new DbContextOptionsBuilder<ApplicationDbContext>()
|
|
.UseInMemoryDatabase(Guid.NewGuid().ToString())
|
|
.Options;
|
|
return new ApplicationDbContext(options);
|
|
}
|
|
|
|
private static async Task SeedAsync(ApplicationDbContext context)
|
|
{
|
|
context.Locations.Add(new Locations { Id = 100, Name = "Site A", Address1 = "1 Main St" });
|
|
context.workOrders.AddRange(
|
|
new WorkOrder { Id = 1, InternalWONumber = "WO-1", WorkerOrderTitle = "First", LocationId = 100 },
|
|
new WorkOrder { Id = 2, InternalWONumber = "WO-2", WorkerOrderTitle = "Second", LocationId = 100 },
|
|
new WorkOrder { Id = 3, InternalWONumber = "WO-3", WorkerOrderTitle = "Third", LocationId = 100 },
|
|
new WorkOrder { Id = 4, InternalWONumber = "WO-T", WorkerOrderTitle = "Template", LocationId = 100, istemplate = true });
|
|
await context.SaveChangesAsync();
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByRequestedIdsWithDetailsAsync_ReturnsResultsInInputOrder()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
var result = await service.GetByRequestedIdsWithDetailsAsync(
|
|
new[] { 3, 1, 2 },
|
|
CancellationToken.None);
|
|
|
|
result.Select(w => w.Id).Should().Equal(new[] { 3, 1, 2 });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByRequestedIdsWithDetailsAsync_DeduplicatesIdsAndSkipsMissing()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
var result = await service.GetByRequestedIdsWithDetailsAsync(
|
|
new[] { 2, 2, 999, 1 },
|
|
CancellationToken.None);
|
|
|
|
result.Select(w => w.Id).Should().Equal(new[] { 2, 1 });
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByRequestedIdsWithDetailsAsync_ExcludesTemplatesAndEagerLoadsLocation()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
var result = await service.GetByRequestedIdsWithDetailsAsync(
|
|
new[] { 1, 4 },
|
|
CancellationToken.None);
|
|
|
|
result.Select(w => w.Id).Should().Equal(new[] { 1 });
|
|
result.Single().Locations.Should().NotBeNull();
|
|
result.Single().Locations!.Name.Should().Be("Site A");
|
|
}
|
|
|
|
[Fact]
|
|
public async Task AddAuditLogsAsync_PersistsAllRowsInSingleSave()
|
|
{
|
|
await using var context = NewContext();
|
|
await SeedAsync(context);
|
|
var service = new WorkOrderDataService(context);
|
|
|
|
await service.AddAuditLogsAsync(new[]
|
|
{
|
|
new WorkOrderAuditLog { WorkOrderId = 1, FieldName = "Dispatch", Action = "Dispatch" },
|
|
new WorkOrderAuditLog { WorkOrderId = 2, FieldName = "Dispatch", Action = "Dispatch" }
|
|
});
|
|
|
|
context.WorkOrderAuditLogs.Should().HaveCount(2);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task GetByIdsOrderedAsync_PreservesInputOrderDeduplicatesAndSkipsMissing()
|
|
{
|
|
await using var context = NewContext();
|
|
context.Vendors.AddRange(
|
|
new Vendor { Id = 7, CompanyName = "V7", Email = "v7@x.io" },
|
|
new Vendor { Id = 8, CompanyName = "V8", Email = "v8@x.io" });
|
|
await context.SaveChangesAsync();
|
|
var service = new VendorDataService(context);
|
|
|
|
var result = await service.GetByIdsOrderedAsync(new[] { 8, 8, 7, 999 }, CancellationToken.None);
|
|
|
|
result.Select(v => v.Id).Should().Equal(new[] { 8, 7 });
|
|
}
|
|
}
|