Skip to content

Commit

Permalink
security worker licence search done
Browse files Browse the repository at this point in the history
  • Loading branch information
peggy-quartech committed Dec 6, 2024
1 parent de3575d commit 3c7c511
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 22 deletions.
46 changes: 29 additions & 17 deletions src/Spd.Manager.Licence/LicenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,36 +146,48 @@ public async Task<IEnumerable<LicenceBasicResponse>> Handle(LicenceListQuery que
return new FileResponse();
}

public Task<IEnumerable<LicenceBasicResponse>> Handle(LicenceListSearch search, CancellationToken cancellationToken)
public async Task<IEnumerable<LicenceBasicResponse>> Handle(LicenceListSearch search, CancellationToken cancellationToken)
{
LicenceListResp response = new LicenceListResp();
if (search.ServiceTypeCode == ServiceTypeCode.SecurityWorkerLicence)
{
if (string.IsNullOrWhiteSpace(search.LicenceNumber) && string.IsNullOrWhiteSpace(search.FirstName) && string.IsNullOrWhiteSpace(search.LastName))
throw new ApiException(HttpStatusCode.BadRequest, "Not enough parameter");

if ((!string.IsNullOrWhiteSpace(search.LicenceNumber) && !string.IsNullOrWhiteSpace(search.FirstName))
|| (!string.IsNullOrWhiteSpace(search.LicenceNumber) && !string.IsNullOrWhiteSpace(search.LastName)))
throw new ApiException(HttpStatusCode.BadRequest, "Cannot input name and licence number together.");
response = await _licenceRepository.QueryAsync(
new LicenceQry
{
LicenceNumber = search.LicenceNumber,
FirstName = search.FirstName,
LastName = search.LastName,
Type = ServiceTypeEnum.SecurityWorkerLicence,
IncludeInactive = true
}, cancellationToken);
}

if (search.ServiceTypeCode == ServiceTypeCode.SecurityBusinessLicence)
{
if (string.IsNullOrWhiteSpace(search.LicenceNumber) && string.IsNullOrWhiteSpace(search.BizName))
throw new ApiException(HttpStatusCode.BadRequest, "Not enough parameter");

response = await _licenceRepository.QueryAsync(
new LicenceQry
{
LicenceNumber = search.LicenceNumber,
BizName = search.BizName,
IncludeInactive = true,
Type = ServiceTypeEnum.SecurityBusinessLicence,
}, cancellationToken);
}

var response = await _licenceRepository.QueryAsync(
new LicenceQry
{
ContactId = query.ApplicantId,
AccountId = query.BizId,
IncludeInactive = true
}, cancellationToken);

if (!response.Items.Any())
{
_logger.LogDebug("No licence found.");
return Array.Empty<LicenceBasicResponse>();
}

var result = response.Items.Where(r => r.LicenceStatusCode == LicenceStatusEnum.Active || r.LicenceStatusCode == LicenceStatusEnum.Expired || r.LicenceStatusCode == LicenceStatusEnum.Preview)
.GroupBy(r => r.LicenceNumber)
.Select(g => g.OrderByDescending(i => i.CreatedOn).FirstOrDefault())
.ToList();
//only return expired and active ones
return _mapper.Map<IEnumerable<LicenceBasicResponse>>(response.Items.Where(r => r.LicenceStatusCode == LicenceStatusEnum.Active || r.LicenceStatusCode == LicenceStatusEnum.Expired || r.LicenceStatusCode == LicenceStatusEnum.Preview));
return _mapper.Map<IEnumerable<LicenceBasicResponse>>(result);

}

Expand Down
9 changes: 4 additions & 5 deletions src/Spd.Resource.Repository/Licence/LicenceRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ public LicenceRepository(IDynamicsContextFactory ctx,

public async Task<LicenceListResp> QueryAsync(LicenceQry qry, CancellationToken ct)
{
if (qry.LicenceNumber == null && qry.AccountId == null && qry.ContactId == null && qry.LicenceId == null)
{
throw new ArgumentException("at least need 1 parameter to do licence query.");
}

IQueryable<spd_licence> lics = _context.spd_licences
.Expand(i => i.spd_spd_licence_spd_caselicencecategory_licenceid)
.Expand(i => i.spd_LicenceHolder_contact)
Expand Down Expand Up @@ -94,6 +89,10 @@ public async Task<LicenceListResp> QueryAsync(LicenceQry qry, CancellationToken
{
lics = lics.Where(a => a.spd_LicenceHolder_contact.firstname == qry.FirstName && a.spd_LicenceHolder_contact.lastname == qry.LastName);
}
if (qry.BizName != null)
{
lics = lics.Where(a => a.spd_LicenceHolder_account.name == qry.BizName || a.spd_LicenceHolder_account.spd_organizationlegalname == qry.BizName);
}
return new LicenceListResp()
{
Items = _mapper.Map<IEnumerable<LicenceResp>>(lics)
Expand Down

0 comments on commit 3c7c511

Please sign in to comment.