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

212 lines
6.9 KiB
C#

using System.Security.Claims;
using Api.SeaHavenIndustries.DTOs;
using Api.SeaHavenIndustries.Helper;
using Data.SeaHavenIndustries;
using FluentValidation;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.Interfaces;
[ApiController]
[Authorize]
[Route("api/FollowUp")]
[Route("api/followups")]
public class FollowUpController : ControllerBase
{
private readonly IFollowUpService _followUpService;
private readonly ILogger<FollowUpController> _logger;
public FollowUpController(IFollowUpService followUpService, ILogger<FollowUpController> logger)
{
_followUpService = followUpService;
_logger = logger;
}
[HttpGet("GetFollowUpList")]
public async Task<IActionResult> GetFollowUps(
string? search = "",
string? status = "Open",
int page = 1,
int pageSize = 12,
CancellationToken cancellationToken = default)
{
var pagedResult = await _followUpService.GetFollowUpListAsync(page, pageSize, search, status, cancellationToken);
var items = pagedResult.Items;
var data = items.Select(x => new
{
x.Id,
x.Reason,
x.ScheduleDate,
x.ScheduleStartTime,
x.EmployeeId,
x.WorkOrderId,
x.AccountId,
x.LocationId,
x.AccountName,
x.AssignedTo,
x.LocationName,
x.WorkOrderNumber,
x.InternalWONumber,
x.Status,
x.CreatedDate,
x.CreatedById,
x.CreatedByName
}).ToList();
return Ok(new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
TotalCount = pagedResult.TotalCount,
TotalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize)
});
}
[HttpGet("GetById")]
public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
{
var model = await _followUpService.GetFollowUpDetailAsync(id, cancellationToken);
if (model == null) return NotFound();
return Ok(new
{
model.Id,
model.Reason,
model.ScheduleDate,
model.ScheduleStartTime,
model.EmployeeId,
model.WorkOrderId,
model.AccountId,
model.LocationId,
model.AccountName,
model.AssignedTo,
model.LocationName,
model.WorkOrderNumber,
model.InternalWONumber,
model.Status,
model.CreatedDate,
model.CreatedById,
model.CreatedByName
});
}
[HttpPost("Create")]
public async Task<IActionResult> Create(FollowUp_DTO dto, CancellationToken cancellationToken)
{
try
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
if (userId == null)
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
var id = await _followUpService.CreateOpenFollowUpAsync(MapToCreateRequest(dto), userId, cancellationToken);
return Ok(new DataResponse { Status = "200", Message = "Follow Up created", Data = id });
}
catch (ValidationException vex)
{
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
return BadRequest(new Response { Status = "Validation Error", Message = errors });
}
catch (Exception ex)
{
return StatusCode(500, new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpPost("Update")]
public async Task<IActionResult> Update(FollowUp_DTO dto, CancellationToken cancellationToken)
{
try
{
await _followUpService.UpdateFollowUpFieldsAsync(dto.Id, MapToUpdateRequest(dto), cancellationToken);
return Ok(new DataResponse { Status = "200", Message = "Follow Up updated" });
}
catch (ValidationException vex)
{
var errors = string.Join(", ", vex.Errors.Select(e => e.ErrorMessage));
return BadRequest(new Response { Status = "Validation Error", Message = errors });
}
catch (KeyNotFoundException)
{
return NotFound(new Response { Status = "Error", Message = "Follow Up not found" });
}
catch (Exception ex)
{
return StatusCode(500, new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
[HttpPost("Complete")]
public async Task<IActionResult> Complete(int id, CancellationToken cancellationToken)
{
var found = await _followUpService.SetFollowUpStatusAsync(id, "Completed", cancellationToken);
if (!found) return NotFound();
return Ok();
}
[HttpPost("Cancel")]
public async Task<IActionResult> Cancel(int id, CancellationToken cancellationToken)
{
var found = await _followUpService.SetFollowUpStatusAsync(id, "Cancelled", cancellationToken);
if (!found) return NotFound();
return Ok();
}
[HttpPost("Delete")]
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
{
try
{
var found = await _followUpService.RemoveFollowUpAsync(id, cancellationToken);
if (!found)
return NotFound(new Response { Status = "Error", Message = "Follow Up not found" });
return Ok(new DataResponse { Status = "200", Message = "Follow Up deleted" });
}
catch (KeyNotFoundException)
{
return NotFound(new Response { Status = "Error", Message = "Follow Up not found" });
}
catch (Exception ex)
{
return StatusCode(500, new Response { Status = "Error", Message = _logger.Sanitize(ex) });
}
}
private static FollowUpCreateRequestDTO MapToCreateRequest(FollowUp_DTO dto)
{
return new FollowUpCreateRequestDTO
{
Reason = dto.Reason,
ScheduleDate = dto.ScheduleDate,
ScheduleStartTime = dto.ScheduleStartTime,
EmployeeId = dto.EmployeeId,
WorkOrderId = dto.WorkOrderId,
AccountId = dto.AccountId,
LocationId = dto.LocationId
};
}
private static FollowUpUpdateRequestDTO MapToUpdateRequest(FollowUp_DTO dto)
{
return new FollowUpUpdateRequestDTO
{
Id = dto.Id,
Reason = dto.Reason,
ScheduleDate = dto.ScheduleDate,
ScheduleStartTime = dto.ScheduleStartTime,
EmployeeId = dto.EmployeeId,
WorkOrderId = dto.WorkOrderId,
AccountId = dto.AccountId,
LocationId = dto.LocationId
};
}
}