From aa604f4d9eeafbf7e58904755c40b38e4873964a Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Thu, 4 Dec 2025 12:12:20 +0530 Subject: [PATCH] Added the job progression API --- .../Controllers/DashboardController.cs | 165 +++++++++++------- 1 file changed, 99 insertions(+), 66 deletions(-) diff --git a/Marco.Pms.Services/Controllers/DashboardController.cs b/Marco.Pms.Services/Controllers/DashboardController.cs index edccc51..4066166 100644 --- a/Marco.Pms.Services/Controllers/DashboardController.cs +++ b/Marco.Pms.Services/Controllers/DashboardController.cs @@ -2,6 +2,7 @@ using Marco.Pms.Model.Dtos.Attendance; using Marco.Pms.Model.Entitlements; using Marco.Pms.Model.Expenses; +using Marco.Pms.Model.ServiceProject; using Marco.Pms.Model.Utilities; using Marco.Pms.Model.ViewModels.DashBoard; using Marco.Pms.Services.Service; @@ -1075,78 +1076,110 @@ namespace Marco.Pms.Services.Controllers } } - //[HttpGet("job/progression")] - //public async Task GetJobProgressionAsync([FromQuery] Guid? projectId) - //{ - // Guid AssignedStatus = Guid.Parse("cfa1886d-055f-4ded-84c6-42a2a8a14a66"); - // Guid InProgressStatus = Guid.Parse("5a6873a5-fed7-4745-a52f-8f61bf3bd72d"); - // Guid ReviewDoneStatus = Guid.Parse("ed10ab57-dbaa-4ca5-8ecd-56745dcbdbd7"); - // Guid ClosedStatus = Guid.Parse("3ddeefb5-ae3c-4e10-a922-35e0a452bb69"); + [HttpGet("job/progression")] + public async Task GetJobProgressionAsync([FromQuery] Guid? projectId) + { + Guid AssignedStatus = Guid.Parse("cfa1886d-055f-4ded-84c6-42a2a8a14a66"); + Guid InProgressStatus = Guid.Parse("5a6873a5-fed7-4745-a52f-8f61bf3bd72d"); + Guid ReviewDoneStatus = Guid.Parse("ed10ab57-dbaa-4ca5-8ecd-56745dcbdbd7"); + Guid ClosedStatus = Guid.Parse("3ddeefb5-ae3c-4e10-a922-35e0a452bb69"); - // if (tenantId == Guid.Empty) - // { - // _logger.LogWarning("Invalid request: TenantId is empty on progression endpoint"); - // return BadRequest(ApiResponse.ErrorResponse("Invalid TenantId", "Provided Invalid TenantId", 400)); - // } + if (tenantId == Guid.Empty) + { + _logger.LogWarning("Invalid request: TenantId is empty on progression endpoint"); + return BadRequest(ApiResponse.ErrorResponse("Invalid TenantId", "Provided Invalid TenantId", 400)); + } - // var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); - // var jobIds = await _context.JobEmployeeMappings - // .Where(jem => jem.AssigneeId == loggedInEmployee.Id && jem.TenantId == tenantId) - // .Select(jem => jem.JobTicketId) - // .ToListAsync(); + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + var jobIds = await _context.JobEmployeeMappings + .Where(jem => jem.AssigneeId == loggedInEmployee.Id && jem.TenantId == tenantId) + .Select(jem => jem.JobTicketId) + .ToListAsync(); - // if (projectId.HasValue) - // { - // var hasPermission = await _permissionServices.HasServiceProjectPermission(loggedInEmployee.Id, projectId.Value); - // if (!hasPermission) - // { - // return StatusCode(403, - // ApiResponse.ErrorResponse("You do not have permission to access this resource", "You do not have permission to access this resource", 403)); - // } - // var jobs = await _context.JobTickets - // .Include(jt => jt.Project) - // .Include(jt => jt.CreatedBy).ThenInclude(e => e!.JobRole) - // .Where(jt => jt.ProjectId == projectId.Value - // && jt.StatusId != ReviewDoneStatus - // && jt.StatusId != ClosedStatus - // && jt.Project != null - // && jt.CreatedBy != null - // && jt.TenantId == tenantId) - // .ToListAsync(); + var query = _context.JobTickets + .Include(jt => jt.Project) + .Include(jt => jt.CreatedBy).ThenInclude(e => e!.JobRole) + .Where(jt => jt.StatusId != ReviewDoneStatus + && jt.StatusId != ClosedStatus + && jt.Project != null + && jt.CreatedBy != null + && jt.TenantId == tenantId); - // var inProgressJobIds = jobs.Where(jt => jt.StatusId == InProgressStatus).Select(jt => jt.Id).ToList(); + if (projectId.HasValue) + { + var hasPermission = await _permissionServices.HasServiceProjectPermission(loggedInEmployee.Id, projectId.Value); + if (!hasPermission) + { + return StatusCode(403, + ApiResponse.ErrorResponse("You do not have permission to access this resource", "You do not have permission to access this resource", 403)); + } + query = query.Where(jt => jt.ProjectId == projectId.Value); + } + var jobs = await query + .ToListAsync(); - // var latestTagIns = await _context.JobAttendance - // .Include(ja => ja.Employee) - // .Where(ja => inProgressJobIds.Contains(ja.JobTicketId) - // && ja.Action == TAGGING_MARK_TYPE.TAG_IN - // && ja.TaggedOutAt == null - // && ja.TenantId == tenantId) - // .GroupBy(ja => ja.JobTicketId) - // .Select(g => new - // { - // JobTicketId = g.Key, - // Employee = g.Select(ja => ja.Employee).FirstOrDefault(), - // TagInAt = g.Max(ja => ja.TaggedInAt) - // }) - // .ToListAsync(); + var inProgressJobIds = jobs.Where(jt => jt.StatusId == InProgressStatus).Select(jt => jt.Id).ToList(); - // var assignedJobs = jobs - // .Where(jt => jt.StatusId == AssignedStatus) - // .Take(5) - // .Select(jt => new - // { - // Project = jt.Project!.Name, - // AssignedBy = jt.CreatedBy!.FirstName + " " + jt.CreatedBy.LastName, - // Title = jt.Title, - // AssignedAt = jt.CreatedAt, + var latestTagIns = await _context.JobAttendance + .Include(ja => ja.Employee) + .Where(ja => inProgressJobIds.Contains(ja.JobTicketId) + && ja.Action == TAGGING_MARK_TYPE.TAG_IN + && ja.TaggedOutAt == null + && ja.TenantId == tenantId) + .GroupBy(ja => ja.JobTicketId) + .Select(g => new + { + JobTicketId = g.Key, + Employee = g.Select(ja => ja.Employee).FirstOrDefault(), + TagInAt = g.Max(ja => ja.TaggedInAt) + }) + .ToListAsync(); - // }) - // .ToList(); - // var inProgressJobs = jobs.Where(jt => jt.StatusId == InProgressStatus).Take(5).ToList(); - // var selfAssignedJobs = jobs.Where(jt => jobIds.Contains(jt.Id)).Take(5).ToList(); - // } - // return Ok(); - //} + var assignedJobs = jobs + .Where(jt => jt.StatusId == AssignedStatus) + .Take(5) + .Select(jt => new + { + Project = jt.Project!.Name, + AssignedBy = jt.CreatedBy!.FirstName + " " + jt.CreatedBy.LastName, + Title = jt.Title, + AssignedAt = jt.CreatedAt, + + }) + .ToList(); + var inProgressJobs = jobs + .Where(jt => jt.StatusId == InProgressStatus) + .Take(5) + .Select(jt => new + { + Project = jt.Project!.Name, + AssignedBy = jt.CreatedBy!.FirstName + " " + jt.CreatedBy.LastName, + Title = jt.Title, + AssignedAt = jt.CreatedAt, + + }) + .ToList(); + var selfAssignedJobs = jobs + .Where(jt => jobIds.Contains(jt.Id)) + .Take(5) + .Select(jt => new + { + Project = jt.Project!.Name, + AssignedBy = jt.CreatedBy!.FirstName + " " + jt.CreatedBy.LastName, + Title = jt.Title, + AssignedAt = jt.CreatedAt, + + }) + .ToList(); + + var response = new + { + AssignedJobs = assignedJobs, + InProgressJobs = inProgressJobs, + AllJobs = selfAssignedJobs + }; + + return Ok(ApiResponse.SuccessResponse(response, "job progression fetched successfully", 200)); + } } }