Selecting the default services in project allocation
This commit is contained in:
parent
8609db64d2
commit
c06dc8ebe7
@ -329,7 +329,7 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
|
|
||||||
|
|
||||||
[HttpGet("basic")]
|
[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 loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||||
var employeeQuery = _context.Employees.Where(e => e.IsActive);
|
var employeeQuery = _context.Employees.Where(e => e.IsActive);
|
||||||
@ -923,9 +923,9 @@ namespace MarcoBMS.Services.Controllers
|
|||||||
var emailExists = await _context.Employees
|
var emailExists = await _context.Employees
|
||||||
.AnyAsync(e => e.Email == model.Email && e.OrganizationId == model.OrganizationId);
|
.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));
|
return StatusCode(409, ApiResponse<object>.ErrorResponse("Employee with email already exists", "Employee with this email already exists", 409));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 ---
|
// --- Step 2: Fetch all relevant existing data in ONE database call ---
|
||||||
var projectIdsInDto = allocationsDto.Select(p => p.ProjectId).ToList();
|
var projectIdsInDto = allocationsDto.Select(p => p.ProjectId).ToList();
|
||||||
@ -1028,6 +1034,11 @@ namespace Marco.Pms.Services.Service
|
|||||||
|
|
||||||
var processedAllocations = new List<ProjectAllocation>();
|
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 ---
|
// --- Step 3: Process all logic IN MEMORY, tracking changes ---
|
||||||
foreach (var dto in allocationsDto)
|
foreach (var dto in allocationsDto)
|
||||||
{
|
{
|
||||||
@ -1049,11 +1060,13 @@ namespace Marco.Pms.Services.Service
|
|||||||
{
|
{
|
||||||
if (existingAllocation == null)
|
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.
|
// Create a new allocation because an active one doesn't exist.
|
||||||
var newAllocation = _mapper.Map<ProjectAllocation>(dto);
|
var newAllocation = _mapper.Map<ProjectAllocation>(dto);
|
||||||
newAllocation.EmployeeId = employeeId;
|
newAllocation.EmployeeId = employeeId;
|
||||||
newAllocation.TenantId = tenantId;
|
newAllocation.TenantId = tenantId;
|
||||||
newAllocation.AllocationDate = DateTime.UtcNow;
|
newAllocation.AllocationDate = DateTime.UtcNow;
|
||||||
|
newAllocation.ServiceId = dto.ServiceId ?? serviceProject?.ProjectService?.ServiceId;
|
||||||
newAllocation.IsActive = true;
|
newAllocation.IsActive = true;
|
||||||
_context.ProjectAllocations.Add(newAllocation);
|
_context.ProjectAllocations.Add(newAllocation);
|
||||||
processedAllocations.Add(newAllocation);
|
processedAllocations.Add(newAllocation);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user