From 216113ce82ee54194100b0a73a76ef65bab02163 Mon Sep 17 00:00:00 2001 From: manelcecs Date: Thu, 16 Nov 2023 13:03:05 +0100 Subject: [PATCH] Fix incorrect query when multiple flowObject filter --- .../flows/flow-search-service.ts | 7 ++- .../impl/flow-object-conditions-strategy.ts | 54 +++++++++++++------ 2 files changed, 41 insertions(+), 20 deletions(-) diff --git a/src/domain-services/flows/flow-search-service.ts b/src/domain-services/flows/flow-search-service.ts index 6a19a4f1..6a8fb4c8 100644 --- a/src/domain-services/flows/flow-search-service.ts +++ b/src/domain-services/flows/flow-search-service.ts @@ -262,9 +262,10 @@ export class FlowSearchService { flowObjectFilters: FlowObjectFilters[] ): { strategy: FlowSearchStrategy; conditions: any } { let conditions = {}; + if ( (!flowFilters && - (!flowObjectFilters ?? flowObjectFilters.length === 0)) ?? + (!flowObjectFilters || flowObjectFilters.length === 0)) ?? (flowFilters && (!flowObjectFilters ?? flowObjectFilters.length === 0)) ) { const flowConditions = this.prepareFlowConditions(flowFilters); @@ -298,9 +299,7 @@ export class FlowSearchService { }; } - throw new Error( - 'Invalid combination of flowFilters and flowObjectFilters - temp: only provide flowFilters' - ); + throw new Error('Invalid combination of flowFilters and flowObjectFilters'); } private buildConditionsMap(flowConditions: any, flowObjectConditions: any) { diff --git a/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy.ts b/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy.ts index 3ccb19bd..a9d98507 100644 --- a/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy.ts +++ b/src/domain-services/flows/strategy/impl/flow-object-conditions-strategy.ts @@ -1,11 +1,12 @@ -import { Database } from '@unocha/hpc-api-core/src/db'; +import { type Database } from '@unocha/hpc-api-core/src/db'; +import { type FlowId } from '@unocha/hpc-api-core/src/db/models/flow'; import { Op } from '@unocha/hpc-api-core/src/db/util/conditions'; import { Service } from 'typedi'; import { FlowObjectService } from '../../../flow-object/flow-object-service'; -import { FlowService } from '../../flow-service'; +import { type FlowService } from '../../flow-service'; import { - FlowSearchStrategy, - FlowSearchStrategyResponse, + type FlowSearchStrategy, + type FlowSearchStrategyResponse, } from '../flow-search-strategy'; @Service() @@ -22,22 +23,23 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { cursorCondition: any, models: Database ): Promise { + // Obtain flowObjects conditions const flowObjectsConditions: Map< string, Map - > = flowConditions.get('flowObjects'); - const flowEntityConditions = flowConditions.get('flow'); + > = flowConditions.get('flowObjects') ?? new Map(); + // Obtain flow conditions + const flowEntityConditions = flowConditions.get('flow') ?? new Map(); + + // Obtain where clause for flowObjects const flowObjectWhere = this.mapFlowObjectConditionsToWhereClause( flowObjectsConditions ); // Obtain flowIDs based on provided flowObject conditions - const flowIDsFromFilteredFlowObjects = - await this.flowObjectService.getFlowIdsFromFlowObjects( - models, - flowObjectWhere - ); + const flowIDsFromFilteredFlowObjects: FlowId[] = + await this.getFlowIDsFromFilteredFlowObjects(models, flowObjectWhere); // Combine conditions from flowObjects FlowIDs and flow conditions const mergedFlowConditions = { @@ -62,6 +64,25 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { return { flows, count: countObject.count }; } + private async getFlowIDsFromFilteredFlowObjects( + models: Database, + flowObjectWhere: any[] + ): Promise { + const flowIDsFromFilteredFlowObjects: FlowId[] = []; + const tempFlowIDs: FlowId[][] = await Promise.all( + flowObjectWhere.map((whereClause) => + this.flowObjectService.getFlowIdsFromFlowObjects(models, whereClause) + ) + ); + // Flatten array of arrays keeping only values present in all arrays + const flowIDs = tempFlowIDs.reduce((a, b) => + a.filter((c) => b.includes(c)) + ); + flowIDsFromFilteredFlowObjects.push(...flowIDs); + + return flowIDsFromFilteredFlowObjects; + } + /* * Map structure: * { @@ -74,12 +95,11 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { */ private mapFlowObjectConditionsToWhereClause( flowObjectConditions: Map> - ): any { - let flowObjectWhere: any = {}; + ): any[] { + const whereClauses = []; for (const [objectType, refDirectionMap] of flowObjectConditions) { for (const [refDirection, objectIDs] of refDirectionMap) { - flowObjectWhere = { - ...flowObjectWhere, + const whereClause = { objectID: { [Op.IN]: objectIDs, }, @@ -90,9 +110,11 @@ export class FlowObjectFiltersStrategy implements FlowSearchStrategy { [Op.LIKE]: objectType, }, }; + + whereClauses.push(whereClause); } } - return flowObjectWhere; + return whereClauses; } }