2026-07-24 17:35:34 -03:00
|
|
|
using Api.SeaHavenIndustries.DTOs;
|
|
|
|
|
using Data.SeaHavenIndustries;
|
|
|
|
|
using Data.SeaHavenIndustries.Enums;
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
using SeaHaven.Services.DTOs;
|
|
|
|
|
using SeaHaven.Services.Exceptions;
|
2026-09-08 21:24:10 -03:00
|
|
|
using SeaHaven.Services.Helpers;
|
2026-07-24 17:35:34 -03:00
|
|
|
using SeaHaven.Services.Interfaces;
|
|
|
|
|
using System.Security.Claims;
|
|
|
|
|
|
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
|
|
|
{
|
|
|
|
|
[Authorize]
|
|
|
|
|
[ApiController]
|
|
|
|
|
[Route("api/WorkOrder")]
|
|
|
|
|
[Route("api/workorders")]
|
|
|
|
|
public class WorkOrderCompletionController : Controller
|
|
|
|
|
{
|
|
|
|
|
private readonly IWorkOrderCompletionService _workOrderCompletionService;
|
2026-09-25 11:00:22 -03:00
|
|
|
private readonly ICompletionDocTemplateService _completionDocTemplateService;
|
2026-07-24 17:35:34 -03:00
|
|
|
private readonly IFileStoragePort _fileStorage;
|
|
|
|
|
|
|
|
|
|
public WorkOrderCompletionController(
|
|
|
|
|
IWorkOrderCompletionService workOrderCompletionService,
|
2026-09-25 11:00:22 -03:00
|
|
|
ICompletionDocTemplateService completionDocTemplateService,
|
2026-07-24 17:35:34 -03:00
|
|
|
IFileStoragePort fileStorage)
|
|
|
|
|
{
|
|
|
|
|
_workOrderCompletionService = workOrderCompletionService;
|
2026-09-25 11:00:22 -03:00
|
|
|
_completionDocTemplateService = completionDocTemplateService;
|
2026-07-24 17:35:34 -03:00
|
|
|
_fileStorage = fileStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("completion-templates")]
|
|
|
|
|
public async Task<IActionResult> GetCompletionTemplates(
|
2026-09-25 11:00:22 -03:00
|
|
|
[FromQuery] string? search = null,
|
2026-07-24 17:35:34 -03:00
|
|
|
[FromQuery] string? serviceKey = null,
|
2026-09-25 11:00:22 -03:00
|
|
|
[FromQuery] WorkOrderType? workOrderType = null,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2026-07-24 17:35:34 -03:00
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
var templates = await _completionDocTemplateService.ListAsync(search, serviceKey, workOrderType, cancellationToken);
|
2026-07-24 17:35:34 -03:00
|
|
|
return Ok(templates);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("completion-templates/{id:int}")]
|
2026-09-25 11:00:22 -03:00
|
|
|
public async Task<IActionResult> GetCompletionTemplate(int id, CancellationToken cancellationToken)
|
2026-07-24 17:35:34 -03:00
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
var template = await _completionDocTemplateService.GetAsync(id, cancellationToken);
|
2026-07-24 17:35:34 -03:00
|
|
|
if (template == null)
|
|
|
|
|
return NotFound(new Response { Status = "Error", Message = "Template not found." });
|
|
|
|
|
return Ok(template);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 11:00:22 -03:00
|
|
|
[HttpGet("completion-templates/{id:int}/linked-work-orders")]
|
|
|
|
|
public async Task<IActionResult> GetCompletionTemplateLinkedWorkOrders(int id, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
var linked = await _completionDocTemplateService.GetLinkedWorkOrdersAsync(User, id, cancellationToken);
|
|
|
|
|
return Ok(linked);
|
|
|
|
|
}
|
|
|
|
|
catch (WorkOrderBoardValidationException ex)
|
|
|
|
|
{
|
|
|
|
|
return MapTemplateError(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
[HttpPost("completion-templates")]
|
2026-09-25 11:00:22 -03:00
|
|
|
public async Task<IActionResult> CreateCompletionTemplate(
|
|
|
|
|
[FromBody] CompletionDocTemplateCreateDto request,
|
|
|
|
|
CancellationToken cancellationToken)
|
2026-07-24 17:35:34 -03:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
var created = await _completionDocTemplateService.CreateAsync(User, request, cancellationToken);
|
2026-07-24 17:35:34 -03:00
|
|
|
return Ok(created);
|
|
|
|
|
}
|
|
|
|
|
catch (WorkOrderBoardValidationException ex)
|
|
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
return MapTemplateError(ex);
|
2026-07-24 17:35:34 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPut("completion-templates/{id:int}")]
|
2026-09-25 11:00:22 -03:00
|
|
|
public async Task<IActionResult> UpdateCompletionTemplate(
|
|
|
|
|
int id,
|
|
|
|
|
[FromBody] CompletionDocTemplateCreateDto request,
|
|
|
|
|
CancellationToken cancellationToken)
|
2026-07-24 17:35:34 -03:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
var updated = await _completionDocTemplateService.UpdateAsync(User, id, request, cancellationToken);
|
2026-07-24 17:35:34 -03:00
|
|
|
return Ok(updated);
|
|
|
|
|
}
|
|
|
|
|
catch (WorkOrderBoardValidationException ex)
|
|
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
return MapTemplateError(ex);
|
2026-07-24 17:35:34 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpDelete("completion-templates/{id:int}")]
|
2026-09-25 11:00:22 -03:00
|
|
|
public async Task<IActionResult> DeleteCompletionTemplate(int id, CancellationToken cancellationToken)
|
2026-07-24 17:35:34 -03:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
await _completionDocTemplateService.DeleteAsync(User, id, cancellationToken);
|
2026-07-24 17:35:34 -03:00
|
|
|
return NoContent();
|
|
|
|
|
}
|
2026-09-25 11:00:22 -03:00
|
|
|
catch (WorkOrderBoardValidationException ex)
|
2026-07-24 17:35:34 -03:00
|
|
|
{
|
2026-09-25 11:00:22 -03:00
|
|
|
return MapTemplateError(ex);
|
2026-07-24 17:35:34 -03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 11:00:22 -03:00
|
|
|
private IActionResult MapTemplateError(WorkOrderBoardValidationException ex)
|
|
|
|
|
{
|
|
|
|
|
var body = new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message };
|
|
|
|
|
return ex.Code switch
|
|
|
|
|
{
|
|
|
|
|
"Forbidden" => StatusCode(StatusCodes.Status403Forbidden, body),
|
|
|
|
|
"NotFound" => NotFound(body),
|
|
|
|
|
_ => UnprocessableEntity(body)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
[HttpPost("{id:int}/completion-doc")]
|
2026-09-08 11:07:17 -03:00
|
|
|
[RequestSizeLimit(50_000_000)]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> UploadCompletionDoc(
|
|
|
|
|
int id,
|
|
|
|
|
[FromForm] IFormFile file,
|
|
|
|
|
[FromForm] string? signOffName = null,
|
|
|
|
|
[FromForm] string? signOffSignature = null,
|
|
|
|
|
[FromForm] string? workOrderVersion = null)
|
|
|
|
|
{
|
|
|
|
|
if (file == null || file.Length == 0)
|
|
|
|
|
return BadRequest(new Response { Status = "Error", Message = "file is required." });
|
|
|
|
|
|
2026-08-11 14:52:02 -03:00
|
|
|
string? fileUrl = null;
|
2026-07-24 17:35:34 -03:00
|
|
|
try
|
|
|
|
|
{
|
2026-09-08 21:24:10 -03:00
|
|
|
WorkOrderCompletionDocFileRules.EnsureAllowed(file);
|
2026-07-24 17:35:34 -03:00
|
|
|
var actorId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
2026-08-11 14:52:02 -03:00
|
|
|
await _workOrderCompletionService.EnsureCanUploadCompletionDocAsync(id, User, actorId);
|
|
|
|
|
|
|
|
|
|
fileUrl = await _fileStorage.SaveFileAsync(file);
|
2026-07-24 17:35:34 -03:00
|
|
|
var result = await _workOrderCompletionService.UploadCompletionDocAsync(
|
|
|
|
|
id,
|
|
|
|
|
new WorkOrderCompletionDocUploadDto
|
|
|
|
|
{
|
|
|
|
|
SignOffName = signOffName,
|
|
|
|
|
SignOffSignature = signOffSignature,
|
|
|
|
|
WorkOrderVersion = workOrderVersion
|
|
|
|
|
},
|
|
|
|
|
fileUrl,
|
2026-08-11 14:52:02 -03:00
|
|
|
User,
|
2026-07-24 17:35:34 -03:00
|
|
|
actorId);
|
|
|
|
|
return Ok(result);
|
|
|
|
|
}
|
|
|
|
|
catch (WorkOrderBoardValidationException ex) when (ex.Code is "NotFound")
|
|
|
|
|
{
|
2026-08-11 14:52:02 -03:00
|
|
|
TryCompensateUpload(fileUrl);
|
2026-07-24 17:35:34 -03:00
|
|
|
return NotFound(new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
|
|
|
}
|
2026-08-11 14:52:02 -03:00
|
|
|
catch (WorkOrderBoardValidationException ex) when (ex.Code is "Forbidden")
|
|
|
|
|
{
|
|
|
|
|
TryCompensateUpload(fileUrl);
|
|
|
|
|
return StatusCode(StatusCodes.Status403Forbidden,
|
|
|
|
|
new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
|
|
|
}
|
2026-07-24 17:35:34 -03:00
|
|
|
catch (WorkOrderBoardValidationException ex) when (ex.Code is "ConcurrencyConflict")
|
|
|
|
|
{
|
2026-08-11 14:52:02 -03:00
|
|
|
TryCompensateUpload(fileUrl);
|
2026-07-24 17:35:34 -03:00
|
|
|
return Conflict(new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
|
|
|
}
|
|
|
|
|
catch (WorkOrderBoardValidationException ex)
|
|
|
|
|
{
|
2026-08-11 14:52:02 -03:00
|
|
|
TryCompensateUpload(fileUrl);
|
2026-07-24 17:35:34 -03:00
|
|
|
return UnprocessableEntity(new WorkOrderBoardValidationErrorDto { Code = ex.Code, Message = ex.Message });
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-08-11 14:52:02 -03:00
|
|
|
|
|
|
|
|
private void TryCompensateUpload(string? fileUrl)
|
|
|
|
|
{
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(fileUrl))
|
|
|
|
|
_fileStorage.TryDelete(fileUrl);
|
|
|
|
|
}
|
2026-07-24 17:35:34 -03:00
|
|
|
}
|
|
|
|
|
}
|