Skip to content

Commit

Permalink
psp-9715 fix issues adding properties with no locations, and also ove…
Browse files Browse the repository at this point in the history
…rride research file property updates.
  • Loading branch information
devinleighsmith committed Dec 13, 2024
1 parent 2183180 commit e7a52cc
Show file tree
Hide file tree
Showing 10 changed files with 233 additions and 8 deletions.
5 changes: 5 additions & 0 deletions source/backend/api/Services/AcquisitionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ public PimsAcquisitionFile UpdateProperties(PimsAcquisitionFile acquisitionFile,
// Check if the property is new or if it is being updated
foreach (var incomingAcquisitionProperty in acquisitionFile.PimsPropertyAcquisitionFiles)
{
var matchingProperty = currentFileProperties.FirstOrDefault(c => c.PropertyId == incomingAcquisitionProperty.PropertyId);
if (matchingProperty is not null && incomingAcquisitionProperty.Internal_Id == 0)
{
incomingAcquisitionProperty.Internal_Id = matchingProperty.Internal_Id;
}
// If the property is not new, check if the name has been updated.
if (incomingAcquisitionProperty.Internal_Id != 0)
{
Expand Down
5 changes: 5 additions & 0 deletions source/backend/api/Services/DispositionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,11 @@ public PimsDispositionFile UpdateProperties(PimsDispositionFile dispositionFile,
// Check if the property is new or if it is being updated
foreach (var incomingDispositionProperty in dispositionFile.PimsDispositionFileProperties)
{
var matchingProperty = currentFileProperties.FirstOrDefault(c => c.PropertyId == incomingDispositionProperty.PropertyId);
if (matchingProperty is not null && incomingDispositionProperty.Internal_Id == 0)
{
incomingDispositionProperty.Internal_Id = matchingProperty.Internal_Id;
}
// If the property is not new, check if the name has been updated.
if (incomingDispositionProperty.Internal_Id != 0)
{
Expand Down
5 changes: 5 additions & 0 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ public PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverr
// Update marker locations in the context of this file
foreach (var incomingLeaseProperty in leaseWithProperties.PimsPropertyLeases)
{
var matchingProperty = currentFileProperties.FirstOrDefault(c => c.PropertyId == incomingLeaseProperty.PropertyId);
if (matchingProperty is not null && incomingLeaseProperty.Internal_Id == 0)
{
incomingLeaseProperty.Internal_Id = matchingProperty.Internal_Id;
}
// If the property is not new, check if the marker location has been updated.
if (incomingLeaseProperty.Internal_Id != 0)
{
Expand Down
6 changes: 3 additions & 3 deletions source/backend/api/Services/PropertyService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public PimsProperty Update(PimsProperty property, bool commitTransaction = true)

// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = property.Location;
if (geom.SRID != SpatialReference.BCALBERS)
if (geom != null && geom.SRID != SpatialReference.BCALBERS)
{
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
property.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
Expand Down Expand Up @@ -371,7 +371,7 @@ public PimsProperty PopulateNewProperty(PimsProperty property, bool isOwned = fa

// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = property.Location;
if (geom.SRID != SpatialReference.BCALBERS)
if (geom != null && geom.SRID != SpatialReference.BCALBERS)
{
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
property.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
Expand All @@ -397,7 +397,7 @@ public void UpdateLocation(PimsProperty incomingProperty, ref PimsProperty prope

// convert spatial location from lat/long (4326) to BC Albers (3005) for database storage
var geom = incomingProperty.Location;
if (geom.SRID != SpatialReference.BCALBERS)
if (geom != null && geom.SRID != SpatialReference.BCALBERS)
{
var newCoords = _coordinateService.TransformCoordinates(geom.SRID, SpatialReference.BCALBERS, geom.Coordinate);
propertyToUpdate.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.BCALBERS);
Expand Down
5 changes: 5 additions & 0 deletions source/backend/api/Services/ResearchFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,11 @@ public PimsResearchFile UpdateProperties(PimsResearchFile researchFile, IEnumera
// Check if the property is new or if it is being updated
foreach (var incomingResearchProperty in researchFile.PimsPropertyResearchFiles)
{
var matchingProperty = currentFileProperties.FirstOrDefault(c => c.PropertyId == incomingResearchProperty.PropertyId);
if (matchingProperty is not null && incomingResearchProperty.Internal_Id == 0)
{
incomingResearchProperty.Internal_Id = matchingProperty.Internal_Id;
}
// If the property is not new, check if the name has been updated.
if (incomingResearchProperty.Internal_Id != 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ public void Delete(PimsPropertyResearchFile propertyResearchFile)

public PimsPropertyResearchFile Update(PimsPropertyResearchFile propertyResearchFile)
{
// Mark the property not to be changed it was being tracked.
if (propertyResearchFile.Property != null)
{
Context.Entry(propertyResearchFile.Property).State = EntityState.Unchanged;
}
// Do not allow this method to make any updates to the related property entity.
propertyResearchFile.Property = null;

// Retrieve the existing property research purpose types for the property
// Note: This is needed given the research file properties purpose types may not have the corresponging id, but corresponding code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,46 @@ public void UpdateProperties_MatchProperties_Success()
filePropertyRepository.Verify(x => x.Update(It.IsAny<PimsPropertyAcquisitionFile>()), Times.Once);
propertyService.Verify(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()), Times.Once);
propertyService.Verify(x => x.UpdateFilePropertyLocation<PimsPropertyAcquisitionFile>(It.IsAny<PimsPropertyAcquisitionFile>(), It.IsAny<PimsPropertyAcquisitionFile>()), Times.Once);
}

[Fact]
public void UpdateProperties_MatchProperties_Success_NoInternalId()
{
// Arrange
var service = this.CreateAcquisitionServiceWithPermissions(Permissions.AcquisitionFileEdit, Permissions.PropertyAdd, Permissions.PropertyView);

var acqFile = EntityHelper.CreateAcquisitionFile();
acqFile.ConcurrencyControlNumber = 1;

var property = EntityHelper.CreateProperty(1, regionCode: 1);
acqFile.PimsPropertyAcquisitionFiles = new List<PimsPropertyAcquisitionFile>() { new PimsPropertyAcquisitionFile() { Internal_Id = 0, Property = property, PropertyId = 1 } };
var propertyAcquisitionFiles = new List<PimsPropertyAcquisitionFile>() { new PimsPropertyAcquisitionFile() { Internal_Id = 1, Property = property, PropertyId = 1 } };

var repository = this._helper.GetService<Mock<IAcquisitionFileRepository>>();
repository.Setup(x => x.GetRowVersion(It.IsAny<long>())).Returns(1);
repository.Setup(x => x.GetById(It.IsAny<long>())).Returns(acqFile);

var propertyRepository = this._helper.GetService<Mock<IPropertyRepository>>();
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), true)).Returns(property);
propertyRepository.Setup(x => x.GetPropertyRegion(It.IsAny<long>())).Returns(1);

var filePropertyRepository = this._helper.GetService<Mock<IAcquisitionFilePropertyRepository>>();
filePropertyRepository.Setup(x => x.GetPropertiesByAcquisitionFileId(It.IsAny<long>())).Returns(propertyAcquisitionFiles);

var userRepository = this._helper.GetService<Mock<IUserRepository>>();
userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1));

var propertyService = this._helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()));

var solver = this._helper.GetService<Mock<IAcquisitionStatusSolver>>();
solver.Setup(x => x.CanEditProperties(It.IsAny<AcquisitionStatusTypes?>())).Returns(true);

// Act
var response = service.UpdateProperties(acqFile, new List<UserOverrideCode>() { UserOverrideCode.AddLocationToProperty });

// Assert
var updatedProperty = response.PimsPropertyAcquisitionFiles.FirstOrDefault().Internal_Id.Should().Be(1);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,7 @@ public void Update_Success()
repository.Verify(x => x.Update(It.IsAny<long>(), It.IsAny<PimsDispositionFile>()), Times.Once);
}


[Fact]
public void Update_Success_FinalButAdmin()
{
Expand Down Expand Up @@ -1000,6 +1001,44 @@ public void UpdateProperties_MatchProperties_Success()
propertyService.Verify(x => x.UpdateFilePropertyLocation<PimsDispositionFileProperty>(It.IsAny<PimsDispositionFileProperty>(), It.IsAny<PimsDispositionFileProperty>()), Times.Once);
}

[Fact]
public void UpdateProperties_MatchProperties_Success_NoInternalId()
{
// Arrange
var service = this.CreateDispositionServiceWithPermissions(Permissions.DispositionEdit, Permissions.PropertyAdd, Permissions.PropertyView);

var dspFile = EntityHelper.CreateDispositionFile();
dspFile.ConcurrencyControlNumber = 1;

var property = EntityHelper.CreateProperty(1, regionCode: 1);
dspFile.PimsDispositionFileProperties = new List<PimsDispositionFileProperty>() { new PimsDispositionFileProperty() { Internal_Id = 0, Property = property, PropertyId = 1 } };
var dispositionFileProperties = new List<PimsDispositionFileProperty>() { new PimsDispositionFileProperty() { Internal_Id = 1, Property = property, PropertyId = 1 } };

var repository = this._helper.GetService<Mock<IDispositionFileRepository>>();
repository.Setup(x => x.GetRowVersion(It.IsAny<long>())).Returns(1);
repository.Setup(x => x.GetById(It.IsAny<long>())).Returns(dspFile);

var propertyRepository = this._helper.GetService<Mock<IPropertyRepository>>();
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), true)).Returns(property);
propertyRepository.Setup(x => x.GetPropertyRegion(It.IsAny<long>())).Returns(1);

var filePropertyRepository = this._helper.GetService<Mock<IDispositionFilePropertyRepository>>();
filePropertyRepository.Setup(x => x.GetPropertiesByDispositionFileId(It.IsAny<long>())).Returns(dispositionFileProperties);

var userRepository = this._helper.GetService<Mock<IUserRepository>>();
userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser(1, Guid.NewGuid(), "Test", regionCode: 1));

var propertyService = this._helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()));
propertyService.Setup(x => x.UpdateFilePropertyLocation<PimsDispositionFileProperty>(It.IsAny<PimsDispositionFileProperty>(), It.IsAny<PimsDispositionFileProperty>()));

// Act
var updatedDispositionFile = service.UpdateProperties(dspFile, new List<UserOverrideCode>() { UserOverrideCode.AddLocationToProperty });

// Assert
var updatedProperty = updatedDispositionFile.PimsDispositionFileProperties.FirstOrDefault().Internal_Id.Should().Be(1);
}

[Fact]
public void UpdateProperties_MatchProperties_NewProperty_UserOverride()
{
Expand Down
35 changes: 35 additions & 0 deletions source/backend/tests/unit/api/Services/LeaseServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,41 @@ public void UpdateProperties_MatchProperties_Success()
propertyService.Verify(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()), Times.Once);
}

