1503 lines
74 KiB
C#
1503 lines
74 KiB
C#
using AutoMapper;
|
|
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.Activities;
|
|
using Marco.Pms.Model.Dtos.Project;
|
|
using Marco.Pms.Model.Employees;
|
|
using Marco.Pms.Model.Entitlements;
|
|
using Marco.Pms.Model.Mapper;
|
|
using Marco.Pms.Model.MongoDBModels;
|
|
using Marco.Pms.Model.Projects;
|
|
using Marco.Pms.Model.Utilities;
|
|
using Marco.Pms.Model.ViewModels.Employee;
|
|
using Marco.Pms.Model.ViewModels.Projects;
|
|
using Marco.Pms.Services.Helpers;
|
|
using Marco.Pms.Services.Hubs;
|
|
using Marco.Pms.Services.Service;
|
|
using MarcoBMS.Services.Helpers;
|
|
using MarcoBMS.Services.Service;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.SignalR;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MongoDB.Driver;
|
|
|
|
namespace MarcoBMS.Services.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize]
|
|
public class ProjectController : ControllerBase
|
|
{
|
|
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserHelper _userHelper;
|
|
private readonly ILoggingService _logger;
|
|
private readonly ProjectsHelper _projectsHelper;
|
|
private readonly IHubContext<MarcoHub> _signalR;
|
|
private readonly PermissionServices _permission;
|
|
private readonly CacheUpdateHelper _cache;
|
|
private readonly IMapper _mapper;
|
|
private readonly Guid tenantId;
|
|
|
|
|
|
public ProjectController(IDbContextFactory<ApplicationDbContext> dbContextFactory, ApplicationDbContext context, UserHelper userHelper, ILoggingService logger,
|
|
ProjectsHelper projectHelper, IHubContext<MarcoHub> signalR, CacheUpdateHelper cache, PermissionServices permission, IMapper mapper)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
_context = context;
|
|
_userHelper = userHelper;
|
|
_logger = logger;
|
|
_projectsHelper = projectHelper;
|
|
_signalR = signalR;
|
|
_cache = cache;
|
|
_permission = permission;
|
|
_mapper = mapper;
|
|
tenantId = userHelper.GetTenantId();
|
|
}
|
|
|
|
#region =================================================================== Project Get APIs ===================================================================
|
|
|
|
[HttpGet("list/basic")]
|
|
public async Task<IActionResult> GetAllProjectsBasic()
|
|
{
|
|
// Step 1: Get the current user
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
if (loggedInEmployee == null)
|
|
{
|
|
return Unauthorized(ApiResponse<object>.ErrorResponse("Unauthorized", "User could not be identified.", 401));
|
|
}
|
|
|
|
_logger.LogInfo("Basic project list requested by EmployeeId {EmployeeId}", loggedInEmployee.Id);
|
|
|
|
// Step 2: Get the list of project IDs the user has access to
|
|
Guid tenantId = _userHelper.GetTenantId(); // Assuming this is still needed by the helper
|
|
List<Guid> accessibleProjectIds = await _projectsHelper.GetMyProjects(tenantId, loggedInEmployee);
|
|
|
|
if (accessibleProjectIds == null || !accessibleProjectIds.Any())
|
|
{
|
|
_logger.LogInfo("No accessible projects found for EmployeeId {EmployeeId}", loggedInEmployee.Id);
|
|
return Ok(ApiResponse<List<ProjectInfoVM>>.SuccessResponse(new List<ProjectInfoVM>(), "Success.", 200));
|
|
}
|
|
|
|
// Step 3: Fetch project ViewModels using the optimized, cache-aware helper
|
|
var projectVMs = await GetProjectInfosByIdsAsync(accessibleProjectIds);
|
|
|
|
// Step 4: Return the final list
|
|
_logger.LogInfo("Successfully returned {ProjectCount} projects for EmployeeId {EmployeeId}", projectVMs.Count, loggedInEmployee.Id);
|
|
return Ok(ApiResponse<List<ProjectInfoVM>>.SuccessResponse(projectVMs, $"{projectVMs.Count} records of project fetchd successfully", 200));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a list of projects accessible to the current user, including aggregated details.
|
|
/// This method is optimized to use a cache-first approach. If data is not in the cache,
|
|
/// it fetches and aggregates data efficiently from the database in parallel.
|
|
/// </summary>
|
|
/// <returns>An ApiResponse containing a list of projects or an error.</returns>
|
|
|
|
[HttpGet("list")]
|
|
public async Task<IActionResult> GetAllProjects()
|
|
{
|
|
// --- Step 1: Input Validation and Initial Setup ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
_logger.LogWarning("GetAllProjects called with invalid model state. Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
try
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
_logger.LogInfo("Starting GetAllProjects for TenantId: {TenantId}, User: {UserId}", tenantId, loggedInEmployee.Id);
|
|
|
|
// --- Step 2: Get a list of project IDs the user can access ---
|
|
List<Guid> projectIds = await _projectsHelper.GetMyProjects(tenantId, loggedInEmployee);
|
|
if (!projectIds.Any())
|
|
{
|
|
_logger.LogInfo("User has no assigned projects. Returning empty list.");
|
|
return Ok(ApiResponse<List<ProjectListVM>>.SuccessResponse(new List<ProjectListVM>(), "No projects found for the current user.", 200));
|
|
}
|
|
|
|
// --- Step 3: Efficiently handle partial cache hits ---
|
|
_logger.LogInfo("Attempting to fetch details for {ProjectCount} projects from cache.", projectIds.Count);
|
|
|
|
// Fetch what we can from the cache.
|
|
var cachedDetails = await _cache.GetProjectDetailsList(projectIds) ?? new List<ProjectMongoDB>();
|
|
var cachedDictionary = cachedDetails.ToDictionary(p => Guid.Parse(p.Id));
|
|
|
|
// Identify which projects are missing from the cache.
|
|
var missingIds = projectIds.Where(id => !cachedDictionary.ContainsKey(id)).ToList();
|
|
|
|
// Start building the response with the items we found in the cache.
|
|
var responseVms = _mapper.Map<List<ProjectListVM>>(cachedDictionary.Values);
|
|
|
|
if (missingIds.Any())
|
|
{
|
|
// --- Step 4: Fetch ONLY the missing items from the database ---
|
|
_logger.LogInfo("Cache partial MISS. Found {CachedCount}, fetching {MissingCount} projects from DB.",
|
|
cachedDictionary.Count, missingIds.Count);
|
|
|
|
// Call our dedicated data-fetching method for the missing IDs.
|
|
var newMongoDetails = await FetchAndBuildProjectDetails(missingIds, tenantId);
|
|
|
|
if (newMongoDetails.Any())
|
|
{
|
|
// Map the newly fetched items and add them to our response list.
|
|
responseVms.AddRange(newMongoDetails);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_logger.LogInfo("Cache HIT. All {ProjectCount} projects found in cache.", projectIds.Count);
|
|
}
|
|
|
|
// --- Step 5: Return the combined result ---
|
|
_logger.LogInfo("Successfully retrieved a total of {ProjectCount} projects.", responseVms.Count);
|
|
return Ok(ApiResponse<List<ProjectListVM>>.SuccessResponse(responseVms, "Projects retrieved successfully.", 200));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// --- Step 6: Graceful Error Handling ---
|
|
_logger.LogError("An unexpected error occurred in GetAllProjects for tenant {TenantId}. \n {Error}", tenantId, ex.Message);
|
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("An internal server error occurred. Please try again later.", null, 500));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves details for a specific project by its ID.
|
|
/// This endpoint is optimized with a cache-first strategy and parallel permission checks.
|
|
/// </summary>
|
|
/// <param name="id">The unique identifier of the project.</param>
|
|
/// <returns>An ApiResponse containing the project details or an appropriate error.</returns>
|
|
|
|
[HttpGet("get/{id}")]
|
|
public async Task<IActionResult> Get([FromRoute] Guid id)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get project called with invalid model state for ID {ProjectId}. Errors: {Errors}", id, string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
try
|
|
{
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
// --- Step 2: Run independent operations in PARALLEL ---
|
|
// We can check permissions and fetch data at the same time to reduce latency.
|
|
var permissionTask = _permission.HasProjectPermission(loggedInEmployee, id);
|
|
|
|
// This helper method encapsulates the "cache-first, then database" logic.
|
|
var projectDataTask = GetProjectDataAsync(id);
|
|
|
|
// Await both tasks to complete.
|
|
await Task.WhenAll(permissionTask, projectDataTask);
|
|
|
|
var hasPermission = await permissionTask;
|
|
var projectVm = await projectDataTask;
|
|
|
|
// --- Step 3: Process results sequentially ---
|
|
|
|
// 3a. Check for permission first. Forbid() is the idiomatic way to return 403.
|
|
if (!hasPermission)
|
|
{
|
|
_logger.LogWarning("Access denied for user {UserId} on project {ProjectId}.", loggedInEmployee.Id, id);
|
|
return StatusCode(403, (ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to access this project.", 403)));
|
|
}
|
|
|
|
// 3b. Check if the project was found (either in cache or DB).
|
|
if (projectVm == null)
|
|
{
|
|
_logger.LogInfo("Project with ID {ProjectId} not found.", id);
|
|
return NotFound(ApiResponse<object>.ErrorResponse("Project not found.", $"No project found with ID {id}.", 404));
|
|
}
|
|
|
|
// 3c. Success. Return the consistent ViewModel.
|
|
_logger.LogInfo("Successfully retrieved project {ProjectId}.", id);
|
|
return Ok(ApiResponse<Project>.SuccessResponse(projectVm, "Project retrieved successfully.", 200));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogError("An unexpected error occurred while getting project {ProjectId} : \n {Error}", id, ex.Message);
|
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("An internal server error occurred.", null, 500));
|
|
}
|
|
}
|
|
|
|
|
|
[HttpGet("details/{id}")]
|
|
public async Task<IActionResult> Details([FromRoute] Guid id)
|
|
{
|
|
// Step 1: Validate model state
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
|
|
_logger.LogWarning("Invalid model state in Details endpoint. Errors: {@Errors}", errors);
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
}
|
|
|
|
// Step 2: Get logged-in employee
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
_logger.LogInfo("Details requested by EmployeeId: {EmployeeId} for ProjectId: {ProjectId}", loggedInEmployee.Id, id);
|
|
|
|
// Step 3: Check global view project permission
|
|
var hasViewProjectPermission = await _permission.HasPermission(PermissionsMaster.ViewProject, loggedInEmployee.Id);
|
|
if (!hasViewProjectPermission)
|
|
{
|
|
_logger.LogWarning("ViewProjects permission denied for EmployeeId: {EmployeeId}", loggedInEmployee.Id);
|
|
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access denied", "You don't have permission to view projects", 403));
|
|
}
|
|
|
|
// Step 4: Check permission for this specific project
|
|
var hasProjectPermission = await _permission.HasProjectPermission(loggedInEmployee, id);
|
|
if (!hasProjectPermission)
|
|
{
|
|
_logger.LogWarning("Project-specific access denied. EmployeeId: {EmployeeId}, ProjectId: {ProjectId}", loggedInEmployee.Id, id);
|
|
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access denied", "You don't have access to this project", 403));
|
|
}
|
|
|
|
// Step 5: Fetch project with status
|
|
var projectDetails = await _cache.GetProjectDetails(id);
|
|
ProjectVM? projectVM = null;
|
|
if (projectDetails == null)
|
|
{
|
|
var project = await _context.Projects
|
|
.Include(c => c.ProjectStatus)
|
|
.FirstOrDefaultAsync(c => c.TenantId == tenantId && c.Id == id);
|
|
|
|
projectVM = _mapper.Map<ProjectVM>(project);
|
|
|
|
if (project != null)
|
|
{
|
|
await _cache.AddProjectDetails(project);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
projectVM = _mapper.Map<ProjectVM>(projectDetails);
|
|
if (projectVM.ProjectStatus != null)
|
|
{
|
|
projectVM.ProjectStatus.TenantId = tenantId;
|
|
}
|
|
}
|
|
|
|
if (projectVM == null)
|
|
{
|
|
_logger.LogWarning("Project not found. ProjectId: {ProjectId}", id);
|
|
return NotFound(ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404));
|
|
}
|
|
|
|
// Step 6: Return result
|
|
|
|
_logger.LogInfo("Project details fetched successfully. ProjectId: {ProjectId}", id);
|
|
return Ok(ApiResponse<object>.SuccessResponse(projectVM, "Project details fetched successfully", 200));
|
|
}
|
|
|
|
[HttpGet("details-old/{id}")]
|
|
public async Task<IActionResult> DetailsOld([FromRoute] Guid id)
|
|
{
|
|
// ProjectDetailsVM vm = new ProjectDetailsVM();
|
|
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
|
|
}
|
|
|
|
var project = await _context.Projects.Where(c => c.TenantId == _userHelper.GetTenantId() && c.Id == id).Include(c => c.ProjectStatus).SingleOrDefaultAsync(); // includeProperties: "ProjectStatus,Tenant"); //_context.Stock.FindAsync(id);
|
|
|
|
if (project == null)
|
|
{
|
|
return NotFound(ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404));
|
|
|
|
}
|
|
else
|
|
{
|
|
//var project = projects.Where(c => c.Id == id).SingleOrDefault();
|
|
ProjectDetailsVM vm = await GetProjectViewModel(id, project);
|
|
|
|
OldProjectVM projectVM = new OldProjectVM();
|
|
if (vm.project != null)
|
|
{
|
|
projectVM.Id = vm.project.Id;
|
|
projectVM.Name = vm.project.Name;
|
|
projectVM.ShortName = vm.project.ShortName;
|
|
projectVM.ProjectAddress = vm.project.ProjectAddress;
|
|
projectVM.ContactPerson = vm.project.ContactPerson;
|
|
projectVM.StartDate = vm.project.StartDate;
|
|
projectVM.EndDate = vm.project.EndDate;
|
|
projectVM.ProjectStatusId = vm.project.ProjectStatusId;
|
|
}
|
|
projectVM.Buildings = new List<BuildingVM>();
|
|
if (vm.buildings != null)
|
|
{
|
|
foreach (Building build in vm.buildings)
|
|
{
|
|
BuildingVM buildVM = new BuildingVM() { Id = build.Id, Description = build.Description, Name = build.Name };
|
|
buildVM.Floors = new List<FloorsVM>();
|
|
if (vm.floors != null)
|
|
{
|
|
foreach (Floor floorDto in vm.floors.Where(c => c.BuildingId == build.Id).ToList())
|
|
{
|
|
FloorsVM floorVM = new FloorsVM() { FloorName = floorDto.FloorName, Id = floorDto.Id };
|
|
floorVM.WorkAreas = new List<WorkAreaVM>();
|
|
|
|
if (vm.workAreas != null)
|
|
{
|
|
foreach (WorkArea workAreaDto in vm.workAreas.Where(c => c.FloorId == floorVM.Id).ToList())
|
|
{
|
|
WorkAreaVM workAreaVM = new WorkAreaVM() { Id = workAreaDto.Id, AreaName = workAreaDto.AreaName, WorkItems = new List<WorkItemVM>() };
|
|
|
|
if (vm.workItems != null)
|
|
{
|
|
foreach (WorkItem workItemDto in vm.workItems.Where(c => c.WorkAreaId == workAreaDto.Id).ToList())
|
|
{
|
|
WorkItemVM workItemVM = new WorkItemVM() { WorkItemId = workItemDto.Id, WorkItem = workItemDto };
|
|
|
|
workItemVM.WorkItem.WorkArea = new WorkArea();
|
|
|
|
if (workItemVM.WorkItem.ActivityMaster != null)
|
|
{
|
|
workItemVM.WorkItem.ActivityMaster.Tenant = new Tenant();
|
|
}
|
|
workItemVM.WorkItem.Tenant = new Tenant();
|
|
|
|
double todaysAssigned = 0;
|
|
if (vm.Tasks != null)
|
|
{
|
|
var tasks = vm.Tasks.Where(t => t.WorkItemId == workItemDto.Id).ToList();
|
|
foreach (TaskAllocation task in tasks)
|
|
{
|
|
todaysAssigned += task.PlannedTask;
|
|
}
|
|
}
|
|
workItemVM.TodaysAssigned = todaysAssigned;
|
|
|
|
workAreaVM.WorkItems.Add(workItemVM);
|
|
}
|
|
}
|
|
|
|
floorVM.WorkAreas.Add(workAreaVM);
|
|
}
|
|
}
|
|
|
|
buildVM.Floors.Add(floorVM);
|
|
}
|
|
}
|
|
projectVM.Buildings.Add(buildVM);
|
|
}
|
|
}
|
|
return Ok(ApiResponse<object>.SuccessResponse(projectVM, "Success.", 200));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project Manage APIs ===================================================================
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> Create([FromBody] CreateProjectDto projectDto)
|
|
{
|
|
// 1. Validate input first (early exit)
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
}
|
|
|
|
// 2. Prepare data without I/O
|
|
Guid tenantId = _userHelper.GetTenantId(); // Assuming this is fast and from claims
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var loggedInUserId = loggedInEmployee.Id;
|
|
var project = projectDto.ToProjectFromCreateProjectDto(tenantId);
|
|
|
|
// 3. Store it to database
|
|
try
|
|
{
|
|
_context.Projects.Add(project);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Log the detailed exception
|
|
_logger.LogError("Failed to create project in database. Rolling back transaction. : {Error}", ex.Message);
|
|
// Return a server error as the primary operation failed
|
|
return StatusCode(500, ApiResponse<object>.ErrorResponse("An error occurred while saving the project.", ex.Message, 500));
|
|
}
|
|
|
|
// 4. Perform non-critical side-effects (caching, notifications) concurrently
|
|
try
|
|
{
|
|
// These operations do not depend on each other, so they can run in parallel.
|
|
Task cacheAddDetailsTask = _cache.AddProjectDetails(project);
|
|
Task cacheClearListTask = _cache.ClearAllProjectIdsByPermissionId(PermissionsMaster.ManageProject);
|
|
|
|
var notification = new { LoggedInUserId = loggedInUserId, Keyword = "Create_Project", Response = project.ToProjectDto() };
|
|
// Send notification only to the relevant group (e.g., users in the same tenant)
|
|
Task notificationTask = _signalR.Clients.Group(tenantId.ToString()).SendAsync("NotificationEventHandler", notification);
|
|
|
|
// Await all side-effect tasks to complete in parallel
|
|
await Task.WhenAll(cacheAddDetailsTask, cacheClearListTask, notificationTask);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// The project was created successfully, but a side-effect failed.
|
|
// Log this as a warning, as the primary operation succeeded. Don't return an error to the user.
|
|
_logger.LogWarning("Project {ProjectId} was created, but a post-creation side-effect (caching/notification) failed. : {Error}", project.Id, ex.Message);
|
|
}
|
|
|
|
// 5. Return a success response to the user as soon as the critical data is saved.
|
|
return Ok(ApiResponse<object>.SuccessResponse(project.ToProjectDto(), "Project created successfully.", 200));
|
|
}
|
|
|
|
[HttpPut]
|
|
[Route("update/{id}")]
|
|
public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateProjectDto updateProjectDto)
|
|
{
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
|
|
}
|
|
try
|
|
{
|
|
Guid TenantId = GetTenantId();
|
|
|
|
Project project = updateProjectDto.ToProjectFromUpdateProjectDto(TenantId, id);
|
|
_context.Projects.Update(project);
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
// Cache functions
|
|
bool isUpdated = await _cache.UpdateProjectDetailsOnly(project);
|
|
if (!isUpdated)
|
|
{
|
|
await _cache.AddProjectDetails(project);
|
|
}
|
|
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Update_Project", Response = project.ToProjectDto() };
|
|
|
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(project.ToProjectDto(), "Success.", 200));
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project Allocation APIs ===================================================================
|
|
|
|
[HttpGet]
|
|
[Route("employees/get/{projectid?}/{includeInactive?}")]
|
|
public async Task<IActionResult> GetEmployeeByProjectID(Guid? projectid, bool includeInactive = false)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
|
|
}
|
|
Guid TenantId = GetTenantId();
|
|
|
|
if (projectid != null)
|
|
{
|
|
// Fetch assigned project
|
|
List<Employee> result = new List<Employee>();
|
|
|
|
if ((bool)includeInactive)
|
|
{
|
|
|
|
result = await (from rpm in _context.Employees.Include(c => c.JobRole)
|
|
join fp in _context.ProjectAllocations.Where(c => c.TenantId == TenantId && c.ProjectId == projectid)
|
|
on rpm.Id equals fp.EmployeeId
|
|
select rpm).ToListAsync();
|
|
}
|
|
else
|
|
{
|
|
result = await (from rpm in _context.Employees.Include(c => c.JobRole)
|
|
join fp in _context.ProjectAllocations.Where(c => c.TenantId == TenantId && c.ProjectId == projectid && c.IsActive == true)
|
|
on rpm.Id equals fp.EmployeeId
|
|
select rpm).ToListAsync();
|
|
}
|
|
|
|
List<EmployeeVM> resultVM = new List<EmployeeVM>();
|
|
foreach (Employee employee in result)
|
|
{
|
|
EmployeeVM vm = employee.ToEmployeeVMFromEmployee();
|
|
resultVM.Add(vm);
|
|
}
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(resultVM, "Success.", 200));
|
|
}
|
|
else
|
|
{
|
|
return NotFound(ApiResponse<object>.ErrorResponse("Invalid Input Parameter", 404));
|
|
}
|
|
|
|
|
|
}
|
|
|
|
[HttpGet]
|
|
[Route("allocation/{projectId}")]
|
|
public async Task<IActionResult> GetProjectAllocation(Guid? projectId)
|
|
{
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
|
|
}
|
|
Guid TenantId = GetTenantId();
|
|
|
|
|
|
var employees = await _context.ProjectAllocations
|
|
.Where(c => c.TenantId == TenantId && c.ProjectId == projectId && c.Employee != null)
|
|
.Include(e => e.Employee)
|
|
.Select(e => new
|
|
{
|
|
ID = e.Id,
|
|
EmployeeId = e.EmployeeId,
|
|
ProjectId = e.ProjectId,
|
|
AllocationDate = e.AllocationDate,
|
|
ReAllocationDate = e.ReAllocationDate,
|
|
FirstName = e.Employee != null ? e.Employee.FirstName : string.Empty,
|
|
LastName = e.Employee != null ? e.Employee.LastName : string.Empty,
|
|
MiddleName = e.Employee != null ? e.Employee.MiddleName : string.Empty,
|
|
IsActive = e.IsActive,
|
|
JobRoleId = (e.JobRoleId != null ? e.JobRoleId : e.Employee != null ? e.Employee.JobRoleId : null)
|
|
}).ToListAsync();
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(employees, "Success.", 200));
|
|
}
|
|
|
|
[HttpPost("allocation")]
|
|
public async Task<IActionResult> ManageAllocation(List<ProjectAllocationDot> projectAllocationDot)
|
|
{
|
|
if (projectAllocationDot != null)
|
|
{
|
|
Guid TenentID = GetTenantId();
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
List<object>? result = new List<object>();
|
|
List<Guid> employeeIds = new List<Guid>();
|
|
List<Guid> projectIds = new List<Guid>();
|
|
|
|
foreach (var item in projectAllocationDot)
|
|
{
|
|
try
|
|
{
|
|
ProjectAllocation projectAllocation = item.ToProjectAllocationFromProjectAllocationDto(TenentID);
|
|
ProjectAllocation? projectAllocationFromDb = await _context.ProjectAllocations.Where(c => c.EmployeeId == projectAllocation.EmployeeId
|
|
&& c.ProjectId == projectAllocation.ProjectId
|
|
&& c.ReAllocationDate == null
|
|
&& c.TenantId == TenentID).SingleOrDefaultAsync();
|
|
|
|
if (projectAllocationFromDb != null)
|
|
{
|
|
_context.ProjectAllocations.Attach(projectAllocationFromDb);
|
|
|
|
if (item.Status)
|
|
{
|
|
projectAllocationFromDb.JobRoleId = projectAllocation.JobRoleId; ;
|
|
projectAllocationFromDb.IsActive = true;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.JobRoleId).IsModified = true;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.IsActive).IsModified = true;
|
|
}
|
|
else
|
|
{
|
|
projectAllocationFromDb.ReAllocationDate = DateTime.Now;
|
|
projectAllocationFromDb.IsActive = false;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.ReAllocationDate).IsModified = true;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.IsActive).IsModified = true;
|
|
|
|
employeeIds.Add(projectAllocation.EmployeeId);
|
|
projectIds.Add(projectAllocation.ProjectId);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
var result1 = new
|
|
{
|
|
Id = projectAllocationFromDb.Id,
|
|
EmployeeId = projectAllocation.EmployeeId,
|
|
JobRoleId = projectAllocation.JobRoleId,
|
|
IsActive = projectAllocation.IsActive,
|
|
ProjectId = projectAllocation.ProjectId,
|
|
AllocationDate = projectAllocation.AllocationDate,
|
|
ReAllocationDate = projectAllocation.ReAllocationDate,
|
|
TenantId = projectAllocation.TenantId
|
|
};
|
|
result.Add(result1);
|
|
}
|
|
else
|
|
{
|
|
projectAllocation.AllocationDate = DateTime.Now;
|
|
projectAllocation.IsActive = true;
|
|
_context.ProjectAllocations.Add(projectAllocation);
|
|
await _context.SaveChangesAsync();
|
|
|
|
employeeIds.Add(projectAllocation.EmployeeId);
|
|
projectIds.Add(projectAllocation.ProjectId);
|
|
}
|
|
await _cache.ClearAllProjectIds(item.EmpID);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
|
|
}
|
|
}
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Assign_Project", ProjectIds = projectIds, EmployeeList = employeeIds };
|
|
|
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
|
return Ok(ApiResponse<object>.SuccessResponse(result, "Data saved successfully", 200));
|
|
|
|
}
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
|
|
|
|
}
|
|
|
|
[HttpGet("assigned-projects/{employeeId}")]
|
|
public async Task<IActionResult> GetProjectsByEmployee([FromRoute] Guid employeeId)
|
|
{
|
|
|
|
Guid tenantId = _userHelper.GetTenantId();
|
|
if (employeeId == Guid.Empty)
|
|
{
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Employee id not valid.", 400));
|
|
}
|
|
|
|
List<Guid> projectList = await _context.ProjectAllocations
|
|
.Where(c => c.TenantId == tenantId && c.EmployeeId == employeeId && c.IsActive)
|
|
.Select(c => c.ProjectId).Distinct()
|
|
.ToListAsync();
|
|
|
|
if (!projectList.Any())
|
|
{
|
|
return NotFound(ApiResponse<object>.SuccessResponse(new List<object>(), "No projects found.", 200));
|
|
}
|
|
|
|
|
|
List<Project> projectlist = await _context.Projects
|
|
.Where(p => projectList.Contains(p.Id))
|
|
.ToListAsync();
|
|
|
|
List<ProjectListVM> projects = new List<ProjectListVM>();
|
|
|
|
|
|
foreach (var project in projectlist)
|
|
{
|
|
|
|
projects.Add(project.ToProjectListVMFromProject());
|
|
}
|
|
|
|
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(projects, "Success.", 200));
|
|
}
|
|
|
|
[HttpPost("assign-projects/{employeeId}")]
|
|
public async Task<ActionResult> AssigneProjectsToEmployee([FromBody] List<ProjectsAllocationDto> projectAllocationDtos, [FromRoute] Guid employeeId)
|
|
{
|
|
if (projectAllocationDtos != null && employeeId != Guid.Empty)
|
|
{
|
|
Guid TenentID = GetTenantId();
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
List<object>? result = new List<object>();
|
|
List<Guid> projectIds = new List<Guid>();
|
|
|
|
foreach (var projectAllocationDto in projectAllocationDtos)
|
|
{
|
|
try
|
|
{
|
|
ProjectAllocation projectAllocation = projectAllocationDto.ToProjectAllocationFromProjectsAllocationDto(TenentID, employeeId);
|
|
ProjectAllocation? projectAllocationFromDb = await _context.ProjectAllocations.Where(c => c.EmployeeId == employeeId && c.ProjectId == projectAllocationDto.ProjectId && c.ReAllocationDate == null && c.TenantId == TenentID).SingleOrDefaultAsync();
|
|
|
|
if (projectAllocationFromDb != null)
|
|
{
|
|
|
|
|
|
_context.ProjectAllocations.Attach(projectAllocationFromDb);
|
|
|
|
if (projectAllocationDto.Status)
|
|
{
|
|
projectAllocationFromDb.JobRoleId = projectAllocation.JobRoleId; ;
|
|
projectAllocationFromDb.IsActive = true;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.JobRoleId).IsModified = true;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.IsActive).IsModified = true;
|
|
}
|
|
else
|
|
{
|
|
projectAllocationFromDb.ReAllocationDate = DateTime.UtcNow;
|
|
projectAllocationFromDb.IsActive = false;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.ReAllocationDate).IsModified = true;
|
|
_context.Entry(projectAllocationFromDb).Property(e => e.IsActive).IsModified = true;
|
|
|
|
projectIds.Add(projectAllocation.ProjectId);
|
|
}
|
|
await _context.SaveChangesAsync();
|
|
var result1 = new
|
|
{
|
|
Id = projectAllocationFromDb.Id,
|
|
EmployeeId = projectAllocation.EmployeeId,
|
|
JobRoleId = projectAllocation.JobRoleId,
|
|
IsActive = projectAllocation.IsActive,
|
|
ProjectId = projectAllocation.ProjectId,
|
|
AllocationDate = projectAllocation.AllocationDate,
|
|
ReAllocationDate = projectAllocation.ReAllocationDate,
|
|
TenantId = projectAllocation.TenantId
|
|
};
|
|
result.Add(result1);
|
|
}
|
|
else
|
|
{
|
|
projectAllocation.AllocationDate = DateTime.Now;
|
|
projectAllocation.IsActive = true;
|
|
_context.ProjectAllocations.Add(projectAllocation);
|
|
await _context.SaveChangesAsync();
|
|
|
|
projectIds.Add(projectAllocation.ProjectId);
|
|
|
|
}
|
|
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
return Ok(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
|
|
}
|
|
}
|
|
await _cache.ClearAllProjectIds(employeeId);
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Assign_Project", ProjectIds = projectIds, EmployeeId = employeeId };
|
|
|
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(result, "Data saved successfully", 200));
|
|
}
|
|
else
|
|
{
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "All Field is required", 400));
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project InfraStructure Get APIs ===================================================================
|
|
|
|
[HttpGet("infra-details/{projectId}")]
|
|
public async Task<IActionResult> GetInfraDetails(Guid projectId)
|
|
{
|
|
_logger.LogInfo("GetInfraDetails called for ProjectId: {ProjectId}", projectId);
|
|
|
|
// Step 1: Get logged-in employee
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
// Step 2: Check project-specific permission
|
|
var hasProjectPermission = await _permission.HasProjectPermission(loggedInEmployee, projectId);
|
|
if (!hasProjectPermission)
|
|
{
|
|
_logger.LogWarning("Project access denied for EmployeeId: {EmployeeId} on ProjectId: {ProjectId}", loggedInEmployee.Id, projectId);
|
|
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access denied", "You don't have access to this project", 403));
|
|
}
|
|
|
|
// Step 3: Check 'ViewInfra' permission
|
|
var hasViewInfraPermission = await _permission.HasPermission(PermissionsMaster.ViewProjectInfra, loggedInEmployee.Id);
|
|
if (!hasViewInfraPermission)
|
|
{
|
|
_logger.LogWarning("ViewInfra permission denied for EmployeeId: {EmployeeId}", loggedInEmployee.Id);
|
|
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access denied", "You don't have access to view infra", 403));
|
|
}
|
|
var result = await _cache.GetBuildingInfra(projectId);
|
|
if (result == null)
|
|
{
|
|
|
|
// Step 4: Fetch buildings for the project
|
|
var buildings = await _context.Buildings
|
|
.Where(b => b.ProjectId == projectId)
|
|
.ToListAsync();
|
|
|
|
var buildingIds = buildings.Select(b => b.Id).ToList();
|
|
|
|
// Step 5: Fetch floors associated with the buildings
|
|
var floors = await _context.Floor
|
|
.Where(f => buildingIds.Contains(f.BuildingId))
|
|
.ToListAsync();
|
|
|
|
var floorIds = floors.Select(f => f.Id).ToList();
|
|
|
|
// Step 6: Fetch work areas associated with the floors
|
|
var workAreas = await _context.WorkAreas
|
|
.Where(wa => floorIds.Contains(wa.FloorId))
|
|
.ToListAsync();
|
|
var workAreaIds = workAreas.Select(wa => wa.Id).ToList();
|
|
|
|
// Step 7: Fetch work items associated with the work area
|
|
var workItems = await _context.WorkItems
|
|
.Where(wi => workAreaIds.Contains(wi.WorkAreaId))
|
|
.ToListAsync();
|
|
|
|
// Step 8: Build the infra hierarchy (Building > Floors > Work Areas)
|
|
List<BuildingMongoDB> Buildings = new List<BuildingMongoDB>();
|
|
foreach (var building in buildings)
|
|
{
|
|
double buildingPlannedWorks = 0;
|
|
double buildingCompletedWorks = 0;
|
|
|
|
var selectedFloors = floors.Where(f => f.BuildingId == building.Id).ToList();
|
|
List<FloorMongoDB> Floors = new List<FloorMongoDB>();
|
|
foreach (var floor in selectedFloors)
|
|
{
|
|
double floorPlannedWorks = 0;
|
|
double floorCompletedWorks = 0;
|
|
var selectedWorkAreas = workAreas.Where(wa => wa.FloorId == floor.Id).ToList();
|
|
List<WorkAreaMongoDB> WorkAreas = new List<WorkAreaMongoDB>();
|
|
foreach (var workArea in selectedWorkAreas)
|
|
{
|
|
double workAreaPlannedWorks = 0;
|
|
double workAreaCompletedWorks = 0;
|
|
var selectedWorkItems = workItems.Where(wi => wi.WorkAreaId == workArea.Id).ToList();
|
|
foreach (var workItem in selectedWorkItems)
|
|
{
|
|
workAreaPlannedWorks += workItem.PlannedWork;
|
|
workAreaCompletedWorks += workItem.CompletedWork;
|
|
}
|
|
WorkAreaMongoDB workAreaMongo = new WorkAreaMongoDB
|
|
{
|
|
Id = workArea.Id.ToString(),
|
|
AreaName = workArea.AreaName,
|
|
PlannedWork = workAreaPlannedWorks,
|
|
CompletedWork = workAreaCompletedWorks
|
|
};
|
|
WorkAreas.Add(workAreaMongo);
|
|
floorPlannedWorks += workAreaPlannedWorks;
|
|
floorCompletedWorks += workAreaCompletedWorks;
|
|
}
|
|
FloorMongoDB floorMongoDB = new FloorMongoDB
|
|
{
|
|
Id = floor.Id.ToString(),
|
|
FloorName = floor.FloorName,
|
|
PlannedWork = floorPlannedWorks,
|
|
CompletedWork = floorCompletedWorks,
|
|
WorkAreas = WorkAreas
|
|
};
|
|
Floors.Add(floorMongoDB);
|
|
buildingPlannedWorks += floorPlannedWorks;
|
|
buildingCompletedWorks += floorCompletedWorks;
|
|
}
|
|
|
|
var buildingMongo = new BuildingMongoDB
|
|
{
|
|
Id = building.Id.ToString(),
|
|
BuildingName = building.Name,
|
|
Description = building.Description,
|
|
PlannedWork = buildingPlannedWorks,
|
|
CompletedWork = buildingCompletedWorks,
|
|
Floors = Floors
|
|
};
|
|
Buildings.Add(buildingMongo);
|
|
}
|
|
result = Buildings;
|
|
}
|
|
|
|
_logger.LogInfo("Infra details fetched successfully for ProjectId: {ProjectId}, EmployeeId: {EmployeeId}, Buildings: {Count}",
|
|
projectId, loggedInEmployee.Id, result.Count);
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(result, "Infra details fetched successfully", 200));
|
|
}
|
|
|
|
[HttpGet("tasks/{workAreaId}")]
|
|
public async Task<IActionResult> GetWorkItems(Guid workAreaId)
|
|
{
|
|
_logger.LogInfo("GetWorkItems called for WorkAreaId: {WorkAreaId}", workAreaId);
|
|
|
|
// Step 1: Get the currently logged-in employee
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
// Step 2: Check if the employee has ViewInfra permission
|
|
var hasViewInfraPermission = await _permission.HasPermission(PermissionsMaster.ViewProjectInfra, loggedInEmployee.Id);
|
|
if (!hasViewInfraPermission)
|
|
{
|
|
_logger.LogWarning("ViewInfra permission denied for EmployeeId: {EmployeeId}", loggedInEmployee.Id);
|
|
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access denied", "You don't have permission to view infrastructure", 403));
|
|
}
|
|
|
|
// Step 3: Check if the specified Work Area exists
|
|
var isWorkAreaExist = await _context.WorkAreas.AnyAsync(wa => wa.Id == workAreaId);
|
|
if (!isWorkAreaExist)
|
|
{
|
|
_logger.LogWarning("Work Area not found for WorkAreaId: {WorkAreaId}", workAreaId);
|
|
return NotFound(ApiResponse<object>.ErrorResponse("Work Area not found", "Work Area not found in database", 404));
|
|
}
|
|
|
|
// Step 4: Fetch WorkItems with related Activity and Work Category data
|
|
var workItemVMs = await _cache.GetWorkItemDetailsByWorkArea(workAreaId);
|
|
if (workItemVMs == null)
|
|
{
|
|
var workItems = await _context.WorkItems
|
|
.Include(wi => wi.ActivityMaster)
|
|
.Include(wi => wi.WorkCategoryMaster)
|
|
.Where(wi => wi.WorkAreaId == workAreaId)
|
|
.ToListAsync();
|
|
|
|
workItemVMs = workItems.Select(wi => new WorkItemMongoDB
|
|
{
|
|
Id = wi.Id.ToString(),
|
|
WorkAreaId = wi.WorkAreaId.ToString(),
|
|
ParentTaskId = wi.ParentTaskId.ToString(),
|
|
ActivityMaster = new ActivityMasterMongoDB
|
|
{
|
|
Id = wi.ActivityId.ToString(),
|
|
ActivityName = wi.ActivityMaster != null ? wi.ActivityMaster.ActivityName : null,
|
|
UnitOfMeasurement = wi.ActivityMaster != null ? wi.ActivityMaster.UnitOfMeasurement : null
|
|
},
|
|
WorkCategoryMaster = new WorkCategoryMasterMongoDB
|
|
{
|
|
Id = wi.WorkCategoryId.ToString() ?? "",
|
|
Name = wi.WorkCategoryMaster != null ? wi.WorkCategoryMaster.Name : "",
|
|
Description = wi.WorkCategoryMaster != null ? wi.WorkCategoryMaster.Description : ""
|
|
},
|
|
PlannedWork = wi.PlannedWork,
|
|
CompletedWork = wi.CompletedWork,
|
|
Description = wi.Description,
|
|
TaskDate = wi.TaskDate,
|
|
}).ToList();
|
|
|
|
await _cache.ManageWorkItemDetails(workItems);
|
|
}
|
|
|
|
_logger.LogInfo("{Count} work items fetched successfully for WorkAreaId: {WorkAreaId}", workItemVMs.Count, workAreaId);
|
|
|
|
// Step 5: Return result
|
|
return Ok(ApiResponse<object>.SuccessResponse(workItemVMs, $"{workItemVMs.Count} records of tasks fetched successfully", 200));
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project Infrastructre Manage APIs ===================================================================
|
|
|
|
[HttpPost("task")]
|
|
public async Task<IActionResult> CreateProjectTask(List<WorkItemDot> workItemDtos)
|
|
{
|
|
_logger.LogInfo("CreateProjectTask called with {Count} items", workItemDtos?.Count ?? 0);
|
|
|
|
// Validate request
|
|
if (workItemDtos == null || !workItemDtos.Any())
|
|
{
|
|
_logger.LogWarning("No work items provided in the request.");
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item details are not valid.", 400));
|
|
}
|
|
|
|
Guid tenantId = GetTenantId();
|
|
var workItemsToCreate = new List<WorkItem>();
|
|
var workItemsToUpdate = new List<WorkItem>();
|
|
var responseList = new List<WorkItemVM>();
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
string message = "";
|
|
List<Guid> workAreaIds = new List<Guid>();
|
|
var workItemIds = workItemDtos.Where(wi => wi.Id != null && wi.Id != Guid.Empty).Select(wi => wi.Id).ToList();
|
|
var workItems = await _context.WorkItems.AsNoTracking().Where(wi => workItemIds.Contains(wi.Id)).ToListAsync();
|
|
|
|
foreach (var itemDto in workItemDtos)
|
|
{
|
|
var workItem = itemDto.ToWorkItemFromWorkItemDto(tenantId);
|
|
var workArea = await _context.WorkAreas.Include(a => a.Floor).FirstOrDefaultAsync(a => a.Id == workItem.WorkAreaId) ?? new WorkArea();
|
|
|
|
Building building = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == (workArea.Floor != null ? workArea.Floor.BuildingId : Guid.Empty)) ?? new Building();
|
|
|
|
if (itemDto.Id != null && itemDto.Id != Guid.Empty)
|
|
{
|
|
// Update existing
|
|
workItemsToUpdate.Add(workItem);
|
|
message = $"Task Updated in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
|
var existingWorkItem = workItems.FirstOrDefault(wi => wi.Id == workItem.Id);
|
|
double plannedWork = 0;
|
|
double completedWork = 0;
|
|
if (existingWorkItem != null)
|
|
{
|
|
if (existingWorkItem.PlannedWork != workItem.PlannedWork && existingWorkItem.CompletedWork != workItem.CompletedWork)
|
|
{
|
|
plannedWork = workItem.PlannedWork - existingWorkItem.PlannedWork;
|
|
completedWork = workItem.CompletedWork - existingWorkItem.CompletedWork;
|
|
}
|
|
else if (existingWorkItem.PlannedWork == workItem.PlannedWork && existingWorkItem.CompletedWork != workItem.CompletedWork)
|
|
{
|
|
plannedWork = 0;
|
|
completedWork = workItem.CompletedWork - existingWorkItem.CompletedWork;
|
|
}
|
|
else if (existingWorkItem.PlannedWork != workItem.PlannedWork && existingWorkItem.CompletedWork == workItem.CompletedWork)
|
|
{
|
|
plannedWork = workItem.PlannedWork - existingWorkItem.PlannedWork;
|
|
completedWork = 0;
|
|
}
|
|
await _cache.UpdatePlannedAndCompleteWorksInBuilding(workArea.Id, plannedWork, completedWork);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
// Create new
|
|
workItem.Id = Guid.NewGuid();
|
|
workItemsToCreate.Add(workItem);
|
|
message = $"Task Added in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
|
await _cache.UpdatePlannedAndCompleteWorksInBuilding(workArea.Id, workItem.PlannedWork, workItem.CompletedWork);
|
|
}
|
|
|
|
responseList.Add(new WorkItemVM
|
|
{
|
|
WorkItemId = workItem.Id,
|
|
WorkItem = workItem
|
|
});
|
|
workAreaIds.Add(workItem.WorkAreaId);
|
|
|
|
}
|
|
string responseMessage = "";
|
|
// Apply DB changes
|
|
if (workItemsToCreate.Any())
|
|
{
|
|
_logger.LogInfo("Adding {Count} new work items", workItemsToCreate.Count);
|
|
await _context.WorkItems.AddRangeAsync(workItemsToCreate);
|
|
responseMessage = "Task Added Successfully";
|
|
await _cache.ManageWorkItemDetails(workItemsToCreate);
|
|
}
|
|
|
|
if (workItemsToUpdate.Any())
|
|
{
|
|
_logger.LogInfo("Updating {Count} existing work items", workItemsToUpdate.Count);
|
|
_context.WorkItems.UpdateRange(workItemsToUpdate);
|
|
responseMessage = "Task Updated Successfully";
|
|
await _cache.ManageWorkItemDetails(workItemsToUpdate);
|
|
}
|
|
|
|
await _context.SaveChangesAsync();
|
|
|
|
_logger.LogInfo("CreateProjectTask completed successfully. Created: {Created}, Updated: {Updated}", workItemsToCreate.Count, workItemsToUpdate.Count);
|
|
|
|
|
|
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "WorkItem", WorkAreaIds = workAreaIds, Message = message };
|
|
|
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
|
|
|
return Ok(ApiResponse<object>.SuccessResponse(responseList, responseMessage, 200));
|
|
}
|
|
|
|
[HttpDelete("task/{id}")]
|
|
public async Task<IActionResult> DeleteProjectTask(Guid id)
|
|
{
|
|
Guid tenantId = _userHelper.GetTenantId();
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
List<Guid> workAreaIds = new List<Guid>();
|
|
WorkItem? task = await _context.WorkItems.AsNoTracking().Include(t => t.WorkArea).FirstOrDefaultAsync(t => t.Id == id && t.TenantId == tenantId);
|
|
if (task != null)
|
|
{
|
|
if (task.CompletedWork == 0)
|
|
{
|
|
var assignedTask = await _context.TaskAllocations.Where(t => t.WorkItemId == id).ToListAsync();
|
|
if (assignedTask.Count == 0)
|
|
{
|
|
_context.WorkItems.Remove(task);
|
|
await _context.SaveChangesAsync();
|
|
_logger.LogInfo("Task with ID {WorkItemId} has been successfully deleted.", task.Id);
|
|
|
|
var floorId = task.WorkArea?.FloorId;
|
|
var floor = await _context.Floor.Include(f => f.Building).FirstOrDefaultAsync(f => f.Id == floorId);
|
|
|
|
|
|
workAreaIds.Add(task.WorkAreaId);
|
|
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "WorkItem", WorkAreaIds = workAreaIds, Message = $"Task Deleted in Building: {floor?.Building?.Name}, on Floor: {floor?.FloorName}, in Area: {task.WorkArea?.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}" };
|
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
|
}
|
|
else
|
|
{
|
|
_logger.LogWarning("Task with ID {WorkItemId} is currently assigned and cannot be deleted.", task.Id);
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Task is currently assigned and cannot be deleted.", "Task is currently assigned and cannot be deleted.", 400));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
double percentage = (task.CompletedWork / task.PlannedWork) * 100;
|
|
percentage = Math.Round(percentage, 2);
|
|
_logger.LogWarning("Task with ID {WorkItemId} is {CompletionPercentage}% complete and cannot be deleted", task.Id, percentage);
|
|
return BadRequest(ApiResponse<object>.ErrorResponse(System.String.Format("Task is {0}% complete and cannot be deleted", percentage), System.String.Format("Task is {0}% complete and cannot be deleted", percentage), 400));
|
|
|
|
}
|
|
}
|
|
else
|
|
{
|
|
_logger.LogError("Task with ID {WorkItemId} not found ID database", id);
|
|
}
|
|
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Task deleted successfully", 200));
|
|
}
|
|
|
|
[HttpPost("manage-infra")]
|
|
public async Task<IActionResult> ManageProjectInfra(List<InfraDot> infraDots)
|
|
{
|
|
Guid tenantId = GetTenantId();
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
var responseData = new InfraVM { };
|
|
string responseMessage = "";
|
|
string message = "";
|
|
List<Guid> projectIds = new List<Guid>();
|
|
if (infraDots != null)
|
|
{
|
|
foreach (var item in infraDots)
|
|
{
|
|
if (item.Building != null)
|
|
{
|
|
|
|
Building building = item.Building.ToBuildingFromBuildingDto(tenantId);
|
|
building.TenantId = GetTenantId();
|
|
|
|
if (item.Building.Id == null)
|
|
{
|
|
//create
|
|
_context.Buildings.Add(building);
|
|
await _context.SaveChangesAsync();
|
|
responseData.building = building;
|
|
responseMessage = "Buliding Added Successfully";
|
|
message = "Building Added";
|
|
await _cache.AddBuildngInfra(building.ProjectId, building);
|
|
}
|
|
else
|
|
{
|
|
//update
|
|
_context.Buildings.Update(building);
|
|
await _context.SaveChangesAsync();
|
|
responseData.building = building;
|
|
responseMessage = "Buliding Updated Successfully";
|
|
message = "Building Updated";
|
|
await _cache.UpdateBuildngInfra(building.ProjectId, building);
|
|
}
|
|
projectIds.Add(building.ProjectId);
|
|
}
|
|
if (item.Floor != null)
|
|
{
|
|
Floor floor = item.Floor.ToFloorFromFloorDto(tenantId);
|
|
floor.TenantId = GetTenantId();
|
|
bool isCreated = false;
|
|
|
|
if (item.Floor.Id == null)
|
|
{
|
|
//create
|
|
_context.Floor.Add(floor);
|
|
await _context.SaveChangesAsync();
|
|
responseData.floor = floor;
|
|
responseMessage = "Floor Added Successfully";
|
|
message = "Floor Added";
|
|
isCreated = true;
|
|
}
|
|
else
|
|
{
|
|
//update
|
|
_context.Floor.Update(floor);
|
|
await _context.SaveChangesAsync();
|
|
responseData.floor = floor;
|
|
responseMessage = "Floor Updated Successfully";
|
|
message = "Floor Updated";
|
|
}
|
|
Building? building = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == floor.BuildingId);
|
|
var projectId = building?.ProjectId ?? Guid.Empty;
|
|
projectIds.Add(projectId);
|
|
message = $"{message} in Building: {building?.Name}";
|
|
if (isCreated)
|
|
{
|
|
await _cache.AddBuildngInfra(projectId, floor: floor);
|
|
}
|
|
else
|
|
{
|
|
await _cache.UpdateBuildngInfra(projectId, floor: floor);
|
|
}
|
|
}
|
|
if (item.WorkArea != null)
|
|
{
|
|
WorkArea workArea = item.WorkArea.ToWorkAreaFromWorkAreaDto(tenantId);
|
|
workArea.TenantId = GetTenantId();
|
|
bool isCreated = false;
|
|
|
|
if (item.WorkArea.Id == null)
|
|
{
|
|
//create
|
|
_context.WorkAreas.Add(workArea);
|
|
await _context.SaveChangesAsync();
|
|
responseData.workArea = workArea;
|
|
responseMessage = "Work Area Added Successfully";
|
|
message = "Work Area Added";
|
|
isCreated = true;
|
|
}
|
|
else
|
|
{
|
|
//update
|
|
_context.WorkAreas.Update(workArea);
|
|
await _context.SaveChangesAsync();
|
|
responseData.workArea = workArea;
|
|
responseMessage = "Work Area Updated Successfully";
|
|
message = "Work Area Updated";
|
|
}
|
|
Floor? floor = await _context.Floor.Include(f => f.Building).FirstOrDefaultAsync(f => f.Id == workArea.FloorId);
|
|
var projectId = floor?.Building?.ProjectId ?? Guid.Empty;
|
|
projectIds.Add(projectId);
|
|
message = $"{message} in Building: {floor?.Building?.Name}, on Floor: {floor?.FloorName}";
|
|
if (isCreated)
|
|
{
|
|
await _cache.AddBuildngInfra(projectId, workArea: workArea, buildingId: floor?.BuildingId);
|
|
}
|
|
else
|
|
{
|
|
await _cache.UpdateBuildngInfra(projectId, workArea: workArea, buildingId: floor?.BuildingId);
|
|
}
|
|
}
|
|
}
|
|
message = $"{message} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
|
|
|
|
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
|
|
return Ok(ApiResponse<object>.SuccessResponse(responseData, responseMessage, 200));
|
|
}
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Infra Details are not valid.", 400));
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Helper Functions ===================================================================
|
|
|
|
/// <summary>
|
|
/// Retrieves a list of ProjectInfoVMs by their IDs, using an efficient partial cache-hit strategy.
|
|
/// It fetches what it can from the cache (as ProjectMongoDB), gets the rest from the
|
|
/// database (as Project), updates the cache, and returns a unified list of ViewModels.
|
|
/// </summary>
|
|
/// <param name="projectIds">The list of project IDs to retrieve.</param>
|
|
/// <returns>A list of ProjectInfoVMs.</returns>
|
|
private async Task<List<ProjectInfoVM>> GetProjectInfosByIdsAsync(List<Guid> projectIds)
|
|
{
|
|
// --- Step 1: Fetch from Cache ---
|
|
// The cache returns a list of MongoDB documents for the projects it found.
|
|
var cachedMongoDocs = await _cache.GetProjectDetailsList(projectIds) ?? new List<ProjectMongoDB>();
|
|
var finalViewModels = _mapper.Map<List<ProjectInfoVM>>(cachedMongoDocs);
|
|
|
|
_logger.LogDebug("Cache hit for {CacheCount} of {TotalCount} projects.", finalViewModels.Count, projectIds.Count);
|
|
|
|
// --- Step 2: Identify Missing Projects ---
|
|
// If we found everything in the cache, we can return early.
|
|
if (finalViewModels.Count == projectIds.Count)
|
|
{
|
|
return finalViewModels;
|
|
}
|
|
|
|
var cachedIds = cachedMongoDocs.Select(p => p.Id).ToHashSet(); // Assuming ProjectMongoDB has an Id
|
|
var missingIds = projectIds.Where(id => !cachedIds.Contains(id.ToString())).ToList();
|
|
|
|
// --- Step 3: Fetch Missing from Database ---
|
|
if (missingIds.Any())
|
|
{
|
|
_logger.LogDebug("Cache miss for {MissingCount} projects. Querying database.", missingIds.Count);
|
|
|
|
var projectsFromDb = await _context.Projects
|
|
.Where(p => missingIds.Contains(p.Id))
|
|
.AsNoTracking() // Use AsNoTracking for read-only query performance
|
|
.ToListAsync();
|
|
|
|
if (projectsFromDb.Any())
|
|
{
|
|
// Map the newly fetched projects (from SQL) to their ViewModel
|
|
var vmsFromDb = _mapper.Map<List<ProjectInfoVM>>(projectsFromDb);
|
|
finalViewModels.AddRange(vmsFromDb);
|
|
|
|
// --- Step 4: Update Cache with Missing Items in a new scope ---
|
|
_logger.LogDebug("Updating cache with {DbCount} newly fetched projects.", projectsFromDb.Count);
|
|
await _cache.AddProjectDetailsList(projectsFromDb);
|
|
}
|
|
}
|
|
|
|
return finalViewModels;
|
|
}
|
|
|
|
private Guid GetTenantId()
|
|
{
|
|
return _userHelper.GetTenantId();
|
|
}
|
|
|
|
private async Task<ProjectDetailsVM> GetProjectViewModel(Guid? id, Project project)
|
|
{
|
|
ProjectDetailsVM vm = new ProjectDetailsVM();
|
|
|
|
// List<Building> buildings = _unitOfWork.Building.GetAll(c => c.ProjectId == id).ToList();
|
|
List<Building> buildings = await _context.Buildings.Where(c => c.ProjectId == id).ToListAsync();
|
|
List<Guid> idList = buildings.Select(o => o.Id).ToList();
|
|
// List<Floor> floors = _unitOfWork.Floor.GetAll(c => idList.Contains(c.Id)).ToList();
|
|
List<Floor> floors = await _context.Floor.Where(c => idList.Contains(c.BuildingId)).ToListAsync();
|
|
idList = floors.Select(o => o.Id).ToList();
|
|
//List<WorkArea> workAreas = _unitOfWork.WorkArea.GetAll(c => idList.Contains(c.Id), includeProperties: "WorkItems,WorkItems.ActivityMaster").ToList();
|
|
|
|
List<WorkArea> workAreas = await _context.WorkAreas.Where(c => idList.Contains(c.FloorId)).ToListAsync();
|
|
|
|
idList = workAreas.Select(o => o.Id).ToList();
|
|
List<WorkItem> workItems = await _context.WorkItems.Include(c => c.WorkCategoryMaster).Where(c => idList.Contains(c.WorkAreaId)).Include(c => c.ActivityMaster).ToListAsync();
|
|
// List <WorkItem> workItems = _unitOfWork.WorkItem.GetAll(c => idList.Contains(c.WorkAreaId), includeProperties: "ActivityMaster").ToList();
|
|
idList = workItems.Select(t => t.Id).ToList();
|
|
List<TaskAllocation> tasks = await _context.TaskAllocations.Where(t => idList.Contains(t.WorkItemId) && t.AssignmentDate.Date == DateTime.UtcNow.Date).ToListAsync();
|
|
vm.project = project;
|
|
vm.buildings = buildings;
|
|
vm.floors = floors;
|
|
vm.workAreas = workAreas;
|
|
vm.workItems = workItems;
|
|
vm.Tasks = tasks;
|
|
return vm;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Fetches project details from the database for a given list of project IDs and assembles them into MongoDB models.
|
|
/// This method encapsulates the optimized, parallel database queries.
|
|
/// </summary>
|
|
/// <param name="projectIdsToFetch">The list of project IDs to fetch.</param>
|
|
/// <param name="tenantId">The current tenant ID for filtering.</param>
|
|
/// <returns>A list of fully populated ProjectMongoDB objects.</returns>
|
|
private async Task<List<ProjectListVM>> FetchAndBuildProjectDetails(List<Guid> projectIdsToFetch, Guid tenantId)
|
|
{
|
|
// Task to get base project details for the MISSING projects
|
|
var projectsTask = Task.Run(async () =>
|
|
{
|
|
using var context = _dbContextFactory.CreateDbContext();
|
|
return await context.Projects.AsNoTracking()
|
|
.Where(p => projectIdsToFetch.Contains(p.Id) && p.TenantId == tenantId)
|
|
.ToListAsync();
|
|
});
|
|
|
|
// Task to get team sizes for the MISSING projects
|
|
var teamSizesTask = Task.Run(async () =>
|
|
{
|
|
using var context = _dbContextFactory.CreateDbContext();
|
|
return await context.ProjectAllocations.AsNoTracking()
|
|
.Where(pa => pa.TenantId == tenantId && projectIdsToFetch.Contains(pa.ProjectId) && pa.IsActive)
|
|
.GroupBy(pa => pa.ProjectId)
|
|
.Select(g => new { ProjectId = g.Key, Count = g.Count() })
|
|
.ToDictionaryAsync(x => x.ProjectId, x => x.Count);
|
|
});
|
|
|
|
// Task to get work summaries for the MISSING projects
|
|
var workSummariesTask = Task.Run(async () =>
|
|
{
|
|
using var context = _dbContextFactory.CreateDbContext();
|
|
return await context.WorkItems.AsNoTracking()
|
|
.Where(wi => wi.TenantId == tenantId &&
|
|
wi.WorkArea != null &&
|
|
wi.WorkArea.Floor != null &&
|
|
wi.WorkArea.Floor.Building != null &&
|
|
projectIdsToFetch.Contains(wi.WorkArea.Floor.Building.ProjectId))
|
|
.GroupBy(wi => wi.WorkArea!.Floor!.Building!.ProjectId)
|
|
.Select(g => new { ProjectId = g.Key, PlannedWork = g.Sum(i => i.PlannedWork), CompletedWork = g.Sum(i => i.CompletedWork) })
|
|
.ToDictionaryAsync(x => x.ProjectId);
|
|
});
|
|
|
|
// Await all parallel tasks to complete
|
|
await Task.WhenAll(projectsTask, teamSizesTask, workSummariesTask);
|
|
|
|
var projects = await projectsTask;
|
|
var teamSizes = await teamSizesTask;
|
|
var workSummaries = await workSummariesTask;
|
|
|
|
// Proactively update the cache with the items we just fetched.
|
|
_logger.LogInfo("Updating cache with {NewItemCount} newly fetched projects.", projects.Count);
|
|
await _cache.AddProjectDetailsList(projects);
|
|
|
|
// This section would build the full ProjectMongoDB objects, similar to your AddProjectDetailsList method.
|
|
// For brevity, assuming you have a mapper or a builder for this. Here's a simplified representation:
|
|
var mongoDetailsList = new List<ProjectListVM>();
|
|
foreach (var project in projects)
|
|
{
|
|
// This is a placeholder for the full build logic from your other methods.
|
|
// In a real scenario, you would fetch all hierarchy levels (buildings, floors, etc.)
|
|
// for the `projectIdsToFetch` and build the complete MongoDB object.
|
|
var mongoDetail = _mapper.Map<ProjectListVM>(project);
|
|
mongoDetail.Id = project.Id;
|
|
mongoDetail.TeamSize = teamSizes.GetValueOrDefault(project.Id, 0);
|
|
if (workSummaries.TryGetValue(project.Id, out var summary))
|
|
{
|
|
mongoDetail.PlannedWork = summary.PlannedWork;
|
|
mongoDetail.CompletedWork = summary.CompletedWork;
|
|
}
|
|
mongoDetailsList.Add(mongoDetail);
|
|
}
|
|
|
|
return mongoDetailsList;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Private helper to encapsulate the cache-first data retrieval logic.
|
|
/// </summary>
|
|
/// <returns>A ProjectDetailVM if found, otherwise null.</returns>
|
|
private async Task<Project?> GetProjectDataAsync(Guid projectId)
|
|
{
|
|
// --- Cache First ---
|
|
_logger.LogDebug("Attempting to fetch project {ProjectId} from cache.", projectId);
|
|
var cachedProject = await _cache.GetProjectDetails(projectId);
|
|
if (cachedProject != null)
|
|
{
|
|
_logger.LogInfo("Cache HIT for project {ProjectId}.", projectId);
|
|
// Map from the cache model (e.g., ProjectMongoDB) to the response ViewModel.
|
|
return _mapper.Map<Project>(cachedProject);
|
|
}
|
|
|
|
// --- Database Second (on Cache Miss) ---
|
|
_logger.LogInfo("Cache MISS for project {ProjectId}. Fetching from database.", projectId);
|
|
var dbProject = await _context.Projects
|
|
.AsNoTracking() // Use AsNoTracking for read-only queries.
|
|
.Where(p => p.Id == projectId && p.TenantId == tenantId)
|
|
.SingleOrDefaultAsync();
|
|
|
|
if (dbProject == null)
|
|
{
|
|
return null; // The project doesn't exist.
|
|
}
|
|
|
|
// --- Proactively Update Cache ---
|
|
// The next request for this project will now be a cache hit.
|
|
try
|
|
{
|
|
// Map the DB entity to the cache model (e.g., ProjectMongoDB) before caching.
|
|
await _cache.AddProjectDetails(dbProject);
|
|
_logger.LogInfo("Updated cache with project {ProjectId}.", projectId);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
_logger.LogWarning("Failed to update cache for project {ProjectId} : \n {Error}", projectId, ex.Message);
|
|
}
|
|
|
|
// Map from the database entity to the response ViewModel.
|
|
return dbProject;
|
|
}
|
|
|
|
#endregion
|
|
}
|
|
} |