diff --git a/Marco.Pms.Services/Controllers/EmployeeController.cs b/Marco.Pms.Services/Controllers/EmployeeController.cs index 1bdb4a8..fc67d76 100644 --- a/Marco.Pms.Services/Controllers/EmployeeController.cs +++ b/Marco.Pms.Services/Controllers/EmployeeController.cs @@ -329,7 +329,7 @@ namespace MarcoBMS.Services.Controllers [HttpGet("basic")] - public async Task GetEmployeesByProjectBasic(Guid? projectId, [FromQuery] string? searchString,[FromQuery] bool allEmployee) + public async Task GetEmployeesByProjectBasic(Guid? projectId, [FromQuery] string? searchString, [FromQuery] bool allEmployee) { var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); var employeeQuery = _context.Employees.Where(e => e.IsActive); @@ -356,7 +356,7 @@ namespace MarcoBMS.Services.Controllers var query = employeeQuery.OrderBy(e => e.FirstName); - if (!allEmployee) + if (!allEmployee) { query = (IOrderedQueryable)query.Take(10); } @@ -923,9 +923,9 @@ namespace MarcoBMS.Services.Controllers var emailExists = await _context.Employees .AnyAsync(e => e.Email == model.Email && e.OrganizationId == model.OrganizationId); - if (emailExists) + if (emailExists && !string.IsNullOrWhiteSpace(model.Email)) { - _logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email ?? string.Empty, model.OrganizationId); + _logger.LogInfo("Employee email already exists in org. Email={Email}, Org={OrgId}", model.Email, model.OrganizationId); return StatusCode(409, ApiResponse.ErrorResponse("Employee with email already exists", "Employee with this email already exists", 409)); } diff --git a/Marco.Pms.Services/Service/ProjectServices.cs b/Marco.Pms.Services/Service/ProjectServices.cs index 096ac56..6d8c105 100644 --- a/Marco.Pms.Services/Service/ProjectServices.cs +++ b/Marco.Pms.Services/Service/ProjectServices.cs @@ -1013,6 +1013,12 @@ namespace Marco.Pms.Services.Service } } + var selectedEmployee = await _context.Employees.FirstOrDefaultAsync(e => e.Id == employeeId); + if (selectedEmployee == null) + { + _logger.LogWarning("Employee not found while assigning the projects to employee"); + return ApiResponse>.ErrorResponse("Employee not found", "Employee not found", 404); + } // --- Step 2: Fetch all relevant existing data in ONE database call --- var projectIdsInDto = allocationsDto.Select(p => p.ProjectId).ToList(); @@ -1028,6 +1034,11 @@ namespace Marco.Pms.Services.Service var processedAllocations = new List(); + var serviceProjects = await _context.ProjectOrgMappings + .Include(ps => ps.ProjectService) + .Where(ps => ps.ProjectService != null && projectIdsInDto.Contains(ps.ProjectService.ProjectId) && + ps.OrganizationId == selectedEmployee.OrganizationId && ps.TenantId == tenantId).ToListAsync(); + // --- Step 3: Process all logic IN MEMORY, tracking changes --- foreach (var dto in allocationsDto) { @@ -1049,11 +1060,13 @@ namespace Marco.Pms.Services.Service { if (existingAllocation == null) { + var serviceProject = serviceProjects.FirstOrDefault(ps => ps.ProjectService != null && ps.ProjectService.ProjectId == dto.ProjectId); // Create a new allocation because an active one doesn't exist. var newAllocation = _mapper.Map(dto); newAllocation.EmployeeId = employeeId; newAllocation.TenantId = tenantId; newAllocation.AllocationDate = DateTime.UtcNow; + newAllocation.ServiceId = dto.ServiceId ?? serviceProject?.ProjectService?.ServiceId; newAllocation.IsActive = true; _context.ProjectAllocations.Add(newAllocation); processedAllocations.Add(newAllocation);