mirror of
https://github.com/Sea-Haven-Industries/shoc-backend.git
synced 2026-10-07 16:19:07 +00:00
526 lines
20 KiB
C#
526 lines
20 KiB
C#
using Api.SeaHavenIndustries.DTOs;
|
|
using Data.SeaHavenIndustries;
|
|
using FluentValidation;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SeaHaven.Services.DTOs;
|
|
using SeaHaven.Services.Interfaces;
|
|
using SeaHaven.DataServices.Models;
|
|
using System.Security.Claims;
|
|
|
|
namespace Api.SeaHavenIndustries.Controllers
|
|
{
|
|
[Authorize]
|
|
[ApiController]
|
|
[Route("api/WorkOrder")]
|
|
[Route("api/workorders")]
|
|
public class WorkOrderController : Controller
|
|
{
|
|
private readonly IWorkOrderService _workOrderService;
|
|
private readonly IWorkOrderDispatchService _dispatchService;
|
|
|
|
public WorkOrderController(
|
|
IWorkOrderService workOrderService,
|
|
IWorkOrderDispatchService dispatchService)
|
|
{
|
|
_workOrderService = workOrderService;
|
|
_dispatchService = dispatchService;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("AddWorkorder")]
|
|
public async Task<IActionResult> Addworkorder([FromForm] Workorder_DTO model)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var input = new CreateWorkOrderWithDetailsInput
|
|
{
|
|
WorkerOrderTitle = model.WorkerOrderTitle ?? "",
|
|
Description = model.Description,
|
|
Priority = model.Priority?.ToString(),
|
|
LocationId = model.LocationId,
|
|
AssignTo = model.AssignTo,
|
|
DueDate = model.DueDate,
|
|
ContactIds = model.ContactIds,
|
|
CategoryIds = model.CategoryIds,
|
|
Attachments = model.Attachments,
|
|
BeforPhotoAttachment = model.BeforPhotoAttachment,
|
|
AfterPhotoAttachment = model.AfterPhotoAttachment,
|
|
SignOffAttachment = model.SignOffAttachment
|
|
};
|
|
|
|
var workOrderId = await _workOrderService.CreateWorkOrderWithDetailsAsync(input, userId!);
|
|
return Ok(new DataResponse { Message = "Work order created successfully", Status = "200", Data = workOrderId });
|
|
}
|
|
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 BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[HttpPut("{id:int}")]
|
|
[Route("EditWorkorder")]
|
|
public async Task<IActionResult> Editworkorder([FromForm] EditWorkorder_DTO model)
|
|
{
|
|
try
|
|
{
|
|
if (!model.Id.HasValue)
|
|
return BadRequest(new Response { Status = "Error", Message = "Work order ID is required" });
|
|
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var input = new UpdateWorkOrderWithDetailsInput
|
|
{
|
|
Id = model.Id.Value,
|
|
WorkerOrderTitle = model.WorkerOrderTitle,
|
|
Description = model.Description,
|
|
Status = model.Status?.ToString(),
|
|
Priority = model.Priority?.ToString(),
|
|
LocationId = model.LocationId,
|
|
AssignTo = model.AssignTo,
|
|
DueDate = model.DueDate,
|
|
CompletedDate = model.CompletedDate,
|
|
ContactIds = model.ContactIds,
|
|
CategoryIds = model.CategoryIds,
|
|
Attachments = model.Attachments,
|
|
BeforPhotoAttachment = model.BeforPhotoAttachment,
|
|
AfterPhotoAttachment = model.AfterPhotoAttachment,
|
|
SignOffAttachment = model.SignOffAttachment
|
|
};
|
|
|
|
var success = await _workOrderService.UpdateWorkOrderWithDetailsAsync(input, userId!);
|
|
if (!success)
|
|
return BadRequest(new Response { Status = "Error", Message = "Work order not found" });
|
|
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200" });
|
|
}
|
|
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 BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("GetWorkOrderList")]
|
|
public async Task<IActionResult> GetWorkOrderList(string? search = "", int page = 1, int pageSize = 12, string? sortBy = "lastUpdated", string? sortDir = "desc", string? status = "", string? assignee = "", int? locationId = null)
|
|
{
|
|
var (data, totalCount) = await _workOrderService.GetWorkOrderListPagedAsync(
|
|
page, pageSize, search, status, assignee, locationId, sortBy, sortDir);
|
|
|
|
var viewModel = new Pagination_DTO
|
|
{
|
|
Data = data,
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize)
|
|
};
|
|
|
|
return Ok(viewModel);
|
|
}
|
|
|
|
[HttpDelete]
|
|
[Route("DeleteWorkorder")]
|
|
public async Task<IActionResult> Deleteworkorder([FromBody] EditWorkorder_DTO model)
|
|
{
|
|
int id = model.Id.HasValue ? model.Id.Value : 0;
|
|
if (id == 0)
|
|
return BadRequest("Id is required");
|
|
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var success = await _workOrderService.DeleteWorkOrderCascadeAsync(id, userId!);
|
|
|
|
if (!success)
|
|
return BadRequest("Work order not found!");
|
|
|
|
return Ok(new DataResponse { Message = "Removed Successfully", Status = "200" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetFilteredWorkorder")]
|
|
public async Task<IActionResult> GetFilteredWorkorder(
|
|
[FromQuery] string[] assignto,
|
|
[FromQuery] int[]? location,
|
|
[FromQuery] string[]? priority,
|
|
[FromQuery] string[]? status,
|
|
[FromQuery] string[]? duedate,
|
|
[FromQuery] string search = "",
|
|
[FromQuery] string sort = "Sort Order",
|
|
[FromQuery] string sortby = "Sort By",
|
|
int page = 1,
|
|
int pageSize = 10)
|
|
{
|
|
try
|
|
{
|
|
var result = await _workOrderService.GetFilteredWorkOrdersAsync(
|
|
assignto, location, priority, status, duedate, search, sort, sortby, page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
return StatusCode(500, "Internal Server Error");
|
|
}
|
|
}
|
|
|
|
[HttpGet("{id:int}")]
|
|
[HttpGet("GetWorkorderById")]
|
|
public async Task<IActionResult> GetWorkorderById(int id)
|
|
{
|
|
var detail = await _workOrderService.GetWorkOrderDetailAsync(id);
|
|
if (detail == null)
|
|
return BadRequest(new Response { Status = "Error", Message = "ID not found!" });
|
|
return Ok(detail);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetFilteredWorkorder2")]
|
|
public async Task<IActionResult> GetFilteredWorkorder2(
|
|
[FromQuery] string[] assignto,
|
|
[FromQuery] int[]? location,
|
|
[FromQuery] string[]? priority,
|
|
[FromQuery] string[]? status,
|
|
[FromQuery] string[]? duedate,
|
|
[FromQuery] string search = "",
|
|
[FromQuery] string sort = "Sort Order",
|
|
[FromQuery] string sortby = "Sort By",
|
|
int page = 1,
|
|
int pageSize = 10)
|
|
{
|
|
try
|
|
{
|
|
var result = await _workOrderService.GetFilteredWorkOrders2Async(
|
|
assignto, location, priority, status, duedate, search, sort, sortby, page, pageSize);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception)
|
|
{
|
|
var totalCount = await _workOrderService.GetTotalWorkOrderCountAsync();
|
|
return Ok(new Pagination_DTO
|
|
{
|
|
Data = Array.Empty<FilteredWorkOrderReadModel>(),
|
|
PageNumber = page,
|
|
PageSize = pageSize,
|
|
TotalCount = totalCount,
|
|
TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize)
|
|
});
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("ChangeStatus")]
|
|
public async Task<IActionResult> ChangeStatus(int id, string status)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var result = await _workOrderService.ChangeStatusAsync(id, status, userId!);
|
|
if (result == null)
|
|
return BadRequest(new Response { Status = "Error", Message = "Work order not found" });
|
|
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200", Data = result });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("ChangeAssignment")]
|
|
public async Task<IActionResult> ChangeAssignment(int id, string? assignTo)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var result = await _workOrderService.ChangeAssignmentAsync(id, assignTo, userId!);
|
|
if (result == null)
|
|
return BadRequest(new Response { Status = "Error", Message = "Work order not found" });
|
|
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("AddComment")]
|
|
public async Task<IActionResult> AddComment([FromForm] Comments_DTO model)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var input = new AddCommentInput
|
|
{
|
|
WorkorderId = model.WorkorderId,
|
|
Text = model.Text,
|
|
CommentType = model.CommentType,
|
|
Document = model.Document
|
|
};
|
|
|
|
var saved = await _workOrderService.AddCommentAsync(input, userId!);
|
|
return Ok(new DataResponse { Message = "Updated Successfully", Status = "200", Data = saved });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("AddCommentJson")]
|
|
public async Task<IActionResult> AddCommentJson([FromBody] Comments_DTO model)
|
|
{
|
|
try
|
|
{
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
var input = new AddCommentInput
|
|
{
|
|
WorkorderId = model.WorkorderId,
|
|
Text = model.Text,
|
|
CommentType = model.CommentType
|
|
};
|
|
|
|
var result = await _workOrderService.AddCommentJsonAsync(input, userId!);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetComments")]
|
|
public async Task<IActionResult> GetComments()
|
|
{
|
|
var data = await _workOrderService.GetCommentsAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetCommentsByWorkorderId")]
|
|
public async Task<IActionResult> GetCommentsByWorkorderId(int woid)
|
|
{
|
|
var data = await _workOrderService.GetCommentsByWorkorderIdAsync(woid);
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("GetworkordersDD")]
|
|
public async Task<IActionResult> GetworkordersDD()
|
|
{
|
|
var data = await _workOrderService.GetWorkordersDDAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("Getworkorders")]
|
|
public async Task<IActionResult> Getworkorders()
|
|
{
|
|
var data = await _workOrderService.GetWorkordersAsync();
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("DispatchToVendor")]
|
|
public async Task<IActionResult> DispatchToVendor([FromBody] Dispatch_DTO model)
|
|
{
|
|
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);
|
|
return Ok(result);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpGet("GetDispatches/{workOrderId}")]
|
|
public async Task<IActionResult> GetDispatches(int workOrderId)
|
|
{
|
|
var data = await _dispatchService.GetDispatchesAsync(workOrderId);
|
|
return Ok(data);
|
|
}
|
|
|
|
[HttpGet("GetDispatch/{id}")]
|
|
public async Task<IActionResult> 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<IActionResult> 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 = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost("AddDispatchComment")]
|
|
public async Task<IActionResult> 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 = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost("VerifyDispatch")]
|
|
public async Task<IActionResult> 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 = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost("AddDispatchSignoff")]
|
|
public async Task<IActionResult> 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 = ex.Message });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(new Response { Status = "Error", Message = ex.Message });
|
|
}
|
|
}
|
|
|
|
[HttpPost("UpdateChecklistItem")]
|
|
public async Task<IActionResult> 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<IActionResult> 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);
|
|
}
|
|
}
|
|
}
|