-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #438 from bcgov/tasks/ecer-2758
ECER-2758: Renewal Validation Infra
- Loading branch information
Showing
24 changed files
with
815 additions
and
475 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/ECER.Engines.Validation/Applications/ApplicationRenewalValidationEngine.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
using ECER.Managers.Registry.Contract.Applications; | ||
using ECER.Resources.Documents.Certifications; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace ECER.Engines.Validation.Applications; | ||
|
||
internal sealed partial class ApplicationRenewalValidationEngine : IApplicationValidationEngine | ||
{ | ||
private ICertificationRepository _certificateRepository; | ||
|
||
public ApplicationRenewalValidationEngine(ICertificationRepository certificationRepository) | ||
{ | ||
_certificateRepository = certificationRepository; | ||
} | ||
|
||
public async Task<ValidationResults> Validate(Application application) | ||
{ | ||
await Task.CompletedTask; | ||
var validationErrors = new List<string>(); | ||
|
||
if (application.CertificationTypes.Contains(CertificationType.EceAssistant)) | ||
{ | ||
validationErrors = await EceAssistant(application); | ||
} | ||
else if (application.CertificationTypes.Contains(CertificationType.OneYear)) | ||
{ | ||
validationErrors = await OneYear(application); | ||
} | ||
else if (application.CertificationTypes.Contains(CertificationType.FiveYears)) | ||
{ | ||
validationErrors = await FiveYears(application); | ||
} | ||
return new ValidationResults(validationErrors); | ||
} | ||
|
||
private enum CertificateStatus | ||
{ | ||
Active, | ||
ExpiredLessThanFiveYearsAgo, | ||
ExpiredMoreThanFiveYearsAgo, | ||
NoCertificateFound | ||
} | ||
|
||
private async Task<CertificateStatus> GetCertificateStatus(string applicantId) | ||
{ | ||
try | ||
{ | ||
var expiryDate = await getLastCertificateExpiryDate(applicantId); | ||
var now = DateTime.Now; | ||
|
||
if (expiryDate > now) | ||
{ | ||
return CertificateStatus.Active; | ||
} | ||
else if (expiryDate <= now && expiryDate > now.AddYears(-5)) | ||
{ | ||
return CertificateStatus.ExpiredLessThanFiveYearsAgo; | ||
} | ||
else | ||
{ | ||
return CertificateStatus.ExpiredMoreThanFiveYearsAgo; | ||
} | ||
} | ||
catch (InvalidOperationException) | ||
{ | ||
return CertificateStatus.NoCertificateFound; | ||
} | ||
} | ||
|
||
private async Task<DateTime> getLastCertificateExpiryDate(string applicantId) | ||
{ | ||
var certificates = await _certificateRepository.Query(new UserCertificationQuery() { ByApplicantId = applicantId }); | ||
var lastCertificate = certificates.OrderByDescending(d => d.ExpiryDate).FirstOrDefault(); | ||
if (lastCertificate == null || lastCertificate.ExpiryDate == null) | ||
{ | ||
throw new InvalidOperationException("Certificate or datetime is null"); | ||
} | ||
return (DateTime)lastCertificate.ExpiryDate; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/ECER.Engines.Validation/Applications/ApplicationValidationEngineResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using ECER.Managers.Registry.Contract.Applications; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace ECER.Engines.Validation.Applications; | ||
|
||
public interface IApplicationValidationEngineResolver | ||
{ | ||
IApplicationValidationEngine Resolve(ApplicationTypes appType); | ||
} | ||
|
||
public class ApplicationValidationEngineResolver : IApplicationValidationEngineResolver | ||
{ | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public ApplicationValidationEngineResolver(IServiceProvider serviceProvider) | ||
{ | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public IApplicationValidationEngine Resolve(ApplicationTypes appType) | ||
{ | ||
return appType switch | ||
{ | ||
ApplicationTypes.New => _serviceProvider.GetRequiredService<ApplicationSubmissionValidationEngine>(), | ||
ApplicationTypes.Renewal => _serviceProvider.GetRequiredService<ApplicationRenewalValidationEngine>(), | ||
_ => throw new ArgumentOutOfRangeException(nameof(appType), appType, null) | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/ECER.Engines.Validation/Applications/RenewalValidationConditions/EceAssistant.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using ECER.Managers.Registry.Contract.Applications; | ||
|
||
namespace ECER.Engines.Validation.Applications; | ||
|
||
internal sealed partial class ApplicationRenewalValidationEngine | ||
{ | ||
private async Task<List<string>> EceAssistant(Application application) | ||
{ | ||
await Task.CompletedTask; | ||
var validationErrors = new List<string>(); | ||
switch (await GetCertificateStatus(application.RegistrantId)) | ||
{ | ||
case CertificateStatus.Active: | ||
break; | ||
|
||
case CertificateStatus.ExpiredLessThanFiveYearsAgo: | ||
break; | ||
|
||
case CertificateStatus.ExpiredMoreThanFiveYearsAgo: | ||
break; | ||
|
||
case CertificateStatus.NoCertificateFound: | ||
break; | ||
} | ||
return validationErrors; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/ECER.Engines.Validation/Applications/RenewalValidationConditions/FiveYears.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
using ECER.Managers.Registry.Contract.Applications; | ||
|
||
namespace ECER.Engines.Validation.Applications; | ||
|
||
internal sealed partial class ApplicationRenewalValidationEngine | ||
{ | ||
private async Task<List<string>> FiveYears(Application application) | ||
{ | ||
await Task.CompletedTask; | ||
var validationErrors = new List<string>(); | ||
switch (await GetCertificateStatus(application.RegistrantId)) | ||
{ | ||
case CertificateStatus.Active: | ||
break; | ||
|
||
case CertificateStatus.ExpiredLessThanFiveYearsAgo: | ||
break; | ||
|
||
case CertificateStatus.ExpiredMoreThanFiveYearsAgo: | ||
break; | ||
|
||
case CertificateStatus.NoCertificateFound: | ||
break; | ||
} | ||
return validationErrors; | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/ECER.Engines.Validation/Applications/RenewalValidationConditions/OneYear.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using ECER.Managers.Registry.Contract.Applications; | ||
|
||
namespace ECER.Engines.Validation.Applications; | ||
|
||
internal sealed partial class ApplicationRenewalValidationEngine | ||
{ | ||
private async Task<List<string>> OneYear(Application application) | ||
{ | ||
await Task.CompletedTask; | ||
var validationErrors = new List<string>(); | ||
switch (await GetCertificateStatus(application.RegistrantId)) | ||
{ | ||
case CertificateStatus.Active: | ||
|
||
// each application should contain explanation letter | ||
if (string.IsNullOrEmpty(application.ExplanationLetter)) | ||
{ | ||
validationErrors.Add("the application does not have explanation letter"); | ||
} | ||
// each application should contain at least one character reference | ||
if (!application.CharacterReferences.Any()) | ||
{ | ||
validationErrors.Add("the application does not have any character references"); | ||
} | ||
break; | ||
|
||
case CertificateStatus.ExpiredLessThanFiveYearsAgo: | ||
break; | ||
|
||
case CertificateStatus.ExpiredMoreThanFiveYearsAgo: | ||
break; | ||
|
||
case CertificateStatus.NoCertificateFound: | ||
break; | ||
} | ||
return validationErrors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.