Skip to content

Commit

Permalink
PSP-9415 : FT-REG: Lease & Licences - 500 error thrown when user atte…
Browse files Browse the repository at this point in the history
…mpts to delete a property that is associated to a compensation requisition (#4415)

Co-authored-by: Herrera <[email protected]>
Co-authored-by: Alejandro Sanchez <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent caa5ca6 commit 8f04400
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 0 deletions.
5 changes: 5 additions & 0 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,11 @@ public PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverr
List<PimsPropertyLease> differenceSet = currentFileProperties.Where(x => !lease.PimsPropertyLeases.Any(y => y.Internal_Id == x.Internal_Id)).ToList();
foreach (var deletedProperty in differenceSet)
{
if (_propertyLeaseRepository.LeaseFilePropertyInCompensationReq(deletedProperty.PropertyLeaseId))
{
throw new BusinessRuleViolationException("Lease File property can not be removed since it's assigned as a property for a compensation requisition");
}

var totalAssociationCount = _propertyRepository.GetAllAssociationsCountById(deletedProperty.PropertyId);
if (totalAssociationCount <= 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public interface IPropertyLeaseRepository : IRepository<PimsPropertyLease>
IEnumerable<PimsPropertyLease> GetAllByLeaseId(long leaseId);

IEnumerable<PimsPropertyLease> UpdatePropertyLeases(long leaseId, ICollection<PimsPropertyLease> pimsPropertyLeases);

bool LeaseFilePropertyInCompensationReq(long propertyLeaseFileId);
}
}
10 changes: 10 additions & 0 deletions source/backend/dal/Repositories/PropertyLeaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,16 @@ public IEnumerable<PimsPropertyLease> UpdatePropertyLeases(long leaseId, ICollec
return GetAllByLeaseId(leaseId);
}

/// <summary>
/// Check the existence of a LeaseProperty assigned to a Lease Compensation Requisition.
/// </summary>
/// <param name="propertyLeaseFileId"></param>
/// <returns>True if exists.</returns>
public bool LeaseFilePropertyInCompensationReq(long propertyLeaseFileId)
{
return Context.PimsPropLeaseCompReqs.Where(x => x.PropertyLeaseId == propertyLeaseFileId).AsNoTracking().Any();
}

#endregion
}
}
31 changes: 31 additions & 0 deletions source/backend/tests/unit/api/Services/LeaseServiceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,36 @@ public void UpdateProperties_RemovePropertyFile_Success()
propertyRepository.Verify(x => x.Delete(It.IsAny<PimsProperty>()), Times.Never());
}

[Fact]
public void UpdateProperties_RemoveProperty_Fails_PropertyAssignedToCompReq()
{
// Arrange
var lease = EntityHelper.CreateLease(1);
var deletedProperty = lease.PimsPropertyLeases.FirstOrDefault().Property;
var updatedLease = EntityHelper.CreateLease(2, addProperty: false);

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>>();

propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny<long>())).Returns(lease.PimsPropertyLeases);
propertyLeaseRepository.Setup(x => x.LeaseFilePropertyInCompensationReq(It.IsAny<long>())).Returns(true);
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), false)).Returns(deletedProperty);
propertyRepository.Setup(x => x.GetAllAssociationsById(It.IsAny<long>())).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property);
propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny<long>())).Returns(1);
leaseRepository.Setup(x => x.GetNoTracking(It.IsAny<long>())).Returns(lease);
leaseRepository.Setup(x => x.Get(It.IsAny<long>())).Returns(EntityHelper.CreateLease(1));
userRepository.Setup(x => x.GetByKeycloakUserId(It.IsAny<Guid>())).Returns(EntityHelper.CreateUser("Test"));

// Act
Action act = () => service.Update(updatedLease, new List<UserOverrideCode>());

// Assert
var ex = act.Should().Throw<BusinessRuleViolationException>().WithMessage("Lease File property can not be removed since it's assigned as a property for a compensation requisition");
}

[Fact]
public void UpdateProperties_RemoveProperty_Success()
{
Expand All @@ -619,6 +649,7 @@ public void UpdateProperties_RemoveProperty_Success()
var userRepository = this._helper.GetService<Mock<IUserRepository>>();

propertyLeaseRepository.Setup(x => x.GetAllByLeaseId(It.IsAny<long>())).Returns(lease.PimsPropertyLeases);
propertyLeaseRepository.Setup(x => x.LeaseFilePropertyInCompensationReq(It.IsAny<long>())).Returns(false);
propertyRepository.Setup(x => x.GetByPid(It.IsAny<int>(), false)).Returns(deletedProperty);
propertyRepository.Setup(x => x.GetAllAssociationsById(It.IsAny<long>())).Returns(lease.PimsPropertyLeases.FirstOrDefault().Property);
propertyRepository.Setup(x => x.GetAllAssociationsCountById(It.IsAny<long>())).Returns(1);
Expand Down

0 comments on commit 8f04400

Please sign in to comment.