Merge pull request 'Added the API to verify or reject the document' (#122) from Ashutosh_Task#1025 into Document_Manager

Reviewed-on: #122
This commit is contained in:
ashutosh.nehete 2025-08-29 07:41:07 +00:00
commit 61f5b14fcc

View File

@ -536,16 +536,89 @@ 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}")]
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();
using var scope = _serviceScope.CreateScope();
try
{
// Get current logged-in employee for authentication/auditing
var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync();
var tenantId = loggedInEmployee.TenantId;
_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}")]
public async Task<IActionResult> UpdateDocumentAsync(Guid id, [FromBody] UpdateDocumentAttachmentDto model)
@ -568,7 +641,7 @@ namespace Marco.Pms.Services.Controllers
var oldAttachment = await dbContext.DocumentAttachments
.Include(da => da.DocumentType)
.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)
{