From 09dd02044741bfe6b340d2e5d85c6580ac631e1d Mon Sep 17 00:00:00 2001 From: soletsdev Date: Tue, 26 Mar 2024 10:25:08 -0700 Subject: [PATCH] adding additional validation to the repository level for submit + cancel applications --- .../Applications/ApplicationRepository.cs | 4 ++-- .../Applications/ApplicationRepositoryTests.cs | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/ECER.Resources.Documents/Applications/ApplicationRepository.cs b/src/ECER.Resources.Documents/Applications/ApplicationRepository.cs index 5d91922fb..055681161 100644 --- a/src/ECER.Resources.Documents/Applications/ApplicationRepository.cs +++ b/src/ECER.Resources.Documents/Applications/ApplicationRepository.cs @@ -79,7 +79,7 @@ public async Task SaveDraft(Application application, CancellationToken c public async Task Submit(string applicationId, CancellationToken cancellationToken) { await Task.CompletedTask; - var application = context.ecer_ApplicationSet.FirstOrDefault(d => d.ecer_ApplicationId == Guid.Parse(applicationId)); + var application = context.ecer_ApplicationSet.FirstOrDefault(d => d.ecer_ApplicationId == Guid.Parse(applicationId) && d.StatusCode == ecer_Application_StatusCode.Draft); if (application == null) throw new InvalidOperationException($"Application '{applicationId}' not found"); application.StatusCode = ecer_Application_StatusCode.Submitted; @@ -198,7 +198,7 @@ public async Task Cancel(string applicationId, CancellationToken cancell { await Task.CompletedTask; var application = context.ecer_ApplicationSet.FirstOrDefault( - d => d.ecer_ApplicationId == Guid.Parse(applicationId) + d => d.ecer_ApplicationId == Guid.Parse(applicationId) && d.StatusCode == ecer_Application_StatusCode.Draft ); if (application == null) throw new InvalidOperationException($"Application '{applicationId}' not found"); application.StatusCode = ecer_Application_StatusCode.Cancelled; diff --git a/src/ECER.Tests/Integration/Resources/Applications/ApplicationRepositoryTests.cs b/src/ECER.Tests/Integration/Resources/Applications/ApplicationRepositoryTests.cs index c2830cd5a..dc310c508 100644 --- a/src/ECER.Tests/Integration/Resources/Applications/ApplicationRepositoryTests.cs +++ b/src/ECER.Tests/Integration/Resources/Applications/ApplicationRepositoryTests.cs @@ -349,7 +349,18 @@ public async Task SubmitApplication_QueryShouldReturnStatusSubmitted() } [Fact] - public async Task CancelApplication_QueryShouldnotReturnResults() + public async Task SubmitApplicationNotInDraft_ShouldThrowInvalidOperationException() + { + var applicantId = Fixture.AuthenticatedBcscUserId; + var application = new Application(null, applicantId, new[] { CertificationType.OneYear }) { }; + application.Status = ApplicationStatus.Submitted; + var savedApplicationId = await repository.SaveDraft(application, CancellationToken.None); + + await Assert.ThrowsAsync(async () => await repository.Submit(savedApplicationId, CancellationToken.None)); + } + + [Fact] + public async Task CancelApplication_QueryShouldnotReturnResults_ThenTryToSubmitAgain_ShouldThrowInvalidOperationException() { var applicantId = Fixture.AuthenticatedBcscUserId2; var application = new Application(null, applicantId, new[] { CertificationType.OneYear }); @@ -358,6 +369,8 @@ public async Task CancelApplication_QueryShouldnotReturnResults() await repository.Cancel(savedApplicationId, CancellationToken.None); (await repository.Query(new ApplicationQuery { ById = savedApplicationId })).ShouldBeEmpty(); + + await Assert.ThrowsAsync(async () => await repository.Cancel(savedApplicationId, CancellationToken.None)); } private CharacterReference CreateCharacterReference()