Skip to content

Commit

Permalink
Merge pull request #4357 from FuriousLlama/bugs/re-enable-sync
Browse files Browse the repository at this point in the history
Document sync update to allow of re-enabling doc types
  • Loading branch information
FuriousLlama authored Sep 24, 2024
2 parents 35bf8ee + 1c1af9b commit 20bbce7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
6 changes: 5 additions & 1 deletion source/backend/api/Services/DocumentSyncService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ public DocumentSyncResponse SyncPimsDocumentTypes(SyncModel model)
var matchingDocumentType = pimsDocumentTypes.FirstOrDefault(x => x.DocumentType == documentTypeModel.Name);
if (matchingDocumentType == null)
{
// New documents
var newPimsDocType = new PimsDocumentTyp()
{
DocumentType = documentTypeModel.Name,
Expand All @@ -173,16 +174,19 @@ public DocumentSyncResponse SyncPimsDocumentTypes(SyncModel model)
}
else
{
// Existing documents
var subtypeCodes = matchingDocumentType.PimsDocumentCategorySubtypes.Select(x => x.DocumentCategoryTypeCode).ToList();

var needsLabelUpdate = matchingDocumentType.DocumentTypeDescription != documentTypeModel.Label;
var needsCategoryUpdate = !(subtypeCodes.All(documentTypeModel.Categories.Contains) && subtypeCodes.Count == documentTypeModel.Categories.Count);
var needsOrderUpdate = matchingDocumentType.DisplayOrder != documentTypeModel.DisplayOrder;
if (needsLabelUpdate || needsCategoryUpdate || needsOrderUpdate)
var needsToBeEnabled = matchingDocumentType.IsDisabled == true;
if (needsLabelUpdate || needsCategoryUpdate || needsOrderUpdate || needsToBeEnabled)
{
matchingDocumentType.DocumentTypeDescription = documentTypeModel.Label;
matchingDocumentType.PimsDocumentCategorySubtypes = subcategories;
matchingDocumentType.DisplayOrder = documentTypeModel.DisplayOrder;
matchingDocumentType.IsDisabled = false;

updatedDocumentTypes.Add(documentTypeRepository.Update(matchingDocumentType));
}
Expand Down
37 changes: 37 additions & 0 deletions source/backend/tests/unit/api/Services/DocumentSyncServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,7 @@ public void Update_SyncPimsDocumentTypes_NoLabelChange()
DocumentCategoryTypeCode = "TEST_CATEGORY"
},
},
IsDisabled = false,
DisplayOrder = 1,
},
});
Expand All @@ -563,6 +564,42 @@ public void Update_SyncPimsDocumentTypes_NoLabelChange()
result.Updated.Should().BeEmpty();
}

[Fact]
public void Update_SyncPimsDocumentTypes_Disabled()
{
// Arrange
var service = this.CreateDocumentySyncServiceWithPermissions(Permissions.DocumentAdmin);

SyncModel model = this.CreateSyncModel(documentTypeModels: new List<Pims.Api.Models.PimsSync.DocumentTypeModel>() { new Pims.Api.Models.PimsSync.DocumentTypeModel() { Name = "TEST", Label = "test", Categories = new List<string>() { "TEST_CATEGORY" }, DisplayOrder = 1 } });

var pimsDocumentRepository = this._helper.GetService<Mock<IDocumentTypeRepository>>();
pimsDocumentRepository.Setup(x => x.GetAll()).Returns(new List<PimsDocumentTyp>() {
new PimsDocumentTyp() {
DocumentType = "TEST",
DocumentTypeDescription = "test",
PimsDocumentCategorySubtypes = new List<PimsDocumentCategorySubtype>() {
new PimsDocumentCategorySubtype() {
DocumentCategoryTypeCode = "TEST_CATEGORY"
},
},
IsDisabled = true,
DisplayOrder = 1,
},
});
pimsDocumentRepository.Setup(x => x.Add(It.IsAny<PimsDocumentTyp>())).Returns(new PimsDocumentTyp());
pimsDocumentRepository.Setup(x => x.Update(It.IsAny<PimsDocumentTyp>())).Returns(new PimsDocumentTyp());

// Act
var result = service.SyncPimsDocumentTypes(model);

// Assert
pimsDocumentRepository.Verify(x => x.Add(It.IsAny<PimsDocumentTyp>()), Times.Never());
pimsDocumentRepository.Verify(x => x.Update(It.IsAny<PimsDocumentTyp>()), Times.Once());
result.Added.Should().BeEmpty();
result.Deleted.Should().BeEmpty();
result.Updated.Should().HaveCount(1);
}

[Fact]
public void Update_SyncPimsDocumentTypes_CategoryChange()
{
Expand Down

0 comments on commit 20bbce7

Please sign in to comment.