Skip to content

Commit

Permalink
PSP-8315 : Add Lease and License Checklist (#4062)
Browse files Browse the repository at this point in the history
Co-authored-by: Herrera <eduardo.herrera@quartech.com>
  • Loading branch information
eddherrera and Herrera authored Jun 3, 2024
1 parent 29129d9 commit 078f185
Show file tree
Hide file tree
Showing 45 changed files with 1,556 additions and 22 deletions.
50 changes: 50 additions & 0 deletions source/backend/api/Areas/Leases/Controllers/LeaseController.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MapsterMapper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Pims.Api.Helpers.Exceptions;
using Pims.Api.Models.Concepts.AcquisitionFile;
using Pims.Api.Models.Concepts.File;
using Pims.Api.Models.Concepts.Lease;
using Pims.Api.Policies;
using Pims.Api.Services;
Expand Down Expand Up @@ -142,6 +146,52 @@ public IActionResult UpdateLease(LeaseModel leaseModel, [FromQuery] string[] use

return new JsonResult(_mapper.Map<LeaseModel>(updatedLease));
}

/// <summary>
/// Get the lease checklist items.
/// </summary>
/// <returns>The checklist items.</returns>
[HttpGet("{id:long}/checklist")]
[HasPermission(Permissions.LeaseView)]
[Produces("application/json")]
[ProducesResponseType(typeof(IEnumerable<FileChecklistItemModel>), 200)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult GetLeaseChecklistItems([FromRoute] long id)
{
var checklist = _leaseService.GetChecklistItems(id);

return new JsonResult(_mapper.Map<IEnumerable<FileChecklistItemModel>>(checklist));
}

/// <summary>
/// Update the lease checklist.
/// </summary>
/// <returns>Updated lease.</returns>
[HttpPut("{id:long}/checklist")]
[HasPermission(Permissions.LeaseEdit)]
[Produces("application/json")]
[ProducesResponseType(typeof(LeaseModel), 200)]
[SwaggerOperation(Tags = new[] { "lease" })]
[TypeFilter(typeof(NullJsonResultFilter))]
public IActionResult UpdateLeaseChecklist([FromRoute]long id, [FromBody] IList<FileChecklistItemModel> checklistItems)
{
if(checklistItems.Any(x => x.FileId != id))
{
throw new BadRequestException("All checklist items file id must match the Lease's id");
}

if (checklistItems.Count == 0)
{
throw new BadRequestException("Checklist items must be greater than zero");
}

var checklistItemEntities = _mapper.Map<IList<Dal.Entities.PimsLeaseChecklistItem>>(checklistItems);
var updatedLease = _leaseService.UpdateChecklistItems(id, checklistItemEntities);

return new JsonResult(_mapper.Map<LeaseModel>(updatedLease));
}

#endregion
}
}
4 changes: 4 additions & 0 deletions source/backend/api/Controllers/LookupController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ public IActionResult GetAll()
var dispositionChecklistItemTypes = _mapper.Map<Model.LookupModel[]>(_lookupRepository.GetAllDispositionChecklistItemTypes());
var dispositionChecklistSectionTypes = _mapper.Map<Model.LookupModel[]>(_lookupRepository.GetAllDispositionChecklistSectionTypes());
var historicalNumberTypes = _mapper.Map<Model.LookupModel[]>(_lookupRepository.GetAllHistoricalNumberTypes());
var leaseChecklistStatusTypes = _mapper.Map<Model.LookupModel[]>(_lookupRepository.GetAllLeaseChecklistItemStatusTypes());
var leaseChecklistSectionTypes = _mapper.Map<Model.LookupModel[]>(_lookupRepository.GetAllLeaseChecklistSectionTypes());

var codes = new List<object>();
codes.AddRange(areaUnitTypes);
Expand Down Expand Up @@ -222,6 +224,8 @@ public IActionResult GetAll()
codes.AddRange(dispositionChecklistItemTypes);
codes.AddRange(dispositionChecklistSectionTypes);
codes.AddRange(historicalNumberTypes);
codes.AddRange(leaseChecklistStatusTypes);
codes.AddRange(leaseChecklistSectionTypes);

