Selecting the default services in project allocation

This commit is contained in:
ashutosh.nehete 2025-10-09 16:00:26 +05:30
parent 8609db64d2
commit c06dc8ebe7
2 changed files with 17 additions and 4 deletions

View File

@ -329,7 +329,7 @@ namespace MarcoBMS.Services.Controllers
[HttpGet("basic")]
public async Task<IActionResult> GetEmployeesByProjectBasic(Guid? projectId, [FromQuery] string? searchString,[FromQuery] bool allEmployee)
public async Task<IActionResult> GetEmployeesByProjectBasic(Guid? projectId, [FromQuery] string? searchString, [FromQuery] bool allEmployee)
{
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
var employeeQuery = _context.Employees.Where(e => e.IsActive);
@ -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<object>.ErrorResponse("Employee with email already exists", "Employee with this email already exists", 409));
}

View File

@ -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<List<ProjectAllocationVM>>.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<ProjectAllocation>();
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<ProjectAllocation>(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);