shoc-backend/Api.SeaHavenIndustries/Controllers/QuotesController.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

181 lines
6.4 KiB
C#

using Api.SeaHavenIndustries.DTOs;
using Api.SeaHavenIndustries.Helper;
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
namespace Api.SeaHavenIndustries.Controllers
{
[Authorize]
[ApiController]
[Route("api/Quotes")]
public class QuotesController : Controller
{
private readonly IQuotesService _quotesService;
private readonly ILogger<QuotesController> _logger;
public QuotesController(IQuotesService quotesService, ILogger<QuotesController> logger)
{
_quotesService = quotesService;
_logger = logger;
}
[HttpPost]
[Route("AddQuote")]
public async Task<IActionResult> AddQuote([FromBody] Quote_DTO model, CancellationToken cancellationToken)
{
try
{
await _quotesService.CreateQuoteAsync(MapToCreateInput(model), cancellationToken);
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpPut]
[Route("EditQuote")]
public async Task<IActionResult> EditQuote([FromBody] EditQuote_DTO model, CancellationToken cancellationToken)
{
try
{
var updated = await _quotesService.UpdateQuoteAsync(MapToUpdateInput(model), cancellationToken);
if (!updated)
return BadRequest("ID not matched!");
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpDelete]
[Route("DeleteQuote")]
public async Task<IActionResult> DeleteQuote(EditQuote_DTO model, CancellationToken cancellationToken)
{
int id = model.id;
try
{
var deleted = await _quotesService.DeleteQuoteAsync(id, cancellationToken);
if (!deleted)
return BadRequest("ID not matched!");
return Ok(new DataResponse { Message = "Removed Successfully", Status = "200" });
}
catch (Exception ex)
{
return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpGet]
[Route("GetQuoutes")]
public async Task<IActionResult> GetQuotes(string search = "", int page = 1, int pageSize = 10, CancellationToken cancellationToken = default)
{
var result = await _quotesService.GetQuotesPagedAsync(page, pageSize, search, cancellationToken);
var totalPages = (int)Math.Ceiling(result.TotalCount / (double)pageSize);
var viewModel = new Pagination_DTO
{
Data = result.Items,
PageNumber = page,
PageSize = pageSize,
TotalCount = result.TotalCount,
TotalPages = totalPages
};
return Ok(viewModel);
}
[HttpGet]
[Route("GetQuoutesdetailsAgainstWorkOrder")]
public async Task<IActionResult> GetQuoutesdetailsAgainstWorkOrder(int woId, CancellationToken cancellationToken)
{
var data = await _quotesService.GetWorkOrderQuoteDetailsAsync(woId, cancellationToken);
return Ok(data);
}
[HttpGet]
[Route("GetQuoutesById")]
public async Task<IActionResult> GetQuoutesById(int id, CancellationToken cancellationToken)
{
var data = await _quotesService.GetQuoteDetailsByIdAsync(id, cancellationToken);
return Ok(data);
}
[HttpGet]
[Route("GetReporting")]
public async Task<IActionResult> GetReporting(CancellationToken cancellationToken)
{
var data = await _quotesService.GetReportingAsync(cancellationToken);
var reportingDto = new Reporting_DTO
{
TotalCreatedWorkOrder = data.TotalCreatedWorkOrder,
TotalCompleted = data.TotalCompleted,
TotalSev1 = data.TotalSev1,
TotalSev2 = data.TotalSev2,
TotalSev3 = data.TotalSev3,
TotalSev4 = data.TotalSev4,
TotalSev5 = data.TotalSev5
};
return Ok(reportingDto);
}
private static CreateQuoteInput MapToCreateInput(Quote_DTO dto)
{
return new CreateQuoteInput
{
QuotId = dto.QuotId,
ContactId = dto.ContactId,
PO = dto.PO,
TT = dto.TT,
ProjectName = dto.ProjectName,
Date = dto.Date != null ? DateTime.Parse(dto.Date) : null,
WorkorderId = dto.WorkorderId,
SiteName = dto.SiteName,
SiteId = dto.SiteId,
BillingAddress = dto.BillingAddress,
LineItems = dto.LineItems?.Select(li => new QuoteLineItemInput
{
LineitemNumber = li.LineitemNumber,
Lineitem = li.Lineitem,
Descriptions = li.Descriptions
}).ToList()
};
}
private static UpdateQuoteInput MapToUpdateInput(EditQuote_DTO dto)
{
return new UpdateQuoteInput
{
Id = dto.id,
QuotId = dto.QuotId,
ContactId = dto.ContactId,
PO = dto.PO,
TT = dto.TT,
ProjectName = dto.ProjectName,
Date = dto.Date != null ? DateTime.Parse(dto.Date) : null,
WorkorderId = dto.WorkorderId,
SiteName = dto.SiteName,
SiteId = dto.SiteId,
BillingAddress = dto.BillingAddress,
LineItems = dto.LineItems?.Select(li => new QuoteLineItemInput
{
LineitemNumber = li.LineitemNumber,
Lineitem = li.Lineitem,
Descriptions = li.Descriptions
}).ToList()
};
}
}
}