Skip to content

Commit

Permalink
Merge branch 'dev' into psp-6775
Browse files Browse the repository at this point in the history
  • Loading branch information
Eduardo Herrera authored and Eduardo Herrera committed Sep 18, 2023
2 parents 376916c + be46a90 commit ac58fea
Show file tree
Hide file tree
Showing 17 changed files with 312 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ public class AcquisitionFilterModel : PageFilter
/// </summary>
public string ProjectNameOrNumber { get; set; }

/// <summary>
/// get/set - The MOTI person id to search by for acquisition team members.
/// </summary>
public string AcquisitionTeamMemberPersonId { get; set; }

#endregion

#region Constructors
Expand Down Expand Up @@ -66,6 +71,7 @@ public AcquisitionFilterModel(Dictionary<string, Microsoft.Extensions.Primitives
this.AcquisitionFileStatusTypeCode = filter.GetStringValue(nameof(this.AcquisitionFileStatusTypeCode));
this.AcquisitionFileNameOrNumber = filter.GetStringValue(nameof(this.AcquisitionFileNameOrNumber));
this.ProjectNameOrNumber = filter.GetStringValue(nameof(this.ProjectNameOrNumber));
this.AcquisitionTeamMemberPersonId = filter.GetStringValue(nameof(this.AcquisitionTeamMemberPersonId));

this.Sort = filter.GetStringArrayValue(nameof(this.Sort));
}
Expand All @@ -90,6 +96,7 @@ public static explicit operator AcquisitionFilter(AcquisitionFilterModel model)
AcquisitionFileStatusTypeCode = model.AcquisitionFileStatusTypeCode,
AcquisitionFileNameOrNumber = model.AcquisitionFileNameOrNumber,
ProjectNameOrNumber = model.ProjectNameOrNumber,
AcquisitionTeamMemberPersonId = model.AcquisitionTeamMemberPersonId,

Sort = model.Sort,
};
Expand Down
4 changes: 2 additions & 2 deletions source/backend/api/Pims.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<UserSecretsId>0ef6255f-9ea0-49ec-8c65-c172304b4926</UserSecretsId>
<Version>4.0.0-63.8</Version>
<Version>4.0.0-63.8</Version>
<Version>4.0.0-63.9</Version>
<Version>4.0.0-63.9</Version>
<AssemblyVersion>4.0.0.63</AssemblyVersion>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<ProjectGuid>16BC0468-78F6-4C91-87DA-7403C919E646</ProjectGuid>
Expand Down
12 changes: 7 additions & 5 deletions source/backend/api/Services/AcquisitionFileService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ public Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter)
// Limit search results to user's assigned region(s)
var pimsUser = _userRepository.GetUserInfoByKeycloakUserId(_user.GetUserKey());
var userRegions = pimsUser.PimsRegionUsers.Select(r => r.RegionCode).ToHashSet();
long? personId = pimsUser.IsContractor ? pimsUser.PersonId : null;
long? contractorPersonId = pimsUser.IsContractor ? pimsUser.PersonId : null;

return _acqFileRepository.GetPage(filter, userRegions, personId);
return _acqFileRepository.GetPage(filter, userRegions, contractorPersonId);
}

