diff --git a/Marco.Pms.Services/Controllers/DocumentController.cs b/Marco.Pms.Services/Controllers/DocumentController.cs index b6da516..5f6fc27 100644 --- a/Marco.Pms.Services/Controllers/DocumentController.cs +++ b/Marco.Pms.Services/Controllers/DocumentController.cs @@ -52,10 +52,6 @@ namespace Marco.Pms.Services.Controllers tenantId = userHelper.GetTenantId(); } - - /// - /// Fetch documents for a given entity (Project/Employee) with filtering, searching, and pagination. - /// [HttpGet("list/{entityTypeId}/entity/{entityId}")] public async Task GetDocumentListAsync(Guid entityTypeId, Guid entityId, [FromQuery] string? filter, [FromQuery] string? searchString, [FromQuery] bool isActive = true, [FromQuery] int pageNumber = 1, [FromQuery] int pageSize = 20) @@ -225,16 +221,108 @@ namespace Marco.Pms.Services.Controllers } } - // GET api//5 [HttpGet("{id}")] public async Task Get(int id) { + await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); + using var scope = _serviceScope.CreateScope(); + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); //string preSignedUrl = _s3Service.GeneratePreSignedUrl(objectKey); } + [HttpGet("get/filter{entityTypeId}")] + public async Task GetFilterObjectAsync(Guid entityTypeId) + { + // Log: Starting filter fetch process + _logger.LogInfo("Initiating GetFilterObjectAsync to retrieve document filter data."); + + await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); + using var scope = _serviceScope.CreateScope(); + + // Get current logged-in employee + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + _logger.LogDebug("Fetched current employee: {EmployeeId}", loggedInEmployee.Id); + + // Fetch all relevant document attachments for the tenant with related data + var documentList = await dbContext.DocumentAttachments + .Include(da => da.UploadedBy) + .Include(da => da.DocumentType) + .ThenInclude(dt => dt!.DocumentCategory) + .Where(da => da.DocumentType != null && + da.DocumentType.DocumentCategory != null && + da.DocumentType.DocumentCategory.EntityTypeId == entityTypeId && + da.TenantId == tenantId) + .ToListAsync(); + _logger.LogInfo("Fetched {Count} document attachments for tenant {TenantId}", documentList.Count, tenantId); + + // Select IDs for attachments present in documentList + var documentIds = documentList.Select(da => da.Id).ToList(); + + // Preload tags for given ids + var documentTags = await dbContext.AttachmentTagMappings + .Where(at => documentIds.Contains(at.AttachmentId) && at.DocumentTag != null) + .Select(at => new + { + Id = at.DocumentTag!.Id, + Name = at.DocumentTag.Name + }) + .Distinct() + .ToListAsync(); + _logger.LogInfo("Loaded {Count} document tags", documentTags.Count); + + // Gather unique UploadedBy users + var uploadedBy = documentList + .Where(da => da.UploadedBy != null) + .Select(da => new + { + Id = da.UploadedBy!.Id, + Name = $"{da.UploadedBy.FirstName} {da.UploadedBy.LastName}" + }) + .Distinct() + .ToList(); + _logger.LogInfo("Collected {Count} unique uploaders", uploadedBy.Count); + + // Gather unique DocumentCategories + var documentCategories = documentList + .Where(da => da.DocumentType?.DocumentCategory != null) + .Select(da => new + { + Id = da.DocumentType!.DocumentCategory!.Id, + Name = da.DocumentType.DocumentCategory.Name + }) + .Distinct() + .ToList(); + _logger.LogInfo("Collected {Count} unique document categories", documentCategories.Count); + + // Gather unique DocumentTypes + var documentTypes = documentList + .Where(da => da.DocumentType != null) + .Select(da => new + { + Id = da.DocumentType!.Id, + Name = da.DocumentType.Name + }) + .Distinct() + .ToList(); + _logger.LogInfo("Collected {Count} unique document types", documentTypes.Count); + + // Compose response + var response = new + { + UploadedBy = uploadedBy, + DocumentCategory = documentCategories, + DocumentType = documentTypes, + DocumentTag = documentTags + }; + + _logger.LogInfo("Returning filter response successfully."); + return Ok(ApiResponse.SuccessResponse(response, "Filters for documents fetched successfully", 200)); + } + + /// /// Uploads a document attachment for an Employee or Project. /// Validates permissions, document type, entity existence, tags, and uploads to S3. @@ -739,7 +827,7 @@ namespace Marco.Pms.Services.Controllers response.Version = newVersionMapping.Version; _logger.LogInfo("API completed successfully for AttachmentId: {AttachmentId}", newAttachment.Id); - return Ok(ApiResponse.SuccessResponse(response, "Document added successfully", 200)); + return Ok(ApiResponse.SuccessResponse(response, "Document Updated successfully", 200)); } catch (Exception ex) {