935 lines
		
	
	
		
			43 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			935 lines
		
	
	
		
			43 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
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.Mapper;
 | 
						|
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.Hubs;
 | 
						|
using MarcoBMS.Services.Helpers;
 | 
						|
using MarcoBMS.Services.Service;
 | 
						|
using Microsoft.AspNetCore.Authorization;
 | 
						|
using Microsoft.AspNetCore.Mvc;
 | 
						|
using Microsoft.AspNetCore.SignalR;
 | 
						|
using Microsoft.EntityFrameworkCore;
 | 
						|
 | 
						|
namespace MarcoBMS.Services.Controllers
 | 
						|
{
 | 
						|
    [Route("api/[controller]")]
 | 
						|
    [ApiController]
 | 
						|
    [Authorize]
 | 
						|
    public class ProjectController : ControllerBase
 | 
						|
    {
 | 
						|
        private readonly ApplicationDbContext _context;
 | 
						|
        private readonly UserHelper _userHelper;
 | 
						|
        private readonly ILoggingService _logger;
 | 
						|
        private readonly RolesHelper _rolesHelper;
 | 
						|
        private readonly ProjectsHelper _projectsHelper;
 | 
						|
        private readonly IHubContext<MarcoHub> _signalR;
 | 
						|
 | 
						|
 | 
						|
        public ProjectController(ApplicationDbContext context, UserHelper userHelper, ILoggingService logger, RolesHelper rolesHelper, ProjectsHelper projectHelper, IHubContext<MarcoHub> signalR)
 | 
						|
        {
 | 
						|
            _context = context;
 | 
						|
            _userHelper = userHelper;
 | 
						|
            _logger = logger;
 | 
						|
            _rolesHelper = rolesHelper;
 | 
						|
            _projectsHelper = projectHelper;
 | 
						|
            _signalR = signalR;
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpGet("list/basic")]
 | 
						|
        public async Task<IActionResult> GetAllProjects()
 | 
						|
        {
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
 | 
						|
            // Defensive check for null employee (important for robust APIs)
 | 
						|
            if (LoggedInEmployee == null)
 | 
						|
            {
 | 
						|
                return Unauthorized(ApiResponse<object>.ErrorResponse("Employee not found.", null, 401));
 | 
						|
            }
 | 
						|
 | 
						|
 | 
						|
            List<Project> projects = await _projectsHelper.GetMyProjects(tenantId, LoggedInEmployee);
 | 
						|
 | 
						|
 | 
						|
            // 4. Project projection to ProjectInfoVM
 | 
						|
            // This part is already quite efficient.
 | 
						|
            // Ensure ToProjectInfoVMFromProject() is also optimized and doesn't perform N+1 queries.
 | 
						|
            // If ProjectInfoVM only needs a subset of Project properties,
 | 
						|
            // you can use a LINQ Select directly on the IQueryable before ToListAsync()
 | 
						|
            // to fetch only the required columns from the database.
 | 
						|
            List<ProjectInfoVM> response = projects
 | 
						|
                .Select(project => project.ToProjectInfoVMFromProject())
 | 
						|
                .ToList();
 | 
						|
 | 
						|
 | 
						|
            //List<ProjectInfoVM> response = new List<ProjectInfoVM>();
 | 
						|
 | 
						|
            //foreach (var project in projects)
 | 
						|
            //{
 | 
						|
            //    response.Add(project.ToProjectInfoVMFromProject());
 | 
						|
            //}
 | 
						|
 | 
						|
            return Ok(ApiResponse<object>.SuccessResponse(response, "Success.", 200));
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpGet("list")]
 | 
						|
        public async Task<IActionResult> GetAll()
 | 
						|
        {
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            //List<FeaturePermission> featurePermission = await _rolesHelper.GetFeaturePermissionByEmployeeID(LoggedInEmployee.Id);
 | 
						|
            //string[] projectsId = [];
 | 
						|
            //List<Project> projects = new List<Project>();
 | 
						|
 | 
						|
            ///* User with permission manage project  can see all projects */
 | 
						|
            //if (featurePermission != null && featurePermission.Exists(c => c.Id.ToString() == "172fc9b6-755b-4f62-ab26-55c34a330614"))
 | 
						|
            //{
 | 
						|
            //    projects = await _projectsHelper.GetAllProjectByTanentID(LoggedInEmployee.TenantId);
 | 
						|
            //}
 | 
						|
            //else
 | 
						|
            //{
 | 
						|
            //    List<ProjectAllocation> allocation = await _projectsHelper.GetProjectByEmployeeID(LoggedInEmployee.Id);
 | 
						|
            //    projectsId = allocation.Select(c => c.ProjectId.ToString()).ToArray();
 | 
						|
            //    projects = await _context.Projects.Where(c => projectsId.Contains(c.Id.ToString()) && c.TenantId == tenantId).ToListAsync();
 | 
						|
            //}
 | 
						|
 | 
						|
            List<Project> projects = await _projectsHelper.GetMyProjects(tenantId, LoggedInEmployee);
 | 
						|
 | 
						|
 | 
						|
 | 
						|
 | 
						|
            List<ProjectListVM> response = new List<ProjectListVM>();
 | 
						|
            foreach (var project in projects)
 | 
						|
            {
 | 
						|
                var result = project.ToProjectListVMFromProject();
 | 
						|
                var team = await _context.ProjectAllocations.Where(p => p.TenantId == tenantId && p.ProjectId == project.Id && p.IsActive == true).ToListAsync();
 | 
						|
 | 
						|
                result.TeamSize = team.Count();
 | 
						|
 | 
						|
                List<Building> buildings = await _context.Buildings.Where(b => b.ProjectId == project.Id && b.TenantId == tenantId).ToListAsync();
 | 
						|
                List<Guid> idList = buildings.Select(b => b.Id).ToList();
 | 
						|
 | 
						|
                List<Floor> floors = await _context.Floor.Where(f => idList.Contains(f.BuildingId) && f.TenantId == tenantId).ToListAsync();
 | 
						|
                idList = floors.Select(f => f.Id).ToList();
 | 
						|
 | 
						|
                List<WorkArea> workAreas = await _context.WorkAreas.Where(a => idList.Contains(a.FloorId) && a.TenantId == tenantId).ToListAsync();
 | 
						|
                idList = workAreas.Select(a => a.Id).ToList();
 | 
						|
 | 
						|
                List<WorkItem> workItems = await _context.WorkItems.Where(i => idList.Contains(i.WorkAreaId) && i.TenantId == tenantId).Include(i => i.ActivityMaster).ToListAsync();
 | 
						|
                double completedTask = 0;
 | 
						|
                double plannedTask = 0;
 | 
						|
                foreach (var workItem in workItems)
 | 
						|
                {
 | 
						|
                    completedTask += workItem.CompletedWork;
 | 
						|
                    plannedTask += workItem.PlannedWork;
 | 
						|
                }
 | 
						|
                result.PlannedWork = plannedTask;
 | 
						|
                result.CompletedWork = completedTask;
 | 
						|
                response.Add(result);
 | 
						|
            }
 | 
						|
 | 
						|
            return Ok(ApiResponse<object>.SuccessResponse(response, "Success.", 200));
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpGet("get/{id}")]
 | 
						|
        public async Task<IActionResult> Get([FromRoute] Guid id)
 | 
						|
        {
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
 | 
						|
            var project = await _context.Projects.Where(c => c.TenantId == _userHelper.GetTenantId() && c.Id == id).SingleOrDefaultAsync();
 | 
						|
            if (project == null) return NotFound(ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404));
 | 
						|
            return Ok(ApiResponse<object>.SuccessResponse(project, "Success.", 200));
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpGet("details/{id}")]
 | 
						|
        public async Task<IActionResult> Details([FromRoute] Guid id)
 | 
						|
        {
 | 
						|
            // ProjectDetailsVM vm = new ProjectDetailsVM();
 | 
						|
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
 | 
						|
            var project = await _context.Projects.Where(c => c.TenantId == _userHelper.GetTenantId() && c.Id == id).Include(c => c.ProjectStatus).SingleOrDefaultAsync(); // includeProperties: "ProjectStatus,Tenant"); //_context.Stock.FindAsync(id);
 | 
						|
 | 
						|
            if (project == null)
 | 
						|
            {
 | 
						|
                return NotFound(ApiResponse<object>.ErrorResponse("Project not found", "Project not found", 404));
 | 
						|
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                //var project = projects.Where(c => c.Id == id).SingleOrDefault();
 | 
						|
                ProjectDetailsVM vm = await GetProjectViewModel(id, project);
 | 
						|
 | 
						|
                ProjectVM projectVM = new ProjectVM();
 | 
						|
                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 Ok(ApiResponse<object>.SuccessResponse(projectVM, "Success.", 200));
 | 
						|
            }
 | 
						|
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        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;
 | 
						|
        }
 | 
						|
 | 
						|
        private Guid GetTenantId()
 | 
						|
        {
 | 
						|
            return _userHelper.GetTenantId();
 | 
						|
            //var tenant = User.FindFirst("TenantId")?.Value;
 | 
						|
            //return (tenant != null ? Convert.ToInt32(tenant) : 1);
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpPost]
 | 
						|
        public async Task<IActionResult> Create([FromBody] CreateProjectDto projectDto)
 | 
						|
        {
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
 | 
						|
            Guid TenantId = GetTenantId();
 | 
						|
            var project = projectDto.ToProjectFromCreateProjectDto(TenantId);
 | 
						|
 | 
						|
            _context.Projects.Add(project);
 | 
						|
 | 
						|
            await _context.SaveChangesAsync();
 | 
						|
            var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Create_Project", Response = project.ToProjectDto() };
 | 
						|
 | 
						|
            await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
 | 
						|
            return Ok(ApiResponse<object>.SuccessResponse(project.ToProjectDto(), "Success.", 200));
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpPut]
 | 
						|
        [Route("update/{id}")]
 | 
						|
        public async Task<IActionResult> Update([FromRoute] Guid id, [FromBody] UpdateProjectDto updateProjectDto)
 | 
						|
        {
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
            try
 | 
						|
            {
 | 
						|
                Guid TenantId = GetTenantId();
 | 
						|
 | 
						|
                Project project = updateProjectDto.ToProjectFromUpdateProjectDto(TenantId, id);
 | 
						|
                _context.Projects.Update(project);
 | 
						|
 | 
						|
                await _context.SaveChangesAsync();
 | 
						|
 | 
						|
                var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Update_Project", Response = project.ToProjectDto() };
 | 
						|
 | 
						|
                await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
 | 
						|
                return Ok(ApiResponse<object>.SuccessResponse(project.ToProjectDto(), "Success.", 200));
 | 
						|
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        //[HttpPost("assign-employee")]
 | 
						|
        //public async Task<IActionResult> AssignEmployee(int? allocationid, int employeeId, int projectId)
 | 
						|
        //{
 | 
						|
        //    var employee = await _context.Employees.FindAsync(employeeId);
 | 
						|
        //    var project = _projectrepo.Get(c => c.Id == projectId);
 | 
						|
        //    if (employee == null || project == null)
 | 
						|
        //    {
 | 
						|
        //        return NotFound();
 | 
						|
        //    }
 | 
						|
 | 
						|
        //    // Logic to add the product to a new table (e.g., selected products)
 | 
						|
 | 
						|
        //    if (allocationid == null)
 | 
						|
        //    {
 | 
						|
        //        // Add allocation
 | 
						|
        //        ProjectAllocation allocation = new ProjectAllocation()
 | 
						|
        //        {
 | 
						|
        //            EmployeeId = employeeId,
 | 
						|
        //            ProjectId = project.Id,
 | 
						|
        //            AllocationDate = DateTime.UtcNow,
 | 
						|
        //            //EmployeeRole = employee.Rol
 | 
						|
        //            TenantId = project.TenantId
 | 
						|
        //        };
 | 
						|
 | 
						|
        //        _unitOfWork.ProjectAllocation.CreateAsync(allocation);
 | 
						|
        //    }
 | 
						|
        //    else
 | 
						|
        //    {
 | 
						|
        //        //remove allocation
 | 
						|
        //        var allocation = await _context.ProjectAllocations.FindAsync(allocationid);
 | 
						|
        //        if (allocation != null)
 | 
						|
        //        {
 | 
						|
        //            allocation.ReAllocationDate = DateTime.UtcNow;
 | 
						|
 | 
						|
        //            _unitOfWork.ProjectAllocation.UpdateAsync(allocation.Id, allocation);
 | 
						|
        //        }
 | 
						|
        //        else
 | 
						|
        //        {
 | 
						|
        //            return NotFound();
 | 
						|
        //        }
 | 
						|
        //    }
 | 
						|
 | 
						|
        //    return Ok();
 | 
						|
        //}
 | 
						|
 | 
						|
        [HttpGet]
 | 
						|
        [Route("employees/get/{projectid?}/{includeInactive?}")]
 | 
						|
        public async Task<IActionResult> GetEmployeeByProjectID(Guid? projectid, bool includeInactive = false)
 | 
						|
        {
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
            Guid TenantId = GetTenantId();
 | 
						|
 | 
						|
            if (projectid != null)
 | 
						|
            {
 | 
						|
                // Fetch assigned project
 | 
						|
                List<Employee> result = new List<Employee>();
 | 
						|
 | 
						|
                if ((bool)includeInactive)
 | 
						|
                {
 | 
						|
 | 
						|
                    result = await (from rpm in _context.Employees.Include(c => c.JobRole)
 | 
						|
                                    join fp in _context.ProjectAllocations.Where(c => c.TenantId == TenantId && c.ProjectId == projectid)
 | 
						|
                                    on rpm.Id equals fp.EmployeeId
 | 
						|
                                    select rpm).ToListAsync();
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    result = await (from rpm in _context.Employees.Include(c => c.JobRole)
 | 
						|
                                    join fp in _context.ProjectAllocations.Where(c => c.TenantId == TenantId && c.ProjectId == projectid && c.IsActive == true)
 | 
						|
                                    on rpm.Id equals fp.EmployeeId
 | 
						|
                                    select rpm).ToListAsync();
 | 
						|
                }
 | 
						|
 | 
						|
                List<EmployeeVM> resultVM = new List<EmployeeVM>();
 | 
						|
                foreach (Employee employee in result)
 | 
						|
                {
 | 
						|
                    EmployeeVM vm = employee.ToEmployeeVMFromEmployee();
 | 
						|
                    resultVM.Add(vm);
 | 
						|
                }
 | 
						|
 | 
						|
                return Ok(ApiResponse<object>.SuccessResponse(resultVM, "Success.", 200));
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                return NotFound(ApiResponse<object>.ErrorResponse("Invalid Input Parameter", 404));
 | 
						|
            }
 | 
						|
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpGet]
 | 
						|
        [Route("allocation/{projectId}")]
 | 
						|
        public async Task<IActionResult> GetProjectAllocation(Guid? projectId)
 | 
						|
        {
 | 
						|
            if (!ModelState.IsValid)
 | 
						|
            {
 | 
						|
                var errors = ModelState.Values
 | 
						|
                    .SelectMany(v => v.Errors)
 | 
						|
                    .Select(e => e.ErrorMessage)
 | 
						|
                    .ToList();
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
 | 
						|
 | 
						|
            }
 | 
						|
            Guid TenantId = GetTenantId();
 | 
						|
 | 
						|
 | 
						|
            var employees = await _context.ProjectAllocations
 | 
						|
                .Where(c => c.TenantId == TenantId && c.ProjectId == projectId && c.Employee != null)
 | 
						|
                .Include(e => e.Employee)
 | 
						|
                .Select(e => new
 | 
						|
                {
 | 
						|
                    ID = e.Id,
 | 
						|
                    EmployeeId = e.EmployeeId,
 | 
						|
                    ProjectId = e.ProjectId,
 | 
						|
                    AllocationDate = e.AllocationDate,
 | 
						|
                    ReAllocationDate = e.ReAllocationDate,
 | 
						|
                    FirstName = e.Employee != null ? e.Employee.FirstName : string.Empty,
 | 
						|
                    LastName = e.Employee != null ? e.Employee.LastName : string.Empty,
 | 
						|
                    MiddleName = e.Employee != null ? e.Employee.MiddleName : string.Empty,
 | 
						|
                    IsActive = e.IsActive,
 | 
						|
                    JobRoleId = (e.JobRoleId != null ? e.JobRoleId : e.Employee != null ? e.Employee.JobRoleId : null)
 | 
						|
                }).ToListAsync();
 | 
						|
 | 
						|
            return Ok(ApiResponse<object>.SuccessResponse(employees, "Success.", 200));
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpPost("allocation")]
 | 
						|
        public async Task<IActionResult> ManageAllocation(List<ProjectAllocationDot> projectAllocationDot)
 | 
						|
        {
 | 
						|
            if (projectAllocationDot != null)
 | 
						|
            {
 | 
						|
                Guid TenentID = GetTenantId();
 | 
						|
                var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
 | 
						|
                List<object>? result = new List<object>();
 | 
						|
                List<Guid> employeeIds = new List<Guid>();
 | 
						|
                List<Guid> projectIds = new List<Guid>();
 | 
						|
 | 
						|
                foreach (var item in projectAllocationDot)
 | 
						|
                {
 | 
						|
                    try
 | 
						|
                    {
 | 
						|
                        ProjectAllocation projectAllocation = item.ToProjectAllocationFromProjectAllocationDto(TenentID);
 | 
						|
                        ProjectAllocation? projectAllocationFromDb = await _context.ProjectAllocations.Where(c => c.EmployeeId == projectAllocation.EmployeeId
 | 
						|
                        && c.ProjectId == projectAllocation.ProjectId
 | 
						|
                        && c.ReAllocationDate == null
 | 
						|
                        && c.TenantId == TenentID).SingleOrDefaultAsync();
 | 
						|
 | 
						|
                        if (projectAllocationFromDb != null)
 | 
						|
                        {
 | 
						|
                            _context.ProjectAllocations.Attach(projectAllocationFromDb);
 | 
						|
 | 
						|
                            if (item.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.Now;
 | 
						|
                                projectAllocationFromDb.IsActive = false;
 | 
						|
                                _context.Entry(projectAllocationFromDb).Property(e => e.ReAllocationDate).IsModified = true;
 | 
						|
                                _context.Entry(projectAllocationFromDb).Property(e => e.IsActive).IsModified = true;
 | 
						|
 | 
						|
                                employeeIds.Add(projectAllocation.EmployeeId);
 | 
						|
                                projectIds.Add(projectAllocation.ProjectId);
 | 
						|
                            }
 | 
						|
                            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();
 | 
						|
 | 
						|
                            employeeIds.Add(projectAllocation.EmployeeId);
 | 
						|
                            projectIds.Add(projectAllocation.ProjectId);
 | 
						|
                        }
 | 
						|
 | 
						|
 | 
						|
                    }
 | 
						|
                    catch (Exception ex)
 | 
						|
                    {
 | 
						|
                        return Ok(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Assign_Project", ProjectIds = projectIds, EmployeeList = employeeIds };
 | 
						|
 | 
						|
                await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
                return Ok(ApiResponse<object>.SuccessResponse(result, "Data saved successfully", 200));
 | 
						|
 | 
						|
            }
 | 
						|
            return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpPost("task")]
 | 
						|
        public async Task<IActionResult> CreateProjectTask(List<WorkItemDot> workItemDot)
 | 
						|
        {
 | 
						|
            Guid tenantId = GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            List<WorkItemVM> workItems = new List<WorkItemVM> { };
 | 
						|
            List<Guid> projectIds = new List<Guid>();
 | 
						|
            string message = "";
 | 
						|
            string responseMessage = "";
 | 
						|
            if (workItemDot != null)
 | 
						|
            {
 | 
						|
                foreach (var item in workItemDot)
 | 
						|
                {
 | 
						|
                    WorkItem workItem = item.ToWorkItemFromWorkItemDto(tenantId);
 | 
						|
 | 
						|
                    var workArea = await _context.WorkAreas.Include(a => a.Floor).FirstOrDefaultAsync(a => a.Id == workItem.WorkAreaId) ?? new WorkArea();
 | 
						|
 | 
						|
                    Building building = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == (workArea.Floor != null ? workArea.Floor.BuildingId : Guid.Empty)) ?? new Building();
 | 
						|
 | 
						|
                    if (item.Id != null)
 | 
						|
                    {
 | 
						|
                        //update
 | 
						|
                        _context.WorkItems.Update(workItem);
 | 
						|
                        await _context.SaveChangesAsync();
 | 
						|
                        responseMessage = "Task Updated Successfully";
 | 
						|
                        message = $"Task Updated in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        //create
 | 
						|
                        _context.WorkItems.Add(workItem);
 | 
						|
                        await _context.SaveChangesAsync();
 | 
						|
                        responseMessage = "Task Added Successfully";
 | 
						|
                        message = $"Task Added in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
 | 
						|
                    }
 | 
						|
                    var result = new WorkItemVM
 | 
						|
                    {
 | 
						|
                        WorkItemId = workItem.Id,
 | 
						|
                        WorkItem = workItem
 | 
						|
                    };
 | 
						|
                    workItems.Add(result);
 | 
						|
                    projectIds.Add(building.ProjectId);
 | 
						|
                }
 | 
						|
                var activity = await _context.ActivityMasters.ToListAsync();
 | 
						|
                var category = await _context.WorkCategoryMasters.ToListAsync();
 | 
						|
 | 
						|
                var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
 | 
						|
 | 
						|
                await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
                return Ok(ApiResponse<object>.SuccessResponse(workItems, responseMessage, 200));
 | 
						|
            }
 | 
						|
 | 
						|
            return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item Details are not valid.", 400));
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpDelete("task/{id}")]
 | 
						|
        public async Task<IActionResult> DeleteProjectTask(Guid id)
 | 
						|
        {
 | 
						|
            Guid tenantId = _userHelper.GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
            List<Guid> projectIds = 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);
 | 
						|
 | 
						|
 | 
						|
                        projectIds.Add(floor?.Building?.ProjectId ?? Guid.Empty);
 | 
						|
 | 
						|
                        var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = $"Task Deleted in Building: {floor?.Building?.Name}, on Floor: {floor?.FloorName}, in Area: {task.WorkArea?.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}" };
 | 
						|
                        await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
                    }
 | 
						|
                    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.LogError("Task with ID {WorkItemId} not found ID database", id);
 | 
						|
            }
 | 
						|
            return Ok(ApiResponse<object>.SuccessResponse(new { }, "Task deleted successfully", 200));
 | 
						|
        }
 | 
						|
 | 
						|
        [HttpPost("manage-infra")]
 | 
						|
        public async Task<IActionResult> ManageProjectInfra(List<InfraDot> infraDots)
 | 
						|
        {
 | 
						|
            Guid tenantId = GetTenantId();
 | 
						|
            var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
 | 
						|
            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 = item.Building.ToBuildingFromBuildingDto(tenantId);
 | 
						|
                        building.TenantId = GetTenantId();
 | 
						|
 | 
						|
                        if (item.Building.Id == null)
 | 
						|
                        {
 | 
						|
                            //create
 | 
						|
                            _context.Buildings.Add(building);
 | 
						|
                            await _context.SaveChangesAsync();
 | 
						|
                            responseData.building = building;
 | 
						|
                            responseMessage = "Buliding Added Successfully";
 | 
						|
                            message = "Building Added";
 | 
						|
                        }
 | 
						|
                        else
 | 
						|
                        {
 | 
						|
                            //update
 | 
						|
                            _context.Buildings.Update(building);
 | 
						|
                            await _context.SaveChangesAsync();
 | 
						|
                            responseData.building = building;
 | 
						|
                            responseMessage = "Buliding Updated Successfully";
 | 
						|
                            message = "Building Updated";
 | 
						|
 | 
						|
                        }
 | 
						|
                        projectIds.Add(building.ProjectId);
 | 
						|
                    }
 | 
						|
                    if (item.Floor != null)
 | 
						|
                    {
 | 
						|
                        Floor floor = item.Floor.ToFloorFromFloorDto(tenantId);
 | 
						|
                        floor.TenantId = GetTenantId();
 | 
						|
 | 
						|
                        if (item.Floor.Id == null)
 | 
						|
                        {
 | 
						|
                            //create
 | 
						|
                            _context.Floor.Add(floor);
 | 
						|
                            await _context.SaveChangesAsync();
 | 
						|
                            responseData.floor = floor;
 | 
						|
                            responseMessage = "Floor Added Successfully";
 | 
						|
                            message = "Floor Added";
 | 
						|
                        }
 | 
						|
                        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);
 | 
						|
                        projectIds.Add(building?.ProjectId ?? Guid.Empty);
 | 
						|
                        message = $"{message} in Building: {building?.Name}";
 | 
						|
                    }
 | 
						|
                    if (item.WorkArea != null)
 | 
						|
                    {
 | 
						|
                        WorkArea workArea = item.WorkArea.ToWorkAreaFromWorkAreaDto(tenantId);
 | 
						|
                        workArea.TenantId = GetTenantId();
 | 
						|
 | 
						|
                        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";
 | 
						|
                        }
 | 
						|
                        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);
 | 
						|
                        projectIds.Add(floor?.Building?.ProjectId ?? Guid.Empty);
 | 
						|
                        message = $"{message} in Building: {floor?.Building?.Name}, on Floor: {floor?.FloorName}";
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                message = $"{message} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
 | 
						|
                var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
 | 
						|
 | 
						|
                await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
                return Ok(ApiResponse<object>.SuccessResponse(responseData, responseMessage, 200));
 | 
						|
            }
 | 
						|
            return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Infra Details are not valid.", 400));
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        [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();
 | 
						|
                var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
 | 
						|
                List<object>? result = new List<object>();
 | 
						|
                List<Guid> projectIds = new List<Guid>();
 | 
						|
 | 
						|
                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;
 | 
						|
 | 
						|
                                projectIds.Add(projectAllocation.ProjectId);
 | 
						|
                            }
 | 
						|
                            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();
 | 
						|
 | 
						|
                            projectIds.Add(projectAllocation.ProjectId);
 | 
						|
 | 
						|
                        }
 | 
						|
 | 
						|
 | 
						|
                    }
 | 
						|
                    catch (Exception ex)
 | 
						|
                    {
 | 
						|
 | 
						|
                        return Ok(ApiResponse<object>.ErrorResponse(ex.Message, ex, 400));
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Assign_Project", ProjectIds = projectIds, EmployeeId = employeeId };
 | 
						|
 | 
						|
                await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
 | 
						|
 | 
						|
                return Ok(ApiResponse<object>.SuccessResponse(result, "Data saved successfully", 200));
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "All Field is required", 400));
 | 
						|
            }
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
    }
 | 
						|
} |