public List<AcquisitionFileExportModel> GetAcquisitionFileExport(AcquisitionFilter filter)
Expand All @@ -96,9 +96,9 @@ public List<AcquisitionFileExportModel> GetAcquisitionFileExport(AcquisitionFilt
// Limit search results to user's assigned region(s)
var pimsUser = _userRepository.GetUserInfoByKeycloakUserId(_user.GetUserKey());
var userRegions = pimsUser.PimsRegionUsers.Select(r => r.RegionCode).ToHashSet();
long? personId = pimsUser.IsContractor ? pimsUser.PersonId : null;
long? contractorPersonId = pimsUser.IsContractor ? pimsUser.PersonId : null;

var acqFiles = _acqFileRepository.GetAcquisitionFileExport(filter, userRegions, personId);
var acqFiles = _acqFileRepository.GetAcquisitionFileExport(filter, userRegions, contractorPersonId);

return acqFiles.SelectMany(file => file.PimsPropertyAcquisitionFiles.Where(fp => fp.AcquisitionFileId.Equals(file.AcquisitionFileId)).DefaultIfEmpty(), (file, fp) => (file, fp))
.Select(fileProperty => new AcquisitionFileExportModel
Expand Down Expand Up @@ -163,7 +163,9 @@ public IEnumerable<PimsAcquisitionFilePerson> GetTeamMembers()

var pimsUser = _userRepository.GetUserInfoByKeycloakUserId(_user.GetUserKey());
var userRegions = pimsUser.PimsRegionUsers.Select(r => r.RegionCode).ToHashSet();
return _acqFileRepository.GetTeamMembers(userRegions);
long? contractorPersonId = pimsUser.IsContractor ? pimsUser.PersonId : null;

return _acqFileRepository.GetTeamMembers(userRegions, contractorPersonId);
}

public IEnumerable<PimsAcquisitionChecklistItem> GetChecklistItems(long id)
Expand Down
5 changes: 5 additions & 0 deletions source/backend/dal/Models/AcquisitionFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ public class AcquisitionFilter : PageFilter
/// get/set - The MOTI project name or the project number, search for both simultaneously.
/// </summary>
public string ProjectNameOrNumber { get; set; }

/// <summary>
/// get/set - The MOTI person id to search by for acquisition team members.
/// </summary>
public string AcquisitionTeamMemberPersonId { get; set; }
#endregion

#region Constructors
Expand Down
77 changes: 46 additions & 31 deletions source/backend/dal/Repositories/AcquisitionFileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public AcquisitionFileRepository(PimsContext dbContext, ClaimsPrincipal user, IL
/// <param name="filter"></param>
/// <param name="regions"></param>
/// <returns></returns>
public Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter, HashSet<short> regions, long? filterPersonId = null)
public Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter, HashSet<short> regions, long? contractorPersonId = null)
{
// RECOMMENDED - use a log scope to group all potential SQL statements generated by EF for this method call
using var scope = Logger.QueryScope();
Expand All @@ -53,7 +53,7 @@ public Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter, HashSet<shor
throw new ArgumentException("Argument must have a valid filter", nameof(filter));
}

IQueryable<PimsAcquisitionFile> query = GetCommonAquisitionFileQuery(filter, regions, filterPersonId);
IQueryable<PimsAcquisitionFile> query = GetCommonAquisitionFileQuery(filter, regions, contractorPersonId);

var skip = (filter.Page - 1) * filter.Quantity;
var pageItems = query.Skip(skip).Take(filter.Quantity).ToList();
Expand All @@ -66,9 +66,9 @@ public Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter, HashSet<shor
/// </summary>
/// <param name="filter"></param>
/// <param name="regions"></param>
/// <param name="filterPersonId"></param>
/// <param name="contractorPersonId"></param>
/// <returns></returns>
public List<PimsAcquisitionFile> GetAcquisitionFileExport(AcquisitionFilter filter, HashSet<short> regions, long? filterPersonId = null)
public List<PimsAcquisitionFile> GetAcquisitionFileExport(AcquisitionFilter filter, HashSet<short> regions, long? contractorPersonId = null)
{
// RECOMMENDED - use a log scope to group all potential SQL statements generated by EF for this method call
using var scope = Logger.QueryScope();
Expand All @@ -79,7 +79,7 @@ public List<PimsAcquisitionFile> GetAcquisitionFileExport(AcquisitionFilter filt
throw new ArgumentException("Argument must have a valid filter", nameof(filter));
}

return GetCommonAquisitionFileQuery(filter, regions, filterPersonId).ToList();
return GetCommonAquisitionFileQuery(filter, regions, contractorPersonId).ToList();
}

/// <summary>
Expand Down Expand Up @@ -140,13 +140,21 @@ public List<PimsAcquisitionOwner> GetOwnersByAcquisitionFileId(long acquisitionF
.ToList();
}

