From 0cdbd574db73b96794468405a923d8c8a45187f3 Mon Sep 17 00:00:00 2001 From: "ashutosh.nehete" Date: Fri, 29 Aug 2025 12:42:27 +0530 Subject: [PATCH] changing the documentDataId if attachment is not verified --- .../Controllers/DocumentController.cs | 109 +++++++++++------- 1 file changed, 68 insertions(+), 41 deletions(-) diff --git a/Marco.Pms.Services/Controllers/DocumentController.cs b/Marco.Pms.Services/Controllers/DocumentController.cs index 5f6fc27..adb872e 100644 --- a/Marco.Pms.Services/Controllers/DocumentController.cs +++ b/Marco.Pms.Services/Controllers/DocumentController.cs @@ -327,6 +327,7 @@ namespace Marco.Pms.Services.Controllers /// Uploads a document attachment for an Employee or Project. /// Validates permissions, document type, entity existence, tags, and uploads to S3. /// + [HttpPost("upload")] public async Task UploadDocumentAsync([FromBody] DocumentAttachmentDto model) { @@ -535,6 +536,17 @@ namespace Marco.Pms.Services.Controllers } } + [HttpPost("verify/{id}")] + public async Task VerifyDocumentAsync(Guid id) + { + await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); + using var scope = _serviceScope.CreateScope(); + + var loggedInEmployee = await _userHelper.GetCurrentEmployeeAsync(); + + return Ok(ApiResponse.SuccessResponse(new { }, "Document is verified successfully", 200)); + } + [HttpPut("edit/{id}")] public async Task UpdateDocumentAsync(Guid id, [FromBody] UpdateDocumentAttachmentDto model) { @@ -696,53 +708,68 @@ namespace Marco.Pms.Services.Controllers dbContext.Documents.Add(document); - // Record new document attachment as the current version - var attachment = new DocumentAttachment + if (oldAttachment.IsVerified == true) { - Name = model.Name, - DocumentId = model.DocumentId, - Description = model.Description, - IsCurrentVersion = true, - EntityId = oldAttachment.EntityId, - DocumentDataId = document.Id, - UploadedAt = DateTime.UtcNow, - UploadedById = loggedInEmployee.Id, - DocumentTypeId = oldAttachment.DocumentTypeId, - TenantId = oldAttachment.TenantId - }; - - dbContext.DocumentAttachments.Add(attachment); - - // Mark old version as not current - oldAttachment.IsCurrentVersion = false; - - // Version mapping - AttachmentVersionMapping versionMapping; - if (oldVersionMapping != null) - { - versionMapping = new AttachmentVersionMapping + // Record new document attachment as the current version + var attachment = new DocumentAttachment { - ParentAttachmentId = oldVersionMapping.ParentAttachmentId, - ChildAttachmentId = attachment.Id, - Version = (oldVersionMapping.Version + 1), - TenantId = tenantId + Name = model.Name, + DocumentId = model.DocumentId, + Description = model.Description, + IsCurrentVersion = true, + EntityId = oldAttachment.EntityId, + DocumentDataId = document.Id, + UploadedAt = DateTime.UtcNow, + UploadedById = loggedInEmployee.Id, + DocumentTypeId = oldAttachment.DocumentTypeId, + TenantId = oldAttachment.TenantId }; + + dbContext.DocumentAttachments.Add(attachment); + + // Mark old version as not current + oldAttachment.IsCurrentVersion = false; + + // Version mapping + AttachmentVersionMapping versionMapping; + if (oldVersionMapping != null) + { + versionMapping = new AttachmentVersionMapping + { + ParentAttachmentId = oldVersionMapping.ParentAttachmentId, + ChildAttachmentId = attachment.Id, + Version = (oldVersionMapping.Version + 1), + TenantId = tenantId + }; + } + else + { + versionMapping = new AttachmentVersionMapping + { + ParentAttachmentId = attachment.Id, + ChildAttachmentId = attachment.Id, + Version = 1, + TenantId = tenantId + }; + } + dbContext.AttachmentVersionMappings.Add(versionMapping); + + newAttachment = attachment; + newVersionMapping = versionMapping; + _logger.LogInfo("Created new current version for AttachmentId: {AttachmentId}", attachment.Id); } else { - versionMapping = new AttachmentVersionMapping - { - ParentAttachmentId = attachment.Id, - ChildAttachmentId = attachment.Id, - Version = 1, - TenantId = tenantId - }; - } - dbContext.AttachmentVersionMappings.Add(versionMapping); + oldAttachment.Name = model.Name; + oldAttachment.DocumentId = model.DocumentId; + oldAttachment.Description = model.Description; + oldAttachment.DocumentDataId = document.Id; + + newAttachment = oldAttachment; + newVersionMapping = oldVersionMapping ?? new AttachmentVersionMapping(); + _logger.LogInfo("Attachment metadata updated for AttachmentId: {AttachmentId}", oldAttachment.Id); + } - newAttachment = attachment; - newVersionMapping = versionMapping; - _logger.LogInfo("Created new current version for AttachmentId: {AttachmentId}", attachment.Id); } else { @@ -752,7 +779,7 @@ namespace Marco.Pms.Services.Controllers oldAttachment.Description = model.Description; if (oldAttachment.IsVerified == true) { - oldAttachment.IsVerified = false; + oldAttachment.IsVerified = null; _logger.LogInfo("Reset verification flag for AttachmentId: {AttachmentId}", oldAttachment.Id); } oldAttachment.UpdatedAt = DateTime.UtcNow;