Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PSP-9683 Generated H-120 Compensation Requisition not Populating Electoral District #4564

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pims.Core.Api.Exceptions;
using Pims.Api.Models.CodeTypes;
using Pims.Api.Models.Concepts.AcquisitionFile;
using Pims.Api.Models.Concepts.CompensationRequisition;
using Pims.Api.Models.Concepts.Lease;
using Pims.Core.Api.Policies;
using Pims.Api.Services;
using Pims.Core.Api.Exceptions;
using Pims.Core.Api.Policies;
using Pims.Core.Extensions;
using Pims.Core.Json;
using Pims.Dal.Entities;
using Pims.Core.Security;
using Pims.Dal.Entities;
using Swashbuckle.AspNetCore.Annotations;

namespace Pims.Api.Areas.CompensationRequisition.Controllers
Expand Down Expand Up @@ -189,7 +189,7 @@ public IActionResult DeleteCompensation([FromRoute] long id)
[ProducesResponseType(typeof(List<CompensationRequisitionModel>), 200)]
[SwaggerOperation(Tags = new[] { "compensation-requisition" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult GetFileCompensations([FromRoute]FileTypes fileType, [FromRoute]long fileId)
public IActionResult GetFileCompensations([FromRoute] FileTypes fileType, [FromRoute] long fileId)
{
_logger.LogInformation(
"Request received by Controller: {Controller}, Action: {ControllerAction}, User: {User}, DateTime: {DateTime}",
Expand Down
16 changes: 10 additions & 6 deletions source/backend/api/Services/CompensationRequisitionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@
using System.Linq;
using System.Security.Claims;
using Microsoft.Extensions.Logging;
using Pims.Core.Api.Exceptions;
using Pims.Api.Models.CodeTypes;
using Pims.Core.Api.Exceptions;
using Pims.Core.Exceptions;
using Pims.Core.Extensions;
using Pims.Core.Security;
using Pims.Dal.Entities;
using Pims.Dal.Entities.Extensions;
using Pims.Dal.Helpers.Extensions;
using Pims.Dal.Repositories;
using Pims.Core.Security;

namespace Pims.Api.Services
{
Expand All @@ -26,6 +25,7 @@ public class CompensationRequisitionService : ICompensationRequisitionService
private readonly ICompReqFinancialService _compReqFinancialService;
private readonly IAcquisitionStatusSolver _acquisitionStatusSolver;
private readonly ILeaseRepository _leaseRepository;
private readonly IPropertyService _propertyService;

public CompensationRequisitionService(
ClaimsPrincipal user,
Expand All @@ -36,7 +36,8 @@ public CompensationRequisitionService(
IAcquisitionFileRepository acqFileRepository,
ICompReqFinancialService compReqFinancialService,
IAcquisitionStatusSolver statusSolver,
ILeaseRepository leaseRepository)
ILeaseRepository leaseRepository,
IPropertyService propertyService)
{
_user = user;
_logger = logger;
Expand All @@ -47,6 +48,7 @@ public CompensationRequisitionService(
_compReqFinancialService = compReqFinancialService;
_acquisitionStatusSolver = statusSolver;
_leaseRepository = leaseRepository;
_propertyService = propertyService;
}

public PimsCompensationRequisition GetById(long compensationRequisitionId)
Expand Down Expand Up @@ -121,15 +123,17 @@ public IEnumerable<PimsPropertyAcquisitionFile> GetAcquisitionProperties(long id
_logger.LogInformation("Getting properties for Compensation Requisition with id {id}", id);
_user.ThrowIfNotAuthorized(Permissions.CompensationRequisitionView);

return _compensationRequisitionRepository.GetAcquisitionCompReqPropertiesById(id);
var properties = _compensationRequisitionRepository.GetAcquisitionCompReqPropertiesById(id);
return _propertyService.TransformAllPropertiesToLatLong(properties);
}

public IEnumerable<PimsPropertyLease> GetLeaseProperties(long id)
{
_logger.LogInformation("Getting properties for Compensation Requisition with id {id}", id);
_user.ThrowIfNotAuthorized(Permissions.CompensationRequisitionView);

return _compensationRequisitionRepository.GetLeaseCompReqPropertiesById(id);
var propertyLeases = _compensationRequisitionRepository.GetLeaseCompReqPropertiesById(id);
return _propertyService.TransformAllPropertiesToLatLong(propertyLeases);
}

public IEnumerable<PimsCompensationRequisition> GetFileCompensationRequisitions(FileTypes fileType, long fileId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Pims.Core.Exceptions;
using Pims.Core.Extensions;
using Pims.Core.Security;
using Pims.Dal.Entities;
using Pims.Dal.Entities.Models;
using Pims.Core.Extensions;
using Pims.Dal.Helpers.Extensions;

namespace Pims.Dal.Repositories
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public List<PimsPropertyAcquisitionFile> GetAcquisitionCompReqPropertiesById(lon
{
return Context.PimsPropAcqFlCompReqs
.Where(x => x.CompensationRequisitionId == compensationRequisitionId)
.Include(pa => pa.PropertyAcquisitionFile)
.Include(pa => pa.PropertyAcquisitionFile)
.ThenInclude(p => p.Property)
.ThenInclude(rp => rp.RegionCodeNavigation)
.Include(pa => pa.PropertyAcquisitionFile)
Expand All @@ -165,7 +165,7 @@ public List<PimsPropertyLease> GetLeaseCompReqPropertiesById(long compensationRe
{
return Context.PimsPropLeaseCompReqs
.Where(x => x.CompensationRequisitionId == compensationRequisitionId)
.Include(l => l.PropertyLease)
.Include(l => l.PropertyLease)
.ThenInclude(p => p.Property)
.ThenInclude(rp => rp.RegionCodeNavigation)
.Include(pa => pa.PropertyLease)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,55 @@ public void GetById_NoPermission()
}

[Fact]
public void GetPropertiesById_NoPermission()
public void GetAcquisitionProperties_Success()
{
// Arrange
var property = EntityHelper.CreateProperty(12345);
property.Location = new NetTopologySuite.Geometries.Point(1229480.4045231808, 463288.8298389828) { SRID = SpatialReference.BCALBERS };

var compReqProperty = new PimsPropAcqFlCompReq()
{
Internal_Id = 1,
CompensationRequisitionId = 1,
PropertyAcquisitionFileId = 1,
PropertyAcquisitionFile = new()
{
AcquisitionFileId = 1,
PropertyId = property.Internal_Id,
Property = property,
}
};

var service = CreateCompRequisitionServiceWithPermissions(Permissions.CompensationRequisitionView);
var repository = _helper.GetService<Mock<ICompensationRequisitionRepository>>();
repository.Setup(x => x.GetAcquisitionCompReqPropertiesById(It.IsAny<long>())).Returns(new List<PimsPropertyAcquisitionFile>() { compReqProperty.PropertyAcquisitionFile });
// mock the spatial conversion to lat/long
var propertyService = _helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.TransformAllPropertiesToLatLong(It.IsAny<List<PimsPropertyAcquisitionFile>>()))
.Returns<List<PimsPropertyAcquisitionFile>>(x => x.Select(pp =>
{
pp.Property.Location = new NetTopologySuite.Geometries.Point(-122, 49) { SRID = SpatialReference.WGS84 };
return pp;
}).ToList());

// Act
var result = service.GetAcquisitionProperties(1);

// Assert
repository.Verify(x => x.GetAcquisitionCompReqPropertiesById(It.IsAny<long>()), Times.Once);
propertyService.Verify(x => x.TransformAllPropertiesToLatLong(It.IsAny<List<PimsPropertyAcquisitionFile>>()), Times.Once);
result.Should().NotBeNull();
result.Should().BeAssignableTo<IEnumerable<PimsPropertyAcquisitionFile>>();
result.Should().HaveCount(1);
result.First().Property.Pid.Should().Be(12345);
// service should perform spatial conversion to Lat/Long so that it can be returned to the frontend
result.First().Property.Location.SRID.Should().Be(SpatialReference.WGS84);
result.First().Property.Location.As<NetTopologySuite.Geometries.Point>().X.Should().Be(-122);
result.First().Property.Location.As<NetTopologySuite.Geometries.Point>().Y.Should().Be(49);
}

[Fact]
public void GetAcquisitionProperties_NoPermission()
{
// Arrange
var service = this.CreateCompRequisitionServiceWithPermissions();
Expand All @@ -82,6 +130,67 @@ public void GetPropertiesById_NoPermission()
act.Should().Throw<NotAuthorizedException>();
}

[Fact]
public void GetLeaseProperties_Success()
{
// Arrange
var property = EntityHelper.CreateProperty(12345);
property.Location = new NetTopologySuite.Geometries.Point(1229480.4045231808, 463288.8298389828) { SRID = SpatialReference.BCALBERS };

var leaseCompReqProperty = new PimsPropLeaseCompReq()
{
Internal_Id = 1,
CompensationRequisitionId = 1,
PropertyLeaseId = 1,
PropertyLease = new()
{
LeaseId = 1,
PropertyId = property.Internal_Id,
Property = property,
}
};

var service = CreateCompRequisitionServiceWithPermissions(Permissions.CompensationRequisitionView);
var repository = _helper.GetService<Mock<ICompensationRequisitionRepository>>();
repository.Setup(x => x.GetLeaseCompReqPropertiesById(It.IsAny<long>())).Returns(new List<PimsPropertyLease>() { leaseCompReqProperty.PropertyLease });
// mock the spatial conversion to lat/long
var propertyService = _helper.GetService<Mock<IPropertyService>>();
propertyService.Setup(x => x.TransformAllPropertiesToLatLong(It.IsAny<List<PimsPropertyLease>>()))
.Returns<List<PimsPropertyLease>>(x => x.Select(pp =>
{
pp.Property.Location = new NetTopologySuite.Geometries.Point(-122, 49) { SRID = SpatialReference.WGS84 };
return pp;
}).ToList());

// Act
var result = service.GetLeaseProperties(1);

// Assert
repository.Verify(x => x.GetLeaseCompReqPropertiesById(It.IsAny<long>()), Times.Once);
propertyService.Verify(x => x.TransformAllPropertiesToLatLong(It.IsAny<List<PimsPropertyLease>>()), Times.Once);
result.Should().NotBeNull();
result.Should().BeAssignableTo<IEnumerable<PimsPropertyLease>>();
result.Should().HaveCount(1);
result.First().Property.Pid.Should().Be(12345);
// service should perform spatial conversion to Lat/Long so that it can be returned to the frontend
result.First().Property.Location.SRID.Should().Be(SpatialReference.WGS84);
result.First().Property.Location.As<NetTopologySuite.Geometries.Point>().X.Should().Be(-122);
result.First().Property.Location.As<NetTopologySuite.Geometries.Point>().Y.Should().Be(49);
}

[Fact]
public void GetLeaseProperties_NoPermission()
{
// Arrange
var service = this.CreateCompRequisitionServiceWithPermissions();

// Act
Action act = () => service.GetLeaseProperties(1);

// Assert
act.Should().Throw<NotAuthorizedException>();
}

[Fact]
public void GetById_Success()
{
Expand Down Expand Up @@ -152,7 +261,7 @@ public void GetCompensationsRequisitions_Success()

[Theory]
[MemberData(nameof(FileTypesDataNoAccess))]
public void AddCompensationsRequisitions_NoPermissions(FileTypes fileType,PimsCompensationRequisition compReq, Exception exception)
public void AddCompensationsRequisitions_NoPermissions(FileTypes fileType, PimsCompensationRequisition compReq, Exception exception)
{
// Arrange
var service = this.CreateCompRequisitionServiceWithPermissions();
Expand Down Expand Up @@ -286,7 +395,7 @@ public void Update_NoPermission_AcquisitionFile()
var service = this.CreateCompRequisitionServiceWithPermissions();

// Act
Action act = () => service.Update(FileTypes.Acquisition, new PimsCompensationRequisition() { CompensationRequisitionId = 1, AcquisitionFileId = 1});
Action act = () => service.Update(FileTypes.Acquisition, new PimsCompensationRequisition() { CompensationRequisitionId = 1, AcquisitionFileId = 1 });

// Assert
act.Should().Throw<NotAuthorizedException>();
Expand Down Expand Up @@ -523,7 +632,7 @@ public void Update_Status_BackToDraft_AuthorizedAdmin()
noteRepository.Verify(x => x.Add(It.Is<PimsAcquisitionFileNote>(x => x.AcquisitionFileId == 1
&& x.Note.NoteTxt.Equals("Compensation Requisition with # 1, changed status from 'Final' to 'Draft'"))), Times.Once);
}

[Fact]
public void Update_Status_BackToNull_AuthorizedAdmin()
{
Expand Down Expand Up @@ -564,7 +673,7 @@ public void Update_Status_BackToNull_AuthorizedAdmin()
noteRepository.Verify(x => x.Add(It.Is<PimsAcquisitionFileNote>(x => x.AcquisitionFileId == 1
&& x.Note.NoteTxt.Equals("Compensation Requisition with # 1, changed status from 'Final' to 'No Status'"))), Times.Once);
}

[Fact]
public void Update_Success_Skips_StatusChanged_Note_FromNoStatus()
{
Expand Down Expand Up @@ -646,7 +755,7 @@ public void Update_Success_ValidTotalAllowableCompensation()
result.Should().NotBeNull();
compRepository.Verify(x => x.Update(It.IsAny<PimsCompensationRequisition>()), Times.Once);
}

[Fact]
public void Update_Success_ValidMultipleTotalAllowableCompensation()
{
Expand Down Expand Up @@ -690,7 +799,7 @@ public void Update_Success_ValidMultipleTotalAllowableCompensation()
result.Should().NotBeNull();
compRepository.Verify(x => x.Update(It.IsAny<PimsCompensationRequisition>()), Times.Once);
}

[Fact]
public void Update_Success_TotalAllowableExceededDraft()
{
Expand Down Expand Up @@ -732,7 +841,7 @@ public void Update_Success_TotalAllowableExceededDraft()
result.Should().NotBeNull();
compRepository.Verify(x => x.Update(It.IsAny<PimsCompensationRequisition>()), Times.Once);
}

[Fact]
public void Update_Fail_TotalAllowableExceeded()
{
Expand Down Expand Up @@ -774,7 +883,7 @@ public void Update_Fail_TotalAllowableExceeded()
});
act.Should().Throw<BusinessRuleViolationException>();
}

[Fact]
public void Update_Fail_ValidMultipleTotalAllowableCompensation()
{
Expand Down Expand Up @@ -912,7 +1021,7 @@ public void Delete_NoPermission()
// Assert
act.Should().Throw<NotAuthorizedException>();
}

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