shoc-backend/SeaHaven.DataServices/Implementation/QuotesDataService.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

179 lines
6.4 KiB
C#

using Data.SeaHavenIndustries;
using Microsoft.EntityFrameworkCore;
using SeaHaven.DataServices.Interfaces;
using SeaHaven.DataServices.Models;
namespace SeaHaven.DataServices.Implementation
{
public class QuotesDataService : IQuotesDataService
{
private readonly ApplicationDbContext _context;
public QuotesDataService(ApplicationDbContext context)
{
_context = context;
}
public async Task<Quotes?> GetByIdAsync(int id)
{
return await _context.Quotes.FindAsync(id);
}
public async Task<IEnumerable<Quotes>> GetAllAsync()
{
return await _context.Quotes.ToListAsync();
}
public async Task<IEnumerable<Quotes>> GetByWorkOrderIdAsync(int workOrderId)
{
return await _context.Quotes
.Where(q => q.WorkorderId == workOrderId)
.ToListAsync();
}
public async Task<Quotes> AddAsync(Quotes quote)
{
await _context.Quotes.AddAsync(quote);
await _context.SaveChangesAsync();
return quote;
}
public async Task UpdateAsync(Quotes quote)
{
_context.Quotes.Update(quote);
await _context.SaveChangesAsync();
}
public async Task DeleteAsync(int id)
{
var entity = await GetByIdAsync(id);
if (entity != null)
{
_context.Quotes.Remove(entity);
await _context.SaveChangesAsync();
}
}
public async Task<bool> ExistsAsync(int id)
{
return await _context.Quotes.AnyAsync(q => q.Id == id);
}
public async Task UnlinkFromWorkOrderAsync(int workOrderId)
{
var quotes = await _context.Quotes
.Where(q => q.WorkorderId == workOrderId)
.ToListAsync();
foreach (var quote in quotes)
{
quote.WorkorderId = null;
}
await _context.SaveChangesAsync();
}
public async Task<(IReadOnlyList<QuoteListItemReadModel> Items, int TotalCount)> GetQuotesPagedAsync(
int page, int pageSize, string search, CancellationToken cancellationToken)
{
var normalizedSearch = search.Trim();
var query = _context.Quotes
.AsNoTracking()
.Where(w => normalizedSearch.Length == 0 ||
(w.QuotId != null && w.QuotId.Contains(normalizedSearch)) ||
(w.SiteName != null && w.SiteName.Contains(normalizedSearch)) ||
(w.WorkOrder != null && w.WorkOrder.WorkerOrderTitle != null &&
w.WorkOrder.WorkerOrderTitle.Contains(normalizedSearch)));
var totalCount = await query.CountAsync(cancellationToken);
var data = await query
.OrderByDescending(s => s.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(s => new QuoteListItemReadModel
{
Id = s.Id,
QuoteId = "QO-" + s.QuotId,
WorkOrder = s.WorkOrder != null ? s.WorkOrder.WorkerOrderNumber : "",
ProjectName = s.ProjectName,
TotalAmount = s.LineItems!.Sum(l => l.Lineitem ?? 0),
SiteName = s.SiteName,
SiteId = s.SiteId,
PO = s.PO,
TT = s.TT,
BillingAddress = s.BillingAddress,
Status = s.Status,
ContactId = s.ContactId,
Date = s.Date,
Lineitems = s.LineItems!.Select(q => new QuoteLineItemReadModel
{
Descriptions = q.Descriptions,
Lineitem = q.Lineitem,
LineitemNumber = q.LineitemNumber
}).ToList(),
})
.ToListAsync(cancellationToken);
return (data, totalCount);
}
public async Task<QuoteDetailReadModel?> GetQuoteDetailsByIdAsync(
int id, CancellationToken cancellationToken)
{
return await _context.Quotes
.Where(s => s.Id == id)
.AsNoTracking()
.Select(s => new QuoteDetailReadModel
{
Id = s.Id,
QuotId = s.QuotId,
Date = s.Date,
Name = s.Contacts != null ? (s.Contacts.FirstName + " " + s.Contacts.LastName).Trim() : "",
PO = s.PO,
TT = s.TT,
SiteName = s.SiteName,
ProjectName = s.ProjectName,
SiteId = s.SiteId,
BillingAddress = s.BillingAddress,
ShippingAddress = s.ShippingAddress,
Lineitems = s.LineItems!.Select(q => new QuoteLineItemReadModel
{
Descriptions = q.Descriptions,
Lineitem = q.Lineitem
}).ToList(),
TotalAmount = s.LineItems!.Sum(l => l.Lineitem ?? 0)
})
.FirstOrDefaultAsync(cancellationToken);
}
public async Task<IReadOnlyList<WorkOrderQuoteDetailReadModel>> GetWorkOrderQuoteDetailsAsync(
int woId, CancellationToken cancellationToken)
{
return await _context.workOrders
.AsNoTracking()
.Where(w => w.Id == woId)
.Select(s => new WorkOrderQuoteDetailReadModel
{
Id = s.Id,
PO = s.PO,
TT = s.TT,
Title = s.Locations != null ? s.Locations.Title : "",
Name = s.Locations != null ? s.Locations.Name : ""
})
.ToListAsync(cancellationToken);
}
public async Task DeleteLineItemsByQuoteIdAsync(int quoteId, CancellationToken cancellationToken)
{
var items = await _context.QuotesLines
.Where(w => w.QuoteId == quoteId)
.ToListAsync(cancellationToken);
if (items.Any())
{
_context.QuotesLines.RemoveRange(items);
await _context.SaveChangesAsync(cancellationToken);
}
}
}
}