1893 lines
102 KiB
C#

using AutoMapper;
using AutoMapper.QueryableExtensions;
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.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.Service.ServiceInterfaces;
using MarcoBMS.Services.Helpers;
using MarcoBMS.Services.Service;
using Microsoft.CodeAnalysis;
using Microsoft.EntityFrameworkCore;
using Project = Marco.Pms.Model.Projects.Project;
namespace Marco.Pms.Services.Service
{
public class ProjectServices : IProjectServices
{
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
private readonly ApplicationDbContext _context; // Keeping this for direct scoped context use where appropriate
private readonly ILoggingService _logger;
private readonly ProjectsHelper _projectsHelper;
private readonly PermissionServices _permission;
private readonly CacheUpdateHelper _cache;
private readonly IMapper _mapper;
private readonly GeneralHelper _generalHelper;
public ProjectServices(
IDbContextFactory<ApplicationDbContext> dbContextFactory,
ApplicationDbContext context,
ILoggingService logger,
ProjectsHelper projectsHelper,
PermissionServices permission,
CacheUpdateHelper cache,
IMapper mapper,
GeneralHelper generalHelper)
{
_dbContextFactory = dbContextFactory ?? throw new ArgumentNullException(nameof(dbContextFactory));
_context = context ?? throw new ArgumentNullException(nameof(context));
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
_projectsHelper = projectsHelper ?? throw new ArgumentNullException(nameof(projectsHelper));
_permission = permission ?? throw new ArgumentNullException(nameof(permission));
_cache = cache ?? throw new ArgumentNullException(nameof(cache));
_mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
_generalHelper = generalHelper ?? throw new ArgumentNullException(nameof(generalHelper));
}
#region =================================================================== Project Get APIs ===================================================================
public async Task<ApiResponse<object>> GetAllProjectsBasicAsync(Guid tenantId, Employee loggedInEmployee)
{
try
{
// Step 1: Verify the current user
if (loggedInEmployee == null)
{
return 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
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 ApiResponse<object>.SuccessResponse(new List<ProjectInfoVM>(), "0 records of project fetchd successfully", 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 ApiResponse<object>.SuccessResponse(projectVMs, $"{projectVMs.Count} records of project fetchd successfully", 200);
}
catch (Exception ex)
{
// --- Step 5: Graceful Error Handling ---
_logger.LogError(ex, "An unexpected error occurred in GetAllProjectsBasic for tenant {TenantId}.", tenantId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred. Please try again later.", null, 500);
}
}
public async Task<ApiResponse<object>> GetAllProjectsAsync(Guid tenantId, Employee loggedInEmployee)
{
try
{
_logger.LogInfo("Starting GetAllProjects for TenantId: {TenantId}, User: {UserId}", tenantId, loggedInEmployee.Id);
// --- Step 1: 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 ApiResponse<object>.SuccessResponse(new List<ProjectListVM>(), "No projects found for the current user.", 200);
}
// --- Step 2: 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 3: 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 4: Return the combined result ---
_logger.LogInfo("Successfully retrieved a total of {ProjectCount} projects.", responseVms.Count);
return ApiResponse<object>.SuccessResponse(responseVms, "Projects retrieved successfully.", 200);
}
catch (Exception ex)
{
// --- Step 5: Graceful Error Handling ---
_logger.LogError(ex, "An unexpected error occurred in GetAllProjects for tenant {TenantId}.", tenantId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred. Please try again later.", null, 500);
}
}
public async Task<ApiResponse<object>> GetProjectAsync(Guid id, Guid tenantId, Employee loggedInEmployee)
{
try
{
// --- Step 1: 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, tenantId);
// Await both tasks to complete.
await Task.WhenAll(permissionTask, projectDataTask);
var hasPermission = await permissionTask;
var projectVm = await projectDataTask;
// --- Step 2: Process results sequentially ---
// 2a. 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 ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to access this project.", 403);
}
// 2b. Check if the project was found (either in cache or DB).
if (projectVm == null)
{
_logger.LogInfo("Project with ID {ProjectId} not found.", id);
return ApiResponse<object>.ErrorResponse("Project not found.", $"No project found with ID {id}.", 404);
}
// 2c. Success. Return the consistent ViewModel.
_logger.LogInfo("Successfully retrieved project {ProjectId}.", id);
return ApiResponse<object>.SuccessResponse(projectVm, "Project retrieved successfully.", 200);
}
catch (Exception ex)
{
_logger.LogError(ex, "An unexpected error occurred while getting project {ProjectId}", id);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", null, 500);
}
}
public async Task<ApiResponse<object>> GetProjectDetailsAsync(Guid id, Guid tenantId, Employee loggedInEmployee)
{
try
{
_logger.LogInfo("Details requested by EmployeeId: {EmployeeId} for ProjectId: {ProjectId}", loggedInEmployee.Id, id);
// Step 1: 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 ApiResponse<object>.ErrorResponse("Access denied", "You don't have permission to view projects", 403);
}
// Step 2: 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 ApiResponse<object>.ErrorResponse("Access denied", "You don't have access to this project", 403);
}
// Step 3: 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 ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404);
}
// Step 4: Return result
_logger.LogInfo("Project details fetched successfully. ProjectId: {ProjectId}", id);
return ApiResponse<object>.SuccessResponse(projectVM, "Project details fetched successfully", 200);
}
catch (Exception ex)
{
// --- Step 5: Graceful Error Handling ---
_logger.LogError(ex, "An unexpected error occurred in Get Project Details for project {ProjectId} for tenant {TenantId}. ", id, tenantId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred. Please try again later.", null, 500);
}
}
public async Task<ApiResponse<object>> GetProjectDetailsOldAsync(Guid id, Guid tenantId, Employee loggedInEmployee)
{
var project = await _context.Projects
.Where(c => c.TenantId == tenantId && c.Id == id)
.Include(c => c.ProjectStatus)
.SingleOrDefaultAsync();
if (project == null)
{
return ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404);
}
else
{
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 ApiResponse<object>.SuccessResponse(projectVM, "Success.", 200);
}
}
#endregion
#region =================================================================== Project Manage APIs ===================================================================
public async Task<ApiResponse<object>> CreateProjectAsync(CreateProjectDto projectDto, Guid tenantId, Employee loggedInEmployee)
{
// 1. Prepare data without I/O
var loggedInUserId = loggedInEmployee.Id;
var project = _mapper.Map<Project>(projectDto);
project.TenantId = tenantId;
// 2. Store it to database
try
{
_context.Projects.Add(project);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
// Log the detailed exception
_logger.LogError(ex, "Failed to create project in database. Rolling back transaction.");
// Return a server error as the primary operation failed
return ApiResponse<object>.ErrorResponse("An error occurred while saving the project.", ex.Message, 500);
}
// 3. 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);
// Await all side-effect tasks to complete in parallel
await Task.WhenAll(cacheAddDetailsTask, cacheClearListTask);
}
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.LogError(ex, "Project {ProjectId} was created, but a post-creation side-effect (caching/notification) failed. ", project.Id);
}
// 4. Return a success response to the user as soon as the critical data is saved.
return ApiResponse<object>.SuccessResponse(_mapper.Map<ProjectDto>(project), "Project created successfully.", 200);
}
/// <summary>
/// Updates an existing project's details.
/// This endpoint is secure, handles concurrency, and performs non-essential tasks in the background.
/// </summary>
/// <param name="id">The ID of the project to update.</param>
/// <param name="updateProjectDto">The data to update the project with.</param>
/// <returns>An ApiResponse confirming the update or an appropriate error.</returns>
public async Task<ApiResponse<object>> UpdateProjectAsync(Guid id, UpdateProjectDto updateProjectDto, Guid tenantId, Employee loggedInEmployee)
{
try
{
// --- Step 1: Fetch the Existing Entity from the Database ---
// This is crucial to avoid the data loss bug. We only want to modify an existing record.
var existingProject = await _context.Projects
.Where(p => p.Id == id && p.TenantId == tenantId)
.SingleOrDefaultAsync();
// 1a. Existence Check
if (existingProject == null)
{
_logger.LogWarning("Attempt to update non-existent project with ID {ProjectId} by user {UserId}.", id, loggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Project not found.", $"No project found with ID {id}.", 404);
}
// 1b. Security Check
var hasPermission = await _permission.HasProjectPermission(loggedInEmployee, id);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} attempting to update project {ProjectId}.", loggedInEmployee.Id, id);
return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to modify this project.", 403);
}
// --- Step 2: Apply Changes and Save ---
// Map the changes from the DTO onto the entity we just fetched from the database.
// This only modifies the properties defined in the mapping, preventing data loss.
_mapper.Map(updateProjectDto, existingProject);
// Mark the entity as modified (if your mapping doesn't do it automatically).
_context.Entry(existingProject).State = EntityState.Modified;
try
{
await _context.SaveChangesAsync();
_logger.LogInfo("Successfully updated project {ProjectId} by user {UserId}.", id, loggedInEmployee.Id);
}
catch (DbUpdateConcurrencyException ex)
{
// --- Step 3: Handle Concurrency Conflicts ---
// This happens if another user modified the project after we fetched it.
_logger.LogError(ex, "Concurrency conflict while updating project {ProjectId} ", id);
return ApiResponse<object>.ErrorResponse("Conflict occurred.", "This project has been modified by someone else. Please refresh and try again.", 409);
}
// --- Step 4: Perform Side-Effects (Fire and Forget) ---
// Create a DTO of the updated project to pass to background tasks.
var projectDto = _mapper.Map<ProjectDto>(existingProject);
// 4a. Update Cache
await UpdateCacheInBackground(existingProject);
// --- Step 5: Return Success Response Immediately ---
// The client gets a fast response without waiting for caching or SignalR.
return ApiResponse<object>.SuccessResponse(projectDto, "Project updated successfully.", 200);
}
catch (Exception ex)
{
// --- Step 6: Graceful Error Handling for Unexpected Errors ---
_logger.LogError(ex, "An unexpected error occurred while updating project {ProjectId} ", id);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", null, 500);
}
}
#endregion
#region =================================================================== Project Allocation APIs ===================================================================
/// <summary>
/// Retrieves a list of employees for a specific project.
/// This method is optimized to perform all filtering and mapping on the database server.
/// </summary>
/// <param name="projectId">The ID of the project.</param>
/// <param name="includeInactive">Whether to include employees from inactive allocations.</param>
/// <param name="tenantId">The ID of the current tenant.</param>
/// <param name="loggedInEmployee">The current authenticated employee (used for permission checks).</param>
/// <returns>An ApiResponse containing a list of employees or an error.</returns>
public async Task<ApiResponse<object>> GetEmployeeByProjectIdAsync(Guid? projectId, bool includeInactive, Guid tenantId, Employee loggedInEmployee)
{
// --- Step 1: Input Validation ---
if (projectId == null)
{
_logger.LogWarning("GetEmployeeByProjectID called with a null projectId.");
// 400 Bad Request is more appropriate for invalid input than 404 Not Found.
return ApiResponse<object>.ErrorResponse("Project ID is required.", "Invalid Input Parameter", 400);
}
_logger.LogInfo("Fetching employees for ProjectID: {ProjectId}, IncludeInactive: {IncludeInactive}", projectId, includeInactive);
try
{
// --- CRITICAL: Security Check ---
// Before fetching data, you MUST verify the user has permission to see it.
// This is a placeholder for your actual permission logic.
var hasProjectPermission = await _permission.HasProjectPermission(loggedInEmployee, projectId.Value);
var hasAllEmployeePermission = await _permission.HasPermission(PermissionsMaster.ViewAllEmployees, loggedInEmployee.Id);
var hasviewTeamPermission = await _permission.HasPermission(PermissionsMaster.ViewTeamMembers, loggedInEmployee.Id);
if (!(hasProjectPermission && (hasAllEmployeePermission || hasviewTeamPermission)))
{
_logger.LogWarning("Access DENIED for user {UserId} on project {ProjectId}.", loggedInEmployee.Id, projectId);
return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view this project's team.", 403);
}
// --- Step 2: Build a Single, Efficient IQueryable ---
// We start with the base query and conditionally add filters before executing it.
// This avoids code duplication and is highly performant.
var employeeQuery = _context.ProjectAllocations
.Where(pa => pa.ProjectId == projectId && pa.TenantId == tenantId);
// Conditionally apply the filter for active allocations.
if (!includeInactive)
{
employeeQuery = employeeQuery.Where(pa => pa.IsActive);
}
// --- Step 3: Project Directly to the ViewModel on the Database Server ---
// This is the most significant performance optimization.
// Instead of fetching full Employee entities, we select only the data needed for the EmployeeVM.
// AutoMapper's ProjectTo is perfect for this, as it translates the mapping configuration into an efficient SQL SELECT statement.
var resultVM = await employeeQuery
.Where(pa => pa.Employee != null) // Safety check for data integrity
.Select(pa => pa.Employee) // Navigate to the Employee entity
.ProjectTo<EmployeeVM>(_mapper.ConfigurationProvider) // Let AutoMapper generate the SELECT
.ToListAsync();
_logger.LogInfo("Successfully fetched {EmployeeCount} employees for project {ProjectId}.", resultVM.Count, projectId);
// Note: The original mapping loop is now completely gone, replaced by the single efficient query above.
return ApiResponse<object>.SuccessResponse(resultVM, "Successfully fetched the list of employees for the selected project.", 200);
}
catch (Exception ex)
{
// --- Step 4: Graceful Error Handling ---
_logger.LogError(ex, "An error occurred while fetching employees for project {ProjectId}. ", projectId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", "Database Query Failed", 500);
}
}
/// <summary>
/// Retrieves project allocation details for a specific project.
/// This method is optimized for performance and includes security checks.
/// </summary>
/// <param name="projectId">The ID of the project.</param>
/// <param name="tenantId">The ID of the current tenant.</param>
/// <param name="loggedInEmployee">The current authenticated employee for permission checks.</param>
/// <returns>An ApiResponse containing allocation details or an appropriate error.</returns>
public async Task<ApiResponse<object>> GetProjectAllocationAsync(Guid? projectId, Guid tenantId, Employee loggedInEmployee)
{
// --- Step 1: Input Validation ---
if (projectId == null)
{
_logger.LogWarning("GetProjectAllocation called with a null projectId.");
return ApiResponse<object>.ErrorResponse("Project ID is required.", "Invalid Input Parameter", 400);
}
_logger.LogInfo("Fetching allocations for ProjectID: {ProjectId} for user {UserId}", projectId, loggedInEmployee.Id);
try
{
// --- Step 2: Security and Existence Checks ---
// Before fetching data, you MUST verify the user has permission to see it.
// This is a placeholder for your actual permission logic.
var hasPermission = await _permission.HasProjectPermission(loggedInEmployee, projectId.Value);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} on project {ProjectId}.", loggedInEmployee.Id, projectId);
return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view this project's team.", 403);
}
// --- Step 3: Execute a Single, Optimized Database Query ---
// This query projects directly to a new object on the database server, which is highly efficient.
var allocations = await _context.ProjectAllocations
// Filter down to the relevant records first.
.Where(pa => pa.ProjectId == projectId && pa.TenantId == tenantId && pa.Employee != null)
// Project directly to the final shape. This tells EF Core which columns to select.
// The redundant .Include() is removed as EF Core infers the JOIN from this Select.
.Select(pa => new
{
// Fields from ProjectAllocation
ID = pa.Id,
pa.EmployeeId,
pa.ProjectId,
pa.AllocationDate,
pa.ReAllocationDate,
pa.IsActive,
// Fields from the joined Employee table (no null checks needed due to the 'Where' clause)
FirstName = pa.Employee!.FirstName,
LastName = pa.Employee.LastName,
MiddleName = pa.Employee.MiddleName,
// Simplified JobRoleId logic: Use the allocation's role if it exists, otherwise fall back to the employee's default role.
JobRoleId = pa.JobRoleId ?? pa.Employee.JobRoleId
})
.ToListAsync();
_logger.LogInfo("Successfully fetched {AllocationCount} allocations for project {ProjectId}.", allocations.Count, projectId);
return ApiResponse<object>.SuccessResponse(allocations, "Project allocations retrieved successfully.", 200);
}
catch (Exception ex)
{
// --- Step 4: Graceful Error Handling ---
// Log the full exception for debugging, but return a generic, safe error message.
_logger.LogError(ex, "An error occurred while fetching allocations for project {ProjectId}.", projectId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", "Database query failed.", 500);
}
}
/// <summary>
/// Manages project allocations for a list of employees, either adding new allocations or deactivating existing ones.
/// This method is optimized to perform all database operations in a single transaction.
/// </summary>
/// <param name="allocationsDto">The list of allocation changes to process.</param>
/// <param name="tenantId">The ID of the current tenant.</param>
/// <param name="loggedInEmployee">The current authenticated employee for permission checks.</param>
/// <returns>An ApiResponse containing the list of processed allocations.</returns>
public async Task<ApiResponse<List<ProjectAllocationVM>>> ManageAllocationAsync(List<ProjectAllocationDot> allocationsDto, Guid tenantId, Employee loggedInEmployee)
{
// --- Step 1: Input Validation ---
if (allocationsDto == null || !allocationsDto.Any())
{
return ApiResponse<List<ProjectAllocationVM>>.ErrorResponse("Invalid details.", "Allocation details list cannot be null or empty.", 400);
}
_logger.LogInfo("Starting to manage {AllocationCount} allocations for user {UserId}.", allocationsDto.Count, loggedInEmployee.Id);
// --- (Placeholder) Security Check ---
// In a real application, you would check if the loggedInEmployee has permission
// to manage allocations for ALL projects involved in this batch.
var projectIdsInBatch = allocationsDto.Select(a => a.ProjectId).Distinct().ToList();
var hasPermission = await _permission.HasPermission(PermissionsMaster.ManageTeam, loggedInEmployee.Id);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} trying to manage allocations for projects.", loggedInEmployee.Id);
return ApiResponse<List<ProjectAllocationVM>>.ErrorResponse("Access Denied.", "You do not have permission to manage one or more projects in this request.", 403);
}
// --- Step 2: Fetch all relevant existing data in ONE database call ---
var employeeProjectPairs = allocationsDto.Select(a => new { a.EmpID, a.ProjectId }).ToList();
List<Guid> employeeIds = allocationsDto.Select(a => a.EmpID).Distinct().ToList();
// Fetch all currently active allocations for the employees and projects in this batch.
// We use a dictionary for fast O(1) lookups inside the loop.
var existingAllocations = await _context.ProjectAllocations
.Where(pa => pa.TenantId == tenantId &&
employeeIds.Contains(pa.EmployeeId) &&
pa.ReAllocationDate == null)
.ToDictionaryAsync(pa => (pa.EmployeeId, pa.ProjectId));
var processedAllocations = new List<ProjectAllocation>();
// --- Step 3: Process logic IN MEMORY ---
foreach (var dto in allocationsDto)
{
var key = (dto.EmpID, dto.ProjectId);
existingAllocations.TryGetValue(key, out var existingAllocation);
if (dto.Status == false) // User wants to DEACTIVATE the allocation
{
if (existingAllocation != null)
{
// Mark the existing allocation for deactivation
existingAllocation.ReAllocationDate = DateTime.UtcNow; // Use UtcNow for servers
existingAllocation.IsActive = false;
_context.ProjectAllocations.Update(existingAllocation);
processedAllocations.Add(existingAllocation);
}
// If it doesn't exist, we do nothing. The desired state is "not allocated".
}
else // User wants to ACTIVATE the allocation
{
if (existingAllocation == null)
{
// Create a new allocation because one doesn't exist
var newAllocation = _mapper.Map<ProjectAllocation>(dto);
newAllocation.TenantId = tenantId;
newAllocation.AllocationDate = DateTime.UtcNow;
newAllocation.IsActive = true;
_context.ProjectAllocations.Add(newAllocation);
processedAllocations.Add(newAllocation);
}
// If it already exists and is active, we do nothing. The state is already correct.
}
try
{
await _cache.ClearAllProjectIds(dto.EmpID);
_logger.LogInfo("Successfully completed cache invalidation for employee {EmployeeId}.", dto.EmpID);
}
catch (Exception ex)
{
// Log the error but don't fail the entire request, as the primary DB operation succeeded.
_logger.LogError(ex, "Cache invalidation failed for employees after a successful database update.");
}
}
try
{
// --- Step 4: Save all changes in a SINGLE TRANSACTION ---
// All Adds and Updates are sent to the database in one batch.
// If any part fails, the entire transaction is rolled back.
await _context.SaveChangesAsync();
_logger.LogInfo("Successfully saved {ChangeCount} allocation changes to the database.", processedAllocations.Count);
}
catch (Exception ex)
{
_logger.LogError(ex, "Failed to save allocation changes to the database.");
return ApiResponse<List<ProjectAllocationVM>>.ErrorResponse("Database Error.", "An error occurred while saving the changes.", 500);
}
// --- Step 5: Map results and return success ---
var resultVm = _mapper.Map<List<ProjectAllocationVM>>(processedAllocations);
return ApiResponse<List<ProjectAllocationVM>>.SuccessResponse(resultVm, "Allocations managed successfully.", 200);
}
/// <summary>
/// Retrieves a list of active projects assigned to a specific employee.
/// </summary>
/// <param name="employeeId">The ID of the employee whose projects are being requested.</param>
/// <param name="tenantId">The ID of the current tenant.</param>
/// <param name="loggedInEmployee">The current authenticated employee for permission checks.</param>
/// <returns>An ApiResponse containing a list of basic project details or an error.</returns>
public async Task<ApiResponse<object>> GetProjectsByEmployeeAsync(Guid employeeId, Guid tenantId, Employee loggedInEmployee)
{
// --- Step 1: Input Validation ---
if (employeeId == Guid.Empty)
{
return ApiResponse<object>.ErrorResponse("Invalid details.", "A valid employee ID is required.", 400);
}
_logger.LogInfo("Fetching projects for Employee {EmployeeId} by User {UserId}", employeeId, loggedInEmployee.Id);
try
{
// --- Step 2: Clarified Security Check ---
// The permission should be about viewing another employee's assignments, not a generic "Manage Team".
// This is a placeholder for your actual, more specific permission logic.
// It should also handle the case where a user is requesting their own projects (employeeId == loggedInEmployee.Id).
var hasPermission = await _permission.HasPermission(PermissionsMaster.ViewProject, loggedInEmployee.Id);
var projectIds = await _projectsHelper.GetMyProjects(tenantId, loggedInEmployee);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} trying to view projects for employee {TargetEmployeeId}.", loggedInEmployee.Id, employeeId);
return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view this employee's projects.", 403);
}
// --- Step 3: Execute a Single, Highly Efficient Database Query ---
// This query projects directly to the ViewModel on the database server.
var projects = await _context.ProjectAllocations
// 1. Filter the linking table down to the relevant records.
.Where(pa =>
pa.TenantId == tenantId &&
pa.EmployeeId == employeeId && // Target the specified employee
pa.IsActive && // Only active assignments
projectIds.Contains(pa.ProjectId) &&
pa.Project != null) // Safety check for data integrity
// 2. Navigate to the Project entity.
.Select(pa => pa.Project)
// 3. Ensure the final result set is unique (in case of multiple active allocations to the same project).
.Distinct()
// 4. Project directly to the ViewModel using AutoMapper's IQueryable Extensions.
// This generates an efficient SQL "SELECT Id, Name, Code FROM..." statement.
.ProjectTo<ProjectInfoVM>(_mapper.ConfigurationProvider)
// 5. Execute the query.
.ToListAsync();
_logger.LogInfo("Successfully retrieved {ProjectCount} projects for employee {EmployeeId}.", projects.Count, employeeId);
// The original check for an empty list is still good practice.
if (!projects.Any())
{
return ApiResponse<object>.SuccessResponse(new List<ProjectInfoVM>(), "No active projects found for this employee.", 200);
}
return ApiResponse<object>.SuccessResponse(projects, "Projects retrieved successfully.", 200);
}
catch (Exception ex)
{
// --- Step 4: Graceful Error Handling ---
_logger.LogError(ex, "An error occurred while fetching projects for employee {EmployeeId}.", employeeId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", "Database query failed.", 500);
}
}
/// <summary>
/// Manages project assignments for a single employee, processing a batch of projects to activate or deactivate.
/// This method is optimized to perform all database operations in a single, atomic transaction.
/// </summary>
/// <param name="allocationsDto">A list of projects to assign or un-assign.</param>
/// <param name="employeeId">The ID of the employee whose assignments are being managed.</param>
/// <param name="tenantId">The ID of the current tenant.</param>
/// <param name="loggedInEmployee">The current authenticated employee for permission checks.</param>
/// <returns>An ApiResponse containing the list of processed allocations.</returns>
public async Task<ApiResponse<List<ProjectAllocationVM>>> AssigneProjectsToEmployeeAsync(List<ProjectsAllocationDto> allocationsDto, Guid employeeId, Guid tenantId, Employee loggedInEmployee)
{
// --- Step 1: Input Validation ---
if (allocationsDto == null || !allocationsDto.Any() || employeeId == Guid.Empty)
{
return ApiResponse<List<ProjectAllocationVM>>.ErrorResponse("Invalid details.", "A valid employee ID and a list of projects are required.", 400);
}
_logger.LogInfo("Starting to manage {AllocationCount} project assignments for Employee {EmployeeId}.", allocationsDto.Count, employeeId);
// --- (Placeholder) Security Check ---
// You MUST verify that the loggedInEmployee has permission to modify the assignments for the target employeeId.
var hasPermission = await _permission.HasPermission(PermissionsMaster.ManageTeam, loggedInEmployee.Id);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} trying to manage assignments for employee {TargetEmployeeId}.", loggedInEmployee.Id, employeeId);
return ApiResponse<List<ProjectAllocationVM>>.ErrorResponse("Access Denied.", "You do not have permission to manage this employee's assignments.", 403);
}
// --- Step 2: Fetch all relevant existing data in ONE database call ---
var projectIdsInDto = allocationsDto.Select(p => p.ProjectId).ToList();
// Fetch all currently active allocations for this employee for the projects in the request.
// We use a dictionary keyed by ProjectId for fast O(1) lookups inside the loop.
var existingActiveAllocations = await _context.ProjectAllocations
.Where(pa => pa.TenantId == tenantId &&
pa.EmployeeId == employeeId &&
projectIdsInDto.Contains(pa.ProjectId) &&
pa.ReAllocationDate == null) // Only fetch active ones
.ToDictionaryAsync(pa => pa.ProjectId);
var processedAllocations = new List<ProjectAllocation>();
// --- Step 3: Process all logic IN MEMORY, tracking changes ---
foreach (var dto in allocationsDto)
{
existingActiveAllocations.TryGetValue(dto.ProjectId, out var existingAllocation);
if (dto.Status == false) // DEACTIVATE this project assignment
{
if (existingAllocation != null)
{
// Correct Update Pattern: Modify the fetched entity directly.
existingAllocation.ReAllocationDate = DateTime.UtcNow; // Use UTC for servers
existingAllocation.IsActive = false;
_context.ProjectAllocations.Update(existingAllocation);
processedAllocations.Add(existingAllocation);
}
// If it's not in our dictionary, it's already inactive. Do nothing.
}
else // ACTIVATE this project assignment
{
if (existingAllocation == null)
{
// Create a new allocation because an active one doesn't exist.
var newAllocation = _mapper.Map<ProjectAllocation>(dto);
newAllocation.EmployeeId = employeeId;
newAllocation.TenantId = tenantId;
newAllocation.AllocationDate = DateTime.UtcNow;
newAllocation.IsActive = true;
_context.ProjectAllocations.Add(newAllocation);
processedAllocations.Add(newAllocation);
}
// If it already exists in our dictionary, it's already active. Do nothing.
}
}
try
{
// --- Step 4: Save all Adds and Updates in a SINGLE ATOMIC TRANSACTION ---
if (processedAllocations.Any())
{
await _context.SaveChangesAsync();
_logger.LogInfo("Successfully saved {ChangeCount} assignment changes for employee {EmployeeId}.", processedAllocations.Count, employeeId);
}
}
catch (DbUpdateException ex)
{
_logger.LogError(ex, "Failed to save assignment changes for employee {EmployeeId}.", employeeId);
return ApiResponse<List<ProjectAllocationVM>>.ErrorResponse("Database Error.", "An error occurred while saving the changes.", 500);
}
// --- Step 5: Invalidate Cache ONCE after successful save ---
try
{
await _cache.ClearAllProjectIds(employeeId);
_logger.LogInfo("Successfully queued cache invalidation for employee {EmployeeId}.", employeeId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Background cache invalidation failed for employee {EmployeeId}", employeeId);
}
// --- Step 6: Map results using AutoMapper and return success ---
var resultVm = _mapper.Map<List<ProjectAllocationVM>>(processedAllocations);
return ApiResponse<List<ProjectAllocationVM>>.SuccessResponse(resultVm, "Assignments managed successfully.", 200);
}
#endregion
#region =================================================================== Project InfraStructure Get APIs ===================================================================
/// <summary>
/// Retrieves the full infrastructure hierarchy (Buildings, Floors, Work Areas) for a project,
/// including aggregated work summaries.
/// </summary>
public async Task<ApiResponse<object>> GetInfraDetailsAsync(Guid projectId, Guid tenantId, Employee loggedInEmployee)
{
_logger.LogInfo("GetInfraDetails called for ProjectId: {ProjectId}", projectId);
try
{
// --- Step 1: Run independent permission checks in PARALLEL ---
var projectPermissionTask = _permission.HasProjectPermission(loggedInEmployee, projectId);
var viewInfraPermissionTask = _permission.HasPermission(PermissionsMaster.ViewProjectInfra, loggedInEmployee.Id);
await Task.WhenAll(projectPermissionTask, viewInfraPermissionTask);
if (!await projectPermissionTask)
{
_logger.LogWarning("Project access denied for EmployeeId: {EmployeeId} on ProjectId: {ProjectId}", loggedInEmployee.Id, projectId);
return ApiResponse<object>.ErrorResponse("Access denied", "You don't have access to this project", 403);
}
if (!await viewInfraPermissionTask)
{
_logger.LogWarning("ViewInfra permission denied for EmployeeId: {EmployeeId}", loggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Access denied", "You don't have access to view this project's infrastructure", 403);
}
// --- Step 2: Cache-First Strategy ---
var cachedResult = await _cache.GetBuildingInfra(projectId);
if (cachedResult != null)
{
_logger.LogInfo("Cache HIT for infra details for ProjectId: {ProjectId}", projectId);
return ApiResponse<object>.SuccessResponse(cachedResult, "Infra details fetched successfully from cache.", 200);
}
_logger.LogInfo("Cache MISS for infra details for ProjectId: {ProjectId}. Fetching from database.", projectId);
// --- Step 3: Fetch all required data from the database ---
var buildingMongoList = await _generalHelper.GetProjectInfraFromDB(projectId);
// --- Step 5: Proactively update the cache ---
//await _cache.SetBuildingInfra(projectId, buildingMongoList);
_logger.LogInfo("Infra details fetched successfully for ProjectId: {ProjectId}, Buildings: {Count}", projectId, buildingMongoList.Count);
return ApiResponse<object>.SuccessResponse(buildingMongoList, "Infra details fetched successfully", 200);
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred while fetching infra details for ProjectId: {ProjectId}", projectId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", "An error occurred while processing your request.", 500);
}
}
/// <summary>
/// Retrieves a list of work items for a specific work area, ensuring the user has appropriate permissions.
/// </summary>
/// <param name="workAreaId">The ID of the work area.</param>
/// <param name="tenantId">The ID of the current tenant.</param>
/// <param name="loggedInEmployee">The current authenticated employee for permission checks.</param>
/// <returns>An ApiResponse containing a list of work items or an error.</returns>
public async Task<ApiResponse<object>> GetWorkItemsAsync(Guid workAreaId, Guid tenantId, Employee loggedInEmployee)
{
_logger.LogInfo("GetWorkItems called for WorkAreaId: {WorkAreaId} by User: {UserId}", workAreaId, loggedInEmployee.Id);
try
{
// --- Step 1: Cache-First Strategy ---
var cachedWorkItems = await _cache.GetWorkItemDetailsByWorkArea(workAreaId);
if (cachedWorkItems != null)
{
_logger.LogInfo("Cache HIT for WorkAreaId: {WorkAreaId}. Returning {Count} items from cache.", workAreaId, cachedWorkItems.Count);
return ApiResponse<object>.SuccessResponse(cachedWorkItems, $"{cachedWorkItems.Count} tasks retrieved successfully from cache.", 200);
}
_logger.LogInfo("Cache MISS for WorkAreaId: {WorkAreaId}. Fetching from database.", workAreaId);
// --- Step 2: Security Check First ---
// This pattern remains the most robust: verify permissions before fetching a large list.
var projectInfo = await _context.WorkAreas
.Where(wa => wa.Id == workAreaId && wa.TenantId == tenantId && wa.Floor != null && wa.Floor.Building != null)
.Select(wa => new { wa.Floor!.Building!.ProjectId })
.FirstOrDefaultAsync();
if (projectInfo == null)
{
_logger.LogWarning("Work Area not found for WorkAreaId: {WorkAreaId}", workAreaId);
return ApiResponse<object>.ErrorResponse("Not Found", $"Work Area with ID {workAreaId} not found.", 404);
}
var hasProjectAccess = await _permission.HasProjectPermission(loggedInEmployee, projectInfo.ProjectId);
var hasGenericViewInfraPermission = await _permission.HasPermission(PermissionsMaster.ViewProjectInfra, loggedInEmployee.Id);
if (!hasProjectAccess || !hasGenericViewInfraPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} on WorkAreaId {WorkAreaId}.", loggedInEmployee.Id, workAreaId);
return ApiResponse<object>.ErrorResponse("Access Denied", "You do not have sufficient permissions to view these work items.", 403);
}
// --- Step 3: Fetch Full Entities for Caching and Mapping ---
var workItemVMs = await _generalHelper.GetWorkItemsListFromDB(workAreaId);
// --- Step 5: Proactively Update the Cache with the Correct Object Type ---
// We now pass the 'workItemsFromDb' list, which is the required List<WorkItem>.
try
{
await _cache.ManageWorkItemDetailsByVM(workItemVMs);
_logger.LogInfo("Successfully queued cache update for WorkAreaId: {WorkAreaId}", workAreaId);
}
catch (Exception ex)
{
_logger.LogError(ex, "Background cache update failed for WorkAreaId: {WorkAreaId}", workAreaId);
}
_logger.LogInfo("{Count} work items fetched successfully for WorkAreaId: {WorkAreaId}", workItemVMs.Count, workAreaId);
return ApiResponse<object>.SuccessResponse(workItemVMs, $"{workItemVMs.Count} tasks fetched successfully.", 200);
}
catch (Exception ex)
{
// --- Step 6: Graceful Error Handling ---
_logger.LogError(ex, "An unexpected error occurred while getting work items for WorkAreaId: {WorkAreaId}", workAreaId);
return ApiResponse<object>.ErrorResponse("An internal server error occurred.", null, 500);
}
}
#endregion
#region =================================================================== Project Infrastructre Manage APIs ===================================================================
public async Task<ApiResponse<object>> ManageProjectInfra(List<InfraDto> infraDots, Guid tenantId, Employee loggedInEmployee)
{
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 = _mapper.Map<Building>(item.Building);
building.TenantId = tenantId;
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 = _mapper.Map<Floor>(item.Floor);
floor.TenantId = tenantId;
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 = _mapper.Map<WorkArea>(item.WorkArea);
workArea.TenantId = tenantId;
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 };
return ApiResponse<object>.SuccessResponse(responseData, responseMessage, 200);
}
return ApiResponse<object>.ErrorResponse("Invalid details.", "Infra Details are not valid.", 400);
}
public async Task<ServiceResponse> ManageProjectInfraAsync(List<InfraDto> infraDtos, Guid tenantId, Employee loggedInEmployee)
{
// 1. Guard Clause: Handle null or empty input gracefully.
if (infraDtos == null || !infraDtos.Any())
{
return new ServiceResponse
{
Response = ApiResponse<object>.ErrorResponse("Invalid details.", "No infrastructure details were provided.", 400)
};
}
var responseData = new InfraVM();
var messages = new List<string>();
var projectIds = new HashSet<Guid>(); // Use HashSet for automatic duplicate handling.
var cacheUpdateTasks = new List<Task>();
// --- Pre-fetch parent entities to avoid N+1 query problem ---
// 2. Gather all parent IDs needed for validation and context.
var requiredBuildingIds = infraDtos
.Where(i => i.Floor?.BuildingId != null)
.Select(i => i.Floor!.BuildingId)
.Distinct()
.ToList();
var requiredFloorIds = infraDtos
.Where(i => i.WorkArea?.FloorId != null)
.Select(i => i.WorkArea!.FloorId)
.Distinct()
.ToList();
// 3. Fetch all required parent entities in single batch queries.
var buildingsDict = await _context.Buildings
.Where(b => requiredBuildingIds.Contains(b.Id))
.ToDictionaryAsync(b => b.Id);
var floorsDict = await _context.Floor
.Include(f => f.Building) // Eagerly load Building for later use
.Where(f => requiredFloorIds.Contains(f.Id))
.ToDictionaryAsync(f => f.Id);
// --- End Pre-fetching ---
// 4. Process all entities and add them to the context's change tracker.
foreach (var item in infraDtos)
{
if (item.Building != null)
{
ProcessBuilding(item.Building, tenantId, responseData, messages, projectIds, cacheUpdateTasks);
}
if (item.Floor != null)
{
ProcessFloor(item.Floor, tenantId, responseData, messages, projectIds, cacheUpdateTasks, buildingsDict);
}
if (item.WorkArea != null)
{
ProcessWorkArea(item.WorkArea, tenantId, responseData, messages, projectIds, cacheUpdateTasks, floorsDict);
}
}
// 5. Save all changes to the database in a single transaction.
var changedRecordCount = await _context.SaveChangesAsync();
// If no changes were actually made, we can exit early.
if (changedRecordCount == 0)
{
return new ServiceResponse
{
Response = ApiResponse<object>.SuccessResponse(responseData, "No changes detected in the provided infrastructure details.", 200)
};
}
// 6. Execute all cache updates concurrently after the DB save is successful.
await Task.WhenAll(cacheUpdateTasks);
// 7. Consolidate messages and create notification payload.
string finalResponseMessage = messages.LastOrDefault() ?? "Infrastructure managed successfully.";
string logMessage = $"{string.Join(", ", messages)} by {loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds.ToList(), Message = logMessage };
// TODO: Dispatch the 'notification' object to your notification service.
return new ServiceResponse
{
Notification = notification,
Response = ApiResponse<object>.SuccessResponse(responseData, finalResponseMessage, 200)
};
}
/// <summary>
/// Manages a batch of infrastructure changes (creates/updates for Buildings, Floors, and WorkAreas).
/// This method is optimized to perform all database operations in a single, atomic transaction.
/// </summary>
public async Task<ApiResponse<object>> ManageProjectInfraAsync1(List<InfraDto> infraDtos, Guid tenantId, Employee loggedInEmployee)
{
// --- Step 1: Input Validation ---
if (infraDtos == null || !infraDtos.Any())
{
_logger.LogWarning("ManageProjectInfraAsync called with null or empty DTO list.");
return ApiResponse<object>.ErrorResponse("Invalid details.", "Infrastructure data cannot be empty.", 400);
}
_logger.LogInfo("Begin ManageProjectInfraAsync for {DtoCount} items, TenantId: {TenantId}, User: {UserId}", infraDtos.Count, tenantId, loggedInEmployee.Id);
// --- Step 2: Categorize DTOs by Type and Action ---
var buildingsToCreateDto = infraDtos.Where(i => i.Building != null && i.Building.Id == null).Select(i => i.Building!).ToList();
var buildingsToUpdateDto = infraDtos.Where(i => i.Building != null && i.Building.Id != null).Select(i => i.Building!).ToList();
var floorsToCreateDto = infraDtos.Where(i => i.Floor != null && i.Floor.Id == null).Select(i => i.Floor!).ToList();
var floorsToUpdateDto = infraDtos.Where(i => i.Floor != null && i.Floor.Id != null).Select(i => i.Floor!).ToList();
var workAreasToCreateDto = infraDtos.Where(i => i.WorkArea != null && i.WorkArea.Id == null).Select(i => i.WorkArea!).ToList();
var workAreasToUpdateDto = infraDtos.Where(i => i.WorkArea != null && i.WorkArea.Id != null).Select(i => i.WorkArea!).ToList();
_logger.LogDebug("Categorized DTOs...");
try
{
// --- Step 3: Fetch all required existing data in bulk ---
// Fetch existing entities to be updated
var buildingIdsToUpdate = buildingsToUpdateDto.Select(d => d.Id!.Value).ToList();
var existingBuildings = await _context.Buildings.Where(b => buildingIdsToUpdate.Contains(b.Id) && b.TenantId == tenantId).ToDictionaryAsync(b => b.Id);
var floorIdsToUpdate = floorsToUpdateDto.Select(d => d.Id!.Value).ToList();
var existingFloors = await _context.Floor.Include(f => f.Building).Where(f => floorIdsToUpdate.Contains(f.Id) && f.TenantId == tenantId).ToDictionaryAsync(f => f.Id);
var workAreaIdsToUpdate = workAreasToUpdateDto.Select(d => d.Id!.Value).ToList();
var existingWorkAreas = await _context.WorkAreas.Include(wa => wa.Floor!.Building).Where(wa => workAreaIdsToUpdate.Contains(wa.Id) && wa.TenantId == tenantId).ToDictionaryAsync(wa => wa.Id);
// Fetch parent entities for items being created to get their ProjectIds
var buildingIdsForNewFloors = floorsToCreateDto.Select(f => f.BuildingId).ToList();
var parentBuildingsForNewFloors = await _context.Buildings.Where(b => buildingIdsForNewFloors.Contains(b.Id)).ToDictionaryAsync(b => b.Id);
var floorIdsForNewWorkAreas = workAreasToCreateDto.Select(wa => wa.FloorId).ToList();
var parentFloorsForNewWorkAreas = await _context.Floor.Include(f => f.Building).Where(f => floorIdsForNewWorkAreas.Contains(f.Id)).ToDictionaryAsync(f => f.Id);
_logger.LogInfo("Fetched existing entities and parents for new items.");
// --- Step 4: Aggregate all affected ProjectIds for Security Check ---
var affectedProjectIds = new HashSet<Guid>();
// From buildings being created/updated
buildingsToCreateDto.ForEach(b => affectedProjectIds.Add(b.ProjectId));
foreach (var b in existingBuildings.Values) { affectedProjectIds.Add(b.ProjectId); }
// From floors being created/updated
foreach (var f in floorsToCreateDto) { if (parentBuildingsForNewFloors.TryGetValue(f.BuildingId, out var b)) affectedProjectIds.Add(b.ProjectId); }
foreach (var f in existingFloors.Values) { if (f.Building != null) affectedProjectIds.Add(f.Building.ProjectId); }
// From work areas being created/updated
foreach (var wa in workAreasToCreateDto) { if (parentFloorsForNewWorkAreas.TryGetValue(wa.FloorId, out var f) && f.Building != null) affectedProjectIds.Add(f.Building.ProjectId); }
foreach (var wa in existingWorkAreas.Values) { if (wa.Floor?.Building != null) affectedProjectIds.Add(wa.Floor.Building.ProjectId); }
// Security Check against the complete list of affected projects
var hasPermission = await _permission.HasPermission(PermissionsMaster.ManageProjectInfra, loggedInEmployee.Id);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} trying to manage infrastructure for projects.", loggedInEmployee.Id);
return ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to manage infrastructure for one or more of the specified projects.", 403);
}
// --- Step 5: Process all logic IN MEMORY, tracking changes ---
// Process Buildings
var createdBuildings = new List<Building>();
foreach (var dto in buildingsToCreateDto)
{
var newBuilding = _mapper.Map<Building>(dto);
newBuilding.TenantId = tenantId;
createdBuildings.Add(newBuilding);
}
foreach (var dto in buildingsToUpdateDto) { if (existingBuildings.TryGetValue(dto.Id!.Value, out var b)) _mapper.Map(dto, b); }
// Process Floors
var createdFloors = new List<Floor>();
foreach (var dto in floorsToCreateDto)
{
var newFloor = _mapper.Map<Floor>(dto);
newFloor.TenantId = tenantId;
createdFloors.Add(newFloor);
}
foreach (var dto in floorsToUpdateDto) { if (existingFloors.TryGetValue(dto.Id!.Value, out var f)) _mapper.Map(dto, f); }
// Process WorkAreas
var createdWorkAreas = new List<WorkArea>();
foreach (var dto in workAreasToCreateDto)
{
var newWorkArea = _mapper.Map<WorkArea>(dto);
newWorkArea.TenantId = tenantId;
createdWorkAreas.Add(newWorkArea);
}
foreach (var dto in workAreasToUpdateDto) { if (existingWorkAreas.TryGetValue(dto.Id!.Value, out var wa)) _mapper.Map(dto, wa); }
// --- Step 6: Save all database changes in a SINGLE TRANSACTION ---
if (createdBuildings.Any()) _context.Buildings.AddRange(createdBuildings);
if (createdFloors.Any()) _context.Floor.AddRange(createdFloors);
if (createdWorkAreas.Any()) _context.WorkAreas.AddRange(createdWorkAreas);
if (_context.ChangeTracker.HasChanges())
{
await _context.SaveChangesAsync();
_logger.LogInfo("Database save successful.");
}
// --- Step 7: Update Cache using the aggregated ProjectIds (Non-blocking) ---
var finalProjectIds = affectedProjectIds.ToList();
if (finalProjectIds.Any())
{
_ = Task.Run(async () =>
{
try
{
_logger.LogInfo("Queuing background cache update for {ProjectCount} projects.", finalProjectIds.Count);
// Assuming your cache service has a method to handle this.
await _cache.RemoveProjectsAsync(finalProjectIds);
_logger.LogInfo("Background cache update task completed for projects: {ProjectIds}", string.Join(", ", finalProjectIds));
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred during the background cache update task for projects: {ProjectIds}", string.Join(", ", finalProjectIds));
}
});
}
// --- Step 8: Prepare and return a clear response ---
var responseVm = new { /* ... as before ... */ };
return ApiResponse<object>.SuccessResponse(responseVm, "Infrastructure changes processed successfully.", 200);
}
catch (Exception ex)
{
_logger.LogError(ex, "An unexpected error occurred in ManageProjectInfraAsync.");
return ApiResponse<object>.ErrorResponse("Internal Server Error", "An unexpected error occurred.", 500);
}
}
/// <summary>
/// Creates or updates a batch of work items.
/// This method is optimized to perform all database operations in a single, atomic transaction.
/// </summary>
public async Task<ApiResponse<List<WorkItemVM>>> CreateProjectTaskAsync(List<WorkItemDto> workItemDtos, Guid tenantId, Employee loggedInEmployee)
{
_logger.LogInfo("CreateProjectTask called with {Count} items by user {UserId}", workItemDtos?.Count ?? 0, loggedInEmployee.Id);
// --- Step 1: Input Validation ---
if (workItemDtos == null || !workItemDtos.Any())
{
_logger.LogWarning("No work items provided in the request.");
return ApiResponse<List<WorkItemVM>>.ErrorResponse("Invalid details.", "Work Item details list cannot be empty.", 400);
}
// --- Step 2: Fetch all required existing data in bulk ---
var workAreaIds = workItemDtos.Select(d => d.WorkAreaID).Distinct().ToList();
var workItemIdsToUpdate = workItemDtos.Where(d => d.Id.HasValue).Select(d => d.Id!.Value).ToList();
// Fetch all relevant WorkAreas and their parent hierarchy in ONE query
var workAreasFromDb = await _context.WorkAreas
.Where(wa => wa.Floor != null && wa.Floor.Building != null && workAreaIds.Contains(wa.Id) && wa.TenantId == tenantId)
.Include(wa => wa.Floor!.Building) // Eagerly load the entire path
.ToDictionaryAsync(wa => wa.Id); // Dictionary for fast lookups
// Fetch all existing WorkItems that need updating in ONE query
var existingWorkItemsToUpdate = await _context.WorkItems
.Where(wi => workItemIdsToUpdate.Contains(wi.Id) && wi.TenantId == tenantId)
.ToDictionaryAsync(wi => wi.Id); // Dictionary for fast lookups
// --- (Placeholder) Security Check ---
// You MUST verify the user has permission to modify ALL WorkAreas in the batch.
var projectIdsInBatch = workAreasFromDb.Values.Select(wa => wa.Floor!.Building!.ProjectId).Distinct();
var hasPermission = await _permission.HasPermission(PermissionsMaster.ManageProjectInfra, loggedInEmployee.Id);
if (!hasPermission)
{
_logger.LogWarning("Access DENIED for user {UserId} trying to create/update tasks.", loggedInEmployee.Id);
return ApiResponse<List<WorkItemVM>>.ErrorResponse("Access Denied.", "You do not have permission to modify tasks in one or more of the specified work areas.", 403);
}
var workItemsToCreate = new List<WorkItem>();
var workItemsToModify = new List<WorkItem>();
var workDeltaForCache = new Dictionary<Guid, (double Planned, double Completed)>(); // WorkAreaId -> (Delta)
string message = "";
// --- Step 3: Process all logic IN MEMORY, tracking changes ---
foreach (var dto in workItemDtos)
{
if (!workAreasFromDb.TryGetValue(dto.WorkAreaID, out var workArea))
{
_logger.LogWarning("Skipping item because WorkAreaId {WorkAreaId} was not found or is invalid.", dto.WorkAreaID);
continue; // Skip this item as its parent WorkArea is invalid
}
if (dto.Id.HasValue && existingWorkItemsToUpdate.TryGetValue(dto.Id.Value, out var existingWorkItem))
{
// --- UPDATE Logic ---
var plannedDelta = dto.PlannedWork - existingWorkItem.PlannedWork;
var completedDelta = dto.CompletedWork - existingWorkItem.CompletedWork;
// Apply changes from DTO to the fetched entity to prevent data loss
_mapper.Map(dto, existingWorkItem);
workItemsToModify.Add(existingWorkItem);
// Track the change in work for cache update
workDeltaForCache[workArea.Id] = (
workDeltaForCache.GetValueOrDefault(workArea.Id).Planned + plannedDelta,
workDeltaForCache.GetValueOrDefault(workArea.Id).Completed + completedDelta
);
message = $"Task Updated in Building: {workArea.Floor?.Building?.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
}
else
{
// --- CREATE Logic ---
var newWorkItem = _mapper.Map<WorkItem>(dto);
newWorkItem.Id = Guid.NewGuid(); // Ensure new GUID is set
newWorkItem.TenantId = tenantId;
workItemsToCreate.Add(newWorkItem);
// Track the change in work for cache update
workDeltaForCache[workArea.Id] = (
workDeltaForCache.GetValueOrDefault(workArea.Id).Planned + newWorkItem.PlannedWork,
workDeltaForCache.GetValueOrDefault(workArea.Id).Completed + newWorkItem.CompletedWork
);
message = $"Task Added in Building: {workArea.Floor?.Building?.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {loggedInEmployee.FirstName} {loggedInEmployee.LastName}";
}
}
try
{
// --- Step 4: Save all database changes in a SINGLE TRANSACTION ---
if (workItemsToCreate.Any()) _context.WorkItems.AddRange(workItemsToCreate);
if (workItemsToModify.Any()) _context.WorkItems.UpdateRange(workItemsToModify); // EF Core handles individual updates correctly here
if (workItemsToCreate.Any() || workItemsToModify.Any())
{
await _context.SaveChangesAsync();
_logger.LogInfo("Successfully saved {CreatedCount} new and {UpdatedCount} updated work items.", workItemsToCreate.Count, workItemsToModify.Count);
// --- Step 5: Update Cache and SignalR AFTER successful DB save ---
var allAffectedItems = workItemsToCreate.Concat(workItemsToModify).ToList();
await UpdateCacheAndNotify(workDeltaForCache, allAffectedItems);
}
}
catch (DbUpdateException ex)
{
_logger.LogError(ex, "A database error occurred while creating/updating tasks.");
return ApiResponse<List<WorkItemVM>>.ErrorResponse("Database Error", "Failed to save changes.", 500);
}
// --- Step 6: Prepare and return the response ---
var allProcessedItems = workItemsToCreate.Concat(workItemsToModify).ToList();
var responseList = allProcessedItems.Select(wi => new WorkItemVM
{
WorkItemId = wi.Id,
WorkItem = wi
}).ToList();
return ApiResponse<List<WorkItemVM>>.SuccessResponse(responseList, message, 200);
}
//public async Task<IActionResult> DeleteProjectTask(Guid id)
//{
// 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 projectId = floor?.Building?.ProjectId;
// 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.SendNotificationAsync(notification);
// await _cache.DeleteWorkItemByIdAsync(task.Id);
// if (projectId != null)
// {
// await _cache.DeleteProjectByIdAsync(projectId.Value);
// }
// }
// 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.LogWarning("Task with ID {WorkItemId} not found ID database", id);
// }
// return Ok(ApiResponse<object>.SuccessResponse(new { }, "Task deleted successfully", 200));
//}
#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 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, Guid tenantId)
{
// --- 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.LogError(ex, "Failed to update cache for project {ProjectId} : ", projectId);
}
// Map from the database entity to the response ViewModel.
return dbProject;
}
private async Task UpdateCacheInBackground(Project project)
{
try
{
// This logic can be more complex, but the idea is to update or add.
var demo = await _cache.UpdateProjectDetailsOnly(project);
if (!demo)
{
await _cache.AddProjectDetails(project);
}
_logger.LogInfo("Background cache update succeeded for project {ProjectId}.", project.Id);
}
catch (Exception ex)
{
_logger.LogError(ex, "Background cache update failed for project {ProjectId} ", project.Id);
}
}
private async Task UpdateCacheAndNotify(Dictionary<Guid, (double Planned, double Completed)> workDelta, List<WorkItem> affectedItems)
{
try
{
// Update planned/completed work totals
var cacheUpdateTasks = workDelta.Select(kvp =>
_cache.UpdatePlannedAndCompleteWorksInBuilding(kvp.Key, kvp.Value.Planned, kvp.Value.Completed));
await Task.WhenAll(cacheUpdateTasks);
_logger.LogInfo("Background cache work totals update completed for {AreaCount} areas.", workDelta.Count);
// Update the details of the individual work items in the cache
await _cache.ManageWorkItemDetails(affectedItems);
_logger.LogInfo("Background cache work item details update completed for {ItemCount} items.", affectedItems.Count);
// Add SignalR notification logic here if needed
}
catch (Exception ex)
{
_logger.LogError(ex, "An error occurred during background cache update/notification.");
}
}
private void ProcessBuilding(BuildingDto dto, Guid tenantId, InfraVM responseData, List<string> messages, ISet<Guid> projectIds, List<Task> cacheTasks)
{
Building building = _mapper.Map<Building>(dto);
building.TenantId = tenantId;
bool isNew = dto.Id == null;
if (isNew)
{
_context.Buildings.Add(building);
messages.Add("Building Added");
cacheTasks.Add(_cache.AddBuildngInfra(building.ProjectId, building));
}
else
{
_context.Buildings.Update(building);
messages.Add("Building Updated");
cacheTasks.Add(_cache.UpdateBuildngInfra(building.ProjectId, building));
}
responseData.building = building;
projectIds.Add(building.ProjectId);
}
private void ProcessFloor(FloorDto dto, Guid tenantId, InfraVM responseData, List<string> messages, ISet<Guid> projectIds, List<Task> cacheTasks, IDictionary<Guid, Building> buildings)
{
Floor floor = _mapper.Map<Floor>(dto);
floor.TenantId = tenantId;
// Use the pre-fetched dictionary for parent lookup.
Building? parentBuilding = buildings.TryGetValue(dto.BuildingId, out var b) ? b : null;
bool isNew = dto.Id == null;
if (isNew)
{
_context.Floor.Add(floor);
messages.Add($"Floor Added in Building: {parentBuilding?.Name ?? "Unknown"}");
cacheTasks.Add(_cache.AddBuildngInfra(parentBuilding?.ProjectId ?? Guid.Empty, floor: floor));
}
else
{
_context.Floor.Update(floor);
messages.Add($"Floor Updated in Building: {parentBuilding?.Name ?? "Unknown"}");
cacheTasks.Add(_cache.UpdateBuildngInfra(parentBuilding?.ProjectId ?? Guid.Empty, floor: floor));
}
responseData.floor = floor;
if (parentBuilding != null) projectIds.Add(parentBuilding.ProjectId);
}
private void ProcessWorkArea(WorkAreaDto dto, Guid tenantId, InfraVM responseData, List<string> messages, ISet<Guid> projectIds, List<Task> cacheTasks, IDictionary<Guid, Floor> floors)
{
WorkArea workArea = _mapper.Map<WorkArea>(dto);
workArea.TenantId = tenantId;
// Use the pre-fetched dictionary for parent lookup.
Floor? parentFloor = floors.TryGetValue(dto.FloorId, out var f) ? f : null;
var parentBuilding = parentFloor?.Building;
bool isNew = dto.Id == null;
if (isNew)
{
_context.WorkAreas.Add(workArea);
messages.Add($"Work Area Added in Building: {parentBuilding?.Name ?? "Unknown"}, on Floor: {parentFloor?.FloorName ?? "Unknown"}");
cacheTasks.Add(_cache.AddBuildngInfra(parentBuilding?.ProjectId ?? Guid.Empty, workArea: workArea, buildingId: parentBuilding?.Id));
}
else
{
_context.WorkAreas.Update(workArea);
messages.Add($"Work Area Updated in Building: {parentBuilding?.Name ?? "Unknown"}, on Floor: {parentFloor?.FloorName ?? "Unknown"}");
cacheTasks.Add(_cache.UpdateBuildngInfra(parentBuilding?.ProjectId ?? Guid.Empty, workArea: workArea, buildingId: parentBuilding?.Id));
}
responseData.workArea = workArea;
if (parentBuilding != null) projectIds.Add(parentBuilding.ProjectId);
}
#endregion
}
}