Skip to content

Commit

Permalink
PSP-8051 : FT:User is able to add the retired property record to the … (
Browse files Browse the repository at this point in the history
#3893)

Co-authored-by: Herrera <eduardo.herrera@quartech.com>
  • Loading branch information
eddherrera and Herrera committed Mar 25, 2024
1 parent be67158 commit d8e6ff8
Show file tree
Hide file tree
Showing 15 changed files with 322 additions and 89 deletions.
12 changes: 11 additions & 1 deletion source/backend/api/Services/AcquisitionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,12 @@ private void MatchProperties(PimsAcquisitionFile acquisitionFile, IEnumerable<Us
var pid = acquisitionProperty.Property.Pid.Value;
try
{
var foundProperty = _propertyRepository.GetByPid(pid);
var foundProperty = _propertyRepository.GetByPid(pid, true);
if (foundProperty.IsRetired.HasValue && foundProperty.IsRetired.Value)
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

acquisitionProperty.PropertyId = foundProperty.Internal_Id;
_propertyService.UpdateLocation(acquisitionProperty.Property, ref foundProperty, userOverrideCodes);
acquisitionProperty.Property = foundProperty;
Expand All @@ -643,6 +648,11 @@ private void MatchProperties(PimsAcquisitionFile acquisitionFile, IEnumerable<Us
try
{
var foundProperty = _propertyRepository.GetByPin(pin);
if (foundProperty.IsRetired.HasValue && foundProperty.IsRetired.Value)
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

acquisitionProperty.PropertyId = foundProperty.Internal_Id;
_propertyService.UpdateLocation(acquisitionProperty.Property, ref foundProperty, userOverrideCodes);
acquisitionProperty.Property = foundProperty;
Expand Down
16 changes: 12 additions & 4 deletions source/backend/api/Services/DispositionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Globalization;
using System.Linq;
using System.Security.Claims;
using System.Text.RegularExpressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Pims.Api.Constants;
Expand Down Expand Up @@ -74,7 +73,6 @@ public PimsDispositionFile Add(PimsDispositionFile dispositionFile, IEnumerable<

dispositionFile.DispositionStatusTypeCode ??= EnumDispositionStatusTypeCode.UNKNOWN.ToString();
dispositionFile.DispositionFileStatusTypeCode ??= EnumDispositionFileStatusTypeCode.ACTIVE.ToString();

ValidateStaff(dispositionFile);

MatchProperties(dispositionFile, userOverrides);
Expand Down Expand Up @@ -704,7 +702,12 @@ private void MatchProperties(PimsDispositionFile dispositionFile, IEnumerable<Us
var pid = dispProperty.Property.Pid.Value;
try
{
var foundProperty = _propertyRepository.GetByPid(pid);
var foundProperty = _propertyRepository.GetByPid(pid, true);
if (foundProperty.IsRetired.HasValue && foundProperty.IsRetired.Value)
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

dispProperty.PropertyId = foundProperty.Internal_Id;
_propertyService.UpdateLocation(dispProperty.Property, ref foundProperty, overrideCodes);
dispProperty.Property = null;
Expand All @@ -718,7 +721,7 @@ private void MatchProperties(PimsDispositionFile dispositionFile, IEnumerable<Us
}
else
{
throw new UserOverrideException(UserOverrideCode.DisposingPropertyNotInventoried, "You have added one or more properties to the disposition file that are not in the MoTI Inventory. Do you want to proceed?");
throw new UserOverrideException(UserOverrideCode.DisposingPropertyNotInventoried, "You have added one or more properties to the disposition file that are not in the MOTI Inventory. Do you want to proceed?");
}
}
}
Expand All @@ -728,6 +731,11 @@ private void MatchProperties(PimsDispositionFile dispositionFile, IEnumerable<Us
try
{
var foundProperty = _propertyRepository.GetByPin(pin);
if (foundProperty.IsRetired.HasValue && foundProperty.IsRetired.Value)
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

dispProperty.PropertyId = foundProperty.Internal_Id;
_propertyService.UpdateLocation(dispProperty.Property, ref foundProperty, overrideCodes);
dispProperty.Property = null;
Expand Down
25 changes: 14 additions & 11 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class LeaseService : BaseService, ILeaseService
private readonly ICoordinateTransformService _coordinateService;
private readonly IPropertyRepository _propertyRepository;
private readonly IPropertyLeaseRepository _propertyLeaseRepository;
private readonly ILookupRepository _lookupRepository;
private readonly IEntityNoteRepository _entityNoteRepository;
private readonly IInsuranceRepository _insuranceRepository;
private readonly ILeaseTenantRepository _tenantRepository;
Expand All @@ -41,7 +40,6 @@ public LeaseService(
IPropertyRepository propertyRepository,
IPropertyLeaseRepository propertyLeaseRepository,
IPropertyImprovementRepository propertyImprovementRepository,
ILookupRepository lookupRepository,
IEntityNoteRepository entityNoteRepository,
IInsuranceRepository insuranceRepository,
ILeaseTenantRepository tenantRepository,
Expand All @@ -55,7 +53,6 @@ public LeaseService(
_coordinateService = coordinateTransformService;
_propertyRepository = propertyRepository;
_propertyLeaseRepository = propertyLeaseRepository;
_lookupRepository = lookupRepository;
_entityNoteRepository = entityNoteRepository;
_propertyImprovementRepository = propertyImprovementRepository;
_insuranceRepository = insuranceRepository;
Expand Down Expand Up @@ -213,16 +210,10 @@ public PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverr
pimsUser.ThrowInvalidAccessToLeaseFile(lease.RegionCode);

var currentProperties = _propertyLeaseRepository.GetAllByLeaseId(lease.LeaseId);
var newPropertiesAdded = lease.PimsPropertyLeases.Where(x => !currentProperties.Any(y => y.Internal_Id == x.Internal_Id)).ToList();

if(newPropertiesAdded.Any(x => x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

if (currentLease.LeaseStatusTypeCode != lease.LeaseStatusTypeCode)
{
_entityNoteRepository.Add<PimsLeaseNote>(
_entityNoteRepository.Add(
new PimsLeaseNote()
{
LeaseId = currentLease.LeaseId,
Expand Down Expand Up @@ -300,6 +291,7 @@ private PimsLease AssociatePropertyLeases(PimsLease lease, IEnumerable<UserOverr
var existingPropertyLeases = _propertyLeaseRepository.GetAllByPropertyId(property.PropertyId);
var isPropertyOnOtherLease = existingPropertyLeases.Any(p => p.LeaseId != lease.Internal_Id);
var isPropertyOnThisLease = existingPropertyLeases.Any(p => p.LeaseId == lease.Internal_Id);

if (isPropertyOnOtherLease && !isPropertyOnThisLease && !userOverrides.Contains(UserOverrideCode.AddPropertyToInventory))
{
var genericOverrideErrorMsg = $"is attached to L-File # {existingPropertyLeases.FirstOrDefault().Lease.LFileNo}";
Expand All @@ -313,6 +305,7 @@ private PimsLease AssociatePropertyLeases(PimsLease lease, IEnumerable<UserOverr
}
string overrideError = $"Lng/Lat {propertyLease?.Property?.Location.Coordinate.X.ToString(CultureInfo.CurrentCulture) ?? string.Empty}, " +
$"{propertyLease?.Property?.Location.Coordinate.Y.ToString(CultureInfo.CurrentCulture) ?? string.Empty} {genericOverrideErrorMsg}";

throw new UserOverrideException(UserOverrideCode.AddPropertyToInventory, overrideError);
}

Expand Down Expand Up @@ -366,7 +359,12 @@ private void MatchProperties(PimsLease lease, IEnumerable<UserOverrideCode> user
var pid = leaseProperty.Property.Pid.Value;
try
{
var foundProperty = _propertyRepository.GetByPid(pid);
var foundProperty = _propertyRepository.GetByPid(pid, true);
if (foundProperty.IsRetired.HasValue && foundProperty.IsRetired.Value)
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

leaseProperty.PropertyId = foundProperty.Internal_Id;
UpdateLocation(leaseProperty.Property, ref foundProperty, userOverrides);
leaseProperty.Property = foundProperty;
Expand All @@ -383,6 +381,11 @@ private void MatchProperties(PimsLease lease, IEnumerable<UserOverrideCode> user
try
{
var foundProperty = _propertyRepository.GetByPin(pin);
if (foundProperty.IsRetired.HasValue && foundProperty.IsRetired.Value)
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}

leaseProperty.PropertyId = foundProperty.Internal_Id;
UpdateLocation(leaseProperty.Property, ref foundProperty, userOverrides);
leaseProperty.Property = foundProperty;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Pims.Api.Models.Concepts.File;
using Pims.Api.Models.Base;
using Pims.Api.Models.Concepts.Property;

namespace Pims.Api.Models.Concepts.DispositionFile
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,7 +657,7 @@ public PimsAcquisitionFile Add(PimsAcquisitionFile acquisitionFile)
using var scope = Logger.QueryScope();
acquisitionFile.ThrowIfNull(nameof(acquisitionFile));

if (acquisitionFile.PimsPropertyAcquisitionFiles.Any(x => x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
if (acquisitionFile.PimsPropertyAcquisitionFiles.Any(x =>x.Property != null && x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ public PimsDispositionFile Add(PimsDispositionFile disposition)
using var scope = Logger.QueryScope();
disposition.ThrowIfNull(nameof(disposition));

if (disposition.PimsDispositionFileProperties.Any(x => x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
if (disposition.PimsDispositionFileProperties.Any(x => x.Property != null && x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public interface IPropertyRepository : IRepository<PimsProperty>

PimsProperty GetByPid(string pid);

PimsProperty GetByPid(int pid);
PimsProperty GetByPid(int pid, bool includeRetired = false);

PimsProperty GetByPin(int pin);

Expand Down
2 changes: 1 addition & 1 deletion source/backend/dal/Repositories/LeaseRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,7 @@ public PimsLease Add(PimsLease lease)

User.ThrowIfNotAuthorized(Permissions.LeaseAdd);

if (lease.PimsPropertyLeases.Any(x => x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
if (lease.PimsPropertyLeases.Any(x => x.Property != null && x.Property.IsRetired.HasValue && x.Property.IsRetired.Value))
{
throw new BusinessRuleViolationException("Retired property can not be selected.");
}
Expand Down
65 changes: 37 additions & 28 deletions source/backend/dal/Repositories/PropertyRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,38 +185,47 @@ public PimsProperty GetByPid(string pid)
/// Get the property for the specified PID value.
/// </summary>
/// <param name="pid"></param>
/// <param name="includeRetired"></param>
/// <returns></returns>
public PimsProperty GetByPid(int pid)
public PimsProperty GetByPid(int pid, bool includeRetired = false)
{
this.User.ThrowIfNotAllAuthorized(Permissions.PropertyView);

var property = this.Context.PimsProperties.AsNoTracking()
.Include(p => p.DistrictCodeNavigation)
.Include(p => p.RegionCodeNavigation)
.Include(p => p.PropertyTypeCodeNavigation)
.Include(p => p.PropertyStatusTypeCodeNavigation)
.Include(p => p.PropertyDataSourceTypeCodeNavigation)
.Include(p => p.PropertyClassificationTypeCodeNavigation)
.Include(p => p.PimsPropPropAnomalyTypes)
.ThenInclude(t => t.PropertyAnomalyTypeCodeNavigation)
.Include(p => p.PimsPropPropRoadTypes)
.ThenInclude(t => t.PropertyRoadTypeCodeNavigation)
.Include(p => p.PimsPropPropTenureTypes)
.ThenInclude(t => t.PropertyTenureTypeCodeNavigation)
.Include(p => p.PropertyAreaUnitTypeCodeNavigation)
.Include(p => p.VolumetricTypeCodeNavigation)
.Include(p => p.VolumeUnitTypeCodeNavigation)
.Include(p => p.Address)
.ThenInclude(a => a.RegionCodeNavigation)
.Include(p => p.Address)
.ThenInclude(a => a.DistrictCodeNavigation)
.Include(p => p.Address)
.ThenInclude(a => a.ProvinceState)
.Include(p => p.Address)
.ThenInclude(a => a.Country)
.OrderByDescending(p => p.PropertyId)
.FirstOrDefault(p => p.Pid == pid && p.IsRetired != true) ?? throw new KeyNotFoundException();
return property;
var query = Context.PimsProperties.AsNoTracking();

if(includeRetired)
{
query = query.Where(r => r.IsRetired.HasValue && r.IsRetired.Value);
}
else
{
query = query.Where(r => !r.IsRetired.HasValue || (r.IsRetired.HasValue && !r.IsRetired.Value));
}

return query.Include(p => p.DistrictCodeNavigation)
.Include(p => p.RegionCodeNavigation)
.Include(p => p.PropertyTypeCodeNavigation)
.Include(p => p.PropertyStatusTypeCodeNavigation)
.Include(p => p.PropertyDataSourceTypeCodeNavigation)
.Include(p => p.PropertyClassificationTypeCodeNavigation)
.Include(p => p.PimsPropPropAnomalyTypes)
.ThenInclude(t => t.PropertyAnomalyTypeCodeNavigation)
.Include(p => p.PimsPropPropRoadTypes)
.ThenInclude(t => t.PropertyRoadTypeCodeNavigation)
.Include(p => p.PimsPropPropTenureTypes)
.ThenInclude(t => t.PropertyTenureTypeCodeNavigation)
.Include(p => p.PropertyAreaUnitTypeCodeNavigation)
.Include(p => p.VolumetricTypeCodeNavigation)
.Include(p => p.VolumeUnitTypeCodeNavigation)
.Include(p => p.Address)
.ThenInclude(a => a.RegionCodeNavigation)
.Include(p => p.Address)
.ThenInclude(a => a.DistrictCodeNavigation)
.Include(p => p.Address)
.ThenInclude(a => a.ProvinceState)
.Include(p => p.Address)
.ThenInclude(a => a.Country)
.OrderByDescending(p => p.PropertyId).FirstOrDefault(p => p.Pid == pid) ?? throw new KeyNotFoundException();
}

/// <summary>
Expand Down
Loading

0 comments on commit d8e6ff8

Please sign in to comment.