created two end pointe for assigne and unassign project for perticular employee

This commit is contained in:
Pramod Mahajan 2025-06-09 09:48:35 +05:30
parent 681cdd690c
commit 280fc922ff

View File

@ -13,6 +13,7 @@ using MarcoBMS.Services.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace MarcoBMS.Services.Controllers
{
@ -663,5 +664,122 @@ namespace MarcoBMS.Services.Controllers
}
[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();
List<object>? result = new List<object>();
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;
}
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();
}
}
catch (Exception ex) {
return Ok(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
}
}
return Ok(ApiResponse<object>.SuccessResponse(result, "Data saved successfully", 200));
}
else
{
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "All Field is required", 400));
}
}
}
}