Document_Manager #129

Merged
ashutosh.nehete merged 83 commits from Document_Manager into main 2025-09-11 04:12:01 +00:00
3 changed files with 94 additions and 13 deletions
Showing only changes of commit 2ca9dead1f - Show all commits

View File

@ -0,0 +1,25 @@
using Marco.Pms.Model.ViewModels.Activities;
namespace Marco.Pms.Model.ViewModels.DocumentManager
{
public class DocumentAttachmentDetailsVM
{
public Guid Id { get; set; }
public string? Name { get; set; }
public string? DocumentId { get; set; }
public string? Description { get; set; }
public int Version { get; set; }
public bool IsCurrentVersion { get; set; }
public Guid ParentAttachmentId { get; set; }
public DateTime UploadedAt { get; set; }
public BasicEmployeeVM? UploadedBy { get; set; }
public DateTime? UpdatedAt { get; set; }
public BasicEmployeeVM? UpdatedBy { get; set; }
public DateTime? VerifiedAt { get; set; }
public bool? IsVerified { get; set; }
public BasicEmployeeVM? VerifiedBy { get; set; }
public Guid EntityId { get; set; }
public DocumentTypeVM? DocumentType { get; set; }
public bool IsActive { get; set; } = true;
}
}

View File

@ -221,20 +221,66 @@ namespace Marco.Pms.Services.Controllers
} }
} }
// GET api/<DocumentController>/5 [HttpGet("get/details/{id}")]
[HttpGet("{id}")] public async Task<IActionResult> GetDetailsAsync(Guid id)
public async Task<IActionResult> GetDetails(int id)
{ {
_logger.LogInfo("Starting GetDetails API for AttachmentId: {AttachmentId}", id);
// Create a new DbContext instance to fetch data
await using var _context = await _dbContextFactory.CreateDbContextAsync(); await using var _context = await _dbContextFactory.CreateDbContextAsync();
// Create a new scoped service provider to resolve scoped dependencies
using var scope = _serviceScope.CreateScope(); using var scope = _serviceScope.CreateScope();
// Resolve the permission service from the scoped service provider
var _permission = scope.ServiceProvider.GetRequiredService<PermissionServices>();
// Get the currently logged-in employee
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
//if (versionMapping.ChildAttachment != null && versionMapping.ChildAttachment.Document != null)
//{ _logger.LogDebug("Logged in employee id: {EmployeeId}", loggedInEmployee.Id);
// var s3Service = scope.ServiceProvider.GetRequiredService<S3UploadService>();
// documentVM.PreSignedUrl = s3Service.GeneratePreSignedUrl(versionMapping.ChildAttachment.Document.S3Key); // Fetch the AttachmentVersionMapping with all necessary related data loaded eagerly
//} var versionMapping = await _context.AttachmentVersionMappings
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Filters for documents fetched successfully", 200)); .Include(av => av.ChildAttachment)
.ThenInclude(da => da!.UploadedBy)
.ThenInclude(e => e!.JobRole)
.Include(av => av.ChildAttachment)
.ThenInclude(da => da!.UpdatedBy)
.ThenInclude(e => e!.JobRole)
.Include(av => av.ChildAttachment)
.ThenInclude(da => da!.VerifiedBy)
.ThenInclude(e => e!.JobRole)
.Include(av => av.ChildAttachment)
.ThenInclude(da => da!.DocumentType)
.FirstOrDefaultAsync(av => av.ChildAttachmentId == id && av.TenantId == tenantId);
// If no mapping found, return 404
if (versionMapping == null || versionMapping.ChildAttachment == null)
{
_logger.LogWarning("AttachmentVersionMapping not found for AttachmentId: {AttachmentId}, TenantId: {TenantId}",
id, tenantId);
return NotFound(ApiResponse<object>.ErrorResponse("Document Attachment not found", "Document Attachment not found in database", 404));
}
// Check if the logged in employee has permission to view the document OR is the owner of the attachment entity
var hasViewPermission = await _permission.HasPermission(PermissionsMaster.ViewDocument, loggedInEmployee.Id);
if (!hasViewPermission && loggedInEmployee.Id != versionMapping.ChildAttachment.EntityId)
{
_logger.LogWarning("Access Denied for Employee {EmployeeId} on EntityId {EntityId}",
loggedInEmployee.Id, versionMapping.ChildAttachment.EntityId);
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view documents", 403));
}
// Map the domain entity to the view model
var documentAttachmentVM = _mapper.Map<DocumentAttachmentDetailsVM>(versionMapping.ChildAttachment);
documentAttachmentVM.Version = versionMapping.Version;
documentAttachmentVM.ParentAttachmentId = versionMapping.ParentAttachmentId;
_logger.LogInfo("Document details fetched successfully for AttachmentId: {AttachmentId}", id);
// Return success response with document details
return Ok(ApiResponse<object>.SuccessResponse(documentAttachmentVM, "Document details fetched successfully", 200));
} }
[HttpGet("get/filter/{entityTypeId}")] [HttpGet("get/filter/{entityTypeId}")]
@ -333,6 +379,9 @@ namespace Marco.Pms.Services.Controllers
// Create a new DbContext instance asynchronously // Create a new DbContext instance asynchronously
await using var _context = await _dbContextFactory.CreateDbContextAsync(); await using var _context = await _dbContextFactory.CreateDbContextAsync();
using var scope = _serviceScope.CreateScope();
var _permission = scope.ServiceProvider.GetRequiredService<PermissionServices>();
try try
{ {
@ -353,6 +402,16 @@ namespace Marco.Pms.Services.Controllers
.Where(av => av.ParentAttachmentId == parentAttachmentId && av.TenantId == tenantId) .Where(av => av.ParentAttachmentId == parentAttachmentId && av.TenantId == tenantId)
.ToListAsync(); .ToListAsync();
var entityId = versionMappings.Select(av => av.ChildAttachment?.EntityId).FirstOrDefault();
// Check global permission
var hasViewPermission = await _permission.HasPermission(PermissionsMaster.ViewDocument, loggedInEmployee.Id);
if (!hasViewPermission && loggedInEmployee.Id != entityId)
{
_logger.LogWarning("Access Denied for Employee {EmployeeId} on EntityId {EntityId}", loggedInEmployee.Id, entityId ?? Guid.Empty);
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to view documents", 403));
}
_logger.LogInfo("Found {Count} versions for ParentAttachmentId: {ParentAttachmentId}", versionMappings.Count, parentAttachmentId); _logger.LogInfo("Found {Count} versions for ParentAttachmentId: {ParentAttachmentId}", versionMappings.Count, parentAttachmentId);
// Map the retrieved child attachments to view models with version info // Map the retrieved child attachments to view models with version info
@ -415,7 +474,6 @@ namespace Marco.Pms.Services.Controllers
// Return the pre-signed URL with a success response // Return the pre-signed URL with a success response
} }
/// <summary> /// <summary>
/// Uploads a document attachment for an Employee or Project. /// Uploads a document attachment for an Employee or Project.
/// Validates permissions, document type, entity existence, tags, and uploads to S3. /// Validates permissions, document type, entity existence, tags, and uploads to S3.

View File

@ -311,13 +311,11 @@ namespace Marco.Pms.Services.MappingProfiles
CreateMap<DocumentAttachmentDto, DocumentAttachment>(); CreateMap<DocumentAttachmentDto, DocumentAttachment>();
CreateMap<DocumentAttachment, DocumentListVM>(); CreateMap<DocumentAttachment, DocumentListVM>();
CreateMap<DocumentAttachment, AttachmentVersionVM>(); CreateMap<DocumentAttachment, AttachmentVersionVM>();
CreateMap<DocumentAttachmentDto, DocumentAttachment>(); CreateMap<DocumentAttachment, DocumentAttachmentDetailsVM>();
CreateMap<DocumentCategoryMaster, DocumentCategoryVM>(); CreateMap<DocumentCategoryMaster, DocumentCategoryVM>();
CreateMap<DocumentAttachmentDto, DocumentAttachment>();
CreateMap<DocumentTypeMaster, DocumentTypeVM>(); CreateMap<DocumentTypeMaster, DocumentTypeVM>();
CreateMap<DocumentAttachmentDto, DocumentAttachment>();
#endregion #endregion