From 2aa9ddbebadc61fbe010a760f9c5633c39246f98 Mon Sep 17 00:00:00 2001 From: devinleighsmith <41091511+devinleighsmith@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:47:03 -0800 Subject: [PATCH] psp-9530 allow research files to add retired properties. (#4534) * psp-9530 allow research files to add retired properties. * fix test compilation error. --------- Co-authored-by: Alejandro Sanchez --- .../backend/api/Services/IPropertyService.cs | 2 +- .../backend/api/Services/PropertyService.cs | 8 +++---- .../api/Services/ResearchFileService.cs | 4 ++-- .../Interfaces/IPropertyRepository.cs | 2 +- .../dal/Repositories/PropertyRepository.cs | 4 ++-- .../Services/AcquisitionFileServiceTest.cs | 8 +++---- .../Services/DispositionFileServiceTest.cs | 6 ++--- .../unit/api/Services/LeaseServiceTest.cs | 10 ++++---- .../unit/api/Services/PropertyServiceTest.cs | 24 +++++++++---------- .../api/Services/ResearchFileServiceTest.cs | 8 +++---- 10 files changed, 38 insertions(+), 38 deletions(-) diff --git a/source/backend/api/Services/IPropertyService.cs b/source/backend/api/Services/IPropertyService.cs index 18a52c564b..cd7c81ab22 100644 --- a/source/backend/api/Services/IPropertyService.cs +++ b/source/backend/api/Services/IPropertyService.cs @@ -45,7 +45,7 @@ public interface IPropertyService PimsProperty PopulateNewProperty(PimsProperty property, bool isOwned = false, bool isPropertyOfInterest = true); - void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty propertyToUpdate, IEnumerable overrideCodes); + void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty propertyToUpdate, IEnumerable overrideCodes, bool allowRetired = false); T PopulateNewFileProperty(T fileProperty) where T : IFilePropertyEntity; diff --git a/source/backend/api/Services/PropertyService.cs b/source/backend/api/Services/PropertyService.cs index 863b540e0a..6028afcd99 100644 --- a/source/backend/api/Services/PropertyService.cs +++ b/source/backend/api/Services/PropertyService.cs @@ -6,17 +6,17 @@ using Microsoft.Extensions.Logging; using NetTopologySuite.Geometries; using Pims.Api.Constants; -using Pims.Core.Api.Exceptions; using Pims.Api.Models.CodeTypes; using Pims.Api.Models.Concepts.Property; +using Pims.Core.Api.Exceptions; using Pims.Core.Exceptions; using Pims.Core.Extensions; +using Pims.Core.Security; using Pims.Dal.Entities; using Pims.Dal.Exceptions; using Pims.Dal.Helpers; using Pims.Dal.Helpers.Extensions; using Pims.Dal.Repositories; -using Pims.Core.Security; namespace Pims.Api.Services { @@ -387,7 +387,7 @@ public PimsProperty PopulateNewProperty(PimsProperty property, bool isOwned = fa return property; } - public void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty propertyToUpdate, IEnumerable overrideCodes) + public void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty propertyToUpdate, IEnumerable overrideCodes, bool allowRetired = false) { if (propertyToUpdate.Location == null || propertyToUpdate.Boundary == null) { @@ -415,7 +415,7 @@ public void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty prope if (needsUpdate) { - _propertyRepository.Update(propertyToUpdate, overrideLocation: true); + _propertyRepository.Update(propertyToUpdate, overrideLocation: true, allowRetired: allowRetired); } } else diff --git a/source/backend/api/Services/ResearchFileService.cs b/source/backend/api/Services/ResearchFileService.cs index 988a467b9f..f28025f757 100644 --- a/source/backend/api/Services/ResearchFileService.cs +++ b/source/backend/api/Services/ResearchFileService.cs @@ -209,7 +209,7 @@ private void MatchProperties(PimsResearchFile researchFile, IEnumerable long GetAllAssociationsCountById(long id); - PimsProperty Update(PimsProperty property, bool overrideLocation = false); + PimsProperty Update(PimsProperty property, bool overrideLocation = false, bool allowRetired = false); PimsProperty UpdatePropertyManagement(PimsProperty property); diff --git a/source/backend/dal/Repositories/PropertyRepository.cs b/source/backend/dal/Repositories/PropertyRepository.cs index 4b75be1251..50ec958dcb 100644 --- a/source/backend/dal/Repositories/PropertyRepository.cs +++ b/source/backend/dal/Repositories/PropertyRepository.cs @@ -310,7 +310,7 @@ public long GetAllAssociationsCountById(long id) /// The property to update. /// Whether to update the property spatial location with the incoming value. Defaults to false. /// The updated property. - public PimsProperty Update(PimsProperty property, bool overrideLocation = false) + public PimsProperty Update(PimsProperty property, bool overrideLocation = false, bool allowRetired = false) { property.ThrowIfNull(nameof(property)); @@ -320,7 +320,7 @@ public PimsProperty Update(PimsProperty property, bool overrideLocation = false) .FirstOrDefault(p => p.PropertyId == propertyId) ?? throw new KeyNotFoundException(); // prevent editing on retired properties - if (existingProperty.IsRetired.HasValue && existingProperty.IsRetired.Value) + if (existingProperty.IsRetired.HasValue && existingProperty.IsRetired.Value && !allowRetired) { throw new BusinessRuleViolationException("Retired records are referenced for historical purposes only and cannot be edited or deleted."); } diff --git a/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs b/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs index bea06de2d5..5369e25725 100644 --- a/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/AcquisitionFileServiceTest.cs @@ -1369,7 +1369,7 @@ public void UpdateProperties_MatchProperties_Success() userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); var solver = this._helper.GetService>(); solver.Setup(x => x.CanEditProperties(It.IsAny())).Returns(true); @@ -1380,7 +1380,7 @@ public void UpdateProperties_MatchProperties_Success() // Assert filePropertyRepository.Verify(x => x.GetPropertiesByAcquisitionFileId(It.IsAny()), Times.Once); filePropertyRepository.Verify(x => x.Update(It.IsAny()), Times.Once); - propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false), Times.Once); propertyService.Verify(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny()), Times.Once); } @@ -1412,7 +1412,7 @@ public void UpdateProperties_MatchProperties_Success_NoInternalId() userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); var solver = this._helper.GetService>(); solver.Setup(x => x.CanEditProperties(It.IsAny())).Returns(true); @@ -1888,7 +1888,7 @@ public void UpdateProperties_WithProperty_SelectedForCompensation_Should_Fail() userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); var solver = this._helper.GetService>(); solver.Setup(x => x.CanEditProperties(It.IsAny())).Returns(true); diff --git a/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs b/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs index f57b446a39..3672d5bf98 100644 --- a/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/DispositionFileServiceTest.cs @@ -988,7 +988,7 @@ public void UpdateProperties_MatchProperties_Success() userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); propertyService.Setup(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny())); // Act @@ -997,7 +997,7 @@ public void UpdateProperties_MatchProperties_Success() // Assert filePropertyRepository.Verify(x => x.GetPropertiesByDispositionFileId(It.IsAny()), Times.Once); filePropertyRepository.Verify(x => x.Update(It.IsAny()), Times.Once); - propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false), Times.Once); propertyService.Verify(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny()), Times.Once); } @@ -1029,7 +1029,7 @@ public void UpdateProperties_MatchProperties_Success_NoInternalId() userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1)); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); propertyService.Setup(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny())); // Act diff --git a/source/backend/tests/unit/api/Services/LeaseServiceTest.cs b/source/backend/tests/unit/api/Services/LeaseServiceTest.cs index 14f43e1e26..fccfdbaf4e 100644 --- a/source/backend/tests/unit/api/Services/LeaseServiceTest.cs +++ b/source/backend/tests/unit/api/Services/LeaseServiceTest.cs @@ -510,14 +510,14 @@ public void UpdateProperties_Success() userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); // Act var updatedLease = service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); // Assert leaseRepository.Verify(x => x.Update(lease, false), Times.Once); - propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false), Times.Once); } [Fact] @@ -674,14 +674,14 @@ public void UpdateProperties_MatchProperties_Success() userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); // Act var updatedLease = service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); // Assert leaseRepository.Verify(x => x.Update(lease, false), Times.Once); - propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false), Times.Once); } [Fact] @@ -711,7 +711,7 @@ public void UpdateProperties_MatchProperties_Success_NoInternalId() userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny())).Returns(EntityHelper.CreateUser("Test")); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), false)); // Act var updatedLease = service.Update(lease, new List() { UserOverrideCode.AddLocationToProperty }); diff --git a/source/backend/tests/unit/api/Services/PropertyServiceTest.cs b/source/backend/tests/unit/api/Services/PropertyServiceTest.cs index 3535ce0750..2de98b1cb1 100644 --- a/source/backend/tests/unit/api/Services/PropertyServiceTest.cs +++ b/source/backend/tests/unit/api/Services/PropertyServiceTest.cs @@ -119,7 +119,7 @@ public void Update_Property_No_Reprojection_Success() var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); var repository = this._helper.GetService>(); - repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny(), false)).Returns(property); repository.Setup(x => x.GetById(It.IsAny())).Returns(property); var coordinateService = this._helper.GetService>(); coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())); @@ -133,7 +133,7 @@ public void Update_Property_No_Reprojection_Success() var updatedProperty = service.Update(newValues); // Assert - repository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Once); + repository.Verify(x => x.Update(It.IsAny(), It.IsAny(), false), Times.Once); coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())); } @@ -145,7 +145,7 @@ public void Update_Property_With_Reprojection_Success() var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); var repository = this._helper.GetService>(); - repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny(), false)).Returns(property); repository.Setup(x => x.GetById(It.IsAny())).Returns(property); var projected = new Coordinate(14000, 9200); @@ -162,7 +162,7 @@ public void Update_Property_With_Reprojection_Success() // Assert coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())); - repository.Verify(x => x.Update(It.Is(p => p.Location.Coordinate.Equals(projected)), It.IsAny()), Times.Once); + repository.Verify(x => x.Update(It.Is(p => p.Location.Coordinate.Equals(projected)), It.IsAny(), false), Times.Once); } [Fact] @@ -175,7 +175,7 @@ public void Update_Property_KeyNotFound() var property = EntityHelper.CreateProperty(1); var repository = this._helper.GetService>(); - repository.Setup(x => x.Update(property, It.IsAny())).Throws(); + repository.Setup(x => x.Update(property, It.IsAny(), false)).Throws(); repository.Setup(x => x.GetById(It.IsAny())).Throws(); // Assert @@ -193,7 +193,7 @@ public void Update_Property_NoPermission() // Assert Assert.Throws(() => service.Update(property)); - repository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Never); + repository.Verify(x => x.Update(It.IsAny(), It.IsAny(), false), Times.Never); } #endregion @@ -208,7 +208,7 @@ public void UpdateLocation_Requires_UserOverride() var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); var repository = this._helper.GetService>(); - repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny(), false)).Returns(property); var coordinateService = this._helper.GetService>(); coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())).Returns(new Coordinate(14000, 9200)); @@ -225,7 +225,7 @@ public void UpdateLocation_Requires_UserOverride() ex.Which.UserOverride.Should().Be(UserOverrideCode.AddLocationToProperty); coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Never); - repository.Verify(x => x.Update(It.IsAny(), It.IsAny()), Times.Never); + repository.Verify(x => x.Update(It.IsAny(), It.IsAny(), false), Times.Never); } [Fact] @@ -237,7 +237,7 @@ public void UpdateLocation_Success() var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); var repository = this._helper.GetService>(); - repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny(), false)).Returns(property); var coordinateService = this._helper.GetService>(); coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())) @@ -252,7 +252,7 @@ public void UpdateLocation_Success() // Assert coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); - repository.Verify(x => x.Update(It.Is(p => p.Location.Coordinate.Equals(new Coordinate(14000, 9200))), It.IsAny()), Times.Once); + repository.Verify(x => x.Update(It.Is(p => p.Location.Coordinate.Equals(new Coordinate(14000, 9200))), It.IsAny(), false), Times.Once); } [Fact] @@ -265,7 +265,7 @@ public void UpdateLocation_Boundary_Success() var service = this.CreatePropertyServiceWithPermissions(Permissions.PropertyView, Permissions.PropertyEdit); var repository = this._helper.GetService>(); - repository.Setup(x => x.Update(It.IsAny(), It.IsAny())).Returns(property); + repository.Setup(x => x.Update(It.IsAny(), It.IsAny(), false)).Returns(property); var coordinateService = this._helper.GetService>(); coordinateService.Setup(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny())) @@ -291,7 +291,7 @@ public void UpdateLocation_Boundary_Success() // Assert coordinateService.Verify(x => x.TransformCoordinates(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); coordinateService.Verify(x => x.TransformGeometry(It.IsAny(), It.IsAny(), It.IsAny()), Times.Once); - repository.Verify(x => x.Update(property, true), Times.Once); + repository.Verify(x => x.Update(property, true, false), Times.Once); property.Location.Coordinate.Should().Be(new Coordinate(14000, 9200)); property.Boundary.Should().BeOfType(); var updatedBoundary = property.Boundary as Polygon; diff --git a/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs b/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs index a6267ad0a2..5ea2997992 100644 --- a/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs +++ b/source/backend/tests/unit/api/Services/ResearchFileServiceTest.cs @@ -194,7 +194,7 @@ public void UpdateProperties_MatchProperties_PID_Success() filePropertyRepository.Setup(x => x.GetAllByResearchFileId(It.IsAny())).Returns(researchFile.PimsPropertyResearchFiles.ToList()); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), true)); propertyService.Setup(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny())); // Act @@ -202,7 +202,7 @@ public void UpdateProperties_MatchProperties_PID_Success() // Assert filePropertyRepository.Verify(x => x.Update(It.IsAny()), Times.Once); - propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), true), Times.Once); propertyService.Verify(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny()), Times.Once); } @@ -230,7 +230,7 @@ public void UpdateProperties_MatchProperties_PIN_Success() filePropertyRepository.Setup(x => x.GetAllByResearchFileId(It.IsAny())).Returns(researchFile.PimsPropertyResearchFiles.ToList()); var propertyService = this._helper.GetService>(); - propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>())); + propertyService.Setup(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), true)); propertyService.Setup(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny())); // Act @@ -238,7 +238,7 @@ public void UpdateProperties_MatchProperties_PIN_Success() // Assert filePropertyRepository.Verify(x => x.Update(It.IsAny()), Times.Once); - propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>()), Times.Once); + propertyService.Verify(x => x.UpdateLocation(It.IsAny(), ref It.Ref.IsAny, It.IsAny>(), true), Times.Once); propertyService.Verify(x => x.UpdateFilePropertyLocation(It.IsAny(), It.IsAny()), Times.Once); }