From e2956c0c8cdabf224b0ffeac506218af1d571b50 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Tue, 17 Jun 2025 17:05:52 +0530 Subject: [PATCH] Added signalR functionality in Project Infrastructure --- .../Controllers/ProjectController.cs | 43 ++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/Marco.Pms.Services/Controllers/ProjectController.cs b/Marco.Pms.Services/Controllers/ProjectController.cs index d391909..b120c04 100644 --- a/Marco.Pms.Services/Controllers/ProjectController.cs +++ b/Marco.Pms.Services/Controllers/ProjectController.cs @@ -88,8 +88,6 @@ namespace MarcoBMS.Services.Controllers return Ok(ApiResponse.SuccessResponse(response, "Success.", 200)); } - - [HttpGet("list")] public async Task GetAll() { @@ -603,7 +601,8 @@ namespace MarcoBMS.Services.Controllers Guid tenantId = GetTenantId(); var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); List workItems = new List { }; - Building building = new Building(); + List projectIds = new List(); + string message = ""; string responseMessage = ""; if (workItemDot != null) { @@ -613,21 +612,23 @@ namespace MarcoBMS.Services.Controllers var workArea = await _context.WorkAreas.Include(a => a.Floor).FirstOrDefaultAsync(a => a.Id == workItem.WorkAreaId) ?? new WorkArea(); - building = await _context.Buildings.FirstOrDefaultAsync(b => b.Id == (workArea.Floor != null ? workArea.Floor.BuildingId : Guid.Empty)) ?? new Building(); + 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 Added Successfully"; + responseMessage = "Task Updated Successfully"; + message = $"Task Updated by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}"; } else { //create _context.WorkItems.Add(workItem); await _context.SaveChangesAsync(); - responseMessage = "Task Updated Successfully"; + responseMessage = "Task Added Successfully"; + message = $"Task Added by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}"; } var result = new WorkItemVM { @@ -635,11 +636,12 @@ namespace MarcoBMS.Services.Controllers 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", ProjectId = building.ProjectId }; + var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", ProjectIds = projectIds, Message = message }; await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification); return Ok(ApiResponse.SuccessResponse(workItems, responseMessage, 200)); @@ -654,7 +656,8 @@ namespace MarcoBMS.Services.Controllers { Guid tenantId = _userHelper.GetTenantId(); var LoggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); - WorkItem? task = await _context.WorkItems.AsNoTracking().FirstOrDefaultAsync(t => t.Id == id && t.TenantId == tenantId); + List projectIds = new List(); + 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) @@ -666,7 +669,13 @@ namespace MarcoBMS.Services.Controllers await _context.SaveChangesAsync(); _logger.LogInfo("Task with ID {WorkItemId} has been successfully deleted.", task.Id); - var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", Response = 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 by {LoggedInEmployee.FirstName} {LoggedInEmployee.LastName}" }; await _signalR.Clients.All.SendAsync("NotificationEventHandler", notification); } else @@ -699,6 +708,8 @@ namespace MarcoBMS.Services.Controllers var responseData = new InfraVM { }; string responseMessage = ""; + string message = ""; + List projectIds = new List(); if (infraDots != null) { foreach (var item in infraDots) @@ -716,6 +727,7 @@ namespace MarcoBMS.Services.Controllers await _context.SaveChangesAsync(); responseData.building = building; responseMessage = "Buliding Added Successfully"; + message = "Building Added"; } else { @@ -724,8 +736,10 @@ namespace MarcoBMS.Services.Controllers await _context.SaveChangesAsync(); responseData.building = building; responseMessage = "Buliding Updated Successfully"; + message = "Building Updated"; } + projectIds.Add(building.ProjectId); } if (item.Floor != null) { @@ -739,6 +753,7 @@ namespace MarcoBMS.Services.Controllers await _context.SaveChangesAsync(); responseData.floor = floor; responseMessage = "Floor Added Successfully"; + message = "Floor Added"; } else { @@ -747,7 +762,10 @@ namespace MarcoBMS.Services.Controllers 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); } if (item.WorkArea != null) { @@ -761,6 +779,7 @@ namespace MarcoBMS.Services.Controllers await _context.SaveChangesAsync(); responseData.workArea = workArea; responseMessage = "Work Area Added Successfully"; + message = "Work Area Added"; } else { @@ -769,10 +788,14 @@ namespace MarcoBMS.Services.Controllers 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); } } - var notification = new { LoggedInUserId = LoggedInEmployee.Id, Keyword = "Infra", Response = responseData }; + 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.SuccessResponse(responseData, responseMessage, 200));