@@ -47,7 +47,6 @@ public class ApplicationHandlers(
4747 if ( request . Application . Id == null )
4848 {
4949 // Check if a draft application already exists for the current user
50-
5150 var applications = await applicationRepository . Query ( new ApplicationQuery
5251 {
5352 ByApplicantId = request . Application . RegistrantId ,
@@ -139,18 +138,12 @@ public async Task<ApplicationSubmissionResult> Handle(SubmitApplicationCommand r
139138 {
140139 ArgumentNullException . ThrowIfNull ( request ) ;
141140
142- var draftApplications = await applicationRepository . Query ( new ApplicationQuery
143- {
144- ById = request . applicationId ,
145- ByApplicantId = request . userId ,
146- ByStatus = [ Resources . Documents . Applications . ApplicationStatus . Draft ]
147- } , cancellationToken ) ;
148-
149- var submittedApplications = await applicationRepository . Query ( new ApplicationQuery
141+ var applications = await applicationRepository . Query ( new ApplicationQuery
150142 {
151143 ByApplicantId = request . userId ,
152144 ByStatus = new List < Resources . Documents . Applications . ApplicationStatus >
153145 {
146+ Resources . Documents . Applications . ApplicationStatus . Draft ,
154147 Resources . Documents . Applications . ApplicationStatus . Submitted ,
155148 Resources . Documents . Applications . ApplicationStatus . Ready ,
156149 Resources . Documents . Applications . ApplicationStatus . Escalated ,
@@ -161,15 +154,14 @@ public async Task<ApplicationSubmissionResult> Handle(SubmitApplicationCommand r
161154 }
162155 } , cancellationToken ) ;
163156
164- var draftApplicationResults = new ApplicationsQueryResults ( mapper . Map < IEnumerable < Contract . Applications . Application > > ( draftApplications ) ! ) ;
165- var submittedApplicationResults = new ApplicationsQueryResults ( mapper . Map < IEnumerable < Contract . Applications . Application > > ( submittedApplications ) ! ) ;
157+ var draftApplication = mapper . Map < Contract . Applications . Application > ( applications . SingleOrDefault ( dst =>
158+ dst . Id == request . applicationId && dst . Status == Resources . Documents . Applications . ApplicationStatus . Draft ) ) ;
159+ var submittedApplications = mapper . Map < IEnumerable < Contract . Applications . Application > > ( applications . Where ( dst => dst . Status != Resources . Documents . Applications . ApplicationStatus . Draft ) ) ;
166160
167- if ( ! draftApplicationResults . Items . Any ( ) )
161+ if ( draftApplication == null )
168162 {
169163 return new ApplicationSubmissionResult ( ) { Application = null , Error = SubmissionError . DraftApplicationNotFound , ValidationErrors = new List < string > ( ) { "draft application does not exist" } } ;
170164 }
171- var draftApplication = draftApplicationResults . Items . First ( ) ;
172-
173165 var validationEngine = validationResolver ? . Resolve ( draftApplication . ApplicationType ) ;
174166 var validationErrors = await validationEngine ? . Validate ( draftApplication ) ! ;
175167 if ( validationErrors . ValidationErrors . Any ( ) )
@@ -187,7 +179,7 @@ public async Task<ApplicationSubmissionResult> Handle(SubmitApplicationCommand r
187179 return new ApplicationSubmissionResult ( ) { Application = null , Error = SubmissionError . DraftApplicationNotFound , ValidationErrors = new List < string > ( ) { "draft application does not exist" } } ;
188180 }
189181
190- if ( submittedApplicationResults . Items . Any ( ) )
182+ if ( submittedApplications . Any ( ) )
191183 {
192184 return new ApplicationSubmissionResult ( ) { Application = null , Error = SubmissionError . SubmittedApplicationAlreadyExists , ValidationErrors = new List < string > ( ) { "submitted application already exists" } } ;
193185 }
0 commit comments