var response = new JsonResult(codes);

Expand Down
4 changes: 4 additions & 0 deletions source/backend/api/Services/ILeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,9 @@ public interface ILeaseService
IEnumerable<PimsLeaseTenant> GetTenantsByLeaseId(long leaseId);

IEnumerable<PimsLeaseTenant> UpdateTenantsByLeaseId(long leaseId, IEnumerable<PimsLeaseTenant> pimsLeaseTenants);

IEnumerable<PimsLeaseChecklistItem> GetChecklistItems(long id);

PimsLease UpdateChecklistItems(long leaseId, IList<PimsLeaseChecklistItem> checklistItems);
}
}
95 changes: 95 additions & 0 deletions source/backend/api/Services/LeaseService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
using System.Security.Claims;
using System.Text;
using Microsoft.Extensions.Logging;
using Pims.Api.Helpers.Exceptions;
using Pims.Api.Models.CodeTypes;
using Pims.Core.Exceptions;
using Pims.Core.Extensions;
using Pims.Dal.Entities;
using Pims.Dal.Entities.Extensions;
using Pims.Dal.Entities.Models;
using Pims.Dal.Exceptions;
using Pims.Dal.Helpers;
Expand Down Expand Up @@ -88,6 +90,7 @@ public PimsLease GetById(long leaseId)
property.Location = GeometryHelper.CreatePoint(newCoords, SpatialReference.WGS84);
}
}

return lease;
}

Expand Down Expand Up @@ -188,6 +191,8 @@ public PimsLease Add(PimsLease lease, IEnumerable<UserOverrideCode> userOverride

var leasesWithProperties = AssociatePropertyLeases(lease, userOverrides);

lease.PimsLeaseChecklistItems = GetActiveChecklistItemsForLease();

return _leaseRepository.Add(leasesWithProperties);
}

Expand Down Expand Up @@ -249,6 +254,58 @@ public PimsLease Update(PimsLease lease, IEnumerable<UserOverrideCode> userOverr
return _leaseRepository.GetNoTracking(lease.LeaseId);
}

public IEnumerable<PimsLeaseChecklistItem> GetChecklistItems(long id)
{
_logger.LogInformation("Getting Lease checklist with Id: {id}", id);
_user.ThrowIfNotAuthorized(Permissions.LeaseView);

var pimsUser = _userRepository.GetByKeycloakUserId(_user.GetUserKey());
pimsUser.ThrowInvalidAccessToLeaseFile(_leaseRepository.GetNoTracking(id).RegionCode);

var checklistItems = _leaseRepository.GetAllChecklistItemsByLeaseId(id);

var lease = _leaseRepository.Get(id);
AppendNewItemsToChecklist(lease, ref checklistItems);

return checklistItems;
}

public PimsLease UpdateChecklistItems(long leaseId, IList<PimsLeaseChecklistItem> checklistItems)
{
checklistItems.ThrowIfNull(nameof(checklistItems));

_logger.LogInformation("Updating Lease checklist with id: {leaseId}", leaseId);
_user.ThrowIfNotAuthorized(Permissions.LeaseEdit);

var pimsUser = _userRepository.GetByKeycloakUserId(_user.GetUserKey());
pimsUser.ThrowInvalidAccessToLeaseFile(_leaseRepository.GetNoTracking(leaseId).RegionCode);

// Get the current checklist items for this acquisition file.
var currentItems = _leaseRepository.GetAllChecklistItemsByLeaseId(leaseId).ToDictionary(ci => ci.LeaseChecklistItemId);

foreach (var incomingItem in checklistItems)
{
if (!currentItems.TryGetValue(incomingItem.LeaseChecklistItemId, out var existingItem) && incomingItem.LeaseChecklistItemId != 0)
{
throw new BadRequestException($"Cannot update checklist item. Item with Id: {incomingItem.LeaseChecklistItemId} not found.");
}

// Only update checklist items that changed.
if (existingItem == null)
{
_leaseRepository.AddChecklistItem(incomingItem);
}
else if (existingItem.LeaseChklstItemStatusTypeCode != incomingItem.LeaseChklstItemStatusTypeCode)
{
_leaseRepository.UpdateChecklistItem(incomingItem);
}
}

_leaseRepository.CommitTransaction();

return _leaseRepository.Get(leaseId);
}

