@@ -69,18 +69,11 @@ class GetAllEnvironmentsUseCase {
6969 const { filter, page = { } } = query ;
7070 const { limit = ApiConfig . pagination . limit , offset = 0 } = page ;
7171
72- /**
73- * Prepare a query builder with ordering, limit and offset
74- *
75- * @return {QueryBuilder } the created query builder
76- */
77- const prepareQueryBuilder = ( ) => dataSource . createQueryBuilder ( )
72+ const queryBuilder = dataSource . createQueryBuilder ( )
7873 . orderBy ( 'updatedAt' , 'desc' )
7974 . limit ( limit )
8075 . offset ( offset ) ;
8176
82- const fetchQueryBuilder = prepareQueryBuilder ( ) ;
83-
8477 if ( filter ) {
8578 const {
8679 ids : idsExpression ,
@@ -90,38 +83,36 @@ class GetAllEnvironmentsUseCase {
9083 created,
9184 } = filter ;
9285
93- const filterQueryBuilder = prepareQueryBuilder ( ) ;
94-
9586 if ( created ) {
9687 const from = created . from !== undefined ? created . from : 0 ;
9788 const to = created . to !== undefined ? created . to : Date . now ( ) ;
98- filterQueryBuilder . where ( 'createdAt' ) . between ( from , to ) ;
89+ queryBuilder . where ( 'createdAt' ) . between ( from , to ) ;
9990 }
10091
10192 if ( idsExpression ) {
10293 const filters = idsExpression . split ( ',' ) . map ( ( id ) => id . trim ( ) ) ;
10394
10495 // Filter should be like with only one filter
10596 if ( filters . length === 1 ) {
106- filterQueryBuilder . where ( 'id' ) . substring ( filters [ 0 ] ) ;
97+ queryBuilder . where ( 'id' ) . substring ( filters [ 0 ] ) ;
10798 }
10899
109100 // Filters should be exact with more than one filter
110101 if ( filters . length > 1 ) {
111- filterQueryBuilder . andWhere ( { id : { [ Op . in ] : filters } } ) ;
102+ queryBuilder . andWhere ( { id : { [ Op . in ] : filters } } ) ;
112103 }
113104 }
114105
115106 if ( currentStatusExpression ) {
116107 const filters = currentStatusExpression . split ( ',' ) . map ( ( status ) => status . trim ( ) ) ;
117108
118109 // Filter the environments by current status using the subquery
119- filterQueryBuilder . literalWhere (
110+ queryBuilder . literalWhere (
120111 `${ ENVIRONMENT_LATEST_HISTORY_ITEM_SUBQUERY } IN (:filters)` ,
121112 { filters } ,
122113 ) ;
123114
124- filterQueryBuilder . includeAttribute ( {
115+ queryBuilder . includeAttribute ( {
125116 query : ENVIRONMENT_LATEST_HISTORY_ITEM_SUBQUERY ,
126117 alias : 'currentStatus' ,
127118 } ) ;
@@ -157,7 +148,7 @@ class GetAllEnvironmentsUseCase {
157148 * Use OR condition to match subsequences ending with either DESTROYED or DONE
158149 * Filter the environments by using LIKE for subsequence matching
159150 */
160- filterQueryBuilder . literalWhere (
151+ queryBuilder . literalWhere (
161152 `(${ ENVIRONMENT_STATUS_HISTORY_SUBQUERY } LIKE :statusFiltersWithDestroyed OR ` +
162153 `${ ENVIRONMENT_STATUS_HISTORY_SUBQUERY } LIKE :statusFiltersWithDone)` ,
163154 {
@@ -166,17 +157,17 @@ class GetAllEnvironmentsUseCase {
166157 } ,
167158 ) ;
168159
169- filterQueryBuilder . includeAttribute ( {
160+ queryBuilder . includeAttribute ( {
170161 query : ENVIRONMENT_STATUS_HISTORY_SUBQUERY ,
171162 alias : 'statusHistory' ,
172163 } ) ;
173164 } else {
174- filterQueryBuilder . literalWhere (
165+ queryBuilder . literalWhere (
175166 `${ ENVIRONMENT_STATUS_HISTORY_SUBQUERY } LIKE :statusFilters` ,
176167 { statusFilters : `%${ statusFilters . join ( ',' ) } %` } ,
177168 ) ;
178169
179- filterQueryBuilder . includeAttribute ( {
170+ queryBuilder . includeAttribute ( {
180171 query : ENVIRONMENT_STATUS_HISTORY_SUBQUERY ,
181172 alias : 'statusHistory' ,
182173 } ) ;
@@ -190,30 +181,20 @@ class GetAllEnvironmentsUseCase {
190181
191182 // Check that the final run numbers list contains at least one valid run number
192183 if ( finalRunNumberList . length > 0 ) {
193- filterQueryBuilder . include ( {
184+ queryBuilder . include ( {
194185 association : 'runs' ,
195186 where : {
196187 // Filter should be like with only one filter and exact with more than one filter
197188 runNumber : { [ finalRunNumberList . length === 1 ? Op . substring : Op . in ] : finalRunNumberList } ,
198189 } ,
199190 } ) ;
200191 }
201- } ;
202-
203- const filteredEnvironmentsIds = ( await EnvironmentRepository . findAll ( filterQueryBuilder ) ) . map ( ( { id } ) => id ) ;
204- // If no environments match the filter, return an empty result
205- if ( filteredEnvironmentsIds . length === 0 ) {
206- return {
207- count : 0 ,
208- environments : [ ] ,
209- } ;
210192 }
211- fetchQueryBuilder . where ( 'id' ) . oneOf ( filteredEnvironmentsIds ) ;
212193 }
213194
214- fetchQueryBuilder . include ( { association : 'runs' } ) ;
215- fetchQueryBuilder . include ( { association : 'historyItems' } ) ;
216- const { count, rows } = await EnvironmentRepository . findAndCountAll ( fetchQueryBuilder ) ;
195+ queryBuilder . include ( { association : 'runs' } ) ;
196+ queryBuilder . include ( { association : 'historyItems' } ) ;
197+ const { count, rows } = await EnvironmentRepository . findAndCountAll ( queryBuilder ) ;
217198 return {
218199 count,
219200 environments : rows . map ( ( environment ) => environmentAdapter . toEntity ( environment ) ) ,
0 commit comments