Document_Manager #129

Merged
ashutosh.nehete merged 83 commits from Document_Manager into main 2025-09-11 04:12:01 +00:00
Showing only changes of commit 87cf37ca03 - Show all commits

View File

@ -536,17 +536,90 @@ namespace Marco.Pms.Services.Controllers
} }
} }
/// <summary>
/// Verifies a document attachment by its ID. Checks permissions, logs the operation, and updates verification fields.
/// </summary>
/// <param name="id">Document Attachment ID (Guid)</param>
/// <param name="isVerify">Flag to verify or unverify the document (default: true)</param>
[HttpPost("verify/{id}")] [HttpPost("verify/{id}")]
public async Task<IActionResult> VerifyDocumentAsync(Guid id) public async Task<IActionResult> VerifyDocumentAsync(Guid id, [FromQuery] bool isVerify = true)
{ {
// Begin: Create DbContext and DI scope
await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); await using var dbContext = await _dbContextFactory.CreateDbContextAsync();
using var scope = _serviceScope.CreateScope(); using var scope = _serviceScope.CreateScope();
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); try
{
// Get current logged-in employee for authentication/auditing
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
var tenantId = loggedInEmployee.TenantId;
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Document is verified successfully", 200)); _logger.LogInfo("Attempting to verify document. EmployeeId: {EmployeeId}, DocumentId: {DocumentId}, IsVerify: {IsVerify}",
loggedInEmployee.Id, id, isVerify);
// Fetch active/current document by Id, TenantId, and relevant conditions
var documentAttachment = await dbContext.DocumentAttachments
.FirstOrDefaultAsync(da => da.Id == id && da.IsActive && da.IsCurrentVersion && da.TenantId == tenantId);
if (documentAttachment == null)
{
_logger.LogWarning("Document attachment not found. Requested Id: {DocumentId}, TenantId: {TenantId}", id, tenantId);
return NotFound(ApiResponse<object>.ErrorResponse("Attachment not found", "Attachment not found in database", 404));
}
// Permission service: check if employee is authorized to verify documents
var permissionService = scope.ServiceProvider.GetRequiredService<PermissionServices>();
var hasVerifyPermission = await permissionService.HasPermission(PermissionsMaster.VerifyDocument, loggedInEmployee.Id);
if (!hasVerifyPermission)
{
_logger.LogWarning("Access denied for document verification. EmployeeId: {EmployeeId}, DocumentId: {DocumentId}", loggedInEmployee.Id, id);
return StatusCode(403, ApiResponse<object>.ErrorResponse("Access Denied.", "You do not have permission to verify this document", 403));
}
// Log existing entity state before update (for audit trail)
var updateLogHelper = scope.ServiceProvider.GetRequiredService<UtilityMongoDBHelper>();
var existingEntityBson = updateLogHelper.EntityToBsonDocument(documentAttachment);
// Update document verification status and audit fields
documentAttachment.IsVerified = isVerify;
documentAttachment.VerifiedAt = DateTime.UtcNow;
documentAttachment.VerifiedById = loggedInEmployee.Id;
// Commit changes
await dbContext.SaveChangesAsync();
// Log the update to MongoDB for change tracking
await updateLogHelper.PushToUpdateLogsAsync(new UpdateLogsObject
{
EntityId = documentAttachment.Id.ToString(),
UpdatedById = loggedInEmployee.Id.ToString(),
OldObject = existingEntityBson,
UpdatedAt = DateTime.UtcNow
}, Collection);
var versionMapping = await dbContext.AttachmentVersionMappings.FirstOrDefaultAsync(av => av.ChildAttachmentId == documentAttachment.Id);
var response = _mapper.Map<DocumentListVM>(documentAttachment);
if (versionMapping != null)
{
response.ParentAttachmentId = versionMapping.ParentAttachmentId;
response.Version = versionMapping.Version;
}
_logger.LogInfo("Document verified successfully. DocumentId: {DocumentId}, VerifiedBy: {EmployeeId}", documentAttachment.Id, loggedInEmployee.Id);
return Ok(ApiResponse<object>.SuccessResponse(new { }, "Document is verified successfully", 200));
}
catch (Exception ex)
{
// Handle unexpected errors gracefully
_logger.LogError(ex, "Error occurred during document verification. DocumentId: {DocumentId}", id);
return StatusCode(500, ApiResponse<object>.ErrorResponse("Server Error", "An error occurred while verifying the document", 500));
}
} }
[HttpPut("edit/{id}")] [HttpPut("edit/{id}")]
public async Task<IActionResult> UpdateDocumentAsync(Guid id, [FromBody] UpdateDocumentAttachmentDto model) public async Task<IActionResult> UpdateDocumentAsync(Guid id, [FromBody] UpdateDocumentAttachmentDto model)
{ {
@ -568,7 +641,7 @@ namespace Marco.Pms.Services.Controllers
var oldAttachment = await dbContext.DocumentAttachments var oldAttachment = await dbContext.DocumentAttachments
.Include(da => da.DocumentType) .Include(da => da.DocumentType)
.ThenInclude(dt => dt!.DocumentCategory) .ThenInclude(dt => dt!.DocumentCategory)
.FirstOrDefaultAsync(da => da.Id == id && da.IsCurrentVersion && da.TenantId == tenantId); .FirstOrDefaultAsync(da => da.Id == id && da.IsActive && da.IsCurrentVersion && da.TenantId == tenantId);
if (oldAttachment == null) if (oldAttachment == null)
{ {