private PimsLeaseNote GeneratePimsLeaseNote(PimsLease currentLease, PimsLease lease)
{
var leaseStatuses = _lookupRepository.GetAllLeaseStatusTypes();
Expand Down Expand Up @@ -435,5 +492,43 @@ private void MatchProperties(PimsLease lease, IEnumerable<UserOverrideCode> user
}
}
}

private List<PimsLeaseChecklistItem> GetActiveChecklistItemsForLease()
{
List<PimsLeaseChecklistItem> chklistItems = new();
foreach (var itemType in _leaseRepository.GetAllChecklistItemTypes().Where(x => !x.IsExpiredType() && !x.IsDisabled))
{
PimsLeaseChecklistItem checklistItem = new ()
{
LeaseChklstItemTypeCode = itemType.LeaseChklstItemTypeCode,
LeaseChklstItemStatusTypeCode = LeaseChecklistItemStatusTypes.INCOMP.ToString(),
};

chklistItems.Add(checklistItem);
}

return chklistItems;
}

private void AppendNewItemsToChecklist(PimsLease lease, ref List<PimsLeaseChecklistItem> pimsLeaseChecklistItems)
{
PimsLeaseChklstItemStatusType incompleteStatusType = _lookupRepository.GetAllLeaseChecklistItemStatusTypes().FirstOrDefault(cst => cst.Id == LeaseChecklistItemStatusTypes.INCOMP.ToString());
foreach (var itemType in _leaseRepository.GetAllChecklistItemTypes().Where(x => !x.IsExpiredType() && !x.IsDisabled))
{
if (!pimsLeaseChecklistItems.Any(cli => cli.LeaseChklstItemTypeCode == itemType.LeaseChklstItemTypeCode))
{
var checklistItem = new PimsLeaseChecklistItem
{
LeaseChklstItemTypeCode = itemType.LeaseChklstItemTypeCode,
LeaseChklstItemTypeCodeNavigation = itemType,
LeaseChklstItemStatusTypeCode = incompleteStatusType.Id,
LeaseId = lease.LeaseId,
LeaseChklstItemStatusTypeCodeNavigation = incompleteStatusType,
};

pimsLeaseChecklistItems.Add(checklistItem);
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace Pims.Api.Models.CodeTypes
{
[JsonConverter(typeof(JsonStringEnumMemberConverter))]
public enum LeaseChecklistItemSectionTypes
{
[EnumMember(Value = "AGREEPREP")]
AGREEPREP,

[EnumMember(Value = "FILEINIT")]
FILEINIT,

[EnumMember(Value = "LLCOMPLTN")]
LLCOMPLTN,

[EnumMember(Value = "REFERAPPR")]
REFERAPPR,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.Serialization;
using System.Text.Json.Serialization;

namespace Pims.Api.Models.CodeTypes
{
[JsonConverter(typeof(JsonStringEnumMemberConverter))]
public enum LeaseChecklistItemStatusTypes
{
[EnumMember(Value = "COMPLT")]
COMPLT,

[EnumMember(Value = "INCOMP")]
INCOMP,

[EnumMember(Value = "NA")]
NA,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Mapster;
using Pims.Api.Models.Base;
using Pims.Api.Models.Concepts.File;
using Entity = Pims.Dal.Entities;

namespace Pims.Api.Models.Models.Concepts.Lease
{
public class LeaseChecklistItemMap : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.NewConfig<Entity.PimsLeaseChecklistItem, FileChecklistItemModel>()
.Map(dest => dest.Id, src => src.LeaseChecklistItemId)
.Map(dest => dest.FileId, src => src.LeaseId)
.Map(dest => dest.ItemType, src => src.LeaseChklstItemTypeCodeNavigation)
.Map(dest => dest.StatusTypeCode, src => src.LeaseChklstItemStatusTypeCodeNavigation)
.Inherits<Entity.IBaseAppEntity, BaseAuditModel>();

config.NewConfig<FileChecklistItemModel, Entity.PimsLeaseChecklistItem>()
.Map(dest => dest.LeaseChecklistItemId, src => src.Id)
.Map(dest => dest.LeaseId, src => src.FileId)
.Map(dest => dest.LeaseChklstItemTypeCode, src => src.ItemType.Code)
.Map(dest => dest.LeaseChklstItemStatusTypeCode, src => src.StatusTypeCode.Id)
.Inherits<BaseAuditModel, Entity.IBaseAppEntity>();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using Mapster;
using Pims.Api.Models.Base;
using Pims.Api.Models.Concepts.File;
using Entity = Pims.Dal.Entities;

namespace Pims.Api.Models.Models.Concepts.Lease
{
public class LeaseChecklistItemTypeMap : IRegister
{
public void Register(TypeAdapterConfig config)
{
config.NewConfig<Entity.PimsLeaseChklstItemType, FileChecklistItemTypeModel>()
.Map(dest => dest.Code, src => src.Id)
.Map(dest => dest.SectionCode, src => src.ParentId)
.Map(dest => dest.Description, src => src.Description)
.Map(dest => dest.Hint, src => src.Hint)
.Map(dest => dest.IsDisabled, src => src.IsDisabled)
.Map(dest => dest.DisplayOrder, src => src.DisplayOrder)
.Inherits<Entity.IBaseEntity, BaseConcurrentModel>();
}
}
}
2 changes: 2 additions & 0 deletions source/backend/apimodels/Models/Concepts/Lease/LeaseMap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public void Register(TypeAdapterConfig config)
.Map(dest => dest.TerminationReason, src => src.TerminationReason)
.Map(dest => dest.Project, src => src.Project)
.Map(dest => dest.Tenants, src => src.PimsLeaseTenants)
.Map(dest => dest.FileChecklistItems, src => src.PimsLeaseChecklistItems)
.Map(dest => dest.Terms, src => src.PimsLeaseTerms);

config.NewConfig<LeaseModel, PimsLease>()
Expand Down Expand Up @@ -97,6 +98,7 @@ public void Register(TypeAdapterConfig config)
.Map(dest => dest.HasDigitalLicense, src => src.HasDigitalLicense)
.Map(dest => dest.CancellationReason, src => src.CancellationReason)
.Map(dest => dest.TerminationReason, src => src.TerminationReason)
.Map(dest => dest.PimsLeaseChecklistItems, src => src.FileChecklistItems)
.Map(dest => dest.ProjectId, src => src.Project != null ? src.Project.Id : (long?)null)
.IgnoreNullValues(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Pims.Api.Models.Concepts.Lease
/// <summary>
/// Provides a lease-oriented model.
/// </summary>
public class LeaseModel : FileModel
public class LeaseModel : FileWithChecklistModel
{
#region Properties

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,13 @@ public interface ILeaseRepository : IRepository<PimsLease>
PimsLease Update(PimsLease lease, bool commitTransaction = true);

PimsLease UpdateLeaseConsultations(long leaseId, long? rowVersion, ICollection<PimsLeaseConsultation> pimsLeaseConsultations);

IEnumerable<PimsLeaseChklstItemType> GetAllChecklistItemTypes();

List<PimsLeaseChecklistItem> GetAllChecklistItemsByLeaseId(long leaseId);

PimsLeaseChecklistItem AddChecklistItem(PimsLeaseChecklistItem checklistItem);

PimsLeaseChecklistItem UpdateChecklistItem(PimsLeaseChecklistItem checklistItem);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,5 +157,9 @@ public interface ILookupRepository : IRepository
IEnumerable<PimsDspChklstSectionType> GetAllDispositionChecklistSectionTypes();

IEnumerable<PimsHistoricalFileNumberType> GetAllHistoricalNumberTypes();

IEnumerable<PimsLeaseChklstItemStatusType> GetAllLeaseChecklistItemStatusTypes();

IEnumerable<PimsLeaseChklstSectionType> GetAllLeaseChecklistSectionTypes();
}
}
Loading

0 comments on commit 078f185

Please sign in to comment.