Optimized the task management API

This commit is contained in:
ashutosh.nehete 2025-06-18 19:08:06 +05:30
parent 2e925efcf7
commit c9bb18d8e5

View File

@ -578,7 +578,6 @@ namespace MarcoBMS.Services.Controllers
projectIds.Add(projectAllocation.ProjectId); projectIds.Add(projectAllocation.ProjectId);
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -596,59 +595,82 @@ namespace MarcoBMS.Services.Controllers
} }
[HttpPost("task")] [HttpPost("task")]
public async Task<IActionResult> CreateProjectTask(List<WorkItemDot> workItemDot) public async Task<IActionResult> CreateProjectTask(List<WorkItemDot> workItemDtos)
{ {
Guid tenantId = GetTenantId(); _logger.LogInfo("CreateProjectTask called with {Count} items", workItemDtos?.Count ?? 0);
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
List<WorkItemVM> workItems = new List<WorkItemVM> { }; // Validate request
List<Guid> projectIds = new List<Guid>(); if (workItemDtos == null || !workItemDtos.Any())
string message = "";
string responseMessage = "";
if (workItemDot != null)
{ {
foreach (var item in workItemDot) _logger.LogWarning("No work items provided in the request.");
{ return BadRequest(ApiResponse<object>.ErrorResponse("Invalid details.", "Work Item details are not valid.", 400));
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)); Guid tenantId = GetTenantId();
var workItemsToCreate = new List<WorkItem>();
var workItemsToUpdate = new List<WorkItem>();
var responseList = new List<WorkItemVM>();
var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
string message = "";
List<Guid> projectIds = new List<Guid>();
foreach (var itemDto in workItemDtos)
{
var workItem = itemDto.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 (itemDto.Id != null && itemDto.Id != Guid.Empty)
{
// Update existing
workItemsToUpdate.Add(workItem);
message = $"Task Added in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
}
else
{
// Create new
workItem.Id = Guid.NewGuid();
workItemsToCreate.Add(workItem);
message = $"Task Updated in Building: {building.Name}, on Floor: {workArea.Floor?.FloorName}, in Area: {workArea.AreaName} by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}";
}
responseList.Add(new WorkItemVM
{
WorkItemId = workItem.Id,
WorkItem = workItem
});
projectIds.Add(building.ProjectId);
}
string responseMessage = "";
// Apply DB changes
if (workItemsToCreate.Any())
{
_logger.LogInfo("Adding {Count} new work items", workItemsToCreate.Count);
await _context.WorkItems.AddRangeAsync(workItemsToCreate);
responseMessage = "Task Added Successfully";
}
if (workItemsToUpdate.Any())
{
_logger.LogInfo("Updating {Count} existing work items", workItemsToUpdate.Count);
_context.WorkItems.UpdateRange(workItemsToUpdate);
responseMessage = "Task Updated Successfully";
}
await _context.SaveChangesAsync();
_logger.LogInfo("CreateProjectTask completed successfully. Created: {Created}, Updated: {Updated}", workItemsToCreate.Count, workItemsToUpdate.Count);
var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message };
await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification);
return Ok(ApiResponse<object>.SuccessResponse(responseList, responseMessage, 200));
} }
[HttpDelete("task/{id}")] [HttpDelete("task/{id}")]