2026-04-20 13:49:45 -04:00
|
|
|
using System.Security.Claims;
|
|
|
|
|
using Api.SeaHavenIndustries.DTOs;
|
2026-07-24 17:35:34 -03:00
|
|
|
using Api.SeaHavenIndustries.Helper;
|
2026-01-21 15:41:19 -06:00
|
|
|
using Data.SeaHavenIndustries;
|
2026-05-14 11:00:12 -05:00
|
|
|
using FluentValidation;
|
2026-04-20 13:49:45 -04:00
|
|
|
using Microsoft.AspNetCore.Authorization;
|
2026-01-21 15:41:19 -06:00
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2026-07-24 17:35:34 -03:00
|
|
|
using Microsoft.Extensions.Logging;
|
2026-05-06 10:49:33 -05:00
|
|
|
using SeaHaven.Services.DTOs;
|
2026-07-24 17:35:34 -03:00
|
|
|
using SeaHaven.Services.Interfaces;
|
2026-01-21 15:41:19 -06:00
|
|
|
|
|
|
|
|
[ApiController]
|
2026-04-20 13:49:45 -04:00
|
|
|
[Authorize]
|
2026-01-21 15:41:19 -06:00
|
|
|
[Route("api/FollowUp")]
|
2026-04-16 17:32:28 -04:00
|
|
|
[Route("api/followups")]
|
2026-01-21 15:41:19 -06:00
|
|
|
public class FollowUpController : ControllerBase
|
|
|
|
|
{
|
2026-05-06 10:49:33 -05:00
|
|
|
private readonly IFollowUpService _followUpService;
|
2026-07-24 17:35:34 -03:00
|
|
|
private readonly ILogger<FollowUpController> _logger;
|
2026-01-21 15:41:19 -06:00
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
public FollowUpController(IFollowUpService followUpService, ILogger<FollowUpController> logger)
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-05-06 10:49:33 -05:00
|
|
|
_followUpService = followUpService;
|
2026-07-24 17:35:34 -03:00
|
|
|
_logger = logger;
|
2026-01-21 15:41:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpGet("GetFollowUpList")]
|
2026-04-20 13:49:45 -04:00
|
|
|
public async Task<IActionResult> GetFollowUps(
|
2026-01-21 15:41:19 -06:00
|
|
|
string? search = "",
|
2026-04-20 13:49:45 -04:00
|
|
|
string? status = "Open",
|
2026-01-21 15:41:19 -06:00
|
|
|
int page = 1,
|
2026-07-24 17:35:34 -03:00
|
|
|
int pageSize = 12,
|
|
|
|
|
CancellationToken cancellationToken = default)
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var pagedResult = await _followUpService.GetFollowUpListAsync(page, pageSize, search, status, cancellationToken);
|
2026-05-06 10:49:33 -05:00
|
|
|
var items = pagedResult.Items;
|
2026-01-21 15:41:19 -06:00
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
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
|
2026-04-20 13:49:45 -04:00
|
|
|
}).ToList();
|
|
|
|
|
|
2026-01-21 15:41:19 -06:00
|
|
|
return Ok(new Pagination_DTO
|
|
|
|
|
{
|
|
|
|
|
Data = data,
|
|
|
|
|
PageNumber = page,
|
|
|
|
|
PageSize = pageSize,
|
2026-05-06 10:49:33 -05:00
|
|
|
TotalCount = pagedResult.TotalCount,
|
|
|
|
|
TotalPages = (int)Math.Ceiling(pagedResult.TotalCount / (double)pageSize)
|
2026-01-21 15:41:19 -06:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-20 13:49:45 -04:00
|
|
|
[HttpGet("GetById")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> GetById(int id, CancellationToken cancellationToken)
|
2026-04-20 13:49:45 -04:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var model = await _followUpService.GetFollowUpDetailAsync(id, cancellationToken);
|
2026-04-20 13:49:45 -04:00
|
|
|
|
|
|
|
|
if (model == null) return NotFound();
|
|
|
|
|
|
|
|
|
|
return Ok(new
|
|
|
|
|
{
|
|
|
|
|
model.Id,
|
|
|
|
|
model.Reason,
|
|
|
|
|
model.ScheduleDate,
|
|
|
|
|
model.ScheduleStartTime,
|
|
|
|
|
model.EmployeeId,
|
|
|
|
|
model.WorkOrderId,
|
|
|
|
|
model.AccountId,
|
|
|
|
|
model.LocationId,
|
2026-07-24 17:35:34 -03:00
|
|
|
model.AccountName,
|
|
|
|
|
model.AssignedTo,
|
|
|
|
|
model.LocationName,
|
|
|
|
|
model.WorkOrderNumber,
|
|
|
|
|
model.InternalWONumber,
|
2026-04-20 13:49:45 -04:00
|
|
|
model.Status,
|
|
|
|
|
model.CreatedDate,
|
2026-07-24 17:35:34 -03:00
|
|
|
model.CreatedById,
|
|
|
|
|
model.CreatedByName
|
2026-04-20 13:49:45 -04:00
|
|
|
});
|
|
|
|
|
}
|
2026-01-21 15:41:19 -06:00
|
|
|
|
|
|
|
|
[HttpPost("Create")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Create(FollowUp_DTO dto, CancellationToken cancellationToken)
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-05-14 11:00:12 -05:00
|
|
|
try
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-05-14 11:00:12 -05:00
|
|
|
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
|
|
|
|
|
if (userId == null)
|
|
|
|
|
return Unauthorized(new Response { Status = "Error", Message = "User not authenticated" });
|
|
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
var id = await _followUpService.CreateOpenFollowUpAsync(MapToCreateRequest(dto), userId, cancellationToken);
|
2026-01-21 15:41:19 -06:00
|
|
|
|
2026-07-24 17:35:34 -03:00
|
|
|
return Ok(new DataResponse { Status = "200", Message = "Follow Up created", Data = id });
|
2026-05-14 11:00:12 -05:00
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
return StatusCode(500, new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2026-05-14 11:00:12 -05:00
|
|
|
}
|
2026-01-21 15:41:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("Update")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Update(FollowUp_DTO dto, CancellationToken cancellationToken)
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-05-14 11:00:12 -05:00
|
|
|
try
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
await _followUpService.UpdateFollowUpFieldsAsync(dto.Id, MapToUpdateRequest(dto), cancellationToken);
|
2026-05-14 11:00:12 -05:00
|
|
|
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)
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
return StatusCode(500, new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2026-05-14 11:00:12 -05:00
|
|
|
}
|
2026-01-21 15:41:19 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("Complete")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Complete(int id, CancellationToken cancellationToken)
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var found = await _followUpService.SetFollowUpStatusAsync(id, "Completed", cancellationToken);
|
|
|
|
|
if (!found) return NotFound();
|
2026-01-21 15:41:19 -06:00
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[HttpPost("Cancel")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Cancel(int id, CancellationToken cancellationToken)
|
2026-01-21 15:41:19 -06:00
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var found = await _followUpService.SetFollowUpStatusAsync(id, "Cancelled", cancellationToken);
|
|
|
|
|
if (!found) return NotFound();
|
2026-01-21 15:41:19 -06:00
|
|
|
|
|
|
|
|
return Ok();
|
|
|
|
|
}
|
2026-04-20 13:49:45 -04:00
|
|
|
|
|
|
|
|
[HttpPost("Delete")]
|
2026-07-24 17:35:34 -03:00
|
|
|
public async Task<IActionResult> Delete(int id, CancellationToken cancellationToken)
|
2026-04-20 13:49:45 -04:00
|
|
|
{
|
2026-05-14 11:00:12 -05:00
|
|
|
try
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
var found = await _followUpService.RemoveFollowUpAsync(id, cancellationToken);
|
|
|
|
|
if (!found)
|
2026-05-14 11:00:12 -05:00
|
|
|
return NotFound(new Response { Status = "Error", Message = "Follow Up not found" });
|
2026-04-20 13:49:45 -04:00
|
|
|
|
2026-05-14 11:00:12 -05:00
|
|
|
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)
|
|
|
|
|
{
|
2026-07-24 17:35:34 -03:00
|
|
|
return StatusCode(500, new Response { Status = "Error", Message = _logger.Sanitize(ex) });
|
2026-05-14 11:00:12 -05:00
|
|
|
}
|
2026-04-20 13:49:45 -04:00
|
|
|
}
|
2026-07-24 17:35:34 -03:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
};
|
|
|
|
|
}
|
2026-01-21 15:41:19 -06:00
|
|
|
}
|