shoc-backend/Api.SeaHavenIndustries/Controllers/FollowUpController.cs

264 lines
8.3 KiB
C#
Raw Normal View History

using System.Security.Claims;
using Api.SeaHavenIndustries.DTOs;
2026-01-21 15:41:19 -06:00
using Data.SeaHavenIndustries;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
2026-01-21 15:41:19 -06:00
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
[ApiController]
[Authorize]
2026-01-21 15:41:19 -06:00
[Route("api/FollowUp")]
[Route("api/followups")]
2026-01-21 15:41:19 -06:00
public class FollowUpController : ControllerBase
{
private readonly ApplicationDbContext _db;
private readonly UserManager<ApplicationUser> _users;
2026-01-21 15:41:19 -06:00
public FollowUpController(ApplicationDbContext db, UserManager<ApplicationUser> users)
2026-01-21 15:41:19 -06:00
{
_db = db;
_users = users;
2026-01-21 15:41:19 -06:00
}
[HttpGet("GetFollowUpList")]
public async Task<IActionResult> GetFollowUps(
2026-01-21 15:41:19 -06:00
string? search = "",
string? status = "Open",
2026-01-21 15:41:19 -06:00
int page = 1,
int pageSize = 12)
{
status = string.IsNullOrWhiteSpace(status) ? "Open" : status;
2026-01-21 15:41:19 -06:00
var query = _db.FollowUps
.Include(x => x.Employee)
.Include(x => x.Account)
.Include(x => x.Location)
.Include(x => x.WorkOrder)
.Where(x => x.Status == status);
if (!string.IsNullOrWhiteSpace(search))
{
var s = search.ToLower();
2026-01-21 15:41:19 -06:00
query = query.Where(x =>
(x.Reason ?? "").ToLower().Contains(s) ||
(x.Account != null && (x.Account.Name ?? "").ToLower().Contains(s)) ||
2026-01-21 15:41:19 -06:00
(x.Employee != null &&
(((x.Employee.FirstName ?? "") + " " + (x.Employee.LastName ?? ""))
.ToLower().Contains(s))
2026-01-21 15:41:19 -06:00
)
);
}
var totalCount = await query.CountAsync();
2026-01-21 15:41:19 -06:00
var rows = await query
2026-01-21 15:41:19 -06:00
.OrderByDescending(x => x.Id)
.Skip((page - 1) * pageSize)
.Take(pageSize)
.Select(x => new
{
x.Id,
x.Reason,
x.ScheduleDate,
x.ScheduleStartTime,
x.EmployeeId,
x.WorkOrderId,
x.AccountId,
x.LocationId,
AccountName = x.Account != null ? x.Account.Name : "",
2026-01-21 15:41:19 -06:00
AssignedTo = x.Employee != null
? (x.Employee.FirstName + " " + x.Employee.LastName)
: "",
LocationName = x.Location != null ? x.Location.Name : "",
WorkOrderNumber = x.WorkOrder != null ? x.WorkOrder.WorkerOrderNumber : "",
InternalWONumber = x.WorkOrder != null ? x.WorkOrder.InternalWONumber : "",
x.Status,
x.CreatedDate,
CreatedById = x.createdby
2026-01-21 15:41:19 -06:00
})
.ToListAsync();
var creatorIds = rows
.Select(r => r.CreatedById)
.Where(id => !string.IsNullOrEmpty(id))
.Distinct()
2026-01-21 15:41:19 -06:00
.ToList();
var creators = await _db.Users
.Where(u => creatorIds.Contains(u.Id))
.Select(u => new { u.Id, u.FirstName, u.LastName, u.UserName })
.ToDictionaryAsync(u => u.Id, u => u);
string NameFor(string? userId)
{
if (string.IsNullOrEmpty(userId) || !creators.TryGetValue(userId, out var u)) return "";
var full = ((u.FirstName ?? "") + " " + (u.LastName ?? "")).Trim();
return string.IsNullOrWhiteSpace(full) ? (u.UserName ?? "") : full;
}
var data = rows.Select(r => new
{
r.Id,
r.Reason,
r.ScheduleDate,
r.ScheduleStartTime,
r.EmployeeId,
r.WorkOrderId,
r.AccountId,
r.LocationId,
r.AccountName,
r.AssignedTo,
r.LocationName,
r.WorkOrderNumber,
r.InternalWONumber,
r.Status,
r.CreatedDate,
r.CreatedById,
CreatedByName = NameFor(r.CreatedById)
}).ToList();
2026-01-21 15:41:19 -06:00
return Ok(new Pagination_DTO
{
Data = data,
PageNumber = page,
PageSize = pageSize,
TotalCount = totalCount,
TotalPages = (int)Math.Ceiling(totalCount / (double)pageSize)
});
}
[HttpGet("GetById")]
public async Task<IActionResult> GetById(int id)
{
var model = await _db.FollowUps
.Include(x => x.Employee)
.Include(x => x.Account)
.Include(x => x.Location)
.Include(x => x.WorkOrder)
.FirstOrDefaultAsync(x => x.Id == id);
if (model == null) return NotFound();
string createdByName = "";
if (!string.IsNullOrEmpty(model.createdby))
{
var u = await _db.Users
.Where(x => x.Id == model.createdby)
.Select(x => new { x.FirstName, x.LastName, x.UserName })
.FirstOrDefaultAsync();
if (u != null)
{
var n = ((u.FirstName ?? "") + " " + (u.LastName ?? "")).Trim();
createdByName = string.IsNullOrWhiteSpace(n) ? (u.UserName ?? "") : n;
}
}
return Ok(new
{
model.Id,
model.Reason,
model.ScheduleDate,
model.ScheduleStartTime,
model.EmployeeId,
model.WorkOrderId,
model.AccountId,
model.LocationId,
AccountName = model.Account?.Name ?? "",
AssignedTo = model.Employee != null
? (model.Employee.FirstName + " " + model.Employee.LastName)
: "",
LocationName = model.Location?.Name ?? "",
WorkOrderNumber = model.WorkOrder?.WorkerOrderNumber ?? "",
InternalWONumber = model.WorkOrder?.InternalWONumber ?? "",
model.Status,
model.CreatedDate,
CreatedById = model.createdby,
CreatedByName = createdByName
});
}
2026-01-21 15:41:19 -06:00
[HttpPost("Create")]
public async Task<IActionResult> Create(FollowUp_DTO dto)
{
var userId = User.FindFirstValue(ClaimTypes.NameIdentifier);
2026-01-21 15:41:19 -06:00
var model = new FollowUps
{
Reason = dto.Reason,
ScheduleDate = dto.ScheduleDate,
ScheduleStartTime = dto.ScheduleStartTime,
EmployeeId = dto.EmployeeId,
WorkOrderId = dto.WorkOrderId,
AccountId = dto.AccountId,
LocationId = dto.LocationId,
Status = "Open",
CreatedDate = DateTime.UtcNow,
createdby = userId
2026-01-21 15:41:19 -06:00
};
_db.FollowUps.Add(model);
await _db.SaveChangesAsync();
return Ok(new DataResponse { Status = "200", Message = "Follow Up created", Data = model.Id });
2026-01-21 15:41:19 -06:00
}
[HttpPost("Update")]
public async Task<IActionResult> Update(FollowUp_DTO dto)
{
var model = await _db.FollowUps.FindAsync(dto.Id);
if (model == null) return NotFound();
model.Reason = dto.Reason;
model.ScheduleDate = dto.ScheduleDate;
model.ScheduleStartTime = dto.ScheduleStartTime;
model.EmployeeId = dto.EmployeeId;
model.WorkOrderId = dto.WorkOrderId;
model.AccountId = dto.AccountId;
model.LocationId = dto.LocationId;
model.LastModificationTime = DateTime.UtcNow;
await _db.SaveChangesAsync();
return Ok(new DataResponse { Status = "200", Message = "Follow Up updated" });
}
[HttpPost("Complete")]
public async Task<IActionResult> Complete(int id)
{
var model = await _db.FollowUps.FindAsync(id);
if (model == null) return NotFound();
model.Status = "Completed";
model.LastModificationTime = DateTime.UtcNow;
await _db.SaveChangesAsync();
return Ok();
}
[HttpPost("Cancel")]
public async Task<IActionResult> Cancel(int id)
{
var model = await _db.FollowUps.FindAsync(id);
if (model == null) return NotFound();
model.Status = "Cancelled";
model.LastModificationTime = DateTime.UtcNow;
await _db.SaveChangesAsync();
return Ok();
}
[HttpPost("Delete")]
public async Task<IActionResult> Delete(int id)
{
var model = await _db.FollowUps.FindAsync(id);
if (model == null) return NotFound();
_db.FollowUps.Remove(model);
await _db.SaveChangesAsync();
return Ok(new DataResponse { Status = "200", Message = "Follow Up deleted" });
}
2026-01-21 15:41:19 -06:00
}