From 242c345ce1f1d89ae8f35f160f5c5dcde7d3950f Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Fri, 29 Aug 2025 18:23:55 +0530 Subject: [PATCH] Added the API to activate and deactivate the document --- .../Controllers/DocumentController.cs | 68 +++++++++++++++++-- 1 file changed, 64 insertions(+), 4 deletions(-) diff --git a/Marco.Pms.Services/Controllers/DocumentController.cs b/Marco.Pms.Services/Controllers/DocumentController.cs index c0e5091..4e986a6 100644 --- a/Marco.Pms.Services/Controllers/DocumentController.cs +++ b/Marco.Pms.Services/Controllers/DocumentController.cs @@ -1111,14 +1111,74 @@ namespace Marco.Pms.Services.Controllers } } - - // DELETE api//5 - [HttpDelete("{id}")] - public async Task Delete(int id) + [HttpDelete("delete/{id}")] + public async Task DeleteDocumentAsync(Guid id, [FromQuery] bool isActive = false) { + // Create a new DbContext instance asynchronously + await using var _context = await _dbContextFactory.CreateDbContextAsync(); + + // Create a new service scope to resolve scoped services like permission and logging helpers + using var scope = _serviceScope.CreateScope(); + var _permission = scope.ServiceProvider.GetRequiredService(); + var updateLogHelper = scope.ServiceProvider.GetRequiredService(); + + // Message to indicate whether the document is being activated or deactivated + var message = isActive ? "activated" : "deactivated"; + + // Get the currently logged-in employee var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + + // Log the start of the delete operation for traceability + _logger.LogInfo("DeleteDocument started for document ID: {DocumentId} by employee ID: {EmployeeId}", id, loggedInEmployee.Id); + + // Retrieve the document attachment matching the criteria from the database + var documentAttachment = await _context.DocumentAttachments + .FirstOrDefaultAsync(da => da.Id == id && da.IsCurrentVersion && da.TenantId == tenantId && da.IsActive != isActive); + + // If the document attachment is not found, log a warning and return 404 Not Found + if (documentAttachment == null) + { + _logger.LogWarning("DocumentAttachment not found for ID: {DocumentId}", id); + return NotFound(ApiResponse.ErrorResponse("Document Attachment not found", "Document Attachment not found in database", 404)); + } + + // Check if the logged in employee has permission to delete OR is the owner of the document attachment + var hasDeletePermission = await _permission.HasPermission(PermissionsMaster.DeleteDocument, loggedInEmployee.Id); + if (!hasDeletePermission && loggedInEmployee.Id != documentAttachment.EntityId) + { + _logger.LogWarning("Access denied for employee ID: {EmployeeId} when attempting to delete document ID: {DocumentId}", loggedInEmployee.Id, id); + return StatusCode(403, ApiResponse.ErrorResponse("Access Denied.", "You do not have permission to delete documents", 403)); + } + + // Log the current state of the document attachment before updating for audit trail + var existingEntityBson = updateLogHelper.EntityToBsonDocument(documentAttachment); + + // Update document attachment status and metadata + documentAttachment.IsActive = isActive; + documentAttachment.IsVerified = null; + documentAttachment.UpdatedAt = DateTime.UtcNow; + documentAttachment.UpdatedById = loggedInEmployee.Id; + + // Persist changes to the database + await _context.SaveChangesAsync(); + + // Log the update operation to MongoDB for inspection and history + await updateLogHelper.PushToUpdateLogsAsync(new UpdateLogsObject + { + EntityId = documentAttachment.Id.ToString(), + UpdatedById = loggedInEmployee.Id.ToString(), + OldObject = existingEntityBson, + UpdatedAt = DateTime.UtcNow + }, Collection); + + // Log the successful completion of the operation + _logger.LogInfo("DocumentAttachment ID: {DocumentId} has been {Message} by employee ID: {EmployeeId}", id, message, loggedInEmployee.Id); + + // Return success response + return Ok(ApiResponse.SuccessResponse(new { }, $"Document attachment is {message}", 200)); } + #region =================================================================== Helper Functions =================================================================== private DocumentFilter? TryDeserializeFilter(string? filter)