diff --git a/src/domain-services/flows/flow-search-service.ts b/src/domain-services/flows/flow-search-service.ts index d84223a6..794207e8 100644 --- a/src/domain-services/flows/flow-search-service.ts +++ b/src/domain-services/flows/flow-search-service.ts @@ -751,6 +751,53 @@ export class FlowSearchService { flowFilters.activeStatus = true; } + if (flowObjectFilters) { + const usageYearFilter = flowObjectFilters.find( + (filter) => filter.objectType === 'usageYear' + ); + + if (!usageYearFilter) { + // Find the flowObjectFilters since 2021 until currentYear + let startYear = 2021; + const currentYear = new Date().getFullYear(); + + const usageYearsArrayFilter: string[] = []; + while (startYear <= currentYear) { + usageYearsArrayFilter.push(startYear.toString()); + startYear++; + } + const usageYears = await models.usageYear.find({ + where: { + year: { + [Op.IN]: usageYearsArrayFilter, + }, + }, + }); + + for (const usageYear of usageYears) { + // Map the usageYear filters to the flowObjectFilters + const sourceUsageYearFilter: FlowObjectFilters = { + objectType: 'usageYear', + direction: 'source', + objectID: usageYear.id.valueOf(), + inclusive: true, + }; + + const destinationUsageYearFilter: FlowObjectFilters = { + objectType: 'usageYear', + direction: 'destination', + objectID: usageYear.id.valueOf(), + inclusive: true, + }; + + flowObjectFilters.push( + sourceUsageYearFilter, + destinationUsageYearFilter + ); + } + } + } + // This filter MUST apply always flowFilters.restricted = false; diff --git a/src/domain-services/flows/strategy/impl/search-flow-by-filters-strategy-impl.ts b/src/domain-services/flows/strategy/impl/search-flow-by-filters-strategy-impl.ts index 816ced47..caca84bd 100644 --- a/src/domain-services/flows/strategy/impl/search-flow-by-filters-strategy-impl.ts +++ b/src/domain-services/flows/strategy/impl/search-flow-by-filters-strategy-impl.ts @@ -247,22 +247,18 @@ export class SearchFlowByFiltersStrategy implements FlowSearchStrategy { // We need to paginate over the searchConditions // Then collect the flows - // 1. Generate an array of arrays with the chunkSize - const chunkedFlows = []; + // 1. Generate an array with the chunkSize for (let i = 0; i < reducedFlows.length; i += chunkSize) { - chunkedFlows.push(reducedFlows.slice(i, i + chunkSize)); - } - - // 2. Iterate over the array of arrays to generate the searchConditions - // And collect the flows - for (const chunk of chunkedFlows) { + const chunk = reducedFlows.slice(i, i + chunkSize); + // 2. Generate the searchConditions + // And collect the flows const chunkSearchConditions = this.buildConditions(chunk, flowFilters); const flowsChunk = await this.flowService.getFlows({ models, conditions: chunkSearchConditions, orderBy: orderByForFlow, }); - flows = [...flows, ...flowsChunk]; + flows.push(...flowsChunk); } } else { // Once the list of elements is reduced, we need to build the conditions