[Fact]
public void UpdateProperties_MatchProperties_Success_NoInternalId()
{
// Arrange
var lease = EntityHelper.CreateLease(1);

var property = EntityHelper.CreateProperty(1, regionCode: 1);
var service = this.CreateLeaseService(Permissions.LeaseEdit, Permissions.PropertyAdd, Permissions.PropertyView);
var leaseRepository = this._helper.GetService<Mock<ILeaseRepository>>();
var propertyLeaseRepository = this._helper.GetService<Mock<IPropertyLeaseRepository>>();
var propertyRepository = this._helper.GetService<Mock<IPropertyRepository>>();
var userRepository = this._helper.GetService<Mock<IUserRepository>>();
var propertyLeases = new List<PimsPropertyLease>() {new PimsPropertyLease()
{
Property = property,
PropertyId = 1,
Lease = lease,
Internal_Id = 1,
} };

propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny<long>())).Returns(propertyLeases);
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), true)).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property);
leaseRepository.Setup(x => x.GetNoTracking(It.IsAny<long>())).Returns(lease);
userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser("Test"));

var propertyService = this._helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.UpdateLocation(It.IsAny<PimsProperty>(), ref It.Ref<PimsProperty>.IsAny, It.IsAny<IEnumerable<UserOverrideCode>>()));

// Act
var updatedLease = service.Update(lease, new List<UserOverrideCode>() { UserOverrideCode.AddLocationToProperty });

// Assert
var updatedProperty = updatedLease.PimsPropertyLeases.FirstOrDefault().Internal_Id.Should().Be(1);
}

[Fact]
public void UpdateProperties_MatchProperties_NewProperty_Success()
{
Expand Down
Loading

0 comments on commit e7a52cc

Please sign in to comment.