From 0ef97a3a9794d38b744703fc22a2ca4380246190 Mon Sep 17 00:00:00 2001 From: devinleighsmith Date: Wed, 17 Jan 2024 17:30:22 -0800 Subject: [PATCH] psp-7609 ensure that changing the status of an acq file generates a note. --- .../api/Services/DispositionFileService.cs | 35 +++++++++++++++++- .../Services/DispositionFileServiceTest.cs | 36 +++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) diff --git a/source/backend/api/Services/DispositionFileService.cs b/source/backend/api/Services/DispositionFileService.cs index 402cb275e9..6e24f5446e 100644 --- a/source/backend/api/Services/DispositionFileService.cs +++ b/source/backend/api/Services/DispositionFileService.cs @@ -31,6 +31,7 @@ public class DispositionFileService : IDispositionFileService private readonly IPropertyService _propertyService; private readonly ILookupRepository _lookupRepository; private readonly IDispositionFileChecklistRepository _checklistRepository; + private readonly IEntityNoteRepository _entityNoteRepository; public DispositionFileService( ClaimsPrincipal user, @@ -41,7 +42,8 @@ public DispositionFileService( IPropertyRepository propertyRepository, IPropertyService propertyService, ILookupRepository lookupRepository, - IDispositionFileChecklistRepository checklistRepository) + IDispositionFileChecklistRepository checklistRepository, + IEntityNoteRepository entityNoteRepository) { _user = user; _logger = logger; @@ -52,6 +54,7 @@ public DispositionFileService( _propertyService = propertyService; _lookupRepository = lookupRepository; _checklistRepository = checklistRepository; + _entityNoteRepository = entityNoteRepository; } public PimsDispositionFile Add(PimsDispositionFile dispositionFile, IEnumerable userOverrides) @@ -111,6 +114,7 @@ public PimsDispositionFile Update(long id, PimsDispositionFile dispositionFile, } _dispositionFileRepository.Update(id, dispositionFile); + AddNoteIfStatusChanged(dispositionFile); _dispositionFileRepository.CommitTransaction(); return _dispositionFileRepository.GetById(id); @@ -380,6 +384,35 @@ public List GetDispositionFileExport(DispositionFilt }).ToList(); } + private void AddNoteIfStatusChanged(PimsDispositionFile updateDispositionFile) + { + var currentDispositionFile = _dispositionFileRepository.GetById(updateDispositionFile.Internal_Id); + bool statusChanged = currentDispositionFile.DispositionFileStatusTypeCode != updateDispositionFile.DispositionFileStatusTypeCode; + if (!statusChanged) + { + return; + } + + var newStatus = _lookupRepository.GetAllDispositionFileStatusTypes() + .FirstOrDefault(x => x.DispositionFileStatusTypeCode == updateDispositionFile.DispositionFileStatusTypeCode); + + PimsDispositionFileNote fileNoteInstance = new() + { + DispositionFileId = updateDispositionFile.Internal_Id, + AppCreateTimestamp = DateTime.Now, + AppCreateUserid = _user.GetUsername(), + Note = new PimsNote() + { + IsSystemGenerated = true, + NoteTxt = $"Disposition File status changed from {currentDispositionFile.DispositionFileStatusTypeCodeNavigation.Description} to {newStatus.Description}", + AppCreateTimestamp = DateTime.Now, + AppCreateUserid = this._user.GetUsername(), + }, + }; + + _entityNoteRepository.Add(fileNoteInstance); + } + private static decimal CalculateNetProceedsBeforeSpp(PimsDispositionSale sale) { if (sale != null) diff --git a/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs b/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs index 5e059f5f65..11a1ac0927 100644 --- a/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs @@ -362,6 +362,42 @@ public void Update_Success() Assert.NotNull(result); repository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Once); } + + [Fact] + public void Update_Success_AddsNote() + { + // Arrange + var service = this.CreateDispositionServiceWithPermissions(Permissions.DispositionEdit); + + var dspFile = EntityHelper.CreateDispositionFile(); + dspFile.ConcurrencyControlNumber = 1; + dspFile.AppCreateUserid = "TESTER"; + + var repository = this._helper.GetService>(); + var noteRepository = this._helper.GetService>(); + var lookupRepository = this._helper.GetService>(); + + repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(dspFile); + repository.Setup(x => x.GetRowVersion(It.IsAny())).Returns(1); + repository.Setup(x => x.GetById(It.IsAny())).Returns(new PimsDispositionFile() + { + DispositionFileStatusTypeCode = "CLOSED", + DispositionFileStatusTypeCodeNavigation = new PimsDispositionFileStatusType() { Description = "Closed" }, + }); + lookupRepository.Setup(x => x.GetAllRegions()).Returns(new List() { new PimsRegion() { Code = 4, RegionName = "Cannot determine" } }); + lookupRepository.Setup(x => x.GetAllDispositionFileStatusTypes()).Returns(new PimsDispositionFileStatusType[]{ new PimsDispositionFileStatusType() { + Id = dspFile.DispositionFileStatusTypeCodeNavigation.Id, + Description = dspFile.DispositionFileStatusTypeCodeNavigation.Description, + },}); + + // Act + var result = service.Update(dspFile.Internal_Id, dspFile, new List() { UserOverrideCode.UpdateRegion }); + + // Assert + repository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Once); + noteRepository.Verify(x => x.Add(It.Is(x => x.DispositionFileId == 1 + && x.Note.NoteTxt == "Disposition File status changed from Closed to Active")), Times.Once); + } #endregion #region GetTeamMembers