573 lines
28 KiB
C#
573 lines
28 KiB
C#
using Marco.Pms.DataAccess.Data;
|
|
using Marco.Pms.Model.Dtos.Project;
|
|
using Marco.Pms.Model.Employees;
|
|
using Marco.Pms.Model.Mapper;
|
|
using Marco.Pms.Model.Projects;
|
|
using Marco.Pms.Model.Utilities;
|
|
using Marco.Pms.Model.ViewModels.Projects;
|
|
using Marco.Pms.Services.Helpers;
|
|
using Marco.Pms.Services.Service;
|
|
using Marco.Pms.Services.Service.ServiceInterfaces;
|
|
using MarcoBMS.Services.Helpers;
|
|
using MarcoBMS.Services.Service;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.CodeAnalysis;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MongoDB.Driver;
|
|
|
|
namespace MarcoBMS.Services.Controllers
|
|
{
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
[Authorize]
|
|
public class ProjectController : ControllerBase
|
|
{
|
|
private readonly IProjectServices _projectServices;
|
|
private readonly ApplicationDbContext _context;
|
|
private readonly UserHelper _userHelper;
|
|
private readonly ILoggingService _logger;
|
|
private readonly ISignalRService _signalR;
|
|
private readonly PermissionServices _permission;
|
|
private readonly CacheUpdateHelper _cache;
|
|
private readonly Guid tenantId;
|
|
|
|
|
|
public ProjectController(
|
|
ApplicationDbContext context,
|
|
UserHelper userHelper,
|
|
ILoggingService logger,
|
|
ISignalRService signalR,
|
|
CacheUpdateHelper cache,
|
|
PermissionServices permission,
|
|
IProjectServices projectServices)
|
|
{
|
|
_context = context;
|
|
_userHelper = userHelper;
|
|
_logger = logger;
|
|
_signalR = signalR;
|
|
_cache = cache;
|
|
_permission = permission;
|
|
_projectServices = projectServices;
|
|
tenantId = userHelper.GetTenantId();
|
|
}
|
|
|
|
#region =================================================================== Project Get APIs ===================================================================
|
|
|
|
[HttpGet("list/basic")]
|
|
public async Task<IActionResult> GetAllProjectsBasic()
|
|
{
|
|
// Get the current user
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetAllProjectsBasicAsync(tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves a list of projects accessible to the current user, including aggregated details.
|
|
/// This method is optimized to use a cache-first approach. If data is not in the cache,
|
|
/// it fetches and aggregates data efficiently from the database in parallel.
|
|
/// </summary>
|
|
/// <returns>An ApiResponse containing a list of projects or an error.</returns>
|
|
|
|
[HttpGet("list")]
|
|
public async Task<IActionResult> GetAllProjects()
|
|
{
|
|
// --- Input Validation and Initial Setup ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
_logger.LogWarning("GetAllProjects called with invalid model state. Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetAllProjectsAsync(tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Retrieves details for a specific project by its ID.
|
|
/// This endpoint is optimized with a cache-first strategy and parallel permission checks.
|
|
/// </summary>
|
|
/// <param name="id">The unique identifier of the project.</param>
|
|
/// <returns>An ApiResponse containing the project details or an appropriate error.</returns>
|
|
|
|
[HttpGet("get/{id}")]
|
|
public async Task<IActionResult> GetProject([FromRoute] Guid id)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get project called with invalid model state for ID {ProjectId}. Errors: {Errors}", id, string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetProjectAsync(id, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
|
|
[HttpGet("details/{id}")]
|
|
public async Task<IActionResult> GetProjectDetails([FromRoute] Guid id)
|
|
{
|
|
// Step 1: Validate model state
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values
|
|
.SelectMany(v => v.Errors)
|
|
.Select(e => e.ErrorMessage)
|
|
.ToList();
|
|
|
|
_logger.LogWarning("Invalid model state in Details endpoint. Errors: {@Errors}", errors);
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid data", errors, 400));
|
|
}
|
|
|
|
// Step 2: Get logged-in employee
|
|
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
var response = await _projectServices.GetProjectDetailsAsync(id, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
[HttpGet("details-old/{id}")]
|
|
public async Task<IActionResult> GetProjectDetailsOld([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 loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
|
|
var response = await _projectServices.GetProjectDetailsAsync(id, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project Manage APIs ===================================================================
|
|
|
|
[HttpPost]
|
|
public async Task<IActionResult> CreateProject([FromBody] CreateProjectDto projectDto)
|
|
{
|
|
// 1. Validate input first (early exit)
|
|
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));
|
|
}
|
|
|
|
// 2. Prepare data without I/O
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.CreateProjectAsync(projectDto, tenantId, loggedInEmployee);
|
|
if (response.Success)
|
|
{
|
|
var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Create_Project", Response = response.Data };
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Updates an existing project's details.
|
|
/// This endpoint is secure, handles concurrency, and performs non-essential tasks in the background.
|
|
/// </summary>
|
|
/// <param name="id">The ID of the project to update.</param>
|
|
/// <param name="updateProjectDto">The data to update the project with.</param>
|
|
/// <returns>An ApiResponse confirming the update or an appropriate error.</returns>
|
|
|
|
[HttpPut("update/{id}")]
|
|
public async Task<IActionResult> UpdateProject([FromRoute] Guid id, [FromBody] UpdateProjectDto updateProjectDto)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Update project called with invalid model state for ID {ProjectId}. Errors: {Errors}", id, string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.UpdateProjectAsync(id, updateProjectDto, tenantId, loggedInEmployee);
|
|
if (response.Success)
|
|
{
|
|
var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Update_Project", Response = response.Data };
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project Allocation APIs ===================================================================
|
|
|
|
[HttpGet("employees/get/{projectid?}/{includeInactive?}")]
|
|
public async Task<IActionResult> GetEmployeeByProjectId(Guid? projectId, bool includeInactive = false)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get employee list by ProjectId called with invalid model state \n Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetEmployeeByProjectIdAsync(projectId, includeInactive, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
[HttpGet("allocation/{projectId}")]
|
|
public async Task<IActionResult> GetProjectAllocation(Guid? projectId)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get employee list by ProjectId called with invalid model state \n Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetProjectAllocationAsync(projectId, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
[HttpPost("allocation")]
|
|
public async Task<IActionResult> ManageAllocation([FromBody] List<ProjectAllocationDot> projectAllocationDot)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("project Alocation called with invalid model state for list of projects. Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.ManageAllocationAsync(projectAllocationDot, tenantId, loggedInEmployee);
|
|
if (response.Success)
|
|
{
|
|
List<Guid> employeeIds = response.Data.Select(pa => pa.EmployeeId).ToList();
|
|
List<Guid> projectIds = response.Data.Select(pa => pa.ProjectId).ToList();
|
|
|
|
var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Assign_Project", ProjectIds = projectIds, EmployeeList = employeeIds };
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
return StatusCode(response.StatusCode, response);
|
|
|
|
}
|
|
|
|
[HttpGet("assigned-projects/{employeeId}")]
|
|
public async Task<IActionResult> GetProjectsByEmployee([FromRoute] Guid employeeId)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get project list by employee Id called with invalid model state \n Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetProjectsByEmployeeAsync(employeeId, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
[HttpPost("assign-projects/{employeeId}")]
|
|
public async Task<IActionResult> AssigneProjectsToEmployee([FromBody] List<ProjectsAllocationDto> projectAllocationDtos, [FromRoute] Guid employeeId)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("project Alocation called with invalid model state for list of projects. Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.AssigneProjectsToEmployeeAsync(projectAllocationDtos, employeeId, tenantId, loggedInEmployee);
|
|
if (response.Success)
|
|
{
|
|
List<Guid> projectIds = response.Data.Select(pa => pa.ProjectId).ToList();
|
|
|
|
var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "Assign_Project", ProjectIds = projectIds, EmployeeId = employeeId };
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project InfraStructure Get APIs ===================================================================
|
|
|
|
[HttpGet("infra-details/{projectId}")]
|
|
public async Task<IActionResult> GetInfraDetails(Guid projectId)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get Project Infrastructure by ProjectId called with invalid model state \n Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetInfraDetailsAsync(projectId, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
|
|
}
|
|
|
|
[HttpGet("tasks/{workAreaId}")]
|
|
public async Task<IActionResult> GetWorkItems(Guid workAreaId)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("Get Work Items by WorkAreaId called with invalid model state \n Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.GetWorkItemsAsync(workAreaId, tenantId, loggedInEmployee);
|
|
return StatusCode(response.StatusCode, response);
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region =================================================================== Project Infrastructre Manage APIs ===================================================================
|
|
|
|
[HttpPost("task")]
|
|
public async Task<IActionResult> CreateProjectTask([FromBody] List<WorkItemDto> workItemDtos)
|
|
{
|
|
// --- Step 1: Input Validation ---
|
|
if (!ModelState.IsValid)
|
|
{
|
|
var errors = ModelState.Values.SelectMany(v => v.Errors).Select(e => e.ErrorMessage).ToList();
|
|
_logger.LogWarning("project Alocation called with invalid model state for list of projects. Errors: {Errors}", string.Join(", ", errors));
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid request data provided.", errors, 400));
|
|
}
|
|
|
|
// --- Step 2: Prepare data without I/O ---
|
|
Employee loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
var response = await _projectServices.CreateProjectTaskAsync(workItemDtos, tenantId, loggedInEmployee);
|
|
if (response.Success)
|
|
{
|
|
List<Guid> workAreaIds = response.Data.Select(pa => pa.WorkItem?.WorkAreaId ?? Guid.Empty).ToList();
|
|
string message = response.Message;
|
|
|
|
var notification = new { LoggedInUserId = loggedInEmployee.Id, Keyword = "WorkItem", WorkAreaIds = workAreaIds, Message = message };
|
|
await _signalR.SendNotificationAsync(notification);
|
|
}
|
|
return StatusCode(response.StatusCode, response);
|
|
|
|
}
|
|
|
|
[HttpDelete("task/{id}")]
|
|
public async Task<IActionResult> DeleteProjectTask(Guid id)
|
|
{
|
|
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
|
List<Guid> workAreaIds = 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);
|
|
|
|
|
|
workAreaIds.Add(task.WorkAreaId);
|
|
var projectId = floor?.Building?.ProjectId;
|
|
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "WorkItem", WorkAreaIds = workAreaIds, Message = $"Task Deleted in Building: {floor?.Building?.Name}, on Floor: {floor?.FloorName}, in Area: {task.WorkArea?.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}" };
|
|
await _signalR.SendNotificationAsync(notification);
|
|
await _cache.DeleteWorkItemByIdAsync(task.Id);
|
|
if (projectId != null)
|
|
{
|
|
await _cache.DeleteProjectByIdAsync(projectId.Value);
|
|
}
|
|
}
|
|
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.LogWarning("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)
|
|
{
|
|
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 = tenantId;
|
|
|
|
if (item.Building.Id == null)
|
|
{
|
|
//create
|
|
_context.Buildings.Add(building);
|
|
await _context.SaveChangesAsync();
|
|
responseData.building = building;
|
|
responseMessage = "Buliding Added Successfully";
|
|
message = "Building Added";
|
|
await _cache.AddBuildngInfra(building.ProjectId, building);
|
|
}
|
|
else
|
|
{
|
|
//update
|
|
_context.Buildings.Update(building);
|
|
await _context.SaveChangesAsync();
|
|
responseData.building = building;
|
|
responseMessage = "Buliding Updated Successfully";
|
|
message = "Building Updated";
|
|
await _cache.UpdateBuildngInfra(building.ProjectId, building);
|
|
}
|
|
projectIds.Add(building.ProjectId);
|
|
}
|
|
if (item.Floor != null)
|
|
{
|
|
Floor floor = item.Floor.ToFloorFromFloorDto(tenantId);
|
|
floor.TenantId = tenantId;
|
|
bool isCreated = false;
|
|
|
|
if (item.Floor.Id == null)
|
|
{
|
|
//create
|
|
_context.Floor.Add(floor);
|
|
await _context.SaveChangesAsync();
|
|
responseData.floor = floor;
|
|
responseMessage = "Floor Added Successfully";
|
|
message = "Floor Added";
|
|
isCreated = true;
|
|
}
|
|
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);
|
|
var projectId = building?.ProjectId ?? Guid.Empty;
|
|
projectIds.Add(projectId);
|
|
message = $"{message} in Building: {building?.Name}";
|
|
if (isCreated)
|
|
{
|
|
await _cache.AddBuildngInfra(projectId, floor: floor);
|
|
}
|
|
else
|
|
{
|
|
await _cache.UpdateBuildngInfra(projectId, floor: floor);
|
|
}
|
|
}
|
|
if (item.WorkArea != null)
|
|
{
|
|
WorkArea workArea = item.WorkArea.ToWorkAreaFromWorkAreaDto(tenantId);
|
|
workArea.TenantId = tenantId;
|
|
bool isCreated = false;
|
|
|
|
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";
|
|
isCreated = true;
|
|
}
|
|
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);
|
|
var projectId = floor?.Building?.ProjectId ?? Guid.Empty;
|
|
projectIds.Add(projectId);
|
|
message = $"{message} in Building: {floor?.Building?.Name}, on Floor: {floor?.FloorName}";
|
|
if (isCreated)
|
|
{
|
|
await _cache.AddBuildngInfra(projectId, workArea: workArea, buildingId: floor?.BuildingId);
|
|
}
|
|
else
|
|
{
|
|
await _cache.UpdateBuildngInfra(projectId, workArea: workArea, buildingId: floor?.BuildingId);
|
|
}
|
|
}
|
|
}
|
|
message = $"{message} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
|
|
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
|
|
|
|
await _signalR.SendNotificationAsync(notification);
|
|
return Ok(ApiResponse<object>.SuccessResponse(responseData, responseMessage, 200));
|
|
}
|
|
return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Infra Details are not valid.", 400));
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
}
|
|
} |