Compare commits
No commits in common. "a24db1716fa7152f2d0cff70c43e2e5385a13cb8" and "61f5b14fcc051fe6871bfb64793184121afe3d45" have entirely different histories.
a24db1716f
...
61f5b14fcc
@ -1,18 +0,0 @@
|
||||
using Marco.Pms.Model.ViewModels.Activities;
|
||||
|
||||
namespace Marco.Pms.Model.ViewModels.DocumentManager
|
||||
{
|
||||
public class AttachmentVersionVM
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string? Name { get; set; }
|
||||
public string? DocumentId { get; set; }
|
||||
public string? PreSignedUrl { get; set; }
|
||||
public int Version { get; set; }
|
||||
public DateTime UploadedAt { get; set; }
|
||||
public BasicEmployeeVM? UploadedBy { get; set; }
|
||||
public DateTime? UpdatedAt { get; set; }
|
||||
public BasicEmployeeVM? UpdatedBy { get; set; }
|
||||
public bool? IsVerified { get; set; }
|
||||
}
|
||||
}
|
||||
@ -223,27 +223,23 @@ namespace Marco.Pms.Services.Controllers
|
||||
|
||||
// GET api/<DocumentController>/5
|
||||
[HttpGet("{id}")]
|
||||
public async Task<IActionResult> GetDetails(int id)
|
||||
public async Task Get(int id)
|
||||
{
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
using var scope = _serviceScope.CreateScope();
|
||||
|
||||
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
//if (versionMapping.ChildAttachment != null && versionMapping.ChildAttachment.Document != null)
|
||||
//{
|
||||
// var s3Service = scope.ServiceProvider.GetRequiredService<S3UploadService>();
|
||||
// documentVM.PreSignedUrl = s3Service.GeneratePreSignedUrl(versionMapping.ChildAttachment.Document.S3Key);
|
||||
//}
|
||||
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Filters for documents fetched successfully", 200));
|
||||
|
||||
//string preSignedUrl = _s3Service.GeneratePreSignedUrl(objectKey);
|
||||
}
|
||||
|
||||
[HttpGet("get/filter/{entityTypeId}")]
|
||||
[HttpGet("get/filter{entityTypeId}")]
|
||||
public async Task<IActionResult> GetFilterObjectAsync(Guid entityTypeId)
|
||||
{
|
||||
// Log: Starting filter fetch process
|
||||
_logger.LogInfo("Initiating GetFilterObjectAsync to retrieve document filter data.");
|
||||
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
using var scope = _serviceScope.CreateScope();
|
||||
|
||||
// Get current logged-in employee
|
||||
@ -251,7 +247,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
_logger.LogDebug("Fetched current employee: {EmployeeId}", loggedInEmployee.Id);
|
||||
|
||||
// Fetch all relevant document attachments for the tenant with related data
|
||||
var documentList = await _context.DocumentAttachments
|
||||
var documentList = await dbContext.DocumentAttachments
|
||||
.Include(da => da.UploadedBy)
|
||||
.Include(da => da.DocumentType)
|
||||
.ThenInclude(dt => dt!.DocumentCategory)
|
||||
@ -266,7 +262,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
var documentIds = documentList.Select(da => da.Id).ToList();
|
||||
|
||||
// Preload tags for given ids
|
||||
var documentTags = await _context.AttachmentTagMappings
|
||||
var documentTags = await dbContext.AttachmentTagMappings
|
||||
.Where(at => documentIds.Contains(at.AttachmentId) && at.DocumentTag != null)
|
||||
.Select(at => new
|
||||
{
|
||||
@ -326,60 +322,6 @@ namespace Marco.Pms.Services.Controllers
|
||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Filters for documents fetched successfully", 200));
|
||||
}
|
||||
|
||||
[HttpGet("list/versions/{parentAttachmentId}")]
|
||||
public async Task<IActionResult> GetAllVersionsAsync(Guid parentAttachmentId)
|
||||
{
|
||||
_logger.LogInfo("Start fetching document versions for ParentAttachmentId: {ParentAttachmentId}", parentAttachmentId);
|
||||
|
||||
// Create a new DbContext instance asynchronously
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
|
||||
try
|
||||
{
|
||||
// Retrieve currently logged in employee details for potential security or filtering checks
|
||||
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
if (loggedInEmployee == null)
|
||||
{
|
||||
_logger.LogWarning("No logged in employee found while fetching versions for ParentAttachmentId: {ParentAttachmentId}", parentAttachmentId);
|
||||
return Unauthorized(ApiResponse<object>.ErrorResponse("Unauthorized access", 401));
|
||||
}
|
||||
|
||||
// Retrieve all version mappings linked to the parent attachment and tenant
|
||||
var versionMappings = await _context.AttachmentVersionMappings
|
||||
.Include(av => av.ChildAttachment)
|
||||
.ThenInclude(da => da!.UploadedBy)
|
||||
.Include(av => av.ChildAttachment)
|
||||
.ThenInclude(da => da!.UpdatedBy)
|
||||
.Where(av => av.ParentAttachmentId == parentAttachmentId && av.TenantId == tenantId)
|
||||
.ToListAsync();
|
||||
|
||||
_logger.LogInfo("Found {Count} versions for ParentAttachmentId: {ParentAttachmentId}", versionMappings.Count, parentAttachmentId);
|
||||
|
||||
// Map the retrieved child attachments to view models with version info
|
||||
var response = versionMappings.Select(versionMapping =>
|
||||
{
|
||||
var documentVM = _mapper.Map<AttachmentVersionVM>(versionMapping.ChildAttachment);
|
||||
documentVM.Version = versionMapping.Version;
|
||||
return documentVM;
|
||||
}).ToList();
|
||||
|
||||
_logger.LogInfo("Successfully mapped version data for ParentAttachmentId: {ParentAttachmentId}", parentAttachmentId);
|
||||
|
||||
return Ok(ApiResponse<object>.SuccessResponse(response, "Document versions fetched successfully", 200));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Log the exception and return an internal server error response
|
||||
_logger.LogError(ex, "Error occurred while fetching document versions for ParentAttachmentId: {ParentAttachmentId}", parentAttachmentId);
|
||||
return StatusCode(500, ApiResponse<object>.ErrorResponse("An error occurred while fetching document versions", 500));
|
||||
}
|
||||
finally
|
||||
{
|
||||
_logger.LogInfo("End processing GetAllVersions for ParentAttachmentId: {ParentAttachmentId}", parentAttachmentId);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Uploads a document attachment for an Employee or Project.
|
||||
@ -389,7 +331,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
[HttpPost("upload")]
|
||||
public async Task<IActionResult> UploadDocumentAsync([FromBody] DocumentAttachmentDto model)
|
||||
{
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
using var scope = _serviceScope.CreateScope();
|
||||
|
||||
_logger.LogInfo("Document upload initiated for EntityId: {EntityId}, DocumentTypeId: {DocumentTypeId}", model.EntityId, model.DocumentTypeId);
|
||||
@ -410,7 +352,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
}
|
||||
|
||||
// Validate Document Type
|
||||
var documentType = await _context.DocumentTypeMasters
|
||||
var documentType = await dbContext.DocumentTypeMasters
|
||||
.Include(dt => dt.DocumentCategory)
|
||||
.FirstOrDefaultAsync(dt => dt.Id == model.DocumentTypeId && dt.TenantId == tenantId && dt.DocumentCategory != null);
|
||||
|
||||
@ -442,11 +384,11 @@ namespace Marco.Pms.Services.Controllers
|
||||
bool entityExists = false;
|
||||
if (entityType.Equals(EmployeeEntity))
|
||||
{
|
||||
entityExists = await _context.Employees.AnyAsync(e => e.Id == model.EntityId && e.TenantId == tenantId);
|
||||
entityExists = await dbContext.Employees.AnyAsync(e => e.Id == model.EntityId && e.TenantId == tenantId);
|
||||
}
|
||||
else if (entityType.Equals(ProjectEntity))
|
||||
{
|
||||
entityExists = await _context.Projects.AnyAsync(p => p.Id == model.EntityId && p.TenantId == tenantId);
|
||||
entityExists = await dbContext.Projects.AnyAsync(p => p.Id == model.EntityId && p.TenantId == tenantId);
|
||||
}
|
||||
|
||||
if (!entityExists)
|
||||
@ -463,8 +405,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
attachment.TenantId = tenantId;
|
||||
|
||||
// Validate Attachment
|
||||
var allowedSize = documentType.MaxSizeAllowedInMB * 1024;
|
||||
if (model.Attachment.FileSize > allowedSize)
|
||||
if (model.Attachment.FileSize > documentType.MaxSizeAllowedInMB)
|
||||
{
|
||||
_logger.LogWarning("File size {FileSize} exceeded max allowed {MaxSize}MB", model.Attachment.FileSize, documentType.MaxSizeAllowedInMB);
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("File size limit exceeded", $"Max allowed {documentType.MaxSizeAllowedInMB} MB.", 400));
|
||||
@ -522,11 +463,11 @@ namespace Marco.Pms.Services.Controllers
|
||||
TenantId = tenantId
|
||||
};
|
||||
|
||||
_context.Documents.Add(document);
|
||||
dbContext.Documents.Add(document);
|
||||
|
||||
attachment.DocumentDataId = document.Id;
|
||||
|
||||
_context.DocumentAttachments.Add(attachment);
|
||||
dbContext.DocumentAttachments.Add(attachment);
|
||||
|
||||
//Process Versioning
|
||||
|
||||
@ -537,13 +478,13 @@ namespace Marco.Pms.Services.Controllers
|
||||
Version = 1,
|
||||
TenantId = tenantId
|
||||
};
|
||||
_context.AttachmentVersionMappings.Add(versionMapping);
|
||||
dbContext.AttachmentVersionMappings.Add(versionMapping);
|
||||
|
||||
// Process Tags
|
||||
if (model.Tags?.Any() == true)
|
||||
{
|
||||
var names = model.Tags.Select(t => t.Name).ToList();
|
||||
var existingTags = await _context.DocumentTagMasters
|
||||
var existingTags = await dbContext.DocumentTagMasters
|
||||
.Where(t => names.Contains(t.Name) && t.TenantId == tenantId)
|
||||
.ToListAsync();
|
||||
|
||||
@ -562,7 +503,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
|
||||
if (existingTag == null)
|
||||
{
|
||||
_context.DocumentTagMasters.Add(tagEntity);
|
||||
dbContext.DocumentTagMasters.Add(tagEntity);
|
||||
}
|
||||
|
||||
attachmentTagMappings.Add(new AttachmentTagMapping
|
||||
@ -573,11 +514,11 @@ namespace Marco.Pms.Services.Controllers
|
||||
});
|
||||
}
|
||||
|
||||
_context.AttachmentTagMappings.AddRange(attachmentTagMappings);
|
||||
dbContext.AttachmentTagMappings.AddRange(attachmentTagMappings);
|
||||
}
|
||||
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
_logger.LogInfo("Document uploaded successfully. AttachmentId: {AttachmentId}, DocumentId: {DocumentId}", attachment.Id, document.Id);
|
||||
|
||||
@ -605,7 +546,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
public async Task<IActionResult> VerifyDocumentAsync(Guid id, [FromQuery] bool isVerify = true)
|
||||
{
|
||||
// Begin: Create DbContext and DI scope
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
using var scope = _serviceScope.CreateScope();
|
||||
|
||||
try
|
||||
@ -618,7 +559,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
loggedInEmployee.Id, id, isVerify);
|
||||
|
||||
// Fetch active/current document by Id, TenantId, and relevant conditions
|
||||
var documentAttachment = await _context.DocumentAttachments
|
||||
var documentAttachment = await dbContext.DocumentAttachments
|
||||
.FirstOrDefaultAsync(da => da.Id == id && da.IsActive && da.IsCurrentVersion && da.TenantId == tenantId);
|
||||
|
||||
if (documentAttachment == null)
|
||||
@ -647,7 +588,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
documentAttachment.VerifiedById = loggedInEmployee.Id;
|
||||
|
||||
// Commit changes
|
||||
await _context.SaveChangesAsync();
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
// Log the update to MongoDB for change tracking
|
||||
await updateLogHelper.PushToUpdateLogsAsync(new UpdateLogsObject
|
||||
@ -658,7 +599,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
UpdatedAt = DateTime.UtcNow
|
||||
}, Collection);
|
||||
|
||||
var versionMapping = await _context.AttachmentVersionMappings.FirstOrDefaultAsync(av => av.ChildAttachmentId == documentAttachment.Id);
|
||||
var versionMapping = await dbContext.AttachmentVersionMappings.FirstOrDefaultAsync(av => av.ChildAttachmentId == documentAttachment.Id);
|
||||
|
||||
var response = _mapper.Map<DocumentListVM>(documentAttachment);
|
||||
if (versionMapping != null)
|
||||
@ -688,7 +629,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
|
||||
try
|
||||
{
|
||||
await using var _context = await _dbContextFactory.CreateDbContextAsync();
|
||||
await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
|
||||
|
||||
// Get logged-in employee details
|
||||
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
|
||||
@ -697,7 +638,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
var hasUploadPermission = await permissionService.HasPermission(PermissionsMaster.UploadDocument, loggedInEmployee.Id);
|
||||
|
||||
// Fetch the existing attachment
|
||||
var oldAttachment = await _context.DocumentAttachments
|
||||
var oldAttachment = await dbContext.DocumentAttachments
|
||||
.Include(da => da.DocumentType)
|
||||
.ThenInclude(dt => dt!.DocumentCategory)
|
||||
.FirstOrDefaultAsync(da => da.Id == id && da.IsActive && da.IsCurrentVersion && da.TenantId == tenantId);
|
||||
@ -745,11 +686,11 @@ namespace Marco.Pms.Services.Controllers
|
||||
bool entityExists;
|
||||
if (entityType.Equals(EmployeeEntity))
|
||||
{
|
||||
entityExists = await _context.Employees.AnyAsync(e => e.Id == oldAttachment.EntityId && e.TenantId == tenantId);
|
||||
entityExists = await dbContext.Employees.AnyAsync(e => e.Id == oldAttachment.EntityId && e.TenantId == tenantId);
|
||||
}
|
||||
else if (entityType.Equals(ProjectEntity))
|
||||
{
|
||||
entityExists = await _context.Projects.AnyAsync(p => p.Id == oldAttachment.EntityId && p.TenantId == tenantId);
|
||||
entityExists = await dbContext.Projects.AnyAsync(p => p.Id == oldAttachment.EntityId && p.TenantId == tenantId);
|
||||
if (entityExists)
|
||||
{
|
||||
entityExists = await permissionService.HasProjectPermission(loggedInEmployee, oldAttachment.EntityId);
|
||||
@ -767,7 +708,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
}
|
||||
|
||||
// Prepare for versioning
|
||||
var oldVersionMapping = await _context.AttachmentVersionMappings
|
||||
var oldVersionMapping = await dbContext.AttachmentVersionMappings
|
||||
.FirstOrDefaultAsync(av => av.ChildAttachmentId == oldAttachment.Id && av.TenantId == tenantId);
|
||||
|
||||
var updateLogHelper = scope.ServiceProvider.GetRequiredService<UtilityMongoDBHelper>();
|
||||
@ -779,8 +720,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
if (model.Attachment != null)
|
||||
{
|
||||
// File size check
|
||||
var allowedSize = documentType.MaxSizeAllowedInMB * 1024;
|
||||
if (model.Attachment.FileSize > allowedSize)
|
||||
if (model.Attachment.FileSize > documentType.MaxSizeAllowedInMB)
|
||||
{
|
||||
_logger.LogWarning("Attachment exceeded max file size for DocumentTypeId: {DocumentTypeId}", documentType.Id);
|
||||
return BadRequest(ApiResponse<object>.ErrorResponse("File size limit exceeded", $"Max allowed {documentType.MaxSizeAllowedInMB} MB.", 400));
|
||||
@ -839,7 +779,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
TenantId = tenantId
|
||||
};
|
||||
|
||||
_context.Documents.Add(document);
|
||||
dbContext.Documents.Add(document);
|
||||
|
||||
if (oldAttachment.IsVerified == true)
|
||||
{
|
||||
@ -858,7 +798,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
TenantId = oldAttachment.TenantId
|
||||
};
|
||||
|
||||
_context.DocumentAttachments.Add(attachment);
|
||||
dbContext.DocumentAttachments.Add(attachment);
|
||||
|
||||
// Mark old version as not current
|
||||
oldAttachment.IsCurrentVersion = false;
|
||||
@ -885,7 +825,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
TenantId = tenantId
|
||||
};
|
||||
}
|
||||
_context.AttachmentVersionMappings.Add(versionMapping);
|
||||
dbContext.AttachmentVersionMappings.Add(versionMapping);
|
||||
|
||||
newAttachment = attachment;
|
||||
newVersionMapping = versionMapping;
|
||||
@ -897,13 +837,6 @@ namespace Marco.Pms.Services.Controllers
|
||||
oldAttachment.DocumentId = model.DocumentId;
|
||||
oldAttachment.Description = model.Description;
|
||||
oldAttachment.DocumentDataId = document.Id;
|
||||
if (oldAttachment.IsVerified == true)
|
||||
{
|
||||
oldAttachment.IsVerified = null;
|
||||
_logger.LogInfo("Reset verification flag for AttachmentId: {AttachmentId}", oldAttachment.Id);
|
||||
}
|
||||
oldAttachment.UpdatedAt = DateTime.UtcNow;
|
||||
oldAttachment.UpdatedById = loggedInEmployee.Id;
|
||||
|
||||
newAttachment = oldAttachment;
|
||||
newVersionMapping = oldVersionMapping ?? new AttachmentVersionMapping();
|
||||
@ -934,18 +867,17 @@ namespace Marco.Pms.Services.Controllers
|
||||
if (model.Tags?.Any() == true)
|
||||
{
|
||||
var names = model.Tags.Select(t => t.Name).ToList();
|
||||
var existingTags = await _context.DocumentTagMasters
|
||||
var existingTags = await dbContext.DocumentTagMasters
|
||||
.Where(t => names.Contains(t.Name) && t.TenantId == tenantId)
|
||||
.ToListAsync();
|
||||
|
||||
var attachmentTagMappings = new List<AttachmentTagMapping>();
|
||||
var oldTags = await _context.AttachmentTagMappings
|
||||
var oldTagNames = await dbContext.AttachmentTagMappings
|
||||
.Include(dt => dt.DocumentTag)
|
||||
.Where(dt => dt.DocumentTag != null && dt.AttachmentId == newAttachment.Id && dt.TenantId == tenantId)
|
||||
.Select(dt => dt.DocumentTag!.Name)
|
||||
.ToListAsync();
|
||||
|
||||
var oldTagNames = oldTags.Select(dt => dt.DocumentTag!.Name).ToList();
|
||||
|
||||
foreach (var tag in model.Tags.Where(t => t.IsActive && !oldTagNames.Contains(t.Name)))
|
||||
{
|
||||
var existingTag = existingTags.FirstOrDefault(t => t.Name == tag.Name);
|
||||
@ -959,7 +891,7 @@ namespace Marco.Pms.Services.Controllers
|
||||
|
||||
if (existingTag == null)
|
||||
{
|
||||
_context.DocumentTagMasters.Add(tagEntity);
|
||||
dbContext.DocumentTagMasters.Add(tagEntity);
|
||||
}
|
||||
|
||||
attachmentTagMappings.Add(new AttachmentTagMapping
|
||||
@ -970,26 +902,12 @@ namespace Marco.Pms.Services.Controllers
|
||||
});
|
||||
}
|
||||
|
||||
_context.AttachmentTagMappings.AddRange(attachmentTagMappings);
|
||||
|
||||
var deletedTagMappings = new List<AttachmentTagMapping>();
|
||||
|
||||
foreach (var tag in model.Tags.Where(t => !t.IsActive && oldTagNames.Contains(t.Name)))
|
||||
{
|
||||
var deletedTagMapping = oldTags.FirstOrDefault(at => at.DocumentTag!.Name == tag.Name);
|
||||
|
||||
if (deletedTagMapping != null)
|
||||
{
|
||||
deletedTagMappings.Add(deletedTagMapping);
|
||||
}
|
||||
}
|
||||
_context.AttachmentTagMappings.RemoveRange(deletedTagMappings);
|
||||
|
||||
dbContext.AttachmentTagMappings.AddRange(attachmentTagMappings);
|
||||
_logger.LogInfo("Tags processed for AttachmentId: {AttachmentId}", newAttachment.Id);
|
||||
}
|
||||
|
||||
// Persist changes to database
|
||||
await _context.SaveChangesAsync();
|
||||
await dbContext.SaveChangesAsync();
|
||||
_logger.LogInfo("Database changes committed for AttachmentId: {AttachmentId}", newAttachment.Id);
|
||||
|
||||
// Update logs
|
||||
|
||||
@ -310,7 +310,7 @@ namespace Marco.Pms.Services.MappingProfiles
|
||||
|
||||
CreateMap<DocumentAttachmentDto, DocumentAttachment>();
|
||||
CreateMap<DocumentAttachment, DocumentListVM>();
|
||||
CreateMap<DocumentAttachment, AttachmentVersionVM>();
|
||||
CreateMap<DocumentAttachmentDto, DocumentAttachment>();
|
||||
CreateMap<DocumentAttachmentDto, DocumentAttachment>();
|
||||
|
||||
CreateMap<DocumentCategoryMaster, DocumentCategoryVM>();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user