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.Exceptions; using SeaHaven.Services.Interfaces; using System.Security.Claims; namespace Api.SeaHavenIndustries.Controllers { [Authorize] [ApiController] [Route("api/WorkOrder")] [Route("api/workorders")] public class WorkOrderDispatchController : Controller { private readonly IWorkOrderDispatchService _dispatchService; private readonly ILogger _logger; public WorkOrderDispatchController( IWorkOrderDispatchService dispatchService, ILogger logger) { _dispatchService = dispatchService; _logger = logger; } [HttpPost] [Route("DispatchToVendor")] public async Task DispatchToVendor([FromBody] Dispatch_DTO model, CancellationToken cancellationToken) { try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var input = new DispatchToVendorInput { WorkOrderIds = model.WorkOrderIds, VendorIds = model.VendorIds, NTEAmount = model.NTEAmount, Description = model.Description, ScheduledDate = model.ScheduledDate, TaskListTemplateId = model.TaskListTemplateId, CustomChecklistItems = model.CustomChecklistItems, UserId = userId }; var result = await _dispatchService.DispatchToVendorAsync(input, cancellationToken); return Ok(result); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpGet("GetDispatches/{workOrderId}")] public async Task GetDispatches(int workOrderId) { var data = await _dispatchService.GetDispatchesAsync(workOrderId); return Ok(data); } [HttpGet("GetDispatch/{id}")] public async Task GetDispatchById(int id) { var d = await _dispatchService.GetDispatchDetailAsync(id); if (d == null) return NotFound(new Response { Status = "Error", Message = "Dispatch not found" }); return Ok(d); } [HttpPost("UpdateDispatch")] public async Task UpdateDispatch([FromBody] UpdateDispatch_DTO model) { try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var input = new UpdateDispatchInput { Id = model.Id, Status = model.Status, NTEAmount = model.NTEAmount, ScheduledDate = model.ScheduledDate, CompletedDate = model.CompletedDate, Description = model.Description, UserId = userId }; var success = await _dispatchService.UpdateDispatchAsync(input); if (!success) return BadRequest(new Response { Status = "Error", Message = "Dispatch not found" }); return Ok(new { message = "Dispatch updated" }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpPost("AddDispatchComment")] public async Task AddDispatchComment([FromBody] DispatchComment_DTO model) { try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var input = new AddDispatchCommentInput { DispatchId = model.DispatchId, Text = model.Text, SendEmail = model.SendEmail, UserId = userId }; var result = await _dispatchService.AddDispatchCommentAsync(input); if (result == null) return BadRequest(new Response { Status = "Error", Message = "Dispatch not found" }); return Ok(result); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpPost("VerifyDispatch")] public async Task VerifyDispatch(int dispatchId) { try { var userId = User.FindFirstValue(ClaimTypes.NameIdentifier); var result = await _dispatchService.VerifyDispatchAsync(dispatchId, userId!); if (result == null) return BadRequest(new Response { Status = "Error", Message = "Dispatch not found" }); return Ok(result); } catch (DispatchVerificationException ex) { return BadRequest(new { status = "Error", message = "Cannot verify", missing = ex.Missing }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpPost("AddDispatchSignoff")] public async Task AddDispatchSignoff([FromBody] DispatchSignoff_DTO model) { try { var input = new AddDispatchSignoffInput { DispatchId = model.DispatchId, SignoffType = model.SignoffType, Name = model.Name, Signature = model.Signature, SignatureMethod = model.SignatureMethod }; var result = await _dispatchService.AddDispatchSignoffAsync(input); if (result == null) return BadRequest(new Response { Status = "Error", Message = "Dispatch not found" }); return Ok(result); } catch (InvalidOperationException ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex, "The dispatch signoff could not be added") }); } catch (Exception ex) { return BadRequest(new Response { Status = "Error", Message = _logger.Sanitize(ex) }); } } [HttpPost("UpdateChecklistItem")] public async Task UpdateChecklistItem([FromBody] ChecklistItemUpdate_DTO model) { var input = new ChecklistItemUpdateInput { Id = model.Id, IsCompleted = model.IsCompleted, CompletedBy = model.CompletedBy }; var result = await _dispatchService.UpdateChecklistItemAsync(input); if (result == null) return NotFound(new Response { Status = "Error", Message = "Checklist item not found" }); return Ok(result); } [HttpPost("AddChecklistItem")] public async Task AddChecklistItem([FromBody] AddChecklistItem_DTO model) { var input = new AddChecklistItemInput { DispatchId = model.DispatchId, WorkOrderId = model.WorkOrderId, ItemText = model.ItemText }; var result = await _dispatchService.AddChecklistItemAsync(input); return Ok(result); } } }