public List<PimsAcquisitionFilePerson> GetTeamMembers(HashSet<short> regions)
public List<PimsAcquisitionFilePerson> GetTeamMembers(HashSet<short> regions, long? contractorPersonId = null)
{
return Context.PimsAcquisitionFilePeople
.Where(x => regions.Contains(x.AcquisitionFile.RegionCode))
.Include(x => x.AcquisitionFile)
var predicate = PredicateBuilder.New<PimsAcquisitionFilePerson>(acq => true);

predicate.And(x => regions.Contains(x.AcquisitionFile.RegionCode));

if (contractorPersonId != null)
{
predicate.And(x => x.AcquisitionFile.PimsAcquisitionFilePeople.Any(p => p.PersonId == contractorPersonId));
}

return Context.PimsAcquisitionFilePeople.Include(x => x.AcquisitionFile)
.Include(x => x.Person)
.AsNoTracking()
.Where(predicate)
.AsEnumerable()
.DistinctBy(x => x.PersonId).ToList();
}
Expand Down Expand Up @@ -288,9 +296,9 @@ private int GetNextAcquisitionFileNumberSequenceValue()
/// </summary>
/// <param name="filter"></param>
/// <param name="regions"></param>
/// <param name="filterPersonId"></param>
/// <param name="contractorPersonId"></param>
/// <returns></returns>
private IQueryable<PimsAcquisitionFile> GetCommonAquisitionFileQuery(AcquisitionFilter filter, HashSet<short> regions, long? filterPersonId = null)
private IQueryable<PimsAcquisitionFile> GetCommonAquisitionFileQuery(AcquisitionFilter filter, HashSet<short> regions, long? contractorPersonId = null)
{
var predicate = PredicateBuilder.New<PimsAcquisitionFile>(acq => true);

Expand Down Expand Up @@ -332,30 +340,37 @@ private IQueryable<PimsAcquisitionFile> GetCommonAquisitionFileQuery(Acquisition

predicate = predicate.And(acq => regions.Contains(acq.RegionCode));

if (filterPersonId is not null)
if (contractorPersonId is not null)
{
predicate = predicate.And(acq => acq.PimsAcquisitionFilePeople.Any(x => x.PersonId == filterPersonId));
predicate = predicate.And(acq => acq.PimsAcquisitionFilePeople.Any(x => x.PersonId == contractorPersonId));
}

if (!string.IsNullOrWhiteSpace(filter.AcquisitionTeamMemberPersonId))
{
predicate = predicate.And(acq => acq.PimsAcquisitionFilePeople.Any(x => x.PersonId == long.Parse(filter.AcquisitionTeamMemberPersonId)));
}

var query = Context.PimsAcquisitionFiles.AsNoTracking()
.Include(r => r.RegionCodeNavigation)
.Include(p => p.Project)
.Include(s => s.AcquisitionFileStatusTypeCodeNavigation)
.Include(f => f.AcquisitionFundingTypeCodeNavigation)
.Include(ph => ph.AcqPhysFileStatusTypeCodeNavigation)
.Include(t => t.AcquisitionTypeCodeNavigation)
.Include(tm => tm.PimsAcquisitionFilePeople)
.ThenInclude(c => c.Person)
.Include(ow => ow.PimsAcquisitionOwners)
.Include(fp => fp.PimsPropertyAcquisitionFiles)
.ThenInclude(prop => prop.Property)
.ThenInclude(ad => ad.Address)
.ThenInclude(x => x.ProvinceState)
.Include(fp => fp.PimsPropertyAcquisitionFiles)
.ThenInclude(prop => prop.Property)
.ThenInclude(ad => ad.Address)
.ThenInclude(x => x.Country)
.Where(predicate);
.Include(r => r.RegionCodeNavigation)
.Include(p => p.Project)
.Include(s => s.AcquisitionFileStatusTypeCodeNavigation)
.Include(f => f.AcquisitionFundingTypeCodeNavigation)
.Include(ph => ph.AcqPhysFileStatusTypeCodeNavigation)
.Include(t => t.AcquisitionTypeCodeNavigation)
.Include(tm => tm.PimsAcquisitionFilePeople)
.ThenInclude(c => c.Person)
.Include(tm => tm.PimsAcquisitionFilePeople)
.ThenInclude(c => c.AcqFlPersonProfileTypeCodeNavigation)
.Include(ow => ow.PimsAcquisitionOwners)
.Include(fp => fp.PimsPropertyAcquisitionFiles)
.ThenInclude(prop => prop.Property)
.ThenInclude(ad => ad.Address)
.ThenInclude(x => x.ProvinceState)
.Include(fp => fp.PimsPropertyAcquisitionFiles)
.ThenInclude(prop => prop.Property)
.ThenInclude(ad => ad.Address)
.ThenInclude(x => x.Country)
.Where(predicate);

query = (filter.Sort?.Any() == true) ? query.OrderByProperty(filter.Sort) : query.OrderBy(acq => acq.AcquisitionFileId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ namespace Pims.Dal.Repositories
{
public interface IAcquisitionFileRepository : IRepository
{
Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter, HashSet<short> regions, long? filterPersonId = null);
Paged<PimsAcquisitionFile> GetPage(AcquisitionFilter filter, HashSet<short> regions, long? contractorPersonId = null);

PimsAcquisitionFile GetById(long id);

List<PimsAcquisitionOwner> GetOwnersByAcquisitionFileId(long acquisitionFileId);

List<PimsAcquisitionFilePerson> GetTeamMembers(HashSet<short> regions);
List<PimsAcquisitionFilePerson> GetTeamMembers(HashSet<short> regions, long? contractorPersonId = null);

PimsAcquisitionFile Add(PimsAcquisitionFile acquisitionFile);

Expand All @@ -24,6 +24,6 @@ public interface IAcquisitionFileRepository : IRepository

List<PimsAcquisitionFile> GetByProductId(long productId);

List<PimsAcquisitionFile> GetAcquisitionFileExport(AcquisitionFilter filter, HashSet<short> regions, long? filterPersonId = null);
List<PimsAcquisitionFile> GetAcquisitionFileExport(AcquisitionFilter filter, HashSet<short> regions, long? contractorPersonId = null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1945,7 +1945,7 @@ public void GetTeamMembers_NoPermission()
var acqFile = EntityHelper.CreateAcquisitionFile();

var repository = _helper.GetService<Mock<IAcquisitionFileRepository>>();
repository.Setup(x => x.GetTeamMembers(It.IsAny<HashSet<short>>())).Returns(new List<PimsAcquisitionFilePerson>());
repository.Setup(x => x.GetTeamMembers(It.IsAny<HashSet<short>>(), null)).Returns(new List<PimsAcquisitionFilePerson>());

// Act
Action act = () => service.GetOwners(1);
Expand All @@ -1964,7 +1964,7 @@ public void GetTeamMembers_Success()

var repository = _helper.GetService<Mock<IAcquisitionFileRepository>>();
var userRepository = _helper.GetService<Mock<IUserRepository>>();
repository.Setup(x => x.GetTeamMembers(It.IsAny<HashSet<short>>())).Returns(new List<PimsAcquisitionFilePerson>());
repository.Setup(x => x.GetTeamMembers(It.IsAny<HashSet<short>>(), It.IsAny<long>())).Returns(new List<PimsAcquisitionFilePerson>());

var contractorUser = EntityHelper.CreateUser(1, Guid.NewGuid(), username: "Test", isContractor: true);
userRepository.Setup(x => x.GetUserInfoByKeycloakUserId(It.IsAny<Guid>())).Returns(contractorUser);
Expand All @@ -1973,7 +1973,7 @@ public void GetTeamMembers_Success()
var teamMembers = service.GetTeamMembers();

// Assert
repository.Verify(x => x.GetTeamMembers(It.IsAny<HashSet<short>>()), Times.Once);
repository.Verify(x => x.GetTeamMembers(It.IsAny<HashSet<short>>(), contractorUser.PersonId), Times.Once);
}

#endregion
Expand Down
2 changes: 1 addition & 1 deletion source/frontend/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "frontend",
"version": "4.0.0-63.8",
"version": "4.0.0-63.9",
"private": true,
"dependencies": {
"@bcgov/bc-sans": "1.0.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@ import { mockLookups } from '@/mocks/lookups.mock';
import { lookupCodesSlice } from '@/store/slices/lookupCodes';
import { act, fillInput, render, RenderOptions, waitFor } from '@/utils/test-utils';

import { IAcquisitionFilter } from '../interfaces';
import { AcquisitionFilter, defaultAcquisitionFilter } from './AcquisitionFilter';
import { AcquisitionFilterModel, Api_AcquisitionFilter } from '../interfaces';
import { AcquisitionFilter } from './AcquisitionFilter';

jest.mock('@react-keycloak/web');

const setFilter = jest.fn();

// render component under test
const setup = (renderOptions: RenderOptions = {}) => {
const utils = render(<AcquisitionFilter setFilter={setFilter} />, {
const utils = render(<AcquisitionFilter setFilter={setFilter} aquisitionTeam={[]} />, {
store: {
[lookupCodesSlice.name]: { lookupCodes: mockLookups },
},
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('Acquisition Filter', () => {
const { resetButton } = setup();
await act(async () => userEvent.click(resetButton));

expect(setFilter).toHaveBeenCalledWith(defaultAcquisitionFilter);
expect(setFilter).toHaveBeenCalledWith(new AcquisitionFilterModel().toApi());
});

it('searches by acquisition file status', async () => {
Expand Down Expand Up @@ -97,7 +97,7 @@ describe('Acquisition Filter', () => {
await act(async () => userEvent.click(resetButton));

expect(setFilter).toHaveBeenCalledWith(
expect.objectContaining<IAcquisitionFilter>(defaultAcquisitionFilter),
expect.objectContaining<Api_AcquisitionFilter>(new AcquisitionFilterModel().toApi()),
);
});
});
Loading

0 comments on commit ac58fea

Please sign in to comment.