From 3c7c5115ad7142d1dc57d6770ae8da6ee7ac05e8 Mon Sep 17 00:00:00 2001 From: peggy-quartech Date: Thu, 5 Dec 2024 16:31:40 -0800 Subject: [PATCH] security worker licence search done --- src/Spd.Manager.Licence/LicenceManager.cs | 46 ++++++++++++------- .../Licence/LicenceRepository.cs | 9 ++-- 2 files changed, 33 insertions(+), 22 deletions(-) diff --git a/src/Spd.Manager.Licence/LicenceManager.cs b/src/Spd.Manager.Licence/LicenceManager.cs index 3ed914525..106c84504 100644 --- a/src/Spd.Manager.Licence/LicenceManager.cs +++ b/src/Spd.Manager.Licence/LicenceManager.cs @@ -146,36 +146,48 @@ public async Task> Handle(LicenceListQuery que return new FileResponse(); } - public Task> Handle(LicenceListSearch search, CancellationToken cancellationToken) + public async Task> 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(); - } - + 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>(response.Items.Where(r => r.LicenceStatusCode == LicenceStatusEnum.Active || r.LicenceStatusCode == LicenceStatusEnum.Expired || r.LicenceStatusCode == LicenceStatusEnum.Preview)); + return _mapper.Map>(result); } diff --git a/src/Spd.Resource.Repository/Licence/LicenceRepository.cs b/src/Spd.Resource.Repository/Licence/LicenceRepository.cs index 5b0a88182..fa4d5ea78 100644 --- a/src/Spd.Resource.Repository/Licence/LicenceRepository.cs +++ b/src/Spd.Resource.Repository/Licence/LicenceRepository.cs @@ -43,11 +43,6 @@ public LicenceRepository(IDynamicsContextFactory ctx, public async Task 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 lics = _context.spd_licences .Expand(i => i.spd_spd_licence_spd_caselicencecategory_licenceid) .Expand(i => i.spd_LicenceHolder_contact) @@ -94,6 +89,10 @@ public async Task 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>(lics)