diff --git a/Marco.Pms.Services/Controllers/ImageController.cs b/Marco.Pms.Services/Controllers/ImageController.cs index 7a4c556..2a1b057 100644 --- a/Marco.Pms.Services/Controllers/ImageController.cs +++ b/Marco.Pms.Services/Controllers/ImageController.cs @@ -1,6 +1,8 @@ using Marco.Pms.DataAccess.Data; +using Marco.Pms.Model.Activities; using Marco.Pms.Model.Employees; using Marco.Pms.Model.Mapper; +using Marco.Pms.Model.Projects; using Marco.Pms.Model.Utilities; using Marco.Pms.Services.Service; using MarcoBMS.Services.Helpers; @@ -167,5 +169,108 @@ namespace Marco.Pms.Services.Controllers _logger.LogInfo("Image list fetched for ProjectId: {ProjectId}. Total documents: {Count}", projectId, documentVM.Count); return Ok(ApiResponse.SuccessResponse(documentVM, $"{documentVM.Count} image records fetched successfully", 200)); } + + [HttpGet("batch/{batchId}")] + public async Task GetImagesByBatch(Guid batchId) + { + _logger.LogInfo("GetImagesByBatch called for BatchId: {BatchId}", batchId); + + // Step 1: Get the logged-in employee + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + + // Step 2: Retrieve all documents in the batch + var documents = await _context.Documents + .Include(d => d.UploadedBy) + .Where(d => d.BatchId == batchId) + .ToListAsync(); + + if (!documents.Any()) + { + _logger.LogWarning("No documents found for BatchId: {BatchId}", batchId); + return NotFound(ApiResponse.ErrorResponse("No images found", "No images associated with this batch", 404)); + } + + var documentIds = documents.Select(d => d.Id).ToList(); + + // Step 3: Get task/comment reference IDs linked to these documents + var referenceIds = await _context.TaskAttachments + .Where(ta => documentIds.Contains(ta.DocumentId)) + .Select(ta => ta.ReferenceId) + .Distinct() + .ToListAsync(); + + // Step 4: Try to identify the source of the attachment (task or comment) + var task = await _context.TaskAllocations + .Include(t => t.ReportedBy) + .FirstOrDefaultAsync(t => referenceIds.Contains(t.Id)); + + TaskComment? comment = null; + WorkItem? workItem = null; + Employee? uploadedBy = null; + string source = ""; + + if (task != null) + { + uploadedBy = task.ReportedBy; + workItem = await _context.WorkItems + .Include(wi => wi.ActivityMaster) + .FirstOrDefaultAsync(wi => wi.Id == task.WorkItemId); + source = "Report"; + } + else + { + comment = await _context.TaskComments + .Include(tc => tc.TaskAllocation) + .Include(tc => tc.Employee) + .FirstOrDefaultAsync(tc => referenceIds.Contains(tc.Id)); + var workItemId = comment?.TaskAllocation?.WorkItemId; + + uploadedBy = comment?.Employee; + workItem = await _context.WorkItems + .Include(wi => wi.ActivityMaster) + .FirstOrDefaultAsync(wi => wi.Id == workItemId); + source = "Comment"; + } + + // Step 5: Traverse up to building level + var workAreaId = workItem?.WorkAreaId; + var workArea = await _context.WorkAreas + .Include(wa => wa.Floor) + .FirstOrDefaultAsync(wa => wa.Id == workAreaId); + + var buildingId = workArea?.Floor?.BuildingId; + var building = await _context.Buildings + .FirstOrDefaultAsync(b => b.Id == buildingId); + + // Step 6: Construct the response + var response = documents.Select(d => new + { + Id = d.Id, + BatchId = d.BatchId, + thumbnailUrl = d.ThumbS3Key != null + ? _s3Service.GeneratePreSignedUrlAsync(d.ThumbS3Key) + : (d.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.S3Key) : null), + ImageUrl = d.S3Key != null ? _s3Service.GeneratePreSignedUrlAsync(d.S3Key) : null, + UploadedBy = d.UploadedBy?.ToBasicEmployeeVMFromEmployee() ?? uploadedBy?.ToBasicEmployeeVMFromEmployee(), + UploadedAt = d.UploadedAt, + Source = source, + ProjectId = building?.ProjectId, + BuildingId = building?.Id, + BuildingName = building?.Name, + FloorIds = workArea?.Floor?.Id, + FloorName = workArea?.Floor?.FloorName, + WorkAreaId = workArea?.Id, + WorkAreaName = workArea?.AreaName, + TaskId = task?.Id, + ActivityName = workItem?.ActivityMaster?.ActivityName, + CommentId = comment?.Id, + Comment = comment?.Comment + }).ToList(); + + _logger.LogInfo("Fetched {Count} image(s) for BatchId: {BatchId}", response.Count, batchId); + + return Ok(ApiResponse.SuccessResponse(response, "Images for provided batchId fetched successfully", 200)); + } + } }