Skip to content

Commit

Permalink
Psp 10042 fix error when trying to delete documents with no document …
Browse files Browse the repository at this point in the history
…queue. (#4663)

* psp-9813 ensure error messages from backend display properly.

* psp-10042, ignore deletion failures when no document queue item exists.

---------

Co-authored-by: Alejandro Sanchez <[email protected]>
  • Loading branch information
devinleighsmith and asanchezr authored Feb 24, 2025
1 parent 5f1e206 commit ba0d7e7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
7 changes: 6 additions & 1 deletion source/backend/api/Services/DocumentFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -451,10 +451,15 @@ private PimsDocument RemoveDocumentMayanID(PimsDocument doc)
private void DeleteQueuedDocumentItem(long documentId)
{
var documentQueuedItem = _documentQueueRepository.GetByDocumentId(documentId);
if (documentQueuedItem == null)
{
Logger.LogWarning("Document Queue item not found for document {documentId}", documentId);
return;
}
if (documentQueuedItem.DocumentQueueStatusTypeCode == DocumentQueueStatusTypes.PENDING.ToString()
|| documentQueuedItem.DocumentQueueStatusTypeCode == DocumentQueueStatusTypes.PROCESSING.ToString())
{
throw new BadRequestException("Doucment in process can not be deleted");
throw new BadRequestException("Document in process can not be deleted");
}

bool deleted = _documentQueueRepository.Delete(documentQueuedItem);
Expand Down
42 changes: 42 additions & 0 deletions source/backend/tests/unit/api/Services/DocumentFileServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,48 @@ public async void DeleteDocumentResearch_Success_Status_Success()
Assert.Equal(result.Status, ExternalResponseStatus.Success);
}

[Fact]
public async void DeleteDocumentResearch_QueueNull_Status_Success()
{
// Arrange
var service = this.CreateDocumentFileServiceWithPermissions(Permissions.DocumentDelete, Permissions.ResearchFileEdit);
var documentService = this._helper.GetService<Mock<IDocumentService>>();
var documentRepository = this._helper.GetService<Mock<IDocumentRepository>>();
var documentQueueRepository = this._helper.GetService<Mock<IDocumentQueueRepository>>();
var researchDocumentRepository = this._helper.GetService<Mock<IResearchFileDocumentRepository>>();

documentRepository.Setup(x => x.Find(It.IsAny<long>())).Returns(new PimsDocument() { DocumentId = 2, MayanId = 200 });
documentRepository.Setup(x => x.BeginTransaction()).Returns(new Mock<IDbContextTransaction>().Object);
documentService.Setup(x => x.DeleteMayanStorageDocumentAsync(It.IsAny<long>())).ReturnsAsync(new ExternalResponse<string>()
{
Status = ExternalResponseStatus.Success,
HttpStatusCode = System.Net.HttpStatusCode.OK,
});
documentRepository.Setup(x => x.Update(It.IsAny<PimsDocument>(), false)).Returns(new PimsDocument() { DocumentId = 2, MayanId = null });
documentQueueRepository.Setup(x => x.Delete(It.IsAny<PimsDocumentQueue>())).Returns(true);
researchDocumentRepository.Setup(x => x.DeleteResearch(It.IsAny<PimsResearchFileDocument>())).Returns(true);
documentRepository.Setup(x => x.DeleteDocument(It.IsAny<PimsDocument>())).Returns(true);

PimsResearchFileDocument doc = new()
{
Internal_Id = 1,
DocumentId = 2,
Document = new PimsDocument()
{
DocumentId = 2,
MayanId = 200,
}
};

// Act
documentQueueRepository.Setup(x => x.GetByDocumentId(It.IsAny<long>())).Returns((PimsDocumentQueue)null); // mimic the queued document not found.
var result = await service.DeleteResearchDocumentAsync(doc);

// Assert
Assert.NotNull(result);
Assert.Equal(result.Status, ExternalResponseStatus.Success);
}

[Fact]
public async void DeleteDocumentResearch_Success_Status_NotFound()
{
Expand Down

0 comments on commit ba0d7e7

Please sign